Skip to content

Commit

Permalink
feat: automatically update componentconfig tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
Eileen-Yu committed Jan 31, 2023
1 parent 55ac993 commit f50cb54
Show file tree
Hide file tree
Showing 15 changed files with 313 additions and 13 deletions.
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

# Copyright 2019 The Kubernetes Authors.
# 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.
Expand Down Expand Up @@ -63,14 +63,18 @@ install: build ## Build and install the binary with the current source code. Use
##@ Development

.PHONY: generate
generate: generate-testdata ## Update/generate all mock data. You should run this commands to update the mock data after your changes.
generate: generate-testdata generate-docs ## Update/generate all mock data. You should run this commands to update the mock data after your changes.
go mod tidy

.PHONY: generate-testdata
generate-testdata: ## Update/generate the testdata in $GOPATH/src/sigs.k8s.io/kubebuilder
rm -rf testdata/
./test/testdata/generate.sh

.PHONY: generate-docs
generate-docs: ## Update/generate the docs in $GOPATH/src/sigs.k8s.io/kubebuilder
./docs/docs.sh

.PHONY: lint
lint: golangci-lint yamllint ## Run golangci-lint linter & yamllint
$(GOLANGCI_LINT) run
Expand Down
196 changes: 196 additions & 0 deletions docs/book/src/component-config-tutorial/generate_docs.go
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ More information can be found via the [Kubebuilder Documentation](https://book.k

## License

Copyright 2023.
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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023.
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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023.
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.
Expand Down Expand Up @@ -49,7 +49,6 @@ type ProjectConfig struct {

Spec ProjectConfigSpec `json:"spec,omitempty"`
Status ProjectConfigStatus `json:"status,omitempty"`

// ControllerManagerConfigurationSpec returns the configurations for controllers
cfg.ControllerManagerConfigurationSpec `json:",inline"`

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ patchesStrategicMerge:
# endpoint w/o any authn/z, please comment the following line.
- manager_auth_proxy_patch.yaml


# Mount the controller config file for loading manager configurations
# through a ComponentConfig type
- manager_config_patch.yaml



# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
#- manager_webhook_patch.yaml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,8 @@ spec:
requests:
cpu: 5m
memory: 64Mi
- name: manager
args:
- "--health-probe-bind-address=:8081"
- "--metrics-bind-address=127.0.0.1:8080"
- "--leader-elect"
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ webhook:
leaderElection:
leaderElect: true
resourceName: 80807133.tutorial.kubebuilder.io
clusterName: example-test
clusterName: example-test
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ resources:

generatorOptions:
disableNameSuffixHash: true

configMapGenerator:
- name: manager-config
files:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ spec:
containers:
- command:
- /manager
args:
- --leader-elect
image: controller:latest
name: manager
securityContext:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023.
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.
Expand Down
25 changes: 23 additions & 2 deletions docs/book/src/component-config-tutorial/testdata/project/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023.
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.
Expand Down Expand Up @@ -48,6 +48,9 @@ func init() {
}

func main() {
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
var configFile string
flag.StringVar(&configFile, "config", "",
"The controller will load its initial configuration from this file. "+
Expand All @@ -72,7 +75,25 @@ func main() {
}
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options)
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "80807133.tutorial.kubebuilder.io",
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
// when the Manager ends. This requires the binary to immediately end when the
// Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
// speeds up voluntary leader transitions as the new leader don't have to wait
// LeaseDuration time first.
//
// In the default scaffold provided, the program ends immediately after
// the manager stops, so would be fine to enable this option. However,
// if you are doing or is intended to do any operation such as perform cleanups
// after the manager stops then its usage might be unsafe.
// LeaderElectionReleaseOnCancel: true,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
Expand Down
Loading

0 comments on commit f50cb54

Please sign in to comment.