github.com/mkimuram/operator-sdk@v0.7.1-0.20190410172100-52ad33a4bda0/cmd/operator-sdk/internal/genutil/k8s.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 genutil 16 17 import ( 18 "fmt" 19 "os/exec" 20 "path/filepath" 21 "strings" 22 23 "github.com/operator-framework/operator-sdk/internal/pkg/scaffold" 24 "github.com/operator-framework/operator-sdk/internal/util/projutil" 25 26 log "github.com/sirupsen/logrus" 27 ) 28 29 // K8sCodegen performs deepcopy code-generation for all custom resources under 30 // pkg/apis. hf is a path to a header file containing text to add to generated 31 // files. 32 func K8sCodegen(hf string) error { 33 projutil.MustInProjectRoot() 34 35 wd := projutil.MustGetwd() 36 repoPkg := projutil.CheckAndGetProjectGoPkg() 37 srcDir := filepath.Join(wd, "vendor", "k8s.io", "code-generator") 38 binDir := filepath.Join(wd, scaffold.BuildBinDir) 39 40 genDirs := []string{ 41 "./cmd/defaulter-gen", 42 "./cmd/client-gen", 43 "./cmd/lister-gen", 44 "./cmd/informer-gen", 45 "./cmd/deepcopy-gen", 46 } 47 if err := buildCodegenBinaries(genDirs, binDir, srcDir); err != nil { 48 return err 49 } 50 51 gvMap, err := parseGroupVersions() 52 if err != nil { 53 return fmt.Errorf("failed to parse group versions: (%v)", err) 54 } 55 gvb := &strings.Builder{} 56 for g, vs := range gvMap { 57 gvb.WriteString(fmt.Sprintf("%s:%v, ", g, vs)) 58 } 59 60 log.Infof("Running deepcopy code-generation for Custom Resource group versions: [%v]\n", gvb.String()) 61 62 fdc := func(a string) error { return deepcopyGen(binDir, repoPkg, a, gvMap) } 63 if err = withHeaderFile(hf, fdc); err != nil { 64 return err 65 } 66 fd := func(a string) error { return defaulterGen(binDir, repoPkg, a, gvMap) } 67 if err = withHeaderFile(hf, fd); err != nil { 68 return err 69 } 70 71 log.Info("Code-generation complete.") 72 return nil 73 } 74 75 func deepcopyGen(binDir, repoPkg, hf string, gvMap map[string][]string) (err error) { 76 apisPkg := filepath.Join(repoPkg, scaffold.ApisDir) 77 args := []string{ 78 "--input-dirs", createFQApis(apisPkg, gvMap), 79 "--output-file-base", "zz_generated.deepcopy", 80 "--bounding-dirs", apisPkg, 81 // deepcopy-gen requires a boilerplate file. Either use header or an 82 // empty file if header is empty. 83 "--go-header-file", hf, 84 } 85 cmd := exec.Command(filepath.Join(binDir, "deepcopy-gen"), args...) 86 if err = projutil.ExecCmd(cmd); err != nil { 87 return fmt.Errorf("failed to perform deepcopy code-generation: %v", err) 88 } 89 return nil 90 } 91 92 func defaulterGen(binDir, repoPkg, hf string, gvMap map[string][]string) (err error) { 93 apisPkg := filepath.Join(repoPkg, scaffold.ApisDir) 94 args := []string{ 95 "--input-dirs", createFQApis(apisPkg, gvMap), 96 "--output-file-base", "zz_generated.defaults", 97 // defaulter-gen requires a boilerplate file. Either use header or an 98 // empty file if header is empty. 99 "--go-header-file", hf, 100 } 101 cmd := exec.Command(filepath.Join(binDir, "defaulter-gen"), args...) 102 if err = projutil.ExecCmd(cmd); err != nil { 103 return fmt.Errorf("failed to perform defaulter code-generation: %v", err) 104 } 105 return nil 106 }