sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v3/scaffolds/init.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 scaffolds 18 19 import ( 20 log "github.com/sirupsen/logrus" 21 "github.com/spf13/afero" 22 23 "sigs.k8s.io/kubebuilder/v3/pkg/config" 24 "sigs.k8s.io/kubebuilder/v3/pkg/machinery" 25 "sigs.k8s.io/kubebuilder/v3/pkg/plugin" 26 "sigs.k8s.io/kubebuilder/v3/pkg/plugins" 27 kustomizecommonv1 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1" 28 kustomizecommonv2alpha "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v2" 29 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3/scaffolds/internal/templates" 30 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3/scaffolds/internal/templates/hack" 31 ) 32 33 const ( 34 // ControllerRuntimeVersion is the kubernetes-sigs/controller-runtime version to be used in the project 35 ControllerRuntimeVersion = "v0.14.4" 36 // ControllerToolsVersion is the kubernetes-sigs/controller-tools version to be used in the project 37 ControllerToolsVersion = "v0.11.3" 38 39 imageName = "controller:latest" 40 ) 41 42 var _ plugins.Scaffolder = &initScaffolder{} 43 44 var kustomizeVersion string 45 46 type initScaffolder struct { 47 config config.Config 48 boilerplatePath string 49 license string 50 owner string 51 52 // fs is the filesystem that will be used by the scaffolder 53 fs machinery.Filesystem 54 } 55 56 // NewInitScaffolder returns a new Scaffolder for project initialization operations 57 func NewInitScaffolder(config config.Config, license, owner string) plugins.Scaffolder { 58 return &initScaffolder{ 59 config: config, 60 boilerplatePath: hack.DefaultBoilerplatePath, 61 license: license, 62 owner: owner, 63 } 64 } 65 66 // InjectFS implements cmdutil.Scaffolder 67 func (s *initScaffolder) InjectFS(fs machinery.Filesystem) { 68 s.fs = fs 69 } 70 71 // Scaffold implements cmdutil.Scaffolder 72 func (s *initScaffolder) Scaffold() error { 73 log.Println("Writing scaffold for you to edit...") 74 75 // Initialize the machinery.Scaffold that will write the boilerplate file to disk 76 // The boilerplate file needs to be scaffolded as a separate step as it is going to 77 // be used by the rest of the files, even those scaffolded in this command call. 78 scaffold := machinery.NewScaffold(s.fs, 79 machinery.WithConfig(s.config), 80 ) 81 82 bpFile := &hack.Boilerplate{ 83 License: s.license, 84 Owner: s.owner, 85 } 86 bpFile.Path = s.boilerplatePath 87 if err := scaffold.Execute(bpFile); err != nil { 88 return err 89 } 90 91 boilerplate, err := afero.ReadFile(s.fs.FS, s.boilerplatePath) 92 if err != nil { 93 return err 94 } 95 96 // Initialize the machinery.Scaffold that will write the files to disk 97 scaffold = machinery.NewScaffold(s.fs, 98 machinery.WithConfig(s.config), 99 machinery.WithBoilerplate(string(boilerplate)), 100 ) 101 102 // If the KustomizeV2 was used to do the scaffold then 103 // we need to ensure that we use its supported Kustomize Version 104 // in order to support it 105 kustomizeVersion = kustomizecommonv1.KustomizeVersion 106 kustomizev2 := kustomizecommonv2alpha.Plugin{} 107 gov4alpha := "go.kubebuilder.io/v4-alpha" 108 pluginKeyForKustomizeV2 := plugin.KeyFor(kustomizev2) 109 110 for _, pluginKey := range s.config.GetPluginChain() { 111 if pluginKey == pluginKeyForKustomizeV2 || pluginKey == gov4alpha { 112 kustomizeVersion = kustomizecommonv2alpha.KustomizeVersion 113 break 114 } 115 } 116 117 return scaffold.Execute( 118 &templates.Main{}, 119 &templates.GoMod{ 120 ControllerRuntimeVersion: ControllerRuntimeVersion, 121 }, 122 &templates.GitIgnore{}, 123 &templates.Makefile{ 124 Image: imageName, 125 BoilerplatePath: s.boilerplatePath, 126 ControllerToolsVersion: ControllerToolsVersion, 127 KustomizeVersion: kustomizeVersion, 128 ControllerRuntimeVersion: ControllerRuntimeVersion, 129 }, 130 &templates.Dockerfile{}, 131 &templates.DockerIgnore{}, 132 &templates.Readme{}, 133 ) 134 }