-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: automatically update componentconfig tutorial
- Loading branch information
Showing
15 changed files
with
313 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
196 changes: 196 additions & 0 deletions
196
docs/book/src/component-config-tutorial/generate_docs.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package componentconfig | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/spf13/afero" | ||
pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util" | ||
"sigs.k8s.io/kubebuilder/v3/test/e2e/utils" | ||
) | ||
|
||
type Sample struct { | ||
ctx *utils.TestContext | ||
} | ||
|
||
func NewSample(binaryPath, samplePath string) Sample { | ||
log.Infof("Generating the sample context...") | ||
|
||
ctx := newSampleContext(binaryPath, samplePath, "GO111MODULE=on") | ||
|
||
return Sample{&ctx} | ||
} | ||
|
||
func newSampleContext(binaryPath string, samplePath string, env ...string) utils.TestContext { | ||
cmdContext := &utils.CmdContext{ | ||
Env: env, | ||
Dir: samplePath, | ||
} | ||
|
||
testContext := utils.TestContext{ | ||
CmdContext: cmdContext, | ||
BinaryName: binaryPath, | ||
} | ||
|
||
return testContext | ||
} | ||
|
||
// Prepare the Context for the sample project | ||
func (sp *Sample) Prepare() { | ||
log.Infof("destroying directory for sample project") | ||
sp.ctx.Destroy() | ||
|
||
log.Infof("refreshing tools and creating directory...") | ||
err := sp.ctx.Prepare() | ||
|
||
CheckError("creating directory for sample project", err) | ||
} | ||
|
||
func (sp *Sample) GenerateSampleProject() { | ||
log.Infof("Initializing the project") | ||
err := sp.ctx.Init( | ||
"--domain", "tutorial.kubebuilder.io", | ||
"--repo", "tutorial.kubebuilder.io/project", | ||
"--license", "apache2", | ||
"--owner", "The Kubernetes authors", | ||
"component-config", | ||
) | ||
CheckError("Initializing the project", err) | ||
|
||
log.Infof("Adding a new config type") | ||
err = sp.ctx.CreateAPI( | ||
"--group", "config", | ||
"--version", "v2", | ||
"--kind", "ProjectConfig", | ||
"--resource", "--controller=false", | ||
"--make=false", | ||
) | ||
CheckError("Creating the API", err) | ||
} | ||
|
||
func (sp *Sample) UpdateTutorial() { | ||
// 1. generate controller_manager_config.yaml | ||
var fs = afero.NewOsFs() | ||
err := afero.WriteFile(fs, filepath.Join(sp.ctx.Dir, "config/manager/controller_manager_config.yaml"), | ||
[]byte(`apiVersion: controller-runtime.sigs.k8s.io/v1alpha1 | ||
kind: ControllerManagerConfig | ||
metadata: | ||
labels: | ||
app.kubernetes.io/name: controllermanagerconfig | ||
app.kubernetes.io/instance: controller-manager-configuration | ||
app.kubernetes.io/component: manager | ||
app.kubernetes.io/created-by: project | ||
app.kubernetes.io/part-of: project | ||
app.kubernetes.io/managed-by: kustomize | ||
health: | ||
healthProbeBindAddress: :8081 | ||
metrics: | ||
bindAddress: 127.0.0.1:8080 | ||
webhook: | ||
port: 9443 | ||
leaderElection: | ||
leaderElect: true | ||
resourceName: 80807133.tutorial.kubebuilder.io | ||
clusterName: example-test`), 0600, | ||
) | ||
CheckError("fixing controller_manager_config", err) | ||
|
||
// 2. add componentconfig | ||
err = pluginutil.InsertCode( | ||
filepath.Join(sp.ctx.Dir, "PROJECT"), | ||
"More info: https://book.kubebuilder.io/reference/project-config.html", | ||
"\ncomponentConfig: true", | ||
) | ||
CheckError("fixing PROJECT", err) | ||
|
||
// 3. fix projectconfig_types.go | ||
err = pluginutil.InsertCode( | ||
filepath.Join(sp.ctx.Dir, "api/v2/projectconfig_types.go"), | ||
"metav1 \"k8s.io/apimachinery/pkg/apis/meta/v1\"", | ||
"\n cfg \"sigs.k8s.io/controller-runtime/pkg/config/v1alpha1\"", | ||
) | ||
CheckError("fixing projectconfig_types", err) | ||
|
||
err = pluginutil.InsertCode( | ||
filepath.Join(sp.ctx.Dir, "api/v2/projectconfig_types.go"), | ||
"Status ProjectConfigStatus `json:\"status,omitempty\"`", | ||
//nolint:lll | ||
"\n // ControllerManagerConfigurationSpec returns the configurations for controllers\n cfg.ControllerManagerConfigurationSpec `json:\",inline\"`\n\n ClusterName string `json:\"clusterName,omitempty\"`", | ||
) | ||
CheckError("fixing projectconfig_types", err) | ||
|
||
// 4. fix kustomization.yaml | ||
err = pluginutil.InsertCode( | ||
filepath.Join(sp.ctx.Dir, "config/default/kustomization.yaml"), | ||
"manager_auth_proxy_patch.yaml", | ||
//nolint:lll | ||
"\n\n# Mount the controller config file for loading manager configurations\n# through a ComponentConfig type\n- manager_config_patch.yaml", | ||
) | ||
CheckError("fixing default/kustomization", err) | ||
|
||
err = pluginutil.InsertCode( | ||
filepath.Join(sp.ctx.Dir, "config/manager/kustomization.yaml"), | ||
"- manager.yaml", | ||
//nolint:lll | ||
"\n\ngeneratorOptions:\n disableNameSuffixHash: true\nconfigMapGenerator:\n- name: manager-config\n files:\n - controller_manager_config.yaml", | ||
) | ||
CheckError("fixing manager/kustomization", err) | ||
|
||
// 5. fix manager_config_patch | ||
err = pluginutil.InsertCode( | ||
filepath.Join(sp.ctx.Dir, "config/default/manager_config_patch.yaml"), | ||
"name: manager", | ||
//nolint:lll | ||
"\n args:\n - \"--config=controller_manager_config.yaml\"\n volumeMounts:\n - name: manager-config\n mountPath: /controller_manager_config.yaml\n subPath: controller_manager_config.yaml\n volumes:\n - name: manager-config\n configMap:\n name: manager-config", | ||
) | ||
CheckError("fixing manager_config_patch", err) | ||
|
||
// 6. fix main | ||
err = pluginutil.InsertCode( | ||
filepath.Join(sp.ctx.Dir, "main.go"), | ||
"ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))", | ||
//nolint:lll | ||
"\n\n var err error\n ctrlConfig := configv2.ProjectConfig{}\n options := ctrl.Options{Scheme: scheme}\n if configFile != \"\" {\n options, err = options.AndFrom(ctrl.ConfigFile().AtPath(configFile).OfKind(&ctrlConfig))\n if err != nil {\n setupLog.Error(err, \"unable to load the config file\")\n os.Exit(1)\n }\n }", | ||
) | ||
CheckError("fixing main.go", err) | ||
|
||
err = pluginutil.ReplaceInFile( | ||
filepath.Join(sp.ctx.Dir, "main.go"), | ||
//nolint:lll | ||
`flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.") | ||
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") | ||
flag.BoolVar(&enableLeaderElection, "leader-elect", false, | ||
"Enable leader election for controller manager. "+ | ||
"Enabling this will ensure there is only one active controller manager.") | ||
`, | ||
//nolint:lll | ||
"var configFile string\n flag.StringVar(&configFile, \"config\", \"\",\n \"The controller will load its initial configuration from this file. \"+\n \"Omit this flag to use the default configuration values. \"+\n \"Command-line flags override configuration from this file.\")\n", | ||
) | ||
CheckError("fixing main.go", err) | ||
} | ||
|
||
// CheckError will exit with exit code 1 when err is not nil. | ||
func CheckError(msg string, err error) { | ||
if err != nil { | ||
log.Errorf("error %s: %s", msg, err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/book/src/component-config-tutorial/testdata/project/api/v2/groupversion_info.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/book/src/component-config-tutorial/testdata/project/api/v2/zz_generated.deepcopy.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/book/src/component-config-tutorial/testdata/project/hack/boilerplate.go.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.