sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v3/scaffolds/internal/templates/api/group.go (about)

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