Skip to content

Commit

Permalink
Merge pull request #5337 from AcidLeroy/controller-configuration-file
Browse files Browse the repository at this point in the history
Support loading controller configuration from a versioned file
  • Loading branch information
jetstack-bot committed Jul 31, 2023
2 parents 9de9809 + 282a6d5 commit d233758
Show file tree
Hide file tree
Showing 38 changed files with 2,572 additions and 505 deletions.
11 changes: 6 additions & 5 deletions cmd/controller/app/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"k8s.io/utils/clock"

"github.com/cert-manager/cert-manager/controller-binary/app/options"
config "github.com/cert-manager/cert-manager/internal/apis/config/controller"
cmdutil "github.com/cert-manager/cert-manager/internal/cmd/util"
"github.com/cert-manager/cert-manager/internal/controller/feature"
"github.com/cert-manager/cert-manager/pkg/acme/accounts"
Expand All @@ -48,7 +49,7 @@ import (
"github.com/cert-manager/cert-manager/pkg/util/profiling"
)

func Run(opts *options.ControllerOptions, stopCh <-chan struct{}) error {
func Run(opts *config.ControllerConfiguration, stopCh <-chan struct{}) error {
rootCtx, cancelContext := context.WithCancel(cmdutil.ContextWithStopCh(context.Background(), stopCh))
defer cancelContext()
rootCtx = logf.NewContext(rootCtx, logf.Log, "controller")
Expand All @@ -67,7 +68,7 @@ func Run(opts *options.ControllerOptions, stopCh <-chan struct{}) error {
return err
}

enabledControllers := opts.EnabledControllers()
enabledControllers := options.EnabledControllers(opts)
log.Info(fmt.Sprintf("enabled controllers: %s", enabledControllers.List()))

// Start metrics server
Expand Down Expand Up @@ -237,7 +238,7 @@ func Run(opts *options.ControllerOptions, stopCh <-chan struct{}) error {

// buildControllerContextFactory builds a new controller ContextFactory which
// can build controller contexts for each component.
func buildControllerContextFactory(ctx context.Context, opts *options.ControllerOptions) (*controller.ContextFactory, error) {
func buildControllerContextFactory(ctx context.Context, opts *config.ControllerConfiguration) (*controller.ContextFactory, error) {
log := logf.FromContext(ctx)

nameservers := opts.DNS01RecursiveNameservers
Expand Down Expand Up @@ -273,7 +274,7 @@ func buildControllerContextFactory(ctx context.Context, opts *options.Controller
acmeAccountRegistry := accounts.NewDefaultRegistry()

ctxFactory, err := controller.NewContextFactory(ctx, controller.ContextOptions{
Kubeconfig: opts.Kubeconfig,
Kubeconfig: opts.KubeConfig,
KubernetesAPIQPS: opts.KubernetesAPIQPS,
KubernetesAPIBurst: opts.KubernetesAPIBurst,
APIServerHost: opts.APIServerHost,
Expand Down Expand Up @@ -329,7 +330,7 @@ func buildControllerContextFactory(ctx context.Context, opts *options.Controller
return ctxFactory, nil
}

func startLeaderElection(ctx context.Context, opts *options.ControllerOptions, leaderElectionClient kubernetes.Interface, recorder record.EventRecorder, callbacks leaderelection.LeaderCallbacks, healthzAdaptor *leaderelection.HealthzAdaptor) error {
func startLeaderElection(ctx context.Context, opts *config.ControllerConfiguration, leaderElectionClient kubernetes.Interface, recorder record.EventRecorder, callbacks leaderelection.LeaderCallbacks, healthzAdaptor *leaderelection.HealthzAdaptor) error {
// Identity used to distinguish between multiple controller manager instances
id, err := os.Hostname()
if err != nil {
Expand Down
57 changes: 57 additions & 0 deletions cmd/controller/app/controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright 2021 The cert-manager 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 app

import (
"testing"

"github.com/cert-manager/cert-manager/controller-binary/app/options"
)

// Test to ensure flags take precedence over config options.
func TestControllerConfigFlagPrecedence_FlagsTakePrecedence(t *testing.T) {
cfg, err := options.NewControllerConfiguration()
if err != nil {
t.Fatal(err)
}

cfg.KubeConfig = "<invalid>"
if err := controllerConfigFlagPrecedence(cfg, []string{"--kubeconfig=valid"}); err != nil {
t.Fatal(err)
}

if cfg.KubeConfig != "valid" {
t.Errorf("unexpected field value %q, expected %q", cfg.KubeConfig, "valid")
}
}

// Test to ensure that when flags are not provided, config provided values are preserved.
func TestControllerConfigFlagPrecedence_ConfigPersistsWithoutFlags(t *testing.T) {
cfg, err := options.NewControllerConfiguration()
if err != nil {
t.Fatal(err)
}

cfg.KubeConfig = "valid"
if err := controllerConfigFlagPrecedence(cfg, []string{}); err != nil {
t.Fatal(err)
}

if cfg.KubeConfig != "valid" {
t.Errorf("unexpected field value %q, expected %q", cfg.KubeConfig, "valid")
}
}

0 comments on commit d233758

Please sign in to comment.