Skip to content

Commit

Permalink
Add to ability to start controller with config file
Browse files Browse the repository at this point in the history
Signed-off-by: Cody W. Eilar <ecody@vmware.com>
  • Loading branch information
AcidLeroy committed Mar 10, 2023
1 parent 6ba1dca commit c3a4b41
Show file tree
Hide file tree
Showing 34 changed files with 2,107 additions and 489 deletions.
31 changes: 16 additions & 15 deletions cmd/controller/app/controller.go
Expand Up @@ -35,7 +35,8 @@ import (
"k8s.io/utils/clock"

"github.com/cert-manager/cert-manager/cmd/controller/app/options"
cmdutil "github.com/cert-manager/cert-manager/internal/cmd/util"
cmdutil "github.com/cert-manager/cert-manager/cmd/util"
config "github.com/cert-manager/cert-manager/internal/apis/config/controller"
"github.com/cert-manager/cert-manager/internal/controller/feature"
"github.com/cert-manager/cert-manager/pkg/acme/accounts"
"github.com/cert-manager/cert-manager/pkg/controller"
Expand All @@ -47,7 +48,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 @@ -66,7 +67,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 @@ -96,7 +97,7 @@ func Run(opts *options.ControllerOptions, stopCh <-chan struct{}) error {
})

// Start profiler if it is enabled
if opts.EnablePprof {
if *opts.EnablePprof {
profilerLn, err := net.Listen("tcp", opts.PprofAddress)
if err != nil {
return fmt.Errorf("failed to listen on profiler address %s: %v", opts.PprofAddress, err)
Expand Down Expand Up @@ -129,7 +130,7 @@ func Run(opts *options.ControllerOptions, stopCh <-chan struct{}) error {
}

elected := make(chan struct{})
if opts.LeaderElect {
if *opts.LeaderElect {
g.Go(func() error {
log.V(logf.InfoLevel).Info("starting leader election")
ctx, err := ctxFactory.Build("leader-election")
Expand Down Expand Up @@ -229,7 +230,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 @@ -265,9 +266,9 @@ func buildControllerContextFactory(ctx context.Context, opts *options.Controller
acmeAccountRegistry := accounts.NewDefaultRegistry()

ctxFactory, err := controller.NewContextFactory(ctx, controller.ContextOptions{
Kubeconfig: opts.Kubeconfig,
KubernetesAPIQPS: opts.KubernetesAPIQPS,
KubernetesAPIBurst: opts.KubernetesAPIBurst,
Kubeconfig: opts.KubeConfig,
KubernetesAPIQPS: *opts.KubernetesAPIQPS,
KubernetesAPIBurst: *opts.KubernetesAPIBurst,
APIServerHost: opts.APIServerHost,

Namespace: opts.Namespace,
Expand All @@ -287,18 +288,18 @@ func buildControllerContextFactory(ctx context.Context, opts *options.Controller

DNS01Nameservers: nameservers,
DNS01CheckRetryPeriod: opts.DNS01CheckRetryPeriod,
DNS01CheckAuthoritative: !opts.DNS01RecursiveNameserversOnly,
DNS01CheckAuthoritative: !*opts.DNS01RecursiveNameserversOnly,

AccountRegistry: acmeAccountRegistry,
},

SchedulerOptions: controller.SchedulerOptions{
MaxConcurrentChallenges: opts.MaxConcurrentChallenges,
MaxConcurrentChallenges: *opts.MaxConcurrentChallenges,
},

IssuerOptions: controller.IssuerOptions{
ClusterIssuerAmbientCredentials: opts.ClusterIssuerAmbientCredentials,
IssuerAmbientCredentials: opts.IssuerAmbientCredentials,
ClusterIssuerAmbientCredentials: *opts.ClusterIssuerAmbientCredentials,
IssuerAmbientCredentials: *opts.IssuerAmbientCredentials,
ClusterResourceNamespace: opts.ClusterResourceNamespace,
},

Expand All @@ -310,7 +311,7 @@ func buildControllerContextFactory(ctx context.Context, opts *options.Controller
},

CertificateOptions: controller.CertificateOptions{
EnableOwnerRef: opts.EnableCertificateOwnerRef,
EnableOwnerRef: *opts.EnableCertificateOwnerRef,
CopiedAnnotationPrefixes: opts.CopiedAnnotationPrefixes,
},
})
Expand All @@ -321,7 +322,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) error {
func startLeaderElection(ctx context.Context, opts *config.ControllerConfiguration, leaderElectionClient kubernetes.Interface, recorder record.EventRecorder, callbacks leaderelection.LeaderCallbacks) error {
// Identity used to distinguish between multiple controller manager instances
id, err := os.Hostname()
if err != nil {
Expand Down
36 changes: 36 additions & 0 deletions cmd/controller/app/options/globalflags.go
@@ -0,0 +1,36 @@
/*
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 options

import (
"flag"
"os"

"github.com/spf13/pflag"

"github.com/cert-manager/cert-manager/pkg/logs"
)

func AddGlobalFlags(fs *pflag.FlagSet) {
addKlogFlags(fs)
}

func addKlogFlags(fs *pflag.FlagSet) {
local := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
logs.InitLogs(local)
fs.AddGoFlagSet(local)
}

0 comments on commit c3a4b41

Please sign in to comment.