Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support loading controller configuration from a versioned file #5337

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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")
}
}