sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v2/scaffolds/internal/templates/api/group.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 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  			f.Path = filepath.Join("apis", "%[group]", "%[version]", "groupversion_info.go")
    40  		} else {
    41  			f.Path = filepath.Join("api", "%[version]", "groupversion_info.go")
    42  		}
    43  	}
    44  	f.Path = f.Resource.Replacer().Replace(f.Path)
    45  
    46  	f.TemplateBody = groupTemplate
    47  
    48  	return nil
    49  }
    50  
    51  //nolint:lll
    52  const groupTemplate = `{{ .Boilerplate }}
    53  
    54  // Package {{ .Resource.Version }} contains API Schema definitions for the {{ .Resource.Group }} {{ .Resource.Version }} API group
    55  //+kubebuilder:object:generate=true
    56  //+groupName={{ .Resource.QualifiedGroup }}
    57  package {{ .Resource.Version }}
    58  
    59  import (
    60  	"k8s.io/apimachinery/pkg/runtime/schema"
    61  	"sigs.k8s.io/controller-runtime/pkg/scheme"
    62  )
    63  
    64  var (
    65  	// GroupVersion is group version used to register these objects
    66  	GroupVersion = schema.GroupVersion{Group: "{{ .Resource.QualifiedGroup }}", Version: "{{ .Resource.Version }}"}
    67  
    68  	// SchemeBuilder is used to add go types to the GroupVersionKind scheme
    69  	SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}
    70  
    71  	// AddToScheme adds the types in this group-version to the given scheme.
    72  	AddToScheme = SchemeBuilder.AddToScheme
    73  )
    74  `