@@ -31,13 +31,15 @@ import (
31
31
32
32
workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1"
33
33
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
34
+ "github.com/karmada-io/karmada/pkg/resourceinterpreter/default/native/prune"
34
35
"github.com/karmada-io/karmada/pkg/util"
36
+ "github.com/karmada-io/karmada/pkg/util/helper"
35
37
)
36
38
37
39
// 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 ()
39
42
if workMeta .Labels [util .PropagationInstruction ] != util .PropagationInstructionSuppressed {
40
- resource = resource .DeepCopy ()
41
43
// set labels
42
44
util .MergeLabel (resource , util .ManagedByKarmadaLabel , util .ManagedByKarmadaLabelValue )
43
45
// set annotations
@@ -48,42 +50,52 @@ func CreateOrUpdateWork(ctx context.Context, client client.Client, workMeta meta
48
50
util .MergeAnnotation (resource , workv1alpha2 .ResourceConflictResolutionAnnotation , conflictResolution )
49
51
}
50
52
}
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 )
53
56
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 )
55
58
return err
56
59
}
57
60
58
61
work := & workv1alpha1.Work {
59
62
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
- },
71
63
}
72
64
73
65
applyWorkOptions (work , options )
74
66
75
67
runtimeObject := work .DeepCopy ()
76
68
var operationResult controllerutil.OperationResult
77
69
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 {
79
71
if ! runtimeObject .DeletionTimestamp .IsZero () {
80
72
return fmt .Errorf ("work %s/%s is being deleted" , runtimeObject .GetNamespace (), runtimeObject .GetName ())
81
73
}
82
74
83
- runtimeObject .Spec = work .Spec
84
75
runtimeObject .Labels = util .DedupeAndMergeLabels (runtimeObject .Labels , work .Labels )
85
76
runtimeObject .Annotations = util .DedupeAndMergeAnnotations (runtimeObject .Annotations , work .Annotations )
86
77
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
+ }
87
99
return nil
88
100
})
89
101
return err
0 commit comments