Skip to content

Commit

Permalink
feat: add permanent id label to pp/cpp/rb/crb/work resources
Browse files Browse the repository at this point in the history
Signed-off-by: jwcesign <jwcesign@gmail.com>
  • Loading branch information
jwcesign authored and XiShanYongYe-Chang committed Mar 30, 2024
1 parent 4e25482 commit 284749d
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 1 deletion.
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}})
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 @@ -65,7 +66,7 @@ func RetainLabels(desired *unstructured.Unstructured, observed *unstructured.Uns
}

// MergeLabel adds label for the given object.
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
7 changes: 7 additions & 0 deletions pkg/webhook/clusterpropagationpolicy/mutating.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ import (
"fmt"
"net/http"

"github.com/google/uuid"
admissionv1 "k8s.io/api/admission/v1"
"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 +83,10 @@ func (a *MutatingAdmission) Handle(_ context.Context, req admission.Request) adm
}
}

if req.Operation == admissionv1.Create {
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"

admissionv1 "k8s.io/api/admission/v1"

"github.com/google/uuid"
"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)
}

if req.Operation == admissionv1.Create {
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)
}
7 changes: 7 additions & 0 deletions pkg/webhook/propagationpolicy/mutating.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ import (
"fmt"
"net/http"

"github.com/google/uuid"
admissionv1 "k8s.io/api/admission/v1"
"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 +95,10 @@ func (a *MutatingAdmission) Handle(_ context.Context, req admission.Request) adm
}
}

if req.Operation == admissionv1.Create {
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"

admissionv1 "k8s.io/api/admission/v1"

"github.com/google/uuid"
"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)
}

if req.Operation == admissionv1.Create {
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)
}
8 changes: 8 additions & 0 deletions pkg/webhook/work/mutating.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ import (
"encoding/json"
"net/http"

"github.com/google/uuid"
admissionv1 "k8s.io/api/admission/v1"
"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 +82,9 @@ func (a *MutatingAdmission) Handle(_ context.Context, req admission.Request) adm
return admission.Errored(http.StatusInternalServerError, err)
}

if req.Operation == admissionv1.Create {
util.MergeLabel(work, workv1alpha2.WorkPermanentIDLabel, uuid.New().String())
}

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

0 comments on commit 284749d

Please sign in to comment.