sigs.k8s.io/cluster-api@v1.7.1/exp/ipam/internal/webhooks/ipaddressclaim.go (about) 1 /* 2 Copyright 2022 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package webhooks 18 19 import ( 20 "context" 21 "fmt" 22 "reflect" 23 24 apierrors "k8s.io/apimachinery/pkg/api/errors" 25 "k8s.io/apimachinery/pkg/runtime" 26 "k8s.io/apimachinery/pkg/util/validation/field" 27 ctrl "sigs.k8s.io/controller-runtime" 28 "sigs.k8s.io/controller-runtime/pkg/webhook" 29 "sigs.k8s.io/controller-runtime/pkg/webhook/admission" 30 31 ipamv1 "sigs.k8s.io/cluster-api/exp/ipam/api/v1beta1" 32 ) 33 34 // SetupWebhookWithManager sets up IPAddressClaim webhooks. 35 func (webhook *IPAddressClaim) SetupWebhookWithManager(mgr ctrl.Manager) error { 36 return ctrl.NewWebhookManagedBy(mgr). 37 For(&ipamv1.IPAddressClaim{}). 38 WithValidator(webhook). 39 Complete() 40 } 41 42 // +kubebuilder:webhook:verbs=create;update;delete,path=/validate-ipam-cluster-x-k8s-io-v1beta1-ipaddressclaim,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=ipam.cluster.x-k8s.io,resources=ipaddressclaims,versions=v1beta1,name=validation.ipaddressclaim.ipam.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1 43 44 // IPAddressClaim implements a validating webhook for IPAddressClaim. 45 type IPAddressClaim struct { 46 } 47 48 var _ webhook.CustomValidator = &IPAddressClaim{} 49 50 // ValidateCreate implements webhook.CustomValidator. 51 func (webhook *IPAddressClaim) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) { 52 claim, ok := obj.(*ipamv1.IPAddressClaim) 53 if !ok { 54 return nil, apierrors.NewBadRequest(fmt.Sprintf("expected an IPAddressClaim but got a %T", obj)) 55 } 56 57 if claim.Spec.PoolRef.APIGroup == nil { 58 return nil, field.Invalid( 59 field.NewPath("spec.poolRef.apiGroup"), 60 claim.Spec.PoolRef.APIGroup, 61 "the pool reference needs to contain a group") 62 } 63 64 return nil, nil 65 } 66 67 // ValidateUpdate implements webhook.CustomValidator. 68 func (webhook *IPAddressClaim) ValidateUpdate(_ context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { 69 oldClaim, ok := oldObj.(*ipamv1.IPAddressClaim) 70 if !ok { 71 return nil, apierrors.NewBadRequest(fmt.Sprintf("expected an IPAddressClaim but got a %T", oldObj)) 72 } 73 newClaim, ok := newObj.(*ipamv1.IPAddressClaim) 74 if !ok { 75 return nil, apierrors.NewBadRequest(fmt.Sprintf("expected an IPAddressClaim but got a %T", newObj)) 76 } 77 78 if !reflect.DeepEqual(oldClaim.Spec, newClaim.Spec) { 79 return nil, field.Forbidden( 80 field.NewPath("spec"), 81 "the spec of IPAddressClaim is immutable", 82 ) 83 } 84 return nil, nil 85 } 86 87 // ValidateDelete implements webhook.CustomValidator. 88 func (webhook *IPAddressClaim) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) { 89 return nil, nil 90 }