github.com/jmrodri/operator-sdk@v0.5.0/pkg/scaffold/register.go (about) 1 // Copyright 2018 The Operator-SDK Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package scaffold 16 17 import ( 18 "path/filepath" 19 "strings" 20 21 "github.com/operator-framework/operator-sdk/pkg/scaffold/input" 22 ) 23 24 const RegisterFile = "register.go" 25 26 // Register is the input needed to generate a pkg/apis/<group>/<version>/register.go file 27 type Register struct { 28 input.Input 29 30 // Resource defines the inputs for the new custom resource definition 31 Resource *Resource 32 } 33 34 func (s *Register) GetInput() (input.Input, error) { 35 if s.Path == "" { 36 s.Path = filepath.Join(ApisDir, 37 strings.ToLower(s.Resource.Group), 38 strings.ToLower(s.Resource.Version), 39 RegisterFile) 40 } 41 // Do not overwrite this file if it exists. 42 s.IfExistsAction = input.Skip 43 s.TemplateBody = registerTemplate 44 return s.Input, nil 45 } 46 47 const registerTemplate = `// NOTE: Boilerplate only. Ignore this file. 48 49 // Package {{.Resource.Version}} contains API Schema definitions for the {{ .Resource.Group }} {{.Resource.Version}} API group 50 // +k8s:deepcopy-gen=package,register 51 // +groupName={{ .Resource.FullGroup }} 52 package {{.Resource.Version}} 53 54 import ( 55 "k8s.io/apimachinery/pkg/runtime/schema" 56 "sigs.k8s.io/controller-runtime/pkg/runtime/scheme" 57 ) 58 59 var ( 60 // SchemeGroupVersion is group version used to register these objects 61 SchemeGroupVersion = schema.GroupVersion{Group: "{{ .Resource.FullGroup }}", Version: "{{ .Resource.Version }}"} 62 63 // SchemeBuilder is used to add go types to the GroupVersionKind scheme 64 SchemeBuilder = &scheme.Builder{GroupVersion: SchemeGroupVersion} 65 ) 66 `