fix upgrade error with #5003

Signed-off-by: changzhen <changzhen5@huawei.com>
This commit is contained in:
changzhen 2024-05-30 18:07:07 +08:00
parent 9c85fbddf9
commit 6edf69debb
2 changed files with 14 additions and 11 deletions

View File

@ -37,8 +37,21 @@ import (
"github.com/karmada-io/karmada/pkg/util"
)
// CreateOrUpdateWork creates a Work object if not exist, or updates if it already exist.
// CreateOrUpdateWork creates a Work object if not exist, or updates if it already exists.
func CreateOrUpdateWork(client client.Client, workMeta metav1.ObjectMeta, resource *unstructured.Unstructured) error {
if workMeta.Labels[util.PropagationInstruction] != util.PropagationInstructionSuppressed {
resource = resource.DeepCopy()
// set labels
util.MergeLabel(resource, util.ManagedByKarmadaLabel, util.ManagedByKarmadaLabelValue)
// set annotations
util.MergeAnnotation(resource, workv1alpha2.ResourceTemplateUIDAnnotation, string(resource.GetUID()))
util.MergeAnnotation(resource, workv1alpha2.WorkNameAnnotation, workMeta.Name)
util.MergeAnnotation(resource, workv1alpha2.WorkNamespaceAnnotation, workMeta.Namespace)
if conflictResolution, ok := workMeta.GetAnnotations()[workv1alpha2.ResourceConflictResolutionAnnotation]; ok {
util.MergeAnnotation(resource, workv1alpha2.ResourceConflictResolutionAnnotation, conflictResolution)
}
}
workloadJSON, err := resource.MarshalJSON()
if err != nil {
klog.Errorf("Failed to marshal workload(%s/%s), error: %v", resource.GetNamespace(), resource.GetName(), err)

View File

@ -95,18 +95,8 @@ func (a *MutatingAdmission) Handle(_ context.Context, req admission.Request) adm
// setLabelsAndAnnotationsForWorkload sets the associated work object labels and annotations for workload.
func setLabelsAndAnnotationsForWorkload(workload *unstructured.Unstructured, work *workv1alpha1.Work) {
workload.SetAnnotations(util.DedupeAndMergeAnnotations(workload.GetAnnotations(), map[string]string{
workv1alpha2.ResourceTemplateUIDAnnotation: string(workload.GetUID()),
workv1alpha2.WorkNamespaceAnnotation: work.GetNamespace(),
workv1alpha2.WorkNameAnnotation: work.GetName(),
}))
if conflictResolution, ok := work.Annotations[workv1alpha2.ResourceConflictResolutionAnnotation]; ok {
util.MergeAnnotation(workload, workv1alpha2.ResourceConflictResolutionAnnotation, conflictResolution)
}
util.RecordManagedAnnotations(workload)
workload.SetLabels(util.DedupeAndMergeLabels(workload.GetLabels(), map[string]string{
util.ManagedByKarmadaLabel: util.ManagedByKarmadaLabelValue,
workv1alpha2.WorkPermanentIDLabel: work.Labels[workv1alpha2.WorkPermanentIDLabel],
}))
util.RecordManagedLabels(workload)