sigs.k8s.io/cluster-api@v1.7.1/webhooks/alias.go (about) 1 /* 2 Copyright 2021 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 "k8s.io/apimachinery/pkg/util/validation/field" 21 ctrl "sigs.k8s.io/controller-runtime" 22 "sigs.k8s.io/controller-runtime/pkg/client" 23 24 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 25 "sigs.k8s.io/cluster-api/internal/webhooks" 26 ) 27 28 // Cluster implements a validating and defaulting webhook for Cluster. 29 type Cluster struct { 30 Client client.Reader 31 ClusterCacheTrackerReader ClusterCacheTrackerReader 32 } 33 34 // ClusterCacheTrackerReader is a read-only ClusterCacheTracker useful to gather information 35 // for MachinePool nodes for workload clusters. 36 type ClusterCacheTrackerReader = webhooks.ClusterCacheTrackerReader 37 38 // SetupWebhookWithManager sets up Cluster webhooks. 39 func (webhook *Cluster) SetupWebhookWithManager(mgr ctrl.Manager) error { 40 return (&webhooks.Cluster{ 41 Client: webhook.Client, 42 Tracker: webhook.ClusterCacheTrackerReader, 43 }).SetupWebhookWithManager(mgr) 44 } 45 46 // DefaultAndValidateVariables can be used to default and validate variables of a Cluster 47 // based on the corresponding ClusterClass. 48 // Before it can be used, all fields of the webhooks.Cluster have to be set 49 // and SetupWithManager has to be called. 50 // This method can be used when testing the behavior of the desired state computation of 51 // the Cluster topology controller (because variables are always defaulted and validated 52 // before the desired state is computed). 53 func (webhook *Cluster) DefaultAndValidateVariables(cluster *clusterv1.Cluster, clusterClass *clusterv1.ClusterClass) field.ErrorList { 54 // As of today this func is not a method on internal/webhooks.Cluster because it doesn't use 55 // any of its fields. But it seems more consistent and future-proof to expose it as a method. 56 return webhooks.DefaultAndValidateVariables(cluster, clusterClass) 57 } 58 59 // ClusterClass implements a validation and defaulting webhook for ClusterClass. 60 type ClusterClass struct { 61 Client client.Reader 62 } 63 64 // SetupWebhookWithManager sets up ClusterClass webhooks. 65 func (webhook *ClusterClass) SetupWebhookWithManager(mgr ctrl.Manager) error { 66 return (&webhooks.ClusterClass{ 67 Client: webhook.Client, 68 }).SetupWebhookWithManager(mgr) 69 } 70 71 // Machine implements a validating and defaulting webhook for Machine. 72 type Machine struct{} 73 74 // SetupWebhookWithManager sets up Machine webhooks. 75 func (webhook *Machine) SetupWebhookWithManager(mgr ctrl.Manager) error { 76 return (&webhooks.Machine{}).SetupWebhookWithManager(mgr) 77 } 78 79 // MachineDeployment implements a validating and defaulting webhook for MachineDeployment. 80 type MachineDeployment struct{} 81 82 // SetupWebhookWithManager sets up MachineDeployment webhooks. 83 func (webhook *MachineDeployment) SetupWebhookWithManager(mgr ctrl.Manager) error { 84 return (&webhooks.MachineDeployment{}).SetupWebhookWithManager(mgr) 85 } 86 87 // MachineSet implements a validating and defaulting webhook for MachineSet. 88 type MachineSet struct{} 89 90 // SetupWebhookWithManager sets up MachineSet webhooks. 91 func (webhook *MachineSet) SetupWebhookWithManager(mgr ctrl.Manager) error { 92 return (&webhooks.MachineSet{}).SetupWebhookWithManager(mgr) 93 } 94 95 // MachineHealthCheck implements a validating and defaulting webhook for MachineHealthCheck. 96 type MachineHealthCheck struct{} 97 98 // SetupWebhookWithManager sets up MachineHealthCheck webhooks. 99 func (webhook *MachineHealthCheck) SetupWebhookWithManager(mgr ctrl.Manager) error { 100 return (&webhooks.MachineHealthCheck{}).SetupWebhookWithManager(mgr) 101 }