sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/model/resource/resource.go (about) 1 /* 2 Copyright 2018 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 "strings" 22 23 "sigs.k8s.io/kubebuilder/v3/pkg/internal/validation" 24 ) 25 26 // Resource contains the information required to scaffold files for a resource. 27 type Resource struct { 28 // GVK contains the resource's Group-Version-Kind triplet. 29 GVK `json:",inline"` 30 31 // Plural is the resource's kind plural form. 32 Plural string `json:"plural,omitempty"` 33 34 // Path is the path to the go package where the types are defined. 35 Path string `json:"path,omitempty"` 36 37 // API holds the information related to the resource API. 38 API *API `json:"api,omitempty"` 39 40 // Controller specifies if a controller has been scaffolded. 41 Controller bool `json:"controller,omitempty"` 42 43 // Webhooks holds the information related to the associated webhooks. 44 Webhooks *Webhooks `json:"webhooks,omitempty"` 45 } 46 47 // Validate checks that the Resource is valid. 48 func (r Resource) Validate() error { 49 // Validate the GVK 50 if err := r.GVK.Validate(); err != nil { 51 return err 52 } 53 54 // Validate the Plural 55 // NOTE: IsDNS1035Label returns a slice of strings instead of an error, so no wrapping 56 if errors := validation.IsDNS1035Label(r.Plural); len(errors) != 0 { 57 return fmt.Errorf("invalid Plural: %#v", errors) 58 } 59 60 // TODO: validate the path 61 62 // Validate the API 63 if r.API != nil && !r.API.IsEmpty() { 64 if err := r.API.Validate(); err != nil { 65 return fmt.Errorf("invalid API: %w", err) 66 } 67 } 68 69 // Validate the Webhooks 70 if r.Webhooks != nil && !r.Webhooks.IsEmpty() { 71 if err := r.Webhooks.Validate(); err != nil { 72 return fmt.Errorf("invalid Webhooks: %w", err) 73 } 74 } 75 76 return nil 77 } 78 79 // PackageName returns a name valid to be used por go packages. 80 func (r Resource) PackageName() string { 81 if r.Group == "" { 82 return safeImport(r.Domain) 83 } 84 85 return safeImport(r.Group) 86 } 87 88 // ImportAlias returns a identifier usable as an import alias for this resource. 89 func (r Resource) ImportAlias() string { 90 if r.Group == "" { 91 return safeImport(r.Domain + r.Version) 92 } 93 94 return safeImport(r.Group + r.Version) 95 } 96 97 // HasAPI returns true if the resource has an associated API. 98 func (r Resource) HasAPI() bool { 99 return r.API != nil && r.API.CRDVersion != "" 100 } 101 102 // HasController returns true if the resource has an associated controller. 103 func (r Resource) HasController() bool { 104 return r.Controller 105 } 106 107 // HasDefaultingWebhook returns true if the resource has an associated defaulting webhook. 108 func (r Resource) HasDefaultingWebhook() bool { 109 return r.Webhooks != nil && r.Webhooks.Defaulting 110 } 111 112 // HasValidationWebhook returns true if the resource has an associated validation webhook. 113 func (r Resource) HasValidationWebhook() bool { 114 return r.Webhooks != nil && r.Webhooks.Validation 115 } 116 117 // HasConversionWebhook returns true if the resource has an associated conversion webhook. 118 func (r Resource) HasConversionWebhook() bool { 119 return r.Webhooks != nil && r.Webhooks.Conversion 120 } 121 122 // IsRegularPlural returns true if the plural is the regular plural form for the kind. 123 func (r Resource) IsRegularPlural() bool { 124 return r.Plural == RegularPlural(r.Kind) 125 } 126 127 // Copy returns a deep copy of the Resource that can be safely modified without affecting the original. 128 func (r Resource) Copy() Resource { 129 // As this function doesn't use a pointer receiver, r is already a shallow copy. 130 // Any field that is a pointer, slice or map needs to be deep copied. 131 if r.API != nil { 132 api := r.API.Copy() 133 r.API = &api 134 } 135 if r.Webhooks != nil { 136 webhooks := r.Webhooks.Copy() 137 r.Webhooks = &webhooks 138 } 139 return r 140 } 141 142 // Update combines fields of two resources that have matching GVK favoring the receiver's values. 143 func (r *Resource) Update(other Resource) error { 144 // If self is nil, return an error 145 if r == nil { 146 return fmt.Errorf("unable to update a nil Resource") 147 } 148 149 // Make sure we are not merging resources for different GVKs. 150 if !r.GVK.IsEqualTo(other.GVK) { 151 return fmt.Errorf("unable to update a Resource (GVK %+v) with another with non-matching GVK %+v", r.GVK, other.GVK) 152 } 153 154 if r.Plural != other.Plural { 155 return fmt.Errorf("unable to update Resource (Plural %q) with another with non-matching Plural %q", 156 r.Plural, other.Plural) 157 } 158 159 if other.Path != "" && r.Path != other.Path { 160 if r.Path == "" { 161 r.Path = other.Path 162 } else { 163 return fmt.Errorf("unable to update Resource (Path %q) with another with non-matching Path %q", r.Path, other.Path) 164 } 165 } 166 167 // Update API. 168 if r.API == nil && other.API != nil { 169 r.API = &API{} 170 } 171 if err := r.API.Update(other.API); err != nil { 172 return err 173 } 174 175 // Update controller. 176 r.Controller = r.Controller || other.Controller 177 178 // Update Webhooks. 179 if r.Webhooks == nil && other.Webhooks != nil { 180 r.Webhooks = &Webhooks{} 181 } 182 183 return r.Webhooks.Update(other.Webhooks) 184 } 185 186 func wrapKey(key string) string { 187 return fmt.Sprintf("%%[%s]", key) 188 } 189 190 // Replacer returns a strings.Replacer that replaces resource keywords with values. 191 func (r Resource) Replacer() *strings.Replacer { 192 var replacements []string 193 194 replacements = append(replacements, wrapKey("group"), r.Group) 195 replacements = append(replacements, wrapKey("version"), r.Version) 196 replacements = append(replacements, wrapKey("kind"), strings.ToLower(r.Kind)) 197 replacements = append(replacements, wrapKey("plural"), strings.ToLower(r.Plural)) 198 replacements = append(replacements, wrapKey("package-name"), r.PackageName()) 199 200 return strings.NewReplacer(replacements...) 201 }