Skip to content

Commit

Permalink
Aggregate status in remoterootsyncset (#2826)
Browse files Browse the repository at this point in the history
This lets us present it more nicely in kubectl get.
  • Loading branch information
justinsb committed Feb 23, 2022
1 parent c0b4a02 commit c9b7522
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import (

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:printcolumn:name="AppliedCount",type=integer,JSONPath=`.status.aggregated.applied`
//+kubebuilder:printcolumn:name="ReadyCount",type=integer,JSONPath=`.status.aggregated.ready`
//+kubebuilder:printcolumn:name="Total",type=integer,JSONPath=`.status.aggregated.total`
//+kubebuilder:printcolumn:name="Applied",type=string,JSONPath=`.status.aggregated.conditions[?(@.type=='Applied')].reason`
//+kubebuilder:printcolumn:name="Ready",type=string,JSONPath=`.status.aggregated.conditions[?(@.type=='Ready')].reason`

// RemoteRootSyncSet represents applying a package to multiple target clusters.
// In future, this should use ConfigSync, but while we're iterating on OCI/porch support,
Expand Down Expand Up @@ -95,7 +100,8 @@ func (o *OCISpec) GetRepository() string {

// RootSyncSetStatus defines the observed state of RootSyncSet
type RemoteRootSyncSetStatus struct {
Targets []TargetStatus `json:"targets,omitempty"`
Targets []TargetStatus `json:"targets,omitempty"`
AggregatedStatus AggregatedStatus `json:"aggregated,omitempty"`
}

type TargetStatus struct {
Expand All @@ -104,6 +110,14 @@ type TargetStatus struct {
Conditions []metav1.Condition `json:"conditions,omitempty"`
}

type AggregatedStatus struct {
Targets int32 `json:"total"`
Applied int32 `json:"applied"`
Ready int32 `json:"ready"`
// Conditions describes the reconciliation state of the object.
Conditions []metav1.Condition `json:"conditions,omitempty"`
}

//+kubebuilder:object:root=true

// RemoteRootSyncSetList contains a list of RemoteRootSyncSet
Expand Down

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 @@ -30,7 +30,23 @@ spec:
singular: remoterootsyncset
scope: Namespaced
versions:
- name: v1alpha1
- additionalPrinterColumns:
- jsonPath: .status.aggregated.applied
name: AppliedCount
type: integer
- jsonPath: .status.aggregated.ready
name: ReadyCount
type: integer
- jsonPath: .status.aggregated.total
name: Total
type: integer
- jsonPath: .status.aggregated.conditions[?(@.type=='Applied')].reason
name: Applied
type: string
- jsonPath: .status.aggregated.conditions[?(@.type=='Ready')].reason
name: Ready
type: string
name: v1alpha1
schema:
openAPIV3Schema:
description: "RemoteRootSyncSet represents applying a package to multiple
Expand Down Expand Up @@ -87,6 +103,96 @@ spec:
status:
description: RootSyncSetStatus defines the observed state of RootSyncSet
properties:
aggregated:
properties:
applied:
format: int32
type: integer
conditions:
description: Conditions describes the reconciliation state of
the object.
items:
description: "Condition contains details for one aspect of the
current state of this API Resource. --- This struct is intended
for direct use as an array at the field path .status.conditions.
\ For example, type FooStatus struct{ // Represents the
observations of a foo's current state. // Known .status.conditions.type
are: \"Available\", \"Progressing\", and \"Degraded\" //
+patchMergeKey=type // +patchStrategy=merge // +listType=map
\ // +listMapKey=type Conditions []metav1.Condition
`json:\"conditions,omitempty\" patchStrategy:\"merge\" patchMergeKey:\"type\"
protobuf:\"bytes,1,rep,name=conditions\"` \n // other
fields }"
properties:
lastTransitionTime:
description: lastTransitionTime is the last time the condition
transitioned from one status to another. This should be
when the underlying condition changed. If that is not
known, then using the time when the API field changed
is acceptable.
format: date-time
type: string
message:
description: message is a human readable message indicating
details about the transition. This may be an empty string.
maxLength: 32768
type: string
observedGeneration:
description: observedGeneration represents the .metadata.generation
that the condition was set based upon. For instance, if
.metadata.generation is currently 12, but the .status.conditions[x].observedGeneration
is 9, the condition is out of date with respect to the
current state of the instance.
format: int64
minimum: 0
type: integer
reason:
description: reason contains a programmatic identifier indicating
the reason for the condition's last transition. Producers
of specific condition types may define expected values
and meanings for this field, and whether the values are
considered a guaranteed API. The value should be a CamelCase
string. This field may not be empty.
maxLength: 1024
minLength: 1
pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$
type: string
status:
description: status of the condition, one of True, False,
Unknown.
enum:
- "True"
- "False"
- Unknown
type: string
type:
description: type of condition in CamelCase or in foo.example.com/CamelCase.
--- Many .condition.type values are consistent across
resources like Available, but because arbitrary conditions
can be useful (see .node.status.conditions), the ability
to deconflict is important. The regex it matches is (dns1123SubdomainFmt/)?(qualifiedNameFmt)
maxLength: 316
pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$
type: string
required:
- lastTransitionTime
- message
- reason
- status
- type
type: object
type: array
ready:
format: int32
type: integer
total:
format: int32
type: integer
required:
- applied
- ready
- total
type: object
targets:
items:
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,57 @@ func updateTargetStatus(subject *api.RemoteRootSyncSet, ref *api.ClusterRef, app
}
// TODO: SetStatusCondition should return an indiciation if anything has changes

return updateAggregateStatus(subject)
}

func updateAggregateStatus(subject *api.RemoteRootSyncSet) bool {
// TODO: Verify that all targets are accounted for

applied := make(map[string]int32)
ready := make(map[string]int32)

targetCount := int32(0)
for _, status := range subject.Status.Targets {
targetCount++
appliedCondition := meta.FindStatusCondition(status.Conditions, "Applied")
if appliedCondition == nil {
applied["UnknownStatus"]++
} else {
applied[appliedCondition.Reason]++
}
readyCondition := meta.FindStatusCondition(status.Conditions, "Ready")
if appliedCondition == nil {
ready["UnknownStatus"]++
} else {
ready[readyCondition.Reason]++
}
}

conditions := &subject.Status.AggregatedStatus.Conditions
if applied["UpdateInProgress"] > 0 {
meta.SetStatusCondition(conditions, metav1.Condition{Type: "Applied", Status: metav1.ConditionTrue, Reason: "UpdateInProgress"})
} else if applied["Error"] > 0 {
meta.SetStatusCondition(conditions, metav1.Condition{Type: "Applied", Status: metav1.ConditionTrue, Reason: "Error"})
} else if applied["Applied"] >= targetCount {
meta.SetStatusCondition(conditions, metav1.Condition{Type: "Applied", Status: metav1.ConditionTrue, Reason: "Applied"})
} else {
meta.SetStatusCondition(conditions, metav1.Condition{Type: "Applied", Status: metav1.ConditionTrue, Reason: "UnknownStatus"})
}

if ready["UpdateInProgress"] > 0 {
meta.SetStatusCondition(conditions, metav1.Condition{Type: "Ready", Status: metav1.ConditionTrue, Reason: "UpdateInProgress"})
} else if ready["WaitingForReady"] > 0 {
meta.SetStatusCondition(conditions, metav1.Condition{Type: "Ready", Status: metav1.ConditionTrue, Reason: "WaitingForReady"})
} else if ready["Ready"] >= targetCount {
meta.SetStatusCondition(conditions, metav1.Condition{Type: "Ready", Status: metav1.ConditionTrue, Reason: "Ready"})
} else {
meta.SetStatusCondition(conditions, metav1.Condition{Type: "Ready", Status: metav1.ConditionTrue, Reason: "UnknownStatus"})
}

subject.Status.AggregatedStatus.Targets = targetCount
subject.Status.AggregatedStatus.Applied = applied["Applied"]
subject.Status.AggregatedStatus.Ready = ready["Ready"]

return true
}

Expand Down

0 comments on commit c9b7522

Please sign in to comment.