From 4df1ae680695b25ab2b95deb7003fd8efd681c6c Mon Sep 17 00:00:00 2001 From: pigletfly Date: Tue, 10 Aug 2021 19:42:07 +0800 Subject: [PATCH] Add applied result in aggregated status Signed-off-by: pigletfly --- ...rk.karmada.io_clusterresourcebindings.yaml | 10 +++++ .../work.karmada.io_resourcebindings.yaml | 10 +++++ pkg/apis/work/v1alpha1/binding_types.go | 9 +++++ pkg/util/helper/workstatus.go | 39 ++++++++++++++++--- 4 files changed, 62 insertions(+), 6 deletions(-) diff --git a/artifacts/deploy/work.karmada.io_clusterresourcebindings.yaml b/artifacts/deploy/work.karmada.io_clusterresourcebindings.yaml index 766800f9d..dd43ebd98 100644 --- a/artifacts/deploy/work.karmada.io_clusterresourcebindings.yaml +++ b/artifacts/deploy/work.karmada.io_clusterresourcebindings.yaml @@ -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. diff --git a/artifacts/deploy/work.karmada.io_resourcebindings.yaml b/artifacts/deploy/work.karmada.io_resourcebindings.yaml index cb13c6a5f..d6aa13f46 100644 --- a/artifacts/deploy/work.karmada.io_resourcebindings.yaml +++ b/artifacts/deploy/work.karmada.io_resourcebindings.yaml @@ -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. diff --git a/pkg/apis/work/v1alpha1/binding_types.go b/pkg/apis/work/v1alpha1/binding_types.go index 8777682aa..5d0c7d117 100644 --- a/pkg/apis/work/v1alpha1/binding_types.go +++ b/pkg/apis/work/v1alpha1/binding_types.go @@ -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 diff --git a/pkg/util/helper/workstatus.go b/pkg/util/helper/workstatus.go index 99e4f7eb8..e44dd57b8 100644 --- a/pkg/util/helper/workstatus.go +++ b/pkg/util/helper/workstatus.go @@ -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" @@ -84,6 +85,37 @@ 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) @@ -91,15 +123,10 @@ func assembleWorkStatus(c client.Client, selector labels.Selector, workload *uns 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