sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v4/scaffolds/internal/templates/api/group.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 api 18 19 import ( 20 "path/filepath" 21 22 log "github.com/sirupsen/logrus" 23 24 "sigs.k8s.io/kubebuilder/v3/pkg/machinery" 25 ) 26 27 var _ machinery.Template = &Group{} 28 29 // Group scaffolds the file that defines the registration methods for a certain group and version 30 type Group struct { 31 machinery.TemplateMixin 32 machinery.MultiGroupMixin 33 machinery.BoilerplateMixin 34 machinery.ResourceMixin 35 } 36 37 // SetTemplateDefaults implements file.Template 38 func (f *Group) SetTemplateDefaults() error { 39 if f.Path == "" { 40 if f.MultiGroup && f.Resource.Group != "" { 41 f.Path = filepath.Join("api", "%[group]", "%[version]", "groupversion_info.go") 42 } else { 43 f.Path = filepath.Join("api", "%[version]", "groupversion_info.go") 44 } 45 } 46 47 f.Path = f.Resource.Replacer().Replace(f.Path) 48 log.Println(f.Path) 49 f.TemplateBody = groupTemplate 50 51 return nil 52 } 53 54 //nolint:lll 55 const groupTemplate = `{{ .Boilerplate }} 56 57 // Package {{ .Resource.Version }} contains API Schema definitions for the {{ .Resource.Group }} {{ .Resource.Version }} API group 58 //+kubebuilder:object:generate=true 59 //+groupName={{ .Resource.QualifiedGroup }} 60 package {{ .Resource.Version }} 61 62 import ( 63 "k8s.io/apimachinery/pkg/runtime/schema" 64 "sigs.k8s.io/controller-runtime/pkg/scheme" 65 ) 66 67 var ( 68 // GroupVersion is group version used to register these objects 69 GroupVersion = schema.GroupVersion{Group: "{{ .Resource.QualifiedGroup }}", Version: "{{ .Resource.Version }}"} 70 71 // SchemeBuilder is used to add go types to the GroupVersionKind scheme 72 SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion} 73 74 // AddToScheme adds the types in this group-version to the given scheme. 75 AddToScheme = SchemeBuilder.AddToScheme 76 ) 77 `