sigs.k8s.io/kubebuilder/v3@v3.14.0/hack/docs/internal/component-config-tutorial/generate_component_config.go (about) 1 /* 2 Copyright 2023 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 componentconfig 18 19 import ( 20 "os" 21 "os/exec" 22 "path/filepath" 23 24 log "github.com/sirupsen/logrus" 25 26 "github.com/spf13/afero" 27 pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util" 28 "sigs.k8s.io/kubebuilder/v3/test/e2e/utils" 29 ) 30 31 type Sample struct { 32 ctx *utils.TestContext 33 } 34 35 func NewSample(binaryPath, samplePath string) Sample { 36 log.Infof("Generating the sample context of component-config...") 37 38 ctx := newSampleContext(binaryPath, samplePath, "GO111MODULE=on") 39 40 return Sample{&ctx} 41 } 42 43 func newSampleContext(binaryPath string, samplePath string, env ...string) utils.TestContext { 44 cmdContext := &utils.CmdContext{ 45 Env: env, 46 Dir: samplePath, 47 } 48 49 testContext := utils.TestContext{ 50 CmdContext: cmdContext, 51 BinaryName: binaryPath, 52 } 53 54 return testContext 55 } 56 57 // Prepare the Context for the sample project 58 func (sp *Sample) Prepare() { 59 log.Infof("destroying directory for component_config sample project") 60 sp.ctx.Destroy() 61 62 log.Infof("refreshing tools and creating directory...") 63 err := sp.ctx.Prepare() 64 65 CheckError("creating directory for sample project", err) 66 } 67 68 func (sp *Sample) GenerateSampleProject() { 69 log.Infof("Initializing the component config project") 70 err := sp.ctx.Init( 71 "--domain", "tutorial.kubebuilder.io", 72 "--repo", "tutorial.kubebuilder.io/project", 73 "--license", "apache2", 74 "--owner", "The Kubernetes authors", 75 "--plugins=go/v4", 76 "--component-config", 77 ) 78 CheckError("Initializing the component config project", err) 79 80 log.Infof("Adding a new config type") 81 err = sp.ctx.CreateAPI( 82 "--group", "config", 83 "--version", "v2", 84 "--kind", "ProjectConfig", 85 "--resource", "--controller=false", 86 "--make=false", 87 ) 88 CheckError("Creating the API", err) 89 } 90 91 func (sp *Sample) UpdateTutorial() { 92 // 1. generate controller_manager_config.yaml 93 var fs = afero.NewOsFs() 94 err := afero.WriteFile(fs, filepath.Join(sp.ctx.Dir, "config/manager/controller_manager_config.yaml"), 95 []byte(`apiVersion: controller-runtime.sigs.k8s.io/v1alpha1 96 kind: ControllerManagerConfig 97 metadata: 98 labels: 99 app.kubernetes.io/name: controllermanagerconfig 100 app.kubernetes.io/instance: controller-manager-configuration 101 app.kubernetes.io/component: manager 102 app.kubernetes.io/created-by: project 103 app.kubernetes.io/part-of: project 104 app.kubernetes.io/managed-by: kustomize 105 health: 106 healthProbeBindAddress: :8081 107 metrics: 108 bindAddress: 127.0.0.1:8080 109 webhook: 110 port: 9443 111 leaderElection: 112 leaderElect: true 113 resourceName: 80807133.tutorial.kubebuilder.io 114 clusterName: example-test 115 `), 0600) 116 CheckError("fixing controller_manager_config", err) 117 118 // 2. fix projectconfig_types.go 119 err = pluginutil.InsertCode( 120 filepath.Join(sp.ctx.Dir, "api/v2/projectconfig_types.go"), 121 `metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"`, 122 ` 123 cfg "sigs.k8s.io/controller-runtime/pkg/config/v1alpha1"`) 124 CheckError("fixing projectconfig_types", err) 125 126 err = pluginutil.InsertCode( 127 filepath.Join(sp.ctx.Dir, "api/v2/projectconfig_types.go"), 128 `Status ProjectConfigStatus `+"`"+`json:"status,omitempty"`+"`", 129 ` 130 // ControllerManagerConfigurationSpec returns the configurations for controllers 131 cfg.ControllerManagerConfigurationSpec `+"`"+`json:",inline"`+"`"+` 132 133 ClusterName string `+"`"+`json:"clusterName,omitempty"`+"`", 134 ) 135 136 CheckError("fixing projectconfig_types", err) 137 138 // 3. fix main 139 err = pluginutil.InsertCode( 140 filepath.Join(sp.ctx.Dir, "cmd/main.go"), 141 `var err error`, 142 ` 143 ctrlConfig := configv2.ProjectConfig{}`) 144 CheckError("fixing main.go", err) 145 146 err = pluginutil.InsertCode( 147 filepath.Join(sp.ctx.Dir, "cmd/main.go"), 148 `AtPath(configFile)`, 149 `.OfKind(&ctrlConfig)`) 150 CheckError("fixing main.go", err) 151 } 152 153 func (sp *Sample) CodeGen() { 154 155 cmd := exec.Command("make", "manifests") 156 _, err := sp.ctx.Run(cmd) 157 CheckError("Failed to run make manifests for componentConfig tutorial", err) 158 159 cmd = exec.Command("make", "all") 160 _, err = sp.ctx.Run(cmd) 161 CheckError("Failed to run make all for componentConfig tutorial", err) 162 163 cmd = exec.Command("go", "mod", "tidy") 164 _, err = sp.ctx.Run(cmd) 165 CheckError("Failed to run go mod tidy all for componentConfig tutorial", err) 166 } 167 168 // CheckError will exit with exit code 1 when err is not nil. 169 func CheckError(msg string, err error) { 170 if err != nil { 171 log.Errorf("error %s: %s", msg, err) 172 os.Exit(1) 173 } 174 }