Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions api/v1/clusterobjectset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,25 @@ type ObjectCounts struct {
// +kubebuilder:validation:Minimum:=0
// +kubebuilder:validation:Maximum:=1000
Ready *int64 `json:"ready,omitempty"`

// superseded is the number of objects in this object set that are observed
// under the control of another object set in the same group with a higher
// revision. These objects also count as present, but not as synced. This
// describes observed ownership, not a cumulative count of transfers.
// Set to zero during teardown.
// +required
// +kubebuilder:validation:Minimum:=0
// +kubebuilder:validation:Maximum:=1000
Superseded *int64 `json:"superseded,omitempty"`

// colliding is the number of objects in this object set whose adoption is
// refused by the configured collision protection policy. The conflicting
// resources exist on the cluster but do not count as present, synced, or
// superseded. Set to zero during teardown.
// +required
// +kubebuilder:validation:Minimum:=0
// +kubebuilder:validation:Maximum:=1000
Colliding *int64 `json:"colliding,omitempty"`
}

// ObjectStatus identifies a managed object and its failure messages.
Expand Down
10 changes: 5 additions & 5 deletions api/v1/clusterobjectset_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1107,22 +1107,22 @@ func configMap() unstructured.Unstructured {
}

// TestObjectCountsSerialization verifies the per-count serialization semantics of
// the required *int64 count fields. total/present/synced/ready are all required but
// the required *int64 count fields. total/present/synced/ready/superseded/colliding are all required but
// are pointers with omitempty so the controller can distinguish a count it has not
// computed (nil) from one it has computed as zero (a pointer to 0):
// - A count explicitly set to 0 (e.g. ready=0 while a phase is rolling out, or
// synced/ready=0 during teardown) is serialized as 0, not omitted.
// - A count left unset (nil) is omitted entirely, not serialized as 0.
func TestObjectCountsSerialization(t *testing.T) {
// A count explicitly set to 0 serializes as 0 rather than being omitted.
data, err := json.Marshal(ObjectCounts{Total: ptr.To(int64(1)), Present: ptr.To(int64(1)), Synced: ptr.To(int64(1)), Ready: ptr.To(int64(0))})
data, err := json.Marshal(ObjectCounts{Total: ptr.To(int64(1)), Present: ptr.To(int64(1)), Synced: ptr.To(int64(1)), Ready: ptr.To(int64(0)), Superseded: ptr.To(int64(0)), Colliding: ptr.To(int64(0))})
require.NoError(t, err)
require.JSONEq(t, `{"total":1,"present":1,"synced":1,"ready":0}`, string(data))
require.JSONEq(t, `{"total":1,"present":1,"synced":1,"ready":0,"superseded":0,"colliding":0}`, string(data))

// Every count explicitly set to 0 is serialized; none is omitted.
data, err = json.Marshal(ObjectCounts{Total: ptr.To(int64(0)), Present: ptr.To(int64(0)), Synced: ptr.To(int64(0)), Ready: ptr.To(int64(0))})
data, err = json.Marshal(ObjectCounts{Total: ptr.To(int64(0)), Present: ptr.To(int64(0)), Synced: ptr.To(int64(0)), Ready: ptr.To(int64(0)), Superseded: ptr.To(int64(0)), Colliding: ptr.To(int64(0))})
require.NoError(t, err)
require.JSONEq(t, `{"total":0,"present":0,"synced":0,"ready":0}`, string(data))
require.JSONEq(t, `{"total":0,"present":0,"synced":0,"ready":0,"superseded":0,"colliding":0}`, string(data))

// An unset (nil) count is omitted rather than serialized as 0.
data, err = json.Marshal(ObjectCounts{Total: ptr.To(int64(1)), Present: ptr.To(int64(1)), Synced: ptr.To(int64(1))})
Expand Down
66 changes: 66 additions & 0 deletions api/v1/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,69 @@ func TestClusterObjectSetDigestImmutable(t *testing.T) {
err := c.Status().Update(t.Context(), cos)
require.True(t, errors.IsInvalid(err), "expected update to fail as invalid, but got: %v", err)
}

func TestClusterObjectSetObjectCountsValidation(t *testing.T) {
c := newClient(t)

for _, tc := range []struct {
name string
cosName string
counts ObjectCounts
wantInvalid string
}{
{
name: "all counts set is valid",
cosName: "cos-objectcounts-valid-1",
counts: ObjectCounts{Total: ptr.To(int64(3)), Present: ptr.To(int64(2)), Synced: ptr.To(int64(1)), Ready: ptr.To(int64(1)), Superseded: ptr.To(int64(1)), Colliding: ptr.To(int64(1))},
},
{
name: "superseded is required",
cosName: "cos-objectcounts-nosuperseded-1",
counts: ObjectCounts{Total: ptr.To(int64(1)), Present: ptr.To(int64(1)), Synced: ptr.To(int64(1)), Ready: ptr.To(int64(1)), Colliding: ptr.To(int64(0))},
wantInvalid: "status.objectCounts.superseded: Required value",
},
{
name: "colliding is required",
cosName: "cos-objectcounts-nocolliding-1",
counts: ObjectCounts{Total: ptr.To(int64(1)), Present: ptr.To(int64(1)), Synced: ptr.To(int64(1)), Ready: ptr.To(int64(1)), Superseded: ptr.To(int64(0))},
wantInvalid: "status.objectCounts.colliding: Required value",
},
{
name: "superseded must be at most 1000",
cosName: "cos-objectcounts-supersededmax-1",
counts: ObjectCounts{Total: ptr.To(int64(1)), Present: ptr.To(int64(1)), Synced: ptr.To(int64(1)), Ready: ptr.To(int64(1)), Superseded: ptr.To(int64(1001)), Colliding: ptr.To(int64(0))},
wantInvalid: "status.objectCounts.superseded: Invalid value",
},
{
name: "colliding must not be negative",
cosName: "cos-objectcounts-collidingmin-1",
counts: ObjectCounts{Total: ptr.To(int64(1)), Present: ptr.To(int64(1)), Synced: ptr.To(int64(1)), Ready: ptr.To(int64(1)), Superseded: ptr.To(int64(0)), Colliding: ptr.To(int64(-1))},
wantInvalid: "status.objectCounts.colliding: Invalid value",
},
} {
t.Run(tc.name, func(t *testing.T) {
cos := &ClusterObjectSet{
ObjectMeta: metav1.ObjectMeta{Name: tc.cosName},
Spec: ClusterObjectSetSpec{
Group: strings.TrimSuffix(tc.cosName, "-1"),
Revision: 1,
LifecycleState: ClusterObjectSetLifecycleStateActive,
ClusterObjectSetContent: ClusterObjectSetContent{
CollisionProtection: CollisionProtectionPrevent,
Phases: []ClusterObjectSetPhase{{Name: "deploy", Objects: []ClusterObjectSetObject{}}},
},
},
}
require.NoError(t, c.Create(t.Context(), cos))

cos.Status.ObjectCounts = tc.counts
err := c.Status().Update(t.Context(), cos)
if tc.wantInvalid == "" {
require.NoError(t, err)
return
}
require.True(t, errors.IsInvalid(err), "expected update to fail as invalid, but got: %v", err)
require.ErrorContains(t, err, tc.wantInvalid)
})
}
}
10 changes: 10 additions & 0 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions applyconfigurations/api/v1/objectcounts.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions applyconfigurations/internal/internal.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,16 @@ spec:
It is omitted until counts have been computed; when present, all count
fields are reported (including zeros).
properties:
colliding:
description: |-
colliding is the number of objects in this object set whose adoption is
refused by the configured collision protection policy. The conflicting
resources exist on the cluster but do not count as present, synced, or
superseded. Set to zero during teardown.
format: int64
maximum: 1000
minimum: 0
type: integer
present:
description: |-
present is the number of objects in this object set that exist on the
Expand All @@ -748,6 +758,17 @@ spec:
maximum: 1000
minimum: 0
type: integer
superseded:
description: |-
superseded is the number of objects in this object set that are observed
under the control of another object set in the same group with a higher
revision. These objects also count as present, but not as synced. This
describes observed ownership, not a cumulative count of transfers.
Set to zero during teardown.
format: int64
maximum: 1000
minimum: 0
type: integer
synced:
description: |-
synced is the number of objects in this object set whose cluster state
Expand All @@ -763,8 +784,10 @@ spec:
minimum: 0
type: integer
required:
- colliding
- present
- ready
- superseded
- synced
- total
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestClusterObjectDeploymentGroupEvents(t *testing.T) {
assert.NoError(collect, cl.Get(t.Context(), client.ObjectKey{Name: cod.Name + "-1"}, owned))
}, 10*time.Second, 10*time.Millisecond)
owned.Status.CompletedAt = metav1.Now()
owned.Status.ObjectCounts = ocv1.ObjectCounts{Total: ptr.To(int64(1)), Present: ptr.To(int64(1)), Synced: ptr.To(int64(1)), Ready: ptr.To(int64(1))}
owned.Status.ObjectCounts = ocv1.ObjectCounts{Total: ptr.To(int64(1)), Present: ptr.To(int64(1)), Synced: ptr.To(int64(1)), Ready: ptr.To(int64(1)), Superseded: ptr.To(int64(0)), Colliding: ptr.To(int64(0))}
setCOSReady(owned, metav1.ConditionTrue, ocv1.ClusterObjectSetReasonAllObjectsReady)
require.NoError(t, cl.Status().Update(t.Context(), owned))
waitForCODCondition(t, cl, cod.Name, ocv1.ClusterObjectDeploymentTypeAvailable,
Expand All @@ -75,7 +75,7 @@ func TestClusterObjectDeploymentGroupEvents(t *testing.T) {

// A ready foreign revision cannot supply the COD's availability.
setCOSReady(foreign, metav1.ConditionTrue, ocv1.ClusterObjectSetReasonAllObjectsReady)
foreign.Status.ObjectCounts = ocv1.ObjectCounts{Total: ptr.To(int64(1)), Present: ptr.To(int64(1)), Synced: ptr.To(int64(1)), Ready: ptr.To(int64(1))}
foreign.Status.ObjectCounts = ocv1.ObjectCounts{Total: ptr.To(int64(1)), Present: ptr.To(int64(1)), Synced: ptr.To(int64(1)), Ready: ptr.To(int64(1)), Superseded: ptr.To(int64(0)), Colliding: ptr.To(int64(0))}
require.NoError(t, cl.Status().Update(t.Context(), foreign))
markCOSReady(t, cl, owned.Name, false)
waitForCODCondition(t, cl, cod.Name, ocv1.ClusterObjectDeploymentTypeAvailable,
Expand All @@ -91,7 +91,7 @@ func TestClusterObjectDeploymentGroupEvents(t *testing.T) {
metav1.ConditionFalse, ocv1.ClusterObjectDeploymentReasonOwnerConflict, cod.Generation)
require.True(t, apierrors.IsNotFound(cl.Get(t.Context(), client.ObjectKey{Name: cod.Name + "-4"}, &ocv1.ClusterObjectSet{})))
require.NoError(t, cl.Get(t.Context(), client.ObjectKeyFromObject(owned), owned))
require.Equal(t, ocv1.ObjectCounts{Total: ptr.To(int64(1)), Present: ptr.To(int64(1)), Synced: ptr.To(int64(1)), Ready: ptr.To(int64(1))}, owned.Status.ObjectCounts)
require.Equal(t, ocv1.ObjectCounts{Total: ptr.To(int64(1)), Present: ptr.To(int64(1)), Synced: ptr.To(int64(1)), Ready: ptr.To(int64(1)), Superseded: ptr.To(int64(0)), Colliding: ptr.To(int64(0))}, owned.Status.ObjectCounts)

// Only the foreign COS changes here. Its group event must release the
// conflict and reconcile the already-updated COD template.
Expand Down
35 changes: 18 additions & 17 deletions internal/object-controller/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import (
// zeroObjectCounts returns an ObjectCounts aggregate initialized to zero.
func zeroObjectCounts() ocv1.ObjectCounts {
return ocv1.ObjectCounts{
Total: new(int64(0)),
Present: new(int64(0)),
Synced: new(int64(0)),
Ready: new(int64(0)),
Total: new(int64(0)),
Present: new(int64(0)),
Synced: new(int64(0)),
Ready: new(int64(0)),
Superseded: new(int64(0)),
Colliding: new(int64(0)),
}
}

Expand All @@ -37,6 +39,8 @@ func addObjectCounts(agg *ocv1.ObjectCounts, counts ocv1.ObjectCounts) {
*agg.Present += ptr.Deref(counts.Present, 0)
*agg.Synced += ptr.Deref(counts.Synced, 0)
*agg.Ready += ptr.Deref(counts.Ready, 0)
*agg.Superseded += ptr.Deref(counts.Superseded, 0)
*agg.Colliding += ptr.Deref(counts.Colliding, 0)
}

func UpdateObservedPhasesForReconcile(mapper apimeta.RESTMapper, objSet *ocv1.ClusterObjectSet, rres machinery.RevisionResult, now metav1.Time) {
Expand Down Expand Up @@ -71,12 +75,8 @@ func UpdateObservedPhasesForReconcile(mapper apimeta.RESTMapper, objSet *ocv1.Cl
}
// Per-phase counts are computed locally to derive the phase status and to
// feed the object-set aggregate; they are not persisted on the phase status.
phaseObjectCounts := ocv1.ObjectCounts{
Total: new(phaseTotals[pres.GetName()]),
Present: new(int64(0)),
Synced: new(int64(0)),
Ready: new(int64(0)),
}
phaseObjectCounts := zeroObjectCounts()
phaseObjectCounts.Total = new(phaseTotals[pres.GetName()])

if pres.GetValidationError() != nil {
if !precedingPhasesComplete {
Expand Down Expand Up @@ -115,14 +115,18 @@ func UpdateObservedPhasesForReconcile(mapper apimeta.RESTMapper, objSet *ocv1.Cl
*phaseObjectCounts.Present++
*phaseObjectCounts.Synced++
case machinery.ActionProgressed:
// Adopted by a newer sibling revision: present, but not synced to this one.
*phaseObjectCounts.Present++
*phaseObjectCounts.Superseded++
case machinery.ActionUpdated, machinery.ActionRecovered:
*phaseObjectCounts.Present++
if !pobj.IsPaused() {
*phaseObjectCounts.Synced++
}
case machinery.ActionCollision:
// TODO: collect object collisions and put in status
// Adoption refused by collision protection. Counted even in a paused phase:
// a collision does not resolve by rolling out earlier phases.
*phaseObjectCounts.Colliding++
}

// track for phase status derivation
Expand Down Expand Up @@ -193,12 +197,9 @@ func UpdateObservedPhasesForTeardown(mapper apimeta.RESTMapper, objSet *ocv1.Clu

// Per-phase counts feed the object-set aggregate but are not persisted
// on the phase status.
counts := ocv1.ObjectCounts{
Total: new(phaseTotals[pres.GetName()]),
Present: new(int64(len(pres.Waiting()))),
Synced: new(int64(0)),
Ready: new(int64(0)),
}
counts := zeroObjectCounts()
counts.Total = new(phaseTotals[pres.GetName()])
counts.Present = new(int64(len(pres.Waiting())))
addObjectCounts(&aggregate, counts)

if name, ok := tres.GetActivePhaseName(); ok && name == pres.GetName() {
Expand Down
Loading
Loading