Skip to content

Commit

Permalink
Uses manager.Options from ctrl-runtime
Browse files Browse the repository at this point in the history
- This patch uses the Options struct from the controller-runtime to
capture the startup options for the CAPV controller manager

Signed-off-by: Sagar Muchhal <muchhals@vmware.com>
  • Loading branch information
srm09 committed Apr 14, 2021
1 parent ad62f41 commit 39a95c6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 68 deletions.
10 changes: 5 additions & 5 deletions main.go
Expand Up @@ -61,12 +61,12 @@ func main() {
}

flag.StringVar(
&managerOpts.MetricsAddr,
&managerOpts.MetricsBindAddress,
"metrics-addr",
":8080",
"The address the metric endpoint binds to.")
flag.BoolVar(
&managerOpts.LeaderElectionEnabled,
&managerOpts.LeaderElection,
"enable-leader-election",
true,
"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
Expand All @@ -85,7 +85,7 @@ func main() {
defaultProfilerAddr,
"Bind address to expose the pprof profiler (e.g. localhost:6060)")
flag.DurationVar(
&managerOpts.SyncPeriod,
managerOpts.SyncPeriod,
"sync-period",
defaultSyncPeriod,
"The interval at which cluster-api objects are synchronized")
Expand All @@ -100,12 +100,12 @@ func main() {
defaultPodName,
"The name of the pod running the controller manager.")
flag.IntVar(
&managerOpts.WebhookPort,
&managerOpts.Port,
"webhook-port",
defaultWebhookPort,
"Webhook Server port (set to 0 to disable)")
flag.StringVar(
&managerOpts.HealthAddr,
&managerOpts.HealthProbeBindAddress,
"health-addr",
":9440",
"The address the health endpoint binds to.",
Expand Down
6 changes: 0 additions & 6 deletions pkg/manager/constants.go
Expand Up @@ -16,19 +16,13 @@ limitations under the License.

package manager

import "time"

const (
defaultPrefix = "capv-"

// DefaultWebhookServiceContainerPort is the default value for the eponymous
// manager option.
DefaultWebhookServiceContainerPort = 0

// DefaultSyncPeriod is the default value for the eponymous
// manager option.
DefaultSyncPeriod = time.Minute * 10

// DefaultPodName is the default value for the eponymous manager option.
DefaultPodName = defaultPrefix + "controller-manager"

Expand Down
14 changes: 1 addition & 13 deletions pkg/manager/manager.go
Expand Up @@ -60,19 +60,7 @@ func New(opts Options) (Manager, error) {
}

// Build the controller manager.
mgr, err := ctrl.NewManager(opts.KubeConfig, ctrl.Options{
Scheme: opts.Scheme,
MetricsBindAddress: opts.MetricsAddr,
LeaderElection: opts.LeaderElectionEnabled,
LeaderElectionID: opts.LeaderElectionID,
LeaderElectionNamespace: opts.LeaderElectionNamespace,
SyncPeriod: &opts.SyncPeriod,
Namespace: opts.WatchNamespace,
NewCache: opts.NewCache,
Port: opts.WebhookPort,
HealthProbeBindAddress: opts.HealthAddr,
CertDir: opts.CertDir,
})
mgr, err := ctrl.NewManager(opts.KubeConfig, opts.Options)
if err != nil {
return nil, errors.Wrap(err, "unable to create manager")
}
Expand Down
51 changes: 12 additions & 39 deletions pkg/manager/options.go
Expand Up @@ -22,19 +22,23 @@ import (
"strings"
"time"

"sigs.k8s.io/yaml"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client/config"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
ctrlmgr "sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/yaml"

"sigs.k8s.io/cluster-api-provider-vsphere/pkg/context"
// +kubebuilder:scaffold:imports
)

"sigs.k8s.io/cluster-api-provider-vsphere/pkg/context"
var (
// DefaultSyncPeriod is the default value for the eponymous
// manager option.
// Cannot set this as a constant since we need to assign the
// address of this variable to Options.SyncPeriod
DefaultSyncPeriod = time.Minute * 10
)

// AddToManagerFunc is a function that can be optionally specified with
Expand All @@ -44,35 +48,14 @@ type AddToManagerFunc func(*context.ControllerManagerContext, ctrlmgr.Manager) e

// Options describes the options used to create a new CAPV manager.
type Options struct {
// LeaderElectionEnabled is a flag that enables leader election.
LeaderElectionEnabled bool

// LeaderElectionID is the name of the config map to use as the
// locking resource when configuring leader election.
LeaderElectionID string

// SyncPeriod is the amount of time to wait between syncing the local
// object cache with the API server.
SyncPeriod time.Duration
ctrlmgr.Options

// MaxConcurrentReconciles the maximum number of allowed, concurrent
// reconciles.
//
// Defaults to the eponymous constant in this package.
MaxConcurrentReconciles int

// MetricsAddr is the net.Addr string for the metrics server.
MetricsAddr string

// HealthAddr is the net.Addr string for the healthcheck server
HealthAddr string

// LeaderElectionNamespace is the namespace in which the pod running the
// controller maintains a leader election lock
//
// Defaults to ""
LeaderElectionNamespace string

// LeaderElectionNamespace is the namespace in which the pod running the
// controller maintains a leader election lock
//
Expand All @@ -98,20 +81,10 @@ type Options struct {
// endpoints.
Password string

// WebhookPort is the port that the webhook server serves at.
WebhookPort int

// CertDir is the directory that contains the server key and certificate.
// TODO (srm09): Use CertDir from controller-runtime instead
CertDir string

// CredentialsFile is the file that contains credentials of CAPV
CredentialsFile string

Logger logr.Logger
KubeConfig *rest.Config
Scheme *runtime.Scheme
NewCache cache.NewCacheFunc

// AddToManager is a function that can be optionally specified with
// the manager's Options in order to explicitly decide what controllers
Expand All @@ -128,8 +101,8 @@ func (o *Options) defaults() {
o.PodName = DefaultPodName
}

if o.SyncPeriod == 0 {
o.SyncPeriod = DefaultSyncPeriod
if o.SyncPeriod == nil {
o.SyncPeriod = &DefaultSyncPeriod
}

if o.KubeConfig == nil {
Expand Down
12 changes: 7 additions & 5 deletions test/helpers/envtest.go
Expand Up @@ -121,11 +121,13 @@ func NewTestEnvironment() *TestEnvironment {
}

managerOpts := manager.Options{
Scheme: scheme,
MetricsAddr: "0",
WebhookPort: env.WebhookInstallOptions.LocalServingPort,
CertDir: env.WebhookInstallOptions.LocalServingCertDir,
KubeConfig: env.Config,
Options: ctrl.Options{
Scheme: scheme,
Port: env.WebhookInstallOptions.LocalServingPort,
CertDir: env.WebhookInstallOptions.LocalServingCertDir,
MetricsBindAddress: "0",
},
KubeConfig: env.Config,
// TODO (srm09): might need to supply some mock for
// vCenter interactions
Username: "blah",
Expand Down

0 comments on commit 39a95c6

Please sign in to comment.