-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update ClusterSet and ClusterClaim webhooks (#4043)
1. Add a ClusterSet webhook to make Leader's `clusterID` immutable, and allow only one ClusterSet in mc-controller's Namespace. 2. Update ClusterClaim webhook to make the `value` field (ClusterID/ClusterSetID) immutable 3. Remove the unused mutate webhook of ClusterClaim Signed-off-by: Lan Luo <[email protected]>
- Loading branch information
1 parent
d9c4629
commit 13f470e
Showing
9 changed files
with
364 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
multicluster/cmd/multicluster-controller/clusterset_webhook.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
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" | ||
|
||
"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" | ||
) | ||
|
||
//+kubebuilder:webhook:path=/validate-multicluster-crd-antrea-io-v1alpha1-clusterset,mutating=false,failurePolicy=fail,sideEffects=None,groups=multicluster.crd.antrea.io,resources=clustersets,verbs=create;update,versions=v1alpha1,name=vclusterset.kb.io,admissionReviewVersions={v1,v1beta1} | ||
|
||
// ClusterSet validator | ||
type clusterSetValidator struct { | ||
Client client.Client | ||
decoder *admission.Decoder | ||
namespace string | ||
} | ||
|
||
// Handle handles admission requests. | ||
func (v *clusterSetValidator) Handle(ctx context.Context, req admission.Request) admission.Response { | ||
clusterSet := &mcv1alpha1.ClusterSet{} | ||
err := v.decoder.Decode(req, clusterSet) | ||
if err != nil { | ||
klog.ErrorS(err, "Error while decoding ClusterSet", "ClusterSet", req.Namespace+"/"+req.Name) | ||
return admission.Errored(http.StatusBadRequest, err) | ||
} | ||
|
||
oldClusterSet := &mcv1alpha1.ClusterSet{} | ||
if req.OldObject.Raw != nil { | ||
if err := json.Unmarshal(req.OldObject.Raw, &oldClusterSet); err != nil { | ||
klog.ErrorS(err, "Error while decoding old ClusterSet", "ClusterSet", klog.KObj(clusterSet)) | ||
return admission.Errored(http.StatusBadRequest, err) | ||
} | ||
if oldClusterSet.Spec.Leaders[0].ClusterID != clusterSet.Spec.Leaders[0].ClusterID { | ||
klog.ErrorS(err, "the field 'clusterID' is immutable", "ClusterSet", klog.KObj(clusterSet)) | ||
return admission.Denied("the field 'clusterID' is immutable") | ||
} | ||
return admission.Allowed("") | ||
} | ||
|
||
// Check if there is any existing ClusterSet. | ||
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) | ||
} | ||
|
||
if len(clusterSetList.Items) > 0 { | ||
err := fmt.Errorf("multiple ClusterSets in a Namespace are not allowed") | ||
klog.ErrorS(err, "ClusterSet", klog.KObj(clusterSet), "Namespace", v.namespace) | ||
return admission.Errored(http.StatusPreconditionFailed, err) | ||
} | ||
return admission.Allowed("") | ||
} | ||
|
||
func (v *clusterSetValidator) InjectDecoder(d *admission.Decoder) error { | ||
v.decoder = d | ||
return nil | ||
} |
Oops, something went wrong.