-
Notifications
You must be signed in to change notification settings - Fork 346
/
clusterclaim_webhook.go
105 lines (93 loc) · 4.15 KB
/
clusterclaim_webhook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
Copyright 2022 Antrea 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 main
import (
"context"
"encoding/json"
"fmt"
"net/http"
admissionv1 "k8s.io/api/admission/v1"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
mcv1alpha1 "antrea.io/antrea/multicluster/apis/multicluster/v1alpha1"
mcv1alpha2 "antrea.io/antrea/multicluster/apis/multicluster/v1alpha2"
)
//+kubebuilder:webhook:path=/validate-multicluster-crd-antrea-io-v1alpha2-clusterclaim,mutating=false,failurePolicy=fail,sideEffects=None,groups=multicluster.crd.antrea.io,resources=clusterclaims,verbs=create;update;delete,versions=v1alpha2,name=vclusterclaim.kb.io,admissionReviewVersions={v1,v1beta1}
// ClusterClaim validator
type clusterClaimValidator struct {
Client client.Client
decoder *admission.Decoder
namespace string
}
// Handle handles admission requests.
func (v *clusterClaimValidator) Handle(ctx context.Context, req admission.Request) admission.Response {
clusterClaim := &mcv1alpha2.ClusterClaim{}
err := v.decoder.Decode(req, clusterClaim)
if err != nil {
klog.ErrorS(err, "Error while decoding ClusterClaim", "ClusterClaim", req.Namespace+"/"+req.Name)
return admission.Errored(http.StatusBadRequest, err)
}
switch req.Operation {
case admissionv1.Create:
if clusterClaim.Name != mcv1alpha2.WellKnownClusterClaimClusterSet && clusterClaim.Name != mcv1alpha2.WellKnownClusterClaimID {
return admission.Denied(fmt.Sprintf("name %s is not valid. Only 'id.k8s.io' and 'clusterset.k8s.io' are valid names for ClusterClaim\n", clusterClaim.Name))
}
case admissionv1.Update:
oldClusterClaim := &mcv1alpha2.ClusterClaim{}
if req.OldObject.Raw != nil {
if err := json.Unmarshal(req.OldObject.Raw, &oldClusterClaim); err != nil {
klog.ErrorS(err, "Error while decoding old ClusterClaim", "ClusterClaim", klog.KObj(clusterClaim))
return admission.Errored(http.StatusBadRequest, err)
}
if oldClusterClaim.Value != clusterClaim.Value {
klog.ErrorS(err, "The field 'value' is immutable", "ClusterClaim", klog.KObj(clusterClaim))
return admission.Denied("the field 'value' is immutable")
}
return admission.Allowed("")
}
case admissionv1.Delete:
clusterSetList := &mcv1alpha1.ClusterSetList{}
if err := v.Client.List(context.TODO(), clusterSetList, client.InNamespace(v.namespace)); err != nil {
klog.ErrorS(err, "Error reading ClusterSet", "Namespace", v.namespace)
return admission.Errored(http.StatusPreconditionFailed, err)
}
deny := false
var existingClusterSet mcv1alpha1.ClusterSet
if len(clusterSetList.Items) > 0 {
// ClusterSet webhook will guarantee that there is at most one ClusterSet in a given Namespace.
existingClusterSet = clusterSetList.Items[0]
if clusterClaim.Value == existingClusterSet.Name || clusterClaim.Value == existingClusterSet.Spec.Leaders[0].ClusterID {
deny = true
} else {
for _, member := range existingClusterSet.Spec.Members {
if clusterClaim.Value == member.ClusterID {
deny = true
break
}
}
}
}
// Deny ClusterClaim deletion if the ClusterClaim is referred in a ClusterSet.
if deny {
klog.ErrorS(err, "The ClusterClaim is referred by a ClusterSet. Cannot delete it", "ClusterClaim", klog.KObj(clusterClaim), "ClusterSet", klog.KObj(&existingClusterSet))
return admission.Denied(fmt.Sprintf("the ClusterClaim %s is referred by a ClusterSet %s. Please delete the ClusterSet first\n", klog.KObj(clusterClaim), klog.KObj(&existingClusterSet)))
}
return admission.Allowed("")
}
return admission.Allowed("")
}
func (v *clusterClaimValidator) InjectDecoder(d *admission.Decoder) error {
v.decoder = d
return nil
}