Skip to content

Commit f33f3f8

Browse files
authored
Merge pull request #4474 from jwcesign/add-uid
feat: add permanent id label to pp/cpp/rb/crb/work resources
2 parents 211351b + 6a54a9a commit f33f3f8

File tree

11 files changed

+256
-1
lines changed

11 files changed

+256
-1
lines changed

artifacts/deploy/webhook-configuration.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,34 @@ webhooks:
4747
sideEffects: None
4848
admissionReviewVersions: ["v1"]
4949
timeoutSeconds: 3
50+
- name: resourcebinding.karmada.io
51+
rules:
52+
- operations: ["CREATE"]
53+
apiGroups: ["work.karmada.io"]
54+
apiVersions: ["*"]
55+
resources: ["resourcebindings"]
56+
scope: "Namespaced"
57+
clientConfig:
58+
url: https://karmada-webhook.karmada-system.svc:443/mutate-resourcebinding
59+
caBundle: {{caBundle}}
60+
failurePolicy: Fail
61+
sideEffects: None
62+
admissionReviewVersions: ["v1"]
63+
timeoutSeconds: 3
64+
- name: clusterresourcebinding.karmada.io
65+
rules:
66+
- operations: ["CREATE"]
67+
apiGroups: ["work.karmada.io"]
68+
apiVersions: ["*"]
69+
resources: ["clusterresourcebindings"]
70+
scope: "Cluster"
71+
clientConfig:
72+
url: https://karmada-webhook.karmada-system.svc:443/mutate-clusterresourcebinding
73+
caBundle: {{caBundle}}
74+
failurePolicy: Fail
75+
sideEffects: None
76+
admissionReviewVersions: ["v1"]
77+
timeoutSeconds: 3
5078
- name: work.karmada.io
5179
rules:
5280
- operations: ["CREATE", "UPDATE"]

charts/karmada/templates/_karmada_webhook_configuration.tpl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,34 @@ webhooks:
5252
sideEffects: None
5353
admissionReviewVersions: ["v1"]
5454
timeoutSeconds: 3
55+
- name: resourcebinding.karmada.io
56+
rules:
57+
- operations: ["CREATE"]
58+
apiGroups: ["work.karmada.io"]
59+
apiVersions: ["*"]
60+
resources: ["resourcebindings"]
61+
scope: "Namespaced"
62+
clientConfig:
63+
url: https://{{ $name }}-webhook.{{ $namespace }}.svc:443/mutate-resourcebinding
64+
{{- include "karmada.webhook.caBundle" . | nindent 6 }}
65+
failurePolicy: Fail
66+
sideEffects: None
67+
admissionReviewVersions: ["v1"]
68+
timeoutSeconds: 3
69+
- name: clusterresourcebinding.karmada.io
70+
rules:
71+
- operations: ["CREATE"]
72+
apiGroups: ["work.karmada.io"]
73+
apiVersions: ["*"]
74+
resources: ["clusterresourcebindings"]
75+
scope: "Namespaced"
76+
clientConfig:
77+
url: https://{{ $name }}-webhook.{{ $namespace }}.svc:443/mutate-clusterresourcebinding
78+
{{- include "karmada.webhook.caBundle" . | nindent 6 }}
79+
failurePolicy: Fail
80+
sideEffects: None
81+
admissionReviewVersions: ["v1"]
82+
timeoutSeconds: 3
5583
- name: work.karmada.io
5684
rules:
5785
- operations: ["CREATE", "UPDATE"]

cmd/webhook/app/webhook.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
"github.com/karmada-io/karmada/pkg/version/sharedcommand"
4444
"github.com/karmada-io/karmada/pkg/webhook/clusteroverridepolicy"
4545
"github.com/karmada-io/karmada/pkg/webhook/clusterpropagationpolicy"
46+
"github.com/karmada-io/karmada/pkg/webhook/clusterresourcebinding"
4647
"github.com/karmada-io/karmada/pkg/webhook/configuration"
4748
"github.com/karmada-io/karmada/pkg/webhook/cronfederatedhpa"
4849
"github.com/karmada-io/karmada/pkg/webhook/federatedhpa"
@@ -51,6 +52,7 @@ import (
5152
"github.com/karmada-io/karmada/pkg/webhook/multiclusterservice"
5253
"github.com/karmada-io/karmada/pkg/webhook/overridepolicy"
5354
"github.com/karmada-io/karmada/pkg/webhook/propagationpolicy"
55+
"github.com/karmada-io/karmada/pkg/webhook/resourcebinding"
5456
"github.com/karmada-io/karmada/pkg/webhook/resourcedeletionprotection"
5557
"github.com/karmada-io/karmada/pkg/webhook/resourceinterpretercustomization"
5658
"github.com/karmada-io/karmada/pkg/webhook/work"
@@ -176,6 +178,8 @@ func Run(ctx context.Context, opts *options.Options) error {
176178
hookServer.Register("/mutate-multiclusterservice", &webhook.Admission{Handler: &multiclusterservice.MutatingAdmission{Decoder: decoder}})
177179
hookServer.Register("/mutate-federatedhpa", &webhook.Admission{Handler: &federatedhpa.MutatingAdmission{Decoder: decoder}})
178180
hookServer.Register("/validate-resourcedeletionprotection", &webhook.Admission{Handler: &resourcedeletionprotection.ValidatingAdmission{Decoder: decoder}})
181+
hookServer.Register("/mutate-resourcebinding", &webhook.Admission{Handler: &resourcebinding.MutatingAdmission{Decoder: decoder}})
182+
hookServer.Register("/mutate-clusterresourcebinding", &webhook.Admission{Handler: &clusterresourcebinding.MutatingAdmission{Decoder: decoder}})
179183
hookServer.WebhookMux().Handle("/readyz/", http.StripPrefix("/readyz/", &healthz.Handler{}))
180184

181185
// blocks until the context is done.

operator/pkg/karmadaresource/webhookconfiguration/manifests.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,34 @@ webhooks:
6868
sideEffects: None
6969
admissionReviewVersions: ["v1"]
7070
timeoutSeconds: 3
71+
- name: resourcebinding.karmada.io
72+
rules:
73+
- operations: ["CREATE"]
74+
apiGroups: ["work.karmada.io"]
75+
apiVersions: ["*"]
76+
resources: ["resourcebindings"]
77+
scope: "Namespaced"
78+
clientConfig:
79+
url: https://{{ .Service }}.{{ .Namespace }}.svc:443/mutate-resourcebinding
80+
caBundle: {{ .CaBundle }}
81+
failurePolicy: Fail
82+
sideEffects: None
83+
admissionReviewVersions: ["v1"]
84+
timeoutSeconds: 3
85+
- name: clusterresourcebinding.karmada.io
86+
rules:
87+
- operations: ["CREATE"]
88+
apiGroups: ["work.karmada.io"]
89+
apiVersions: ["*"]
90+
resources: ["clusterresourcebindings"]
91+
scope: "Cluster"
92+
clientConfig:
93+
url: https://{{ .Service }}.{{ .Namespace }}.svc:443/mutate-clusterresourcebinding
94+
caBundle: {{ .CaBundle }}
95+
failurePolicy: Fail
96+
sideEffects: None
97+
admissionReviewVersions: ["v1"]
98+
timeoutSeconds: 3
7199
- name: work.karmada.io
72100
rules:
73101
- operations: ["CREATE", "UPDATE"]

pkg/karmadactl/cmdinit/karmada/webhook_configuration.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,34 @@ webhooks:
8080
sideEffects: None
8181
admissionReviewVersions: ["v1"]
8282
timeoutSeconds: 3
83+
- name: resourcebinding.karmada.io
84+
rules:
85+
- operations: ["CREATE"]
86+
apiGroups: ["work.karmada.io"]
87+
apiVersions: ["*"]
88+
resources: ["resourcebindings"]
89+
scope: "Namespaced"
90+
clientConfig:
91+
url: https://karmada-webhook.%[1]s.svc:443/mutate-resourcebinding
92+
caBundle: %[2]s
93+
failurePolicy: Fail
94+
sideEffects: None
95+
admissionReviewVersions: ["v1"]
96+
timeoutSeconds: 3
97+
- name: clusterresourcebinding.karmada.io
98+
rules:
99+
- operations: ["CREATE"]
100+
apiGroups: ["work.karmada.io"]
101+
apiVersions: ["*"]
102+
resources: ["clusterresourcebindings"]
103+
scope: "Cluster"
104+
clientConfig:
105+
url: https://karmada-webhook.%[1]s.svc:443/mutate-clusterresourcebinding
106+
caBundle: %[2]s
107+
failurePolicy: Fail
108+
sideEffects: None
109+
admissionReviewVersions: ["v1"]
110+
timeoutSeconds: 3
83111
- name: work.karmada.io
84112
rules:
85113
- operations: ["CREATE", "UPDATE"]

pkg/util/label.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func RetainLabels(desired *unstructured.Unstructured, observed *unstructured.Uns
6565
}
6666

6767
// MergeLabel adds label for the given object, replace the value if key exist.
68-
func MergeLabel(obj *unstructured.Unstructured, labelKey string, labelValue string) {
68+
func MergeLabel(obj metav1.Object, labelKey string, labelValue string) {
6969
labels := obj.GetLabels()
7070
if labels == nil {
7171
labels = make(map[string]string, 1)

pkg/webhook/clusterpropagationpolicy/mutating.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ import (
2222
"fmt"
2323
"net/http"
2424

25+
"github.com/google/uuid"
2526
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2627

2728
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
29+
"github.com/karmada-io/karmada/pkg/util"
2830
"github.com/karmada-io/karmada/pkg/util/helper"
2931
"github.com/karmada-io/karmada/pkg/util/validation"
3032
)
@@ -80,6 +82,10 @@ func (a *MutatingAdmission) Handle(_ context.Context, req admission.Request) adm
8082
}
8183
}
8284

85+
if util.GetLabelValue(policy.Labels, policyv1alpha1.ClusterPropagationPolicyPermanentIDLabel) == "" {
86+
util.MergeLabel(policy, policyv1alpha1.ClusterPropagationPolicyPermanentIDLabel, uuid.New().String())
87+
}
88+
8389
marshaledBytes, err := json.Marshal(policy)
8490
if err != nil {
8591
return admission.Errored(http.StatusInternalServerError, err)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Copyright 2023 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 clusterresourcebinding
18+
19+
import (
20+
"context"
21+
"encoding/json"
22+
"net/http"
23+
24+
"github.com/google/uuid"
25+
"k8s.io/klog/v2"
26+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
27+
28+
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
29+
"github.com/karmada-io/karmada/pkg/util"
30+
)
31+
32+
// MutatingAdmission mutates API request if necessary.
33+
type MutatingAdmission struct {
34+
Decoder *admission.Decoder
35+
}
36+
37+
// Check if our MutatingAdmission implements necessary interface
38+
var _ admission.Handler = &MutatingAdmission{}
39+
40+
// Handle yields a response to an AdmissionRequest.
41+
func (a *MutatingAdmission) Handle(_ context.Context, req admission.Request) admission.Response {
42+
crb := &workv1alpha2.ClusterResourceBinding{}
43+
44+
err := a.Decoder.Decode(req, crb)
45+
if err != nil {
46+
return admission.Errored(http.StatusBadRequest, err)
47+
}
48+
klog.V(2).Infof("Mutating clusterResourceBinding(%s) for request: %s", crb.Name, req.Operation)
49+
50+
if util.GetLabelValue(crb.Labels, workv1alpha2.ClusterResourceBindingPermanentIDLabel) == "" {
51+
util.MergeLabel(crb, workv1alpha2.ClusterResourceBindingPermanentIDLabel, uuid.New().String())
52+
}
53+
54+
marshaledBytes, err := json.Marshal(crb)
55+
if err != nil {
56+
return admission.Errored(http.StatusInternalServerError, err)
57+
}
58+
59+
return admission.PatchResponseFromRaw(req.Object.Raw, marshaledBytes)
60+
}

pkg/webhook/propagationpolicy/mutating.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ import (
2222
"fmt"
2323
"net/http"
2424

25+
"github.com/google/uuid"
2526
"k8s.io/klog/v2"
2627
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2728

2829
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
30+
"github.com/karmada-io/karmada/pkg/util"
2931
"github.com/karmada-io/karmada/pkg/util/helper"
3032
"github.com/karmada-io/karmada/pkg/util/validation"
3133
)
@@ -92,6 +94,10 @@ func (a *MutatingAdmission) Handle(_ context.Context, req admission.Request) adm
9294
}
9395
}
9496

97+
if util.GetLabelValue(policy.Labels, policyv1alpha1.PropagationPolicyPermanentIDLabel) == "" {
98+
util.MergeLabel(policy, policyv1alpha1.PropagationPolicyPermanentIDLabel, uuid.New().String())
99+
}
100+
95101
marshaledBytes, err := json.Marshal(policy)
96102
if err != nil {
97103
return admission.Errored(http.StatusInternalServerError, err)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Copyright 2023 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 resourcebinding
18+
19+
import (
20+
"context"
21+
"encoding/json"
22+
"net/http"
23+
24+
"github.com/google/uuid"
25+
"k8s.io/klog/v2"
26+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
27+
28+
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
29+
"github.com/karmada-io/karmada/pkg/util"
30+
)
31+
32+
// MutatingAdmission mutates API request if necessary.
33+
type MutatingAdmission struct {
34+
Decoder *admission.Decoder
35+
}
36+
37+
// Check if our MutatingAdmission implements necessary interface
38+
var _ admission.Handler = &MutatingAdmission{}
39+
40+
// Handle yields a response to an AdmissionRequest.
41+
func (a *MutatingAdmission) Handle(_ context.Context, req admission.Request) admission.Response {
42+
rb := &workv1alpha2.ResourceBinding{}
43+
44+
err := a.Decoder.Decode(req, rb)
45+
if err != nil {
46+
return admission.Errored(http.StatusBadRequest, err)
47+
}
48+
klog.V(2).Infof("Mutating resourceBinding(%s/%s) for request: %s", rb.Namespace, rb.Name, req.Operation)
49+
50+
if util.GetLabelValue(rb.Labels, workv1alpha2.ResourceBindingPermanentIDLabel) == "" {
51+
util.MergeLabel(rb, workv1alpha2.ResourceBindingPermanentIDLabel, uuid.New().String())
52+
}
53+
54+
marshaledBytes, err := json.Marshal(rb)
55+
if err != nil {
56+
return admission.Errored(http.StatusInternalServerError, err)
57+
}
58+
59+
return admission.PatchResponseFromRaw(req.Object.Raw, marshaledBytes)
60+
}

0 commit comments

Comments
 (0)