sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/model/resource/webhooks.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 resource 18 19 import ( 20 "fmt" 21 ) 22 23 // Webhooks contains information about scaffolded webhooks 24 type Webhooks struct { 25 // WebhookVersion holds the {Validating,Mutating}WebhookConfiguration API version used for the resource. 26 WebhookVersion string `json:"webhookVersion,omitempty"` 27 28 // Defaulting specifies if a defaulting webhook is associated to the resource. 29 Defaulting bool `json:"defaulting,omitempty"` 30 31 // Validation specifies if a validation webhook is associated to the resource. 32 Validation bool `json:"validation,omitempty"` 33 34 // Conversion specifies if a conversion webhook is associated to the resource. 35 Conversion bool `json:"conversion,omitempty"` 36 } 37 38 // Validate checks that the Webhooks is valid. 39 func (webhooks Webhooks) Validate() error { 40 // Validate the Webhook version 41 if err := validateAPIVersion(webhooks.WebhookVersion); err != nil { 42 return fmt.Errorf("invalid Webhook version: %w", err) 43 } 44 45 return nil 46 } 47 48 // Copy returns a deep copy of the API that can be safely modified without affecting the original. 49 func (webhooks Webhooks) Copy() Webhooks { 50 // As this function doesn't use a pointer receiver, webhooks is already a shallow copy. 51 // Any field that is a pointer, slice or map needs to be deep copied. 52 return webhooks 53 } 54 55 // Update combines fields of the webhooks of two resources. 56 func (webhooks *Webhooks) Update(other *Webhooks) error { 57 // If other is nil, nothing to merge 58 if other == nil { 59 return nil 60 } 61 62 // Update the version. 63 if other.WebhookVersion != "" { 64 if webhooks.WebhookVersion == "" { 65 webhooks.WebhookVersion = other.WebhookVersion 66 } else if webhooks.WebhookVersion != other.WebhookVersion { 67 return fmt.Errorf("webhook versions do not match") 68 } 69 } 70 71 // Update defaulting. 72 webhooks.Defaulting = webhooks.Defaulting || other.Defaulting 73 74 // Update validation. 75 webhooks.Validation = webhooks.Validation || other.Validation 76 77 // Update conversion. 78 webhooks.Conversion = webhooks.Conversion || other.Conversion 79 80 return nil 81 } 82 83 // IsEmpty returns if the Webhooks' fields all contain zero-values. 84 func (webhooks Webhooks) IsEmpty() bool { 85 return webhooks.WebhookVersion == "" && !webhooks.Defaulting && !webhooks.Validation && !webhooks.Conversion 86 }