sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/model/resource/api.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 // API contains information about scaffolded APIs 24 type API struct { 25 // CRDVersion holds the CustomResourceDefinition API version used for the resource. 26 CRDVersion string `json:"crdVersion,omitempty"` 27 28 // Namespaced is true if the API is namespaced. 29 Namespaced bool `json:"namespaced,omitempty"` 30 } 31 32 // Validate checks that the API is valid. 33 func (api API) Validate() error { 34 // Validate the CRD version 35 if err := validateAPIVersion(api.CRDVersion); err != nil { 36 return fmt.Errorf("invalid CRD version: %w", err) 37 } 38 39 return nil 40 } 41 42 // Copy returns a deep copy of the API that can be safely modified without affecting the original. 43 func (api API) Copy() API { 44 // As this function doesn't use a pointer receiver, api is already a shallow copy. 45 // Any field that is a pointer, slice or map needs to be deep copied. 46 return api 47 } 48 49 // Update combines fields of the APIs of two resources. 50 func (api *API) Update(other *API) error { 51 // If other is nil, nothing to merge 52 if other == nil { 53 return nil 54 } 55 56 // Update the version. 57 if other.CRDVersion != "" { 58 if api.CRDVersion == "" { 59 api.CRDVersion = other.CRDVersion 60 } else if api.CRDVersion != other.CRDVersion { 61 return fmt.Errorf("CRD versions do not match") 62 } 63 } 64 65 // Update the namespace. 66 api.Namespaced = api.Namespaced || other.Namespaced 67 68 return nil 69 } 70 71 // IsEmpty returns if the API's fields all contain zero-values. 72 func (api API) IsEmpty() bool { 73 return api.CRDVersion == "" && !api.Namespaced 74 }