Skip to content

Commit b7678c9

Browse files
Add validation on policy permanent ID
Signed-off-by: whitewindmills <[email protected]>
1 parent 3314771 commit b7678c9

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

pkg/webhook/clusterpropagationpolicy/mutating.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"net/http"
2424

2525
"github.com/google/uuid"
26+
admissionv1 "k8s.io/api/admission/v1"
2627
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2728

2829
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
@@ -82,7 +83,7 @@ func (a *MutatingAdmission) Handle(_ context.Context, req admission.Request) adm
8283
}
8384
}
8485

85-
if util.GetLabelValue(policy.Labels, policyv1alpha1.ClusterPropagationPolicyPermanentIDLabel) == "" {
86+
if req.Operation == admissionv1.Create {
8687
util.MergeLabel(policy, policyv1alpha1.ClusterPropagationPolicyPermanentIDLabel, uuid.New().String())
8788
}
8889

pkg/webhook/clusterpropagationpolicy/validating.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ func (v *ValidatingAdmission) Handle(_ context.Context, req admission.Request) a
6060
klog.Error(err)
6161
return admission.Denied(err.Error())
6262
}
63+
64+
if policy.Labels[policyv1alpha1.ClusterPropagationPolicyPermanentIDLabel] !=
65+
oldPolicy.Labels[policyv1alpha1.ClusterPropagationPolicyPermanentIDLabel] {
66+
return admission.Denied(fmt.Sprintf("label %s is immutable, it can only be set by the system during creation",
67+
policyv1alpha1.ClusterPropagationPolicyPermanentIDLabel))
68+
}
69+
}
70+
if _, exist := policy.Labels[policyv1alpha1.ClusterPropagationPolicyPermanentIDLabel]; !exist {
71+
return admission.Denied(fmt.Sprintf("label %s is required, it should be set by the mutating admission webhook during creation",
72+
policyv1alpha1.ClusterPropagationPolicyPermanentIDLabel))
6373
}
6474

6575
errs := validation.ValidatePropagationSpec(policy.Spec)

pkg/webhook/propagationpolicy/mutating.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"net/http"
2424

2525
"github.com/google/uuid"
26+
admissionv1 "k8s.io/api/admission/v1"
2627
"k8s.io/klog/v2"
2728
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2829

@@ -94,7 +95,7 @@ func (a *MutatingAdmission) Handle(_ context.Context, req admission.Request) adm
9495
}
9596
}
9697

97-
if util.GetLabelValue(policy.Labels, policyv1alpha1.PropagationPolicyPermanentIDLabel) == "" {
98+
if req.Operation == admissionv1.Create {
9899
util.MergeLabel(policy, policyv1alpha1.PropagationPolicyPermanentIDLabel, uuid.New().String())
99100
}
100101

pkg/webhook/propagationpolicy/validating.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ func (v *ValidatingAdmission) Handle(_ context.Context, req admission.Request) a
6060
klog.Error(err)
6161
return admission.Denied(err.Error())
6262
}
63+
64+
if policy.Labels[policyv1alpha1.PropagationPolicyPermanentIDLabel] !=
65+
oldPolicy.Labels[policyv1alpha1.PropagationPolicyPermanentIDLabel] {
66+
return admission.Denied(fmt.Sprintf("label %s is immutable, it can only be set by the system during creation",
67+
policyv1alpha1.PropagationPolicyPermanentIDLabel))
68+
}
69+
}
70+
if _, exist := policy.Labels[policyv1alpha1.PropagationPolicyPermanentIDLabel]; !exist {
71+
return admission.Denied(fmt.Sprintf("label %s is required, it should be set by the mutating admission webhook during creation",
72+
policyv1alpha1.PropagationPolicyPermanentIDLabel))
6373
}
6474

6575
errs := validation.ValidatePropagationSpec(policy.Spec)

0 commit comments

Comments
 (0)