diff --git a/Makefile b/Makefile index 3e49bccab68..28c0e5fdd7c 100644 --- a/Makefile +++ b/Makefile @@ -63,7 +63,7 @@ install: build ## Build and install the binary with the current source code. Use ##@ Development .PHONY: generate -generate: generate-testdata generate-docs ## Update/generate all mock data. You should run this commands to update the mock data after your changes. +generate: build 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 diff --git a/docs/book/src/component-config-tutorial/testdata/project/PROJECT b/docs/book/src/component-config-tutorial/testdata/project/PROJECT index cd75b7877e6..b61fe6bf31d 100644 --- a/docs/book/src/component-config-tutorial/testdata/project/PROJECT +++ b/docs/book/src/component-config-tutorial/testdata/project/PROJECT @@ -2,7 +2,6 @@ # This file is used to track the info used to scaffold your project # and allow the plugins properly work. # More info: https://book.kubebuilder.io/reference/project-config.html -componentConfig: true domain: tutorial.kubebuilder.io layout: - go.kubebuilder.io/v4 diff --git a/docs/book/src/component-config-tutorial/testdata/project/cmd/main.go b/docs/book/src/component-config-tutorial/testdata/project/cmd/main.go index 6dee9d72d9d..1204d888ea0 100644 --- a/docs/book/src/component-config-tutorial/testdata/project/cmd/main.go +++ b/docs/book/src/component-config-tutorial/testdata/project/cmd/main.go @@ -48,11 +48,14 @@ func init() { } func main() { - var configFile string - flag.StringVar(&configFile, "config", "", - "The controller will load its initial configuration from this file. "+ - "Omit this flag to use the default configuration values. "+ - "Command-line flags override configuration from this file.") + var metricsAddr string + var enableLeaderElection bool + var probeAddr string + 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.") opts := zap.Options{ Development: true, } @@ -61,18 +64,25 @@ func main() { ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) - var err error - ctrlConfig := configv2.ProjectConfig{} - options := ctrl.Options{Scheme: scheme} - if configFile != "" { - options, err = options.AndFrom(ctrl.ConfigFile().AtPath(configFile).OfKind(&ctrlConfig)) - if err != nil { - setupLog.Error(err, "unable to load the config file") - os.Exit(1) - } - } - - 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) diff --git a/docs/book/src/component-config-tutorial/testdata/project/config/default/kustomization.yaml b/docs/book/src/component-config-tutorial/testdata/project/config/default/kustomization.yaml index ea608f256b7..9705441033c 100644 --- a/docs/book/src/component-config-tutorial/testdata/project/config/default/kustomization.yaml +++ b/docs/book/src/component-config-tutorial/testdata/project/config/default/kustomization.yaml @@ -33,9 +33,6 @@ patchesStrategicMerge: - 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 diff --git a/docs/book/src/component-config-tutorial/testdata/project/config/default/manager_auth_proxy_patch.yaml b/docs/book/src/component-config-tutorial/testdata/project/config/default/manager_auth_proxy_patch.yaml index 18fa56fc4e2..73fad2a6754 100644 --- a/docs/book/src/component-config-tutorial/testdata/project/config/default/manager_auth_proxy_patch.yaml +++ b/docs/book/src/component-config-tutorial/testdata/project/config/default/manager_auth_proxy_patch.yaml @@ -32,3 +32,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" diff --git a/docs/book/src/component-config-tutorial/testdata/project/config/default/manager_config_patch.yaml b/docs/book/src/component-config-tutorial/testdata/project/config/default/manager_config_patch.yaml index 6c400155cfb..f6f58916922 100644 --- a/docs/book/src/component-config-tutorial/testdata/project/config/default/manager_config_patch.yaml +++ b/docs/book/src/component-config-tutorial/testdata/project/config/default/manager_config_patch.yaml @@ -8,13 +8,3 @@ spec: spec: containers: - name: manager - args: - - "--config=controller_manager_config.yaml" - volumeMounts: - - name: manager-config - mountPath: /controller_manager_config.yaml - subPath: controller_manager_config.yaml - volumes: - - name: manager-config - configMap: - name: manager-config diff --git a/docs/book/src/component-config-tutorial/testdata/project/config/manager/kustomization.yaml b/docs/book/src/component-config-tutorial/testdata/project/config/manager/kustomization.yaml index 2bcd3eeaa94..5c5f0b84cba 100644 --- a/docs/book/src/component-config-tutorial/testdata/project/config/manager/kustomization.yaml +++ b/docs/book/src/component-config-tutorial/testdata/project/config/manager/kustomization.yaml @@ -1,10 +1,2 @@ resources: - manager.yaml - -generatorOptions: - disableNameSuffixHash: true - -configMapGenerator: -- name: manager-config - files: - - controller_manager_config.yaml diff --git a/docs/book/src/component-config-tutorial/testdata/project/config/manager/manager.yaml b/docs/book/src/component-config-tutorial/testdata/project/config/manager/manager.yaml index 6e09e6bf26e..a5989e41094 100644 --- a/docs/book/src/component-config-tutorial/testdata/project/config/manager/manager.yaml +++ b/docs/book/src/component-config-tutorial/testdata/project/config/manager/manager.yaml @@ -68,6 +68,8 @@ spec: containers: - command: - /manager + args: + - --leader-elect image: controller:latest name: manager securityContext: diff --git a/hack/docs/internal/component-config-tutorial/generate_component_config.go b/hack/docs/internal/component-config-tutorial/generate_component_config.go index 897f80a306e..e801b7da5dc 100644 --- a/hack/docs/internal/component-config-tutorial/generate_component_config.go +++ b/hack/docs/internal/component-config-tutorial/generate_component_config.go @@ -73,7 +73,6 @@ func (sp *Sample) GenerateSampleProject() { "--license", "apache2", "--owner", "The Kubernetes authors", "--plugins=go/v4", - "--component-config", ) CheckError("Initializing the project", err) @@ -134,20 +133,6 @@ clusterName: example-test ) CheckError("fixing projectconfig_types", err) - - // 3. fix main - err = pluginutil.InsertCode( - filepath.Join(sp.ctx.Dir, "cmd/main.go"), - `var err error`, - ` - ctrlConfig := configv2.ProjectConfig{}`) - CheckError("fixing main.go", err) - - err = pluginutil.InsertCode( - filepath.Join(sp.ctx.Dir, "cmd/main.go"), - `AtPath(configFile)`, - `.OfKind(&ctrlConfig)`) - CheckError("fixing main.go", err) } func (sp *Sample) CodeGen() { diff --git a/pkg/plugins/golang/declarative/v1/scaffolds/internal/templates/controller.go b/pkg/plugins/golang/declarative/v1/scaffolds/internal/templates/controller.go index 23a1340b684..604a359cce9 100644 --- a/pkg/plugins/golang/declarative/v1/scaffolds/internal/templates/controller.go +++ b/pkg/plugins/golang/declarative/v1/scaffolds/internal/templates/controller.go @@ -133,13 +133,13 @@ func (r *{{ .Resource.Kind }}Reconciler) SetupWithManager(mgr ctrl.Manager) erro } // Watch for changes to {{ .Resource.Kind }} - err = c.Watch(&source.Kind{Type: &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}}, &handler.EnqueueRequestForObject{}) + err := c.Watch(source.Kind(mgr.GetCache(), &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}), &handler.EnqueueRequestForObject{}) if err != nil { return err } // Watch for changes to deployed objects - _, err = declarative.WatchAll(mgr.GetConfig(), c, r, watchLabels) + err := declarative.WatchAll(mgr.GetConfig(), c, r, watchLabels) if err != nil { return err } diff --git a/test/e2e/v4/generate_test.go b/test/e2e/v4/generate_test.go index c439751420e..75889b06eaa 100644 --- a/test/e2e/v4/generate_test.go +++ b/test/e2e/v4/generate_test.go @@ -212,7 +212,6 @@ func GenerateV4ComponentConfig(kbc *utils.TestContext) { "--plugins", "go/v4", "--project-version", "3", "--domain", kbc.Domain, - "--component-config=true", ) ExpectWithOffset(1, err).NotTo(HaveOccurred()) diff --git a/test/testdata/generate.sh b/test/testdata/generate.sh index 3f680afe027..7866aff04ae 100755 --- a/test/testdata/generate.sh +++ b/test/testdata/generate.sh @@ -136,6 +136,6 @@ scaffold_test_project project-v3 --plugins="go/v3" scaffold_test_project project-v4 --plugins="go/v4" scaffold_test_project project-v4-multigroup --plugins="go/v4" scaffold_test_project project-v4-declarative-v1 --plugins="go/v4,declarative" -scaffold_test_project project-v4-config --component-config --plugins="go/v4" +scaffold_test_project project-v4-config --plugins="go/v4" scaffold_test_project project-v4-with-deploy-image --plugins="go/v4" scaffold_test_project project-v4-with-grafana --plugins="go/v4" diff --git a/testdata/project-v4-config/PROJECT b/testdata/project-v4-config/PROJECT index fcd404dbfa7..7105fa28ae7 100644 --- a/testdata/project-v4-config/PROJECT +++ b/testdata/project-v4-config/PROJECT @@ -2,7 +2,6 @@ # This file is used to track the info used to scaffold your project # and allow the plugins properly work. # More info: https://book.kubebuilder.io/reference/project-config.html -componentConfig: true domain: testproject.org layout: - go.kubebuilder.io/v4 diff --git a/testdata/project-v4-config/cmd/main.go b/testdata/project-v4-config/cmd/main.go index ba319663fa1..9677d96d2fa 100644 --- a/testdata/project-v4-config/cmd/main.go +++ b/testdata/project-v4-config/cmd/main.go @@ -49,11 +49,14 @@ func init() { } func main() { - var configFile string - flag.StringVar(&configFile, "config", "", - "The controller will load its initial configuration from this file. "+ - "Omit this flag to use the default configuration values. "+ - "Command-line flags override configuration from this file.") + var metricsAddr string + var enableLeaderElection bool + var probeAddr string + 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.") opts := zap.Options{ Development: true, } @@ -62,17 +65,25 @@ func main() { ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) - var err error - options := ctrl.Options{Scheme: scheme} - if configFile != "" { - options, err = options.AndFrom(ctrl.ConfigFile().AtPath(configFile)) - if err != nil { - setupLog.Error(err, "unable to load the config file") - os.Exit(1) - } - } - - 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: "89f7a333.testproject.org", + // 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) diff --git a/testdata/project-v4-config/config/default/kustomization.yaml b/testdata/project-v4-config/config/default/kustomization.yaml index 6ff4eea9962..810590c58db 100644 --- a/testdata/project-v4-config/config/default/kustomization.yaml +++ b/testdata/project-v4-config/config/default/kustomization.yaml @@ -33,9 +33,6 @@ patchesStrategicMerge: - 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 diff --git a/testdata/project-v4-config/config/default/manager_auth_proxy_patch.yaml b/testdata/project-v4-config/config/default/manager_auth_proxy_patch.yaml index 18fa56fc4e2..73fad2a6754 100644 --- a/testdata/project-v4-config/config/default/manager_auth_proxy_patch.yaml +++ b/testdata/project-v4-config/config/default/manager_auth_proxy_patch.yaml @@ -32,3 +32,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" diff --git a/testdata/project-v4-config/config/default/manager_config_patch.yaml b/testdata/project-v4-config/config/default/manager_config_patch.yaml index 6c400155cfb..f6f58916922 100644 --- a/testdata/project-v4-config/config/default/manager_config_patch.yaml +++ b/testdata/project-v4-config/config/default/manager_config_patch.yaml @@ -8,13 +8,3 @@ spec: spec: containers: - name: manager - args: - - "--config=controller_manager_config.yaml" - volumeMounts: - - name: manager-config - mountPath: /controller_manager_config.yaml - subPath: controller_manager_config.yaml - volumes: - - name: manager-config - configMap: - name: manager-config diff --git a/testdata/project-v4-config/config/manager/controller_manager_config.yaml b/testdata/project-v4-config/config/manager/controller_manager_config.yaml deleted file mode 100644 index 85b321f5c2c..00000000000 --- a/testdata/project-v4-config/config/manager/controller_manager_config.yaml +++ /dev/null @@ -1,29 +0,0 @@ -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-v4-config - app.kubernetes.io/part-of: project-v4-config - app.kubernetes.io/managed-by: kustomize -health: - healthProbeBindAddress: :8081 -metrics: - bindAddress: 127.0.0.1:8080 -webhook: - port: 9443 -leaderElection: - leaderElect: true - resourceName: 89f7a333.testproject.org -# leaderElectionReleaseOnCancel defines if the leader should step down volume -# 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 diff --git a/testdata/project-v4-config/config/manager/kustomization.yaml b/testdata/project-v4-config/config/manager/kustomization.yaml index 2bcd3eeaa94..5c5f0b84cba 100644 --- a/testdata/project-v4-config/config/manager/kustomization.yaml +++ b/testdata/project-v4-config/config/manager/kustomization.yaml @@ -1,10 +1,2 @@ resources: - manager.yaml - -generatorOptions: - disableNameSuffixHash: true - -configMapGenerator: -- name: manager-config - files: - - controller_manager_config.yaml diff --git a/testdata/project-v4-config/config/manager/manager.yaml b/testdata/project-v4-config/config/manager/manager.yaml index 562550f9e67..106501945a8 100644 --- a/testdata/project-v4-config/config/manager/manager.yaml +++ b/testdata/project-v4-config/config/manager/manager.yaml @@ -68,6 +68,8 @@ spec: containers: - command: - /manager + args: + - --leader-elect image: controller:latest name: manager securityContext: diff --git a/testdata/project-v4-declarative-v1/internal/controller/admiral_controller.go b/testdata/project-v4-declarative-v1/internal/controller/admiral_controller.go index b9a7f0fb350..cb2569cc3ff 100644 --- a/testdata/project-v4-declarative-v1/internal/controller/admiral_controller.go +++ b/testdata/project-v4-declarative-v1/internal/controller/admiral_controller.go @@ -74,13 +74,13 @@ func (r *AdmiralReconciler) SetupWithManager(mgr ctrl.Manager) error { } // Watch for changes to Admiral - err = c.Watch(&source.Kind{Type: &crewv1.Admiral{}}, &handler.EnqueueRequestForObject{}) + err := c.Watch(source.Kind(mgr.GetCache(), &crewv1.Admiral{}), &handler.EnqueueRequestForObject{}) if err != nil { return err } // Watch for changes to deployed objects - _, err = declarative.WatchAll(mgr.GetConfig(), c, r, watchLabels) + err := declarative.WatchAll(mgr.GetConfig(), c, r, watchLabels) if err != nil { return err } diff --git a/testdata/project-v4-declarative-v1/internal/controller/captain_controller.go b/testdata/project-v4-declarative-v1/internal/controller/captain_controller.go index 81ec7726e1c..4aacef6113b 100644 --- a/testdata/project-v4-declarative-v1/internal/controller/captain_controller.go +++ b/testdata/project-v4-declarative-v1/internal/controller/captain_controller.go @@ -74,13 +74,13 @@ func (r *CaptainReconciler) SetupWithManager(mgr ctrl.Manager) error { } // Watch for changes to Captain - err = c.Watch(&source.Kind{Type: &crewv1.Captain{}}, &handler.EnqueueRequestForObject{}) + err := c.Watch(source.Kind(mgr.GetCache(), &crewv1.Captain{}), &handler.EnqueueRequestForObject{}) if err != nil { return err } // Watch for changes to deployed objects - _, err = declarative.WatchAll(mgr.GetConfig(), c, r, watchLabels) + err := declarative.WatchAll(mgr.GetConfig(), c, r, watchLabels) if err != nil { return err } diff --git a/testdata/project-v4-declarative-v1/internal/controller/firstmate_controller.go b/testdata/project-v4-declarative-v1/internal/controller/firstmate_controller.go index 089c069f6ac..e0b8a9ed136 100644 --- a/testdata/project-v4-declarative-v1/internal/controller/firstmate_controller.go +++ b/testdata/project-v4-declarative-v1/internal/controller/firstmate_controller.go @@ -74,13 +74,13 @@ func (r *FirstMateReconciler) SetupWithManager(mgr ctrl.Manager) error { } // Watch for changes to FirstMate - err = c.Watch(&source.Kind{Type: &crewv1.FirstMate{}}, &handler.EnqueueRequestForObject{}) + err := c.Watch(source.Kind(mgr.GetCache(), &crewv1.FirstMate{}), &handler.EnqueueRequestForObject{}) if err != nil { return err } // Watch for changes to deployed objects - _, err = declarative.WatchAll(mgr.GetConfig(), c, r, watchLabels) + err := declarative.WatchAll(mgr.GetConfig(), c, r, watchLabels) if err != nil { return err }