github.com/theishshah/operator-sdk@v0.6.0/pkg/scaffold/cr.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 "fmt" 19 "path/filepath" 20 "strings" 21 "text/template" 22 23 "github.com/operator-framework/operator-sdk/pkg/scaffold/input" 24 ) 25 26 // CR is the input needed to generate a deploy/crds/<group>_<version>_<kind>_cr.yaml file 27 type CR struct { 28 input.Input 29 30 // Resource defines the inputs for the new custom resource 31 Resource *Resource 32 33 // Spec is a custom spec for the CR. It will be automatically indented. If 34 // unset, a default spec will be created. 35 Spec string 36 } 37 38 func (s *CR) GetInput() (input.Input, error) { 39 if s.Path == "" { 40 fileName := fmt.Sprintf("%s_%s_%s_cr.yaml", 41 strings.ToLower(s.Resource.Group), 42 strings.ToLower(s.Resource.Version), 43 s.Resource.LowerKind) 44 s.Path = filepath.Join(CRDsDir, fileName) 45 } 46 s.TemplateBody = crTemplate 47 if s.TemplateFuncs == nil { 48 s.TemplateFuncs = template.FuncMap{} 49 } 50 s.TemplateFuncs["indent"] = indent 51 return s.Input, nil 52 } 53 54 func indent(spaces int, v string) string { 55 pad := strings.Repeat(" ", spaces) 56 return pad + strings.Replace(v, "\n", "\n"+pad, -1) 57 } 58 59 const crTemplate = `apiVersion: {{ .Resource.APIVersion }} 60 kind: {{ .Resource.Kind }} 61 metadata: 62 name: example-{{ .Resource.LowerKind }} 63 spec: 64 {{- with .Spec }} 65 {{ . | indent 2 }} 66 {{- else }} 67 # Add fields here 68 size: 3 69 {{- end }} 70 `