Skip to content

Commit 006cf70

Browse files
authored
Merge pull request #6076 from ctripcloud/work-mutating
add work mutating in ctrlutil.CreateOrUpdateWork()
2 parents 15800f2 + 707e442 commit 006cf70

File tree

4 files changed

+49
-29
lines changed

4 files changed

+49
-29
lines changed

pkg/controllers/ctrlutil/work.go

+30-18
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ import (
3131

3232
workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1"
3333
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
34+
"github.com/karmada-io/karmada/pkg/resourceinterpreter/default/native/prune"
3435
"github.com/karmada-io/karmada/pkg/util"
36+
"github.com/karmada-io/karmada/pkg/util/helper"
3537
)
3638

3739
// CreateOrUpdateWork creates a Work object if not exist, or updates if it already exists.
38-
func CreateOrUpdateWork(ctx context.Context, client client.Client, workMeta metav1.ObjectMeta, resource *unstructured.Unstructured, options ...WorkOption) error {
40+
func CreateOrUpdateWork(ctx context.Context, c client.Client, workMeta metav1.ObjectMeta, resource *unstructured.Unstructured, options ...WorkOption) error {
41+
resource = resource.DeepCopy()
3942
if workMeta.Labels[util.PropagationInstruction] != util.PropagationInstructionSuppressed {
40-
resource = resource.DeepCopy()
4143
// set labels
4244
util.MergeLabel(resource, util.ManagedByKarmadaLabel, util.ManagedByKarmadaLabelValue)
4345
// set annotations
@@ -48,42 +50,52 @@ func CreateOrUpdateWork(ctx context.Context, client client.Client, workMeta meta
4850
util.MergeAnnotation(resource, workv1alpha2.ResourceConflictResolutionAnnotation, conflictResolution)
4951
}
5052
}
51-
52-
workloadJSON, err := json.Marshal(resource)
53+
// Do the same thing as the mutating webhook does, remove the irrelevant fields for the resource.
54+
// This is to avoid unnecessary updates to the Work object, especially when controller starts.
55+
err := prune.RemoveIrrelevantFields(resource, prune.RemoveJobTTLSeconds)
5356
if err != nil {
54-
klog.Errorf("Failed to marshal workload(%s/%s), error: %v", resource.GetNamespace(), resource.GetName(), err)
57+
klog.Errorf("Failed to prune irrelevant fields for resource %s/%s. Error: %v", resource.GetNamespace(), resource.GetName(), err)
5558
return err
5659
}
5760

5861
work := &workv1alpha1.Work{
5962
ObjectMeta: workMeta,
60-
Spec: workv1alpha1.WorkSpec{
61-
Workload: workv1alpha1.WorkloadTemplate{
62-
Manifests: []workv1alpha1.Manifest{
63-
{
64-
RawExtension: runtime.RawExtension{
65-
Raw: workloadJSON,
66-
},
67-
},
68-
},
69-
},
70-
},
7163
}
7264

7365
applyWorkOptions(work, options)
7466

7567
runtimeObject := work.DeepCopy()
7668
var operationResult controllerutil.OperationResult
7769
err = retry.RetryOnConflict(retry.DefaultRetry, func() (err error) {
78-
operationResult, err = controllerutil.CreateOrUpdate(ctx, client, runtimeObject, func() error {
70+
operationResult, err = controllerutil.CreateOrUpdate(ctx, c, runtimeObject, func() error {
7971
if !runtimeObject.DeletionTimestamp.IsZero() {
8072
return fmt.Errorf("work %s/%s is being deleted", runtimeObject.GetNamespace(), runtimeObject.GetName())
8173
}
8274

83-
runtimeObject.Spec = work.Spec
8475
runtimeObject.Labels = util.DedupeAndMergeLabels(runtimeObject.Labels, work.Labels)
8576
runtimeObject.Annotations = util.DedupeAndMergeAnnotations(runtimeObject.Annotations, work.Annotations)
8677
runtimeObject.Finalizers = work.Finalizers
78+
runtimeObject.Spec = work.Spec
79+
80+
// Do the same thing as the mutating webhook does, add the permanent ID to workload if not exist,
81+
// This is to avoid unnecessary updates to the Work object, especially when controller starts.
82+
if runtimeObject.Labels[util.PropagationInstruction] != util.PropagationInstructionSuppressed {
83+
helper.SetLabelsAndAnnotationsForWorkload(resource, runtimeObject)
84+
}
85+
workloadJSON, err := json.Marshal(resource)
86+
if err != nil {
87+
klog.Errorf("Failed to marshal workload(%s/%s), error: %v", resource.GetNamespace(), resource.GetName(), err)
88+
return err
89+
}
90+
runtimeObject.Spec.Workload = workv1alpha1.WorkloadTemplate{
91+
Manifests: []workv1alpha1.Manifest{
92+
{
93+
RawExtension: runtime.RawExtension{
94+
Raw: workloadJSON,
95+
},
96+
},
97+
},
98+
}
8799
return nil
88100
})
89101
return err

pkg/controllers/multiclusterservice/endpointslice_dispatch_controller_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,9 @@ func TestEnsureEndpointSliceWork(t *testing.T) {
604604
"endpointslice.karmada.io/provision-cluster": "provider",
605605
"work.karmada.io/name": "test-work",
606606
"work.karmada.io/namespace": "karmada-es-consumer",
607-
"resourcetemplate.karmada.io/uid": ""
607+
"resourcetemplate.karmada.io/uid": "",
608+
"resourcetemplate.karmada.io/managed-annotations": "endpointslice.karmada.io/provision-cluster,resourcetemplate.karmada.io/managed-annotations,resourcetemplate.karmada.io/managed-labels,resourcetemplate.karmada.io/uid,work.karmada.io/name,work.karmada.io/namespace",
609+
"resourcetemplate.karmada.io/managed-labels":"endpointslice.kubernetes.io/managed-by,karmada.io/managed,kubernetes.io/service-name"
608610
}
609611
},
610612
"endpoints": [

pkg/util/helper/work.go

+11
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,14 @@ func IsWorkContains(manifests []workv1alpha1.Manifest, targetResource schema.Gro
111111
func IsWorkSuspendDispatching(work *workv1alpha1.Work) bool {
112112
return ptr.Deref(work.Spec.SuspendDispatching, false)
113113
}
114+
115+
// SetLabelsAndAnnotationsForWorkload sets the associated work object labels and annotations for workload.
116+
func SetLabelsAndAnnotationsForWorkload(workload *unstructured.Unstructured, work *workv1alpha1.Work) {
117+
util.RecordManagedAnnotations(workload)
118+
if work.Labels[workv1alpha2.WorkPermanentIDLabel] != "" {
119+
workload.SetLabels(util.DedupeAndMergeLabels(workload.GetLabels(), map[string]string{
120+
workv1alpha2.WorkPermanentIDLabel: work.Labels[workv1alpha2.WorkPermanentIDLabel],
121+
}))
122+
}
123+
util.RecordManagedLabels(workload)
124+
}

pkg/webhook/work/mutating.go

+5-10
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
3232
"github.com/karmada-io/karmada/pkg/resourceinterpreter/default/native/prune"
3333
"github.com/karmada-io/karmada/pkg/util"
34+
"github.com/karmada-io/karmada/pkg/util/helper"
3435
)
3536

3637
// MutatingAdmission mutates API request if necessary.
@@ -42,6 +43,9 @@ type MutatingAdmission struct {
4243
var _ admission.Handler = &MutatingAdmission{}
4344

4445
// Handle yields a response to an AdmissionRequest.
46+
// This function could affect the informer cache of controller-manager.
47+
// In controller-manager, redundant updates to Work are avoided as much as possible, and changes to informer cache will affect this behavior.
48+
// Therefore, if someone modify this function, please ensure that the relevant logic in controller-manager also be updated.
4549
func (a *MutatingAdmission) Handle(_ context.Context, req admission.Request) admission.Response {
4650
work := &workv1alpha1.Work{}
4751

@@ -73,7 +77,7 @@ func (a *MutatingAdmission) Handle(_ context.Context, req admission.Request) adm
7377

7478
// Skip label/annotate the workload of Work that is not intended to be propagated.
7579
if work.Labels[util.PropagationInstruction] != util.PropagationInstructionSuppressed {
76-
setLabelsAndAnnotationsForWorkload(workloadObj, work)
80+
helper.SetLabelsAndAnnotationsForWorkload(workloadObj, work)
7781
}
7882

7983
workloadJSON, err := workloadObj.MarshalJSON()
@@ -92,12 +96,3 @@ func (a *MutatingAdmission) Handle(_ context.Context, req admission.Request) adm
9296

9397
return admission.PatchResponseFromRaw(req.Object.Raw, marshaledBytes)
9498
}
95-
96-
// setLabelsAndAnnotationsForWorkload sets the associated work object labels and annotations for workload.
97-
func setLabelsAndAnnotationsForWorkload(workload *unstructured.Unstructured, work *workv1alpha1.Work) {
98-
util.RecordManagedAnnotations(workload)
99-
workload.SetLabels(util.DedupeAndMergeLabels(workload.GetLabels(), map[string]string{
100-
workv1alpha2.WorkPermanentIDLabel: work.Labels[workv1alpha2.WorkPermanentIDLabel],
101-
}))
102-
util.RecordManagedLabels(workload)
103-
}

0 commit comments

Comments
 (0)