Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add permanent id label to pp/cpp/rb/crb/work resources #4474

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions artifacts/deploy/webhook-configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,34 @@ webhooks:
sideEffects: None
admissionReviewVersions: ["v1"]
timeoutSeconds: 3
- name: resourcebinding.karmada.io
rules:
- operations: ["CREATE"]
apiGroups: ["work.karmada.io"]
apiVersions: ["*"]
resources: ["resourcebindings"]
scope: "Namespaced"
clientConfig:
url: https://karmada-webhook.karmada-system.svc:443/mutate-resourcebinding
caBundle: {{caBundle}}
failurePolicy: Fail
sideEffects: None
admissionReviewVersions: ["v1"]
timeoutSeconds: 3
- name: clusterresourcebinding.karmada.io
rules:
- operations: ["CREATE"]
apiGroups: ["work.karmada.io"]
apiVersions: ["*"]
resources: ["clusterresourcebindings"]
scope: "Cluster"
clientConfig:
url: https://karmada-webhook.karmada-system.svc:443/mutate-clusterresourcebinding
caBundle: {{caBundle}}
failurePolicy: Fail
sideEffects: None
admissionReviewVersions: ["v1"]
timeoutSeconds: 3
- name: work.karmada.io
rules:
- operations: ["CREATE", "UPDATE"]
Expand Down
28 changes: 28 additions & 0 deletions charts/karmada/templates/_karmada_webhook_configuration.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,34 @@ webhooks:
sideEffects: None
admissionReviewVersions: ["v1"]
timeoutSeconds: 3
- name: resourcebinding.karmada.io
rules:
- operations: ["CREATE"]
apiGroups: ["work.karmada.io"]
apiVersions: ["*"]
resources: ["resourcebindings"]
scope: "Namespaced"
clientConfig:
url: https://{{ $name }}-webhook.{{ $namespace }}.svc:443/mutate-resourcebinding
{{- include "karmada.webhook.caBundle" . | nindent 6 }}
failurePolicy: Fail
sideEffects: None
admissionReviewVersions: ["v1"]
timeoutSeconds: 3
- name: clusterresourcebinding.karmada.io
rules:
- operations: ["CREATE"]
apiGroups: ["work.karmada.io"]
apiVersions: ["*"]
resources: ["clusterresourcebindings"]
scope: "Namespaced"
clientConfig:
url: https://{{ $name }}-webhook.{{ $namespace }}.svc:443/mutate-clusterresourcebinding
{{- include "karmada.webhook.caBundle" . | nindent 6 }}
failurePolicy: Fail
sideEffects: None
admissionReviewVersions: ["v1"]
timeoutSeconds: 3
- name: work.karmada.io
rules:
- operations: ["CREATE", "UPDATE"]
Expand Down
4 changes: 4 additions & 0 deletions cmd/webhook/app/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"github.com/karmada-io/karmada/pkg/version/sharedcommand"
"github.com/karmada-io/karmada/pkg/webhook/clusteroverridepolicy"
"github.com/karmada-io/karmada/pkg/webhook/clusterpropagationpolicy"
"github.com/karmada-io/karmada/pkg/webhook/clusterresourcebinding"
"github.com/karmada-io/karmada/pkg/webhook/configuration"
"github.com/karmada-io/karmada/pkg/webhook/cronfederatedhpa"
"github.com/karmada-io/karmada/pkg/webhook/federatedhpa"
Expand All @@ -51,6 +52,7 @@ import (
"github.com/karmada-io/karmada/pkg/webhook/multiclusterservice"
"github.com/karmada-io/karmada/pkg/webhook/overridepolicy"
"github.com/karmada-io/karmada/pkg/webhook/propagationpolicy"
"github.com/karmada-io/karmada/pkg/webhook/resourcebinding"
"github.com/karmada-io/karmada/pkg/webhook/resourcedeletionprotection"
"github.com/karmada-io/karmada/pkg/webhook/resourceinterpretercustomization"
"github.com/karmada-io/karmada/pkg/webhook/work"
Expand Down Expand Up @@ -176,6 +178,8 @@ func Run(ctx context.Context, opts *options.Options) error {
hookServer.Register("/mutate-multiclusterservice", &webhook.Admission{Handler: &multiclusterservice.MutatingAdmission{Decoder: decoder}})
hookServer.Register("/mutate-federatedhpa", &webhook.Admission{Handler: &federatedhpa.MutatingAdmission{Decoder: decoder}})
hookServer.Register("/validate-resourcedeletionprotection", &webhook.Admission{Handler: &resourcedeletionprotection.ValidatingAdmission{Decoder: decoder}})
hookServer.Register("/mutate-resourcebinding", &webhook.Admission{Handler: &resourcebinding.MutatingAdmission{Decoder: decoder}})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if adding this is reasonable, but it seems it can also be handled in the controller without adding it.

Copy link
Member Author

@jwcesign jwcesign Dec 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a little bit complex and difficult to maintain the code in controller, I prefer to add this in weebhook, not in controller

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chaunceyjiang What's your concern?
I can tell a challenge for the user to update the MutatingWebhookConfiguration during upgrading.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I waiting for @chaunceyjiang about this :)

hookServer.Register("/mutate-clusterresourcebinding", &webhook.Admission{Handler: &clusterresourcebinding.MutatingAdmission{Decoder: decoder}})
hookServer.WebhookMux().Handle("/readyz/", http.StripPrefix("/readyz/", &healthz.Handler{}))

// blocks until the context is done.
Expand Down
28 changes: 28 additions & 0 deletions operator/pkg/karmadaresource/webhookconfiguration/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,34 @@ webhooks:
sideEffects: None
admissionReviewVersions: ["v1"]
timeoutSeconds: 3
- name: resourcebinding.karmada.io
rules:
- operations: ["CREATE"]
apiGroups: ["work.karmada.io"]
apiVersions: ["*"]
resources: ["resourcebindings"]
scope: "Namespaced"
clientConfig:
url: https://{{ .Service }}.{{ .Namespace }}.svc:443/mutate-resourcebinding
caBundle: {{ .CaBundle }}
failurePolicy: Fail
sideEffects: None
admissionReviewVersions: ["v1"]
timeoutSeconds: 3
- name: clusterresourcebinding.karmada.io
rules:
- operations: ["CREATE"]
apiGroups: ["work.karmada.io"]
apiVersions: ["*"]
resources: ["clusterresourcebindings"]
scope: "Cluster"
clientConfig:
url: https://{{ .Service }}.{{ .Namespace }}.svc:443/mutate-clusterresourcebinding
caBundle: {{ .CaBundle }}
failurePolicy: Fail
sideEffects: None
admissionReviewVersions: ["v1"]
timeoutSeconds: 3
- name: work.karmada.io
rules:
- operations: ["CREATE", "UPDATE"]
Expand Down
28 changes: 28 additions & 0 deletions pkg/karmadactl/cmdinit/karmada/webhook_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,34 @@ webhooks:
sideEffects: None
admissionReviewVersions: ["v1"]
timeoutSeconds: 3
- name: resourcebinding.karmada.io
rules:
- operations: ["CREATE"]
apiGroups: ["work.karmada.io"]
apiVersions: ["*"]
resources: ["resourcebindings"]
scope: "Namespaced"
clientConfig:
url: https://karmada-webhook.%[1]s.svc:443/mutate-resourcebinding
caBundle: %[2]s
failurePolicy: Fail
sideEffects: None
admissionReviewVersions: ["v1"]
timeoutSeconds: 3
- name: clusterresourcebinding.karmada.io
rules:
- operations: ["CREATE"]
apiGroups: ["work.karmada.io"]
apiVersions: ["*"]
resources: ["clusterresourcebindings"]
scope: "Cluster"
clientConfig:
url: https://karmada-webhook.%[1]s.svc:443/mutate-clusterresourcebinding
caBundle: %[2]s
failurePolicy: Fail
sideEffects: None
admissionReviewVersions: ["v1"]
timeoutSeconds: 3
- name: work.karmada.io
rules:
- operations: ["CREATE", "UPDATE"]
Expand Down
3 changes: 2 additions & 1 deletion pkg/util/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"sort"
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/sets"

Expand Down Expand Up @@ -64,7 +65,7 @@ func RetainLabels(desired *unstructured.Unstructured, observed *unstructured.Uns
}

// MergeLabel adds label for the given object, replace the value if key exist.
func MergeLabel(obj *unstructured.Unstructured, labelKey string, labelValue string) {
func MergeLabel(obj metav1.Object, labelKey string, labelValue string) {
labels := obj.GetLabels()
if labels == nil {
labels = make(map[string]string, 1)
Expand Down
6 changes: 6 additions & 0 deletions pkg/webhook/clusterpropagationpolicy/mutating.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ import (
"fmt"
"net/http"

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

policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
"github.com/karmada-io/karmada/pkg/util"
"github.com/karmada-io/karmada/pkg/util/helper"
"github.com/karmada-io/karmada/pkg/util/validation"
)
Expand Down Expand Up @@ -80,6 +82,10 @@ func (a *MutatingAdmission) Handle(_ context.Context, req admission.Request) adm
}
}

if util.GetLabelValue(policy.Labels, policyv1alpha1.ClusterPropagationPolicyPermanentIDLabel) == "" {
util.MergeLabel(policy, policyv1alpha1.ClusterPropagationPolicyPermanentIDLabel, uuid.New().String())
}

marshaledBytes, err := json.Marshal(policy)
if err != nil {
return admission.Errored(http.StatusInternalServerError, err)
Expand Down
60 changes: 60 additions & 0 deletions pkg/webhook/clusterresourcebinding/mutating.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright 2023 The Karmada Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package clusterresourcebinding

import (
"context"
"encoding/json"
"net/http"

"github.com/google/uuid"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
"github.com/karmada-io/karmada/pkg/util"
)

// MutatingAdmission mutates API request if necessary.
type MutatingAdmission struct {
Decoder *admission.Decoder
}

// Check if our MutatingAdmission implements necessary interface
var _ admission.Handler = &MutatingAdmission{}

// Handle yields a response to an AdmissionRequest.
func (a *MutatingAdmission) Handle(_ context.Context, req admission.Request) admission.Response {
crb := &workv1alpha2.ClusterResourceBinding{}

err := a.Decoder.Decode(req, crb)
if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
klog.V(2).Infof("Mutating clusterResourceBinding(%s) for request: %s", crb.Name, req.Operation)

if util.GetLabelValue(crb.Labels, workv1alpha2.ClusterResourceBindingPermanentIDLabel) == "" {
util.MergeLabel(crb, workv1alpha2.ClusterResourceBindingPermanentIDLabel, uuid.New().String())
}

marshaledBytes, err := json.Marshal(crb)
if err != nil {
return admission.Errored(http.StatusInternalServerError, err)
}

return admission.PatchResponseFromRaw(req.Object.Raw, marshaledBytes)
}
6 changes: 6 additions & 0 deletions pkg/webhook/propagationpolicy/mutating.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import (
"fmt"
"net/http"

"github.com/google/uuid"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
"github.com/karmada-io/karmada/pkg/util"
"github.com/karmada-io/karmada/pkg/util/helper"
"github.com/karmada-io/karmada/pkg/util/validation"
)
Expand Down Expand Up @@ -92,6 +94,10 @@ func (a *MutatingAdmission) Handle(_ context.Context, req admission.Request) adm
}
}

if util.GetLabelValue(policy.Labels, policyv1alpha1.PropagationPolicyPermanentIDLabel) == "" {
util.MergeLabel(policy, policyv1alpha1.PropagationPolicyPermanentIDLabel, uuid.New().String())
}

marshaledBytes, err := json.Marshal(policy)
if err != nil {
return admission.Errored(http.StatusInternalServerError, err)
Expand Down
60 changes: 60 additions & 0 deletions pkg/webhook/resourcebinding/mutating.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright 2023 The Karmada Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package resourcebinding

import (
"context"
"encoding/json"
"net/http"

"github.com/google/uuid"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
"github.com/karmada-io/karmada/pkg/util"
)

// MutatingAdmission mutates API request if necessary.
type MutatingAdmission struct {
Decoder *admission.Decoder
}

// Check if our MutatingAdmission implements necessary interface
var _ admission.Handler = &MutatingAdmission{}

// Handle yields a response to an AdmissionRequest.
func (a *MutatingAdmission) Handle(_ context.Context, req admission.Request) admission.Response {
rb := &workv1alpha2.ResourceBinding{}

err := a.Decoder.Decode(req, rb)
if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
klog.V(2).Infof("Mutating resourceBinding(%s/%s) for request: %s", rb.Namespace, rb.Name, req.Operation)

if util.GetLabelValue(rb.Labels, workv1alpha2.ResourceBindingPermanentIDLabel) == "" {
util.MergeLabel(rb, workv1alpha2.ResourceBindingPermanentIDLabel, uuid.New().String())
}

marshaledBytes, err := json.Marshal(rb)
if err != nil {
return admission.Errored(http.StatusInternalServerError, err)
}

return admission.PatchResponseFromRaw(req.Object.Raw, marshaledBytes)
}
7 changes: 7 additions & 0 deletions pkg/webhook/work/mutating.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ import (
"encoding/json"
"net/http"

"github.com/google/uuid"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1"
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
"github.com/karmada-io/karmada/pkg/resourceinterpreter/default/native/prune"
"github.com/karmada-io/karmada/pkg/util"
)

// MutatingAdmission mutates API request if necessary.
Expand Down Expand Up @@ -78,5 +81,9 @@ func (a *MutatingAdmission) Handle(_ context.Context, req admission.Request) adm
return admission.Errored(http.StatusInternalServerError, err)
}

if util.GetLabelValue(work.Labels, workv1alpha2.WorkPermanentIDLabel) == "" {
util.MergeLabel(work, workv1alpha2.WorkPermanentIDLabel, uuid.New().String())
}

return admission.PatchResponseFromRaw(req.Object.Raw, marshaledBytes)
}
Loading