Skip to content

Commit 253dc79

Browse files
authored
Merge pull request #6018 from ctripcloud/unstructured
move CreateOrUpdateWork() and related functions to controllers/ctrlutl
2 parents be674c7 + 807153f commit 253dc79

14 files changed

+364
-294
lines changed

Diff for: pkg/controllers/binding/common.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
configv1alpha1 "github.com/karmada-io/karmada/pkg/apis/config/v1alpha1"
3232
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
3333
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
34+
"github.com/karmada-io/karmada/pkg/controllers/ctrlutil"
3435
"github.com/karmada-io/karmada/pkg/features"
3536
"github.com/karmada-io/karmada/pkg/resourceinterpreter"
3637
"github.com/karmada-io/karmada/pkg/util"
@@ -128,13 +129,13 @@ func ensureWork(
128129
Annotations: annotations,
129130
}
130131

131-
if err = helper.CreateOrUpdateWork(
132+
if err = ctrlutil.CreateOrUpdateWork(
132133
ctx,
133134
c,
134135
workMeta,
135136
clonedWorkload,
136-
helper.WithSuspendDispatching(shouldSuspendDispatching(bindingSpec.Suspension, targetCluster)),
137-
helper.WithPreserveResourcesOnDeletion(ptr.Deref(bindingSpec.PreserveResourcesOnDeletion, false)),
137+
ctrlutil.WithSuspendDispatching(shouldSuspendDispatching(bindingSpec.Suspension, targetCluster)),
138+
ctrlutil.WithPreserveResourcesOnDeletion(ptr.Deref(bindingSpec.PreserveResourcesOnDeletion, false)),
138139
); err != nil {
139140
return err
140141
}

Diff for: pkg/controllers/ctrlutil/work.go

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
Copyright 2021 The Karmada Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package ctrlutil
18+
19+
import (
20+
"context"
21+
"fmt"
22+
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
25+
"k8s.io/apimachinery/pkg/runtime"
26+
"k8s.io/client-go/util/retry"
27+
"k8s.io/klog/v2"
28+
"sigs.k8s.io/controller-runtime/pkg/client"
29+
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
30+
31+
workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1"
32+
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
33+
"github.com/karmada-io/karmada/pkg/util"
34+
)
35+
36+
// CreateOrUpdateWork creates a Work object if not exist, or updates if it already exists.
37+
func CreateOrUpdateWork(ctx context.Context, client client.Client, workMeta metav1.ObjectMeta, resource *unstructured.Unstructured, options ...WorkOption) error {
38+
if workMeta.Labels[util.PropagationInstruction] != util.PropagationInstructionSuppressed {
39+
resource = resource.DeepCopy()
40+
// set labels
41+
util.MergeLabel(resource, util.ManagedByKarmadaLabel, util.ManagedByKarmadaLabelValue)
42+
// set annotations
43+
util.MergeAnnotation(resource, workv1alpha2.ResourceTemplateUIDAnnotation, string(resource.GetUID()))
44+
util.MergeAnnotation(resource, workv1alpha2.WorkNameAnnotation, workMeta.Name)
45+
util.MergeAnnotation(resource, workv1alpha2.WorkNamespaceAnnotation, workMeta.Namespace)
46+
if conflictResolution, ok := workMeta.GetAnnotations()[workv1alpha2.ResourceConflictResolutionAnnotation]; ok {
47+
util.MergeAnnotation(resource, workv1alpha2.ResourceConflictResolutionAnnotation, conflictResolution)
48+
}
49+
}
50+
51+
workloadJSON, err := resource.MarshalJSON()
52+
if err != nil {
53+
klog.Errorf("Failed to marshal workload(%s/%s), error: %v", resource.GetNamespace(), resource.GetName(), err)
54+
return err
55+
}
56+
57+
work := &workv1alpha1.Work{
58+
ObjectMeta: workMeta,
59+
Spec: workv1alpha1.WorkSpec{
60+
Workload: workv1alpha1.WorkloadTemplate{
61+
Manifests: []workv1alpha1.Manifest{
62+
{
63+
RawExtension: runtime.RawExtension{
64+
Raw: workloadJSON,
65+
},
66+
},
67+
},
68+
},
69+
},
70+
}
71+
72+
applyWorkOptions(work, options)
73+
74+
runtimeObject := work.DeepCopy()
75+
var operationResult controllerutil.OperationResult
76+
err = retry.RetryOnConflict(retry.DefaultRetry, func() (err error) {
77+
operationResult, err = controllerutil.CreateOrUpdate(ctx, client, runtimeObject, func() error {
78+
if !runtimeObject.DeletionTimestamp.IsZero() {
79+
return fmt.Errorf("work %s/%s is being deleted", runtimeObject.GetNamespace(), runtimeObject.GetName())
80+
}
81+
82+
runtimeObject.Spec = work.Spec
83+
runtimeObject.Labels = util.DedupeAndMergeLabels(runtimeObject.Labels, work.Labels)
84+
runtimeObject.Annotations = util.DedupeAndMergeAnnotations(runtimeObject.Annotations, work.Annotations)
85+
runtimeObject.Finalizers = work.Finalizers
86+
return nil
87+
})
88+
return err
89+
})
90+
if err != nil {
91+
klog.Errorf("Failed to create/update work %s/%s. Error: %v", work.GetNamespace(), work.GetName(), err)
92+
return err
93+
}
94+
95+
if operationResult == controllerutil.OperationResultCreated {
96+
klog.V(2).Infof("Create work %s/%s successfully.", work.GetNamespace(), work.GetName())
97+
} else if operationResult == controllerutil.OperationResultUpdated {
98+
klog.V(2).Infof("Update work %s/%s successfully.", work.GetNamespace(), work.GetName())
99+
} else {
100+
klog.V(2).Infof("Work %s/%s is up to date.", work.GetNamespace(), work.GetName())
101+
}
102+
103+
return nil
104+
}

Diff for: pkg/controllers/ctrlutil/work_test.go

+240
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
/*
2+
Copyright 2022 The Karmada Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package ctrlutil
18+
19+
import (
20+
"context"
21+
"testing"
22+
"time"
23+
24+
"github.com/stretchr/testify/assert"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
27+
"k8s.io/apimachinery/pkg/runtime"
28+
"sigs.k8s.io/controller-runtime/pkg/client"
29+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
30+
31+
workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1"
32+
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
33+
"github.com/karmada-io/karmada/pkg/util"
34+
)
35+
36+
func TestCreateOrUpdateWork(t *testing.T) {
37+
scheme := runtime.NewScheme()
38+
assert.NoError(t, workv1alpha1.Install(scheme))
39+
assert.NoError(t, workv1alpha2.Install(scheme))
40+
41+
tests := []struct {
42+
name string
43+
existingWork *workv1alpha1.Work
44+
workMeta metav1.ObjectMeta
45+
resource *unstructured.Unstructured
46+
wantErr bool
47+
verify func(*testing.T, client.Client)
48+
}{
49+
{
50+
name: "create new work",
51+
workMeta: metav1.ObjectMeta{
52+
Namespace: "default",
53+
Name: "test-work",
54+
},
55+
resource: &unstructured.Unstructured{
56+
Object: map[string]interface{}{
57+
"apiVersion": "apps/v1",
58+
"kind": "Deployment",
59+
"metadata": map[string]interface{}{
60+
"name": "test-deployment",
61+
"uid": "test-uid",
62+
},
63+
},
64+
},
65+
verify: func(t *testing.T, c client.Client) {
66+
work := &workv1alpha1.Work{}
67+
err := c.Get(context.TODO(), client.ObjectKey{Namespace: "default", Name: "test-work"}, work)
68+
assert.NoError(t, err)
69+
assert.Equal(t, "test-work", work.Name)
70+
assert.Equal(t, 1, len(work.Spec.Workload.Manifests))
71+
},
72+
},
73+
{
74+
name: "create work with PropagationInstruction",
75+
workMeta: metav1.ObjectMeta{
76+
Namespace: "default",
77+
Name: "test-work",
78+
Labels: map[string]string{
79+
util.PropagationInstruction: "some-value",
80+
},
81+
Annotations: map[string]string{
82+
workv1alpha2.ResourceConflictResolutionAnnotation: "overwrite",
83+
},
84+
},
85+
resource: &unstructured.Unstructured{
86+
Object: map[string]interface{}{
87+
"apiVersion": "apps/v1",
88+
"kind": "Deployment",
89+
"metadata": map[string]interface{}{
90+
"name": "test-deployment",
91+
"uid": "test-uid",
92+
},
93+
},
94+
},
95+
verify: func(t *testing.T, c client.Client) {
96+
work := &workv1alpha1.Work{}
97+
err := c.Get(context.TODO(), client.ObjectKey{Namespace: "default", Name: "test-work"}, work)
98+
assert.NoError(t, err)
99+
100+
// Get the resource from manifests
101+
manifest := &unstructured.Unstructured{}
102+
err = manifest.UnmarshalJSON(work.Spec.Workload.Manifests[0].Raw)
103+
assert.NoError(t, err)
104+
105+
// Verify labels and annotations were set
106+
labels := manifest.GetLabels()
107+
assert.Equal(t, util.ManagedByKarmadaLabelValue, labels[util.ManagedByKarmadaLabel])
108+
109+
annotations := manifest.GetAnnotations()
110+
assert.Equal(t, "test-uid", annotations[workv1alpha2.ResourceTemplateUIDAnnotation])
111+
assert.Equal(t, "test-work", annotations[workv1alpha2.WorkNameAnnotation])
112+
assert.Equal(t, "default", annotations[workv1alpha2.WorkNamespaceAnnotation])
113+
assert.Equal(t, "overwrite", annotations[workv1alpha2.ResourceConflictResolutionAnnotation])
114+
},
115+
},
116+
{
117+
name: "create work with PropagationInstructionSuppressed",
118+
workMeta: metav1.ObjectMeta{
119+
Namespace: "default",
120+
Name: "test-work",
121+
Labels: map[string]string{
122+
util.PropagationInstruction: util.PropagationInstructionSuppressed,
123+
},
124+
},
125+
resource: &unstructured.Unstructured{
126+
Object: map[string]interface{}{
127+
"apiVersion": "apps/v1",
128+
"kind": "Deployment",
129+
"metadata": map[string]interface{}{
130+
"name": "test-deployment",
131+
"uid": "test-uid",
132+
},
133+
},
134+
},
135+
verify: func(t *testing.T, c client.Client) {
136+
work := &workv1alpha1.Work{}
137+
err := c.Get(context.TODO(), client.ObjectKey{Namespace: "default", Name: "test-work"}, work)
138+
assert.NoError(t, err)
139+
140+
// Get the resource from manifests
141+
manifest := &unstructured.Unstructured{}
142+
err = manifest.UnmarshalJSON(work.Spec.Workload.Manifests[0].Raw)
143+
assert.NoError(t, err)
144+
145+
// Verify labels and annotations were NOT set
146+
labels := manifest.GetLabels()
147+
assert.Empty(t, labels[util.ManagedByKarmadaLabel])
148+
149+
annotations := manifest.GetAnnotations()
150+
assert.Empty(t, annotations[workv1alpha2.ResourceTemplateUIDAnnotation])
151+
},
152+
},
153+
{
154+
name: "update existing work",
155+
existingWork: &workv1alpha1.Work{
156+
ObjectMeta: metav1.ObjectMeta{
157+
Namespace: "default",
158+
Name: "test-work",
159+
},
160+
},
161+
workMeta: metav1.ObjectMeta{
162+
Namespace: "default",
163+
Name: "test-work",
164+
},
165+
resource: &unstructured.Unstructured{
166+
Object: map[string]interface{}{
167+
"apiVersion": "apps/v1",
168+
"kind": "Deployment",
169+
"metadata": map[string]interface{}{
170+
"name": "test-deployment",
171+
"uid": "test-uid",
172+
},
173+
},
174+
},
175+
verify: func(t *testing.T, c client.Client) {
176+
work := &workv1alpha1.Work{}
177+
err := c.Get(context.TODO(), client.ObjectKey{Namespace: "default", Name: "test-work"}, work)
178+
assert.NoError(t, err)
179+
assert.Equal(t, 1, len(work.Spec.Workload.Manifests))
180+
},
181+
},
182+
{
183+
name: "error when work is being deleted",
184+
existingWork: &workv1alpha1.Work{
185+
ObjectMeta: metav1.ObjectMeta{
186+
Namespace: "default",
187+
Name: "test-work",
188+
DeletionTimestamp: &metav1.Time{Time: time.Now()},
189+
Finalizers: []string{"test.finalizer.io"}, // Finalizer to satisfy fake client requirement
190+
},
191+
Spec: workv1alpha1.WorkSpec{
192+
Workload: workv1alpha1.WorkloadTemplate{
193+
Manifests: []workv1alpha1.Manifest{
194+
{
195+
RawExtension: runtime.RawExtension{
196+
Raw: []byte(`{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"name":"test-deployment"}}`),
197+
},
198+
},
199+
},
200+
},
201+
},
202+
},
203+
workMeta: metav1.ObjectMeta{
204+
Namespace: "default",
205+
Name: "test-work",
206+
},
207+
resource: &unstructured.Unstructured{
208+
Object: map[string]interface{}{
209+
"apiVersion": "apps/v1",
210+
"kind": "Deployment",
211+
"metadata": map[string]interface{}{
212+
"name": "test-deployment",
213+
},
214+
},
215+
},
216+
wantErr: true,
217+
},
218+
}
219+
220+
for _, tt := range tests {
221+
t.Run(tt.name, func(t *testing.T) {
222+
c := fake.NewClientBuilder().WithScheme(scheme)
223+
if tt.existingWork != nil {
224+
c = c.WithObjects(tt.existingWork)
225+
}
226+
client := c.Build()
227+
228+
err := CreateOrUpdateWork(context.TODO(), client, tt.workMeta, tt.resource)
229+
230+
if tt.wantErr {
231+
assert.Error(t, err)
232+
return
233+
}
234+
assert.NoError(t, err)
235+
if tt.verify != nil {
236+
tt.verify(t, client)
237+
}
238+
})
239+
}
240+
}

Diff for: pkg/util/helper/workoption.go renamed to pkg/controllers/ctrlutil/workoption.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package helper
17+
package ctrlutil
1818

1919
import workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1"
2020

Diff for: pkg/util/helper/workoption_test.go renamed to pkg/controllers/ctrlutil/workoption_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16-
package helper
16+
package ctrlutil
1717

1818
import (
1919
"testing"

0 commit comments

Comments
 (0)