Skip to content

Commit

Permalink
Add applied result in aggregated status
Browse files Browse the repository at this point in the history
Signed-off-by: pigletfly <[email protected]>
  • Loading branch information
pigletfly committed Aug 13, 2021
1 parent f2ac7af commit 4df1ae6
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
10 changes: 10 additions & 0 deletions artifacts/deploy/work.karmada.io_clusterresourcebindings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ spec:
description: AggregatedStatusItem represents status of the resource
running in a member cluster.
properties:
applied:
description: Applied represents if the resource referencing
by ResourceBinding or ClusterResourceBinding is successfully
applied on the cluster.
type: boolean
appliedMessage:
description: AppliedMessage is a human readable message indicating
details about the applied status. This is usually holds the
error message in case of apply failed.
type: string
clusterName:
description: ClusterName represents the member cluster name
which the resource deployed on.
Expand Down
10 changes: 10 additions & 0 deletions artifacts/deploy/work.karmada.io_resourcebindings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ spec:
description: AggregatedStatusItem represents status of the resource
running in a member cluster.
properties:
applied:
description: Applied represents if the resource referencing
by ResourceBinding or ClusterResourceBinding is successfully
applied on the cluster.
type: boolean
appliedMessage:
description: AppliedMessage is a human readable message indicating
details about the applied status. This is usually holds the
error message in case of apply failed.
type: string
clusterName:
description: ClusterName represents the member cluster name
which the resource deployed on.
Expand Down
9 changes: 9 additions & 0 deletions pkg/apis/work/v1alpha1/binding_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ type AggregatedStatusItem struct {
// +kubebuilder:pruning:PreserveUnknownFields
// +optional
Status *runtime.RawExtension `json:"status,omitempty"`
// Applied represents if the resource referencing by ResourceBinding or ClusterResourceBinding
// is successfully applied on the cluster.
// +optional
Applied bool `json:"applied,omitempty"`

// AppliedMessage is a human readable message indicating details about the applied status.
// This is usually holds the error message in case of apply failed.
// +optional
AppliedMessage string `json:"appliedMessage,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
39 changes: 33 additions & 6 deletions pkg/util/helper/workstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"reflect"

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -84,22 +85,48 @@ func assembleWorkStatus(c client.Client, selector labels.Selector, workload *uns
klog.Errorf("Failed to get manifestIndex of workload in work.Spec.Workload.Manifests. Error: %v.", err)
return nil, err
}
clusterName, err := names.GetClusterName(work.Namespace)
if err != nil {
klog.Errorf("Failed to get clusterName from work namespace %s. Error: %v.", work.Namespace, err)
return nil, err
}

// if sync work to member cluster failed, then set status back to resource binding.
var applied bool
var appliedMsg string
if cond := meta.FindStatusCondition(work.Status.Conditions, workv1alpha1.WorkApplied); cond != nil {
switch cond.Status {
case metav1.ConditionTrue:
applied = true
case metav1.ConditionUnknown:
fallthrough
case metav1.ConditionFalse:
applied = false
appliedMsg = cond.Message
default: // should not happen unless the condition api changed.
panic("unexpected status")
}
}
if !applied {
aggregatedStatus := workv1alpha1.AggregatedStatusItem{
ClusterName: clusterName,
Applied: applied,
AppliedMessage: appliedMsg,
}
statuses = append(statuses, aggregatedStatus)
return statuses, nil
}

for _, manifestStatus := range work.Status.ManifestStatuses {
equal, err := equalIdentifier(&manifestStatus.Identifier, identifierIndex, workload)
if err != nil {
return nil, err
}
if equal {
clusterName, err := names.GetClusterName(work.Namespace)
if err != nil {
klog.Errorf("Failed to get clusterName from work namespace %s. Error: %v.", work.Namespace, err)
return nil, err
}

aggregatedStatus := workv1alpha1.AggregatedStatusItem{
ClusterName: clusterName,
Status: manifestStatus.Status,
Applied: applied,
}
statuses = append(statuses, aggregatedStatus)
break
Expand Down

0 comments on commit 4df1ae6

Please sign in to comment.