Skip to content

Commit

Permalink
Rearrange main.go and use same context for manager and controller (#1768
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Karthik-K-N authored May 9, 2024
1 parent 0d56e7a commit b4e0e2a
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 83 deletions.
4 changes: 2 additions & 2 deletions controllers/ibmpowervscluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,9 @@ func (c clusterDescendants) filterOwnedDescendants(cluster *infrav1beta2.IBMPowe
}

// SetupWithManager creates a new IBMPowerVSCluster controller for a manager.
func (r *IBMPowerVSClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *IBMPowerVSClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&infrav1beta2.IBMPowerVSCluster{}).
WithEventFilter(predicates.ResourceIsNotExternallyManaged(ctrl.LoggerFrom(context.TODO()))).
WithEventFilter(predicates.ResourceIsNotExternallyManaged(ctrl.LoggerFrom(ctx))).
Complete(r)
}
4 changes: 2 additions & 2 deletions controllers/ibmvpccluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ func (r *IBMVPCClusterReconciler) reconcileLBState(clusterScope *scope.ClusterSc
}

// SetupWithManager creates a new IBMVPCCluster controller for a manager.
func (r *IBMVPCClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *IBMVPCClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&infrav1beta2.IBMVPCCluster{}).
WithEventFilter(predicates.ResourceIsNotExternallyManaged(ctrl.LoggerFrom(context.TODO()))).
WithEventFilter(predicates.ResourceIsNotExternallyManaged(ctrl.LoggerFrom(ctx))).
Complete(r)
}
164 changes: 85 additions & 79 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
package main

import (
"context"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -56,14 +57,16 @@ var (
healthAddr string
syncPeriod time.Duration
diagnosticsOptions = flags.DiagnosticsOptions{}
webhookPort int
webhookCertDir string

scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
webhookPort int
webhookCertDir string
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)

func init() {
klog.InitFlags(nil)

_ = clientgoscheme.AddToScheme(scheme)

_ = infrav1beta1.AddToScheme(scheme)
Expand All @@ -72,13 +75,79 @@ func init() {
// +kubebuilder:scaffold:scheme
}

func initFlags(fs *pflag.FlagSet) {
fs.BoolVar(
&enableLeaderElection,
"leader-elect",
false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.",
)

fs.StringVar(
&watchNamespace,
"namespace",
"",
"Namespace that the controller watches to reconcile cluster-api objects. If unspecified, the controller watches for cluster-api objects across all namespaces.",
)

fs.StringVar(
&healthAddr,
"health-addr",
":9440",
"The address the health endpoint binds to.",
)

fs.DurationVar(
&syncPeriod,
"sync-period",
10*time.Minute,
"The minimum interval at which watched resources are reconciled.",
)

fs.StringVar(
&options.ProviderIDFormat,
"provider-id-fmt",
string(options.ProviderIDFormatV1),
"ProviderID format is used set the Provider ID format for Machine",
)

fs.StringVar(
&endpoints.ServiceEndpointFormat,
"service-endpoint",
"",
"Set custom service endpoint in semi-colon separated format: ${ServiceRegion1}:${ServiceID1}=${URL1},${ServiceID2}=${URL2};${ServiceRegion2}:${ServiceID1}=${URL1}",
)

fs.IntVar(&webhookPort,
"webhook-port",
9443,
"The webhook server port the manager will listen on.",
)

fs.StringVar(&webhookCertDir, "webhook-cert-dir", "/tmp/k8s-webhook-server/serving-certs/",
"The webhook certificate directory, where the server should find the TLS certificate and key.")

flags.AddDiagnosticsOptions(fs, &diagnosticsOptions)
}

func validateFlags() error {
switch options.ProviderIDFormatType(options.ProviderIDFormat) {
case options.ProviderIDFormatV1:
setupLog.Info("Using v1 version of ProviderID format")
case options.ProviderIDFormatV2:
setupLog.Info("Using v2 version of ProviderID format")
default:
return fmt.Errorf("invalid value for flag provider-id-fmt: %s, Supported values: %s, %s ", options.ProviderIDFormat, options.ProviderIDFormatV1, options.ProviderIDFormatV2)
}
return nil
}

// Add RBAC for the authorized diagnostics endpoint.
// +kubebuilder:rbac:groups=authentication.k8s.io,resources=tokenreviews,verbs=create
// +kubebuilder:rbac:groups=authorization.k8s.io,resources=subjectaccessreviews,verbs=create

func main() {
klog.InitFlags(nil)

initFlags(pflag.CommandLine)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
Expand Down Expand Up @@ -134,6 +203,8 @@ func main() {
Client: client.Options{
Cache: &client.CacheOptions{
DisableFor: []client.Object{
// We want to avoid use of cache for IBMPowerVSCluster as we exclusively depend on IBMPowerVSCluster.Status.[Resource].ControllerCreated
// to mark resources created by controller.
&infrav1beta2.IBMPowerVSCluster{},
},
},
Expand All @@ -147,94 +218,29 @@ func main() {
// Initialize event recorder.
record.InitFromRecorder(mgr.GetEventRecorderFor("ibmcloud-controller"))

setupReconcilers(mgr, serviceEndpoint)
// Setup the context that's going to be used in controllers and for the manager.
ctx := ctrl.SetupSignalHandler()

setupReconcilers(ctx, mgr, serviceEndpoint)
setupWebhooks(mgr)
setupChecks(mgr)

// +kubebuilder:scaffold:builder
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
if err := mgr.Start(ctx); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}

func initFlags(fs *pflag.FlagSet) {
fs.BoolVar(
&enableLeaderElection,
"leader-elect",
false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.",
)

fs.StringVar(
&watchNamespace,
"namespace",
"",
"Namespace that the controller watches to reconcile cluster-api objects. If unspecified, the controller watches for cluster-api objects across all namespaces.",
)

fs.StringVar(
&healthAddr,
"health-addr",
":9440",
"The address the health endpoint binds to.",
)

fs.DurationVar(
&syncPeriod,
"sync-period",
10*time.Minute,
"The minimum interval at which watched resources are reconciled.",
)

fs.StringVar(
&options.ProviderIDFormat,
"provider-id-fmt",
string(options.ProviderIDFormatV1),
"ProviderID format is used set the Provider ID format for Machine",
)

fs.StringVar(
&endpoints.ServiceEndpointFormat,
"service-endpoint",
"",
"Set custom service endpoint in semi-colon separated format: ${ServiceRegion1}:${ServiceID1}=${URL1},${ServiceID2}=${URL2};${ServiceRegion2}:${ServiceID1}=${URL1}",
)

fs.IntVar(&webhookPort,
"webhook-port",
9443,
"The webhook server port the manager will listen on.",
)

fs.StringVar(&webhookCertDir, "webhook-cert-dir", "/tmp/k8s-webhook-server/serving-certs/",
"The webhook certificate directory, where the server should find the TLS certificate and key.")

flags.AddDiagnosticsOptions(fs, &diagnosticsOptions)
}

func validateFlags() error {
switch options.ProviderIDFormatType(options.ProviderIDFormat) {
case options.ProviderIDFormatV1:
setupLog.Info("Using v1 version of ProviderID format")
case options.ProviderIDFormatV2:
setupLog.Info("Using v2 version of ProviderID format")
default:
return fmt.Errorf("invalid value for flag provider-id-fmt: %s, Supported values: %s, %s ", options.ProviderIDFormat, options.ProviderIDFormatV1, options.ProviderIDFormatV2)
}
return nil
}

func setupReconcilers(mgr ctrl.Manager, serviceEndpoint []endpoints.ServiceEndpoint) {
func setupReconcilers(ctx context.Context, mgr ctrl.Manager, serviceEndpoint []endpoints.ServiceEndpoint) {
if err := (&controllers.IBMVPCClusterReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("IBMVPCCluster"),
Recorder: mgr.GetEventRecorderFor("ibmvpccluster-controller"),
ServiceEndpoint: serviceEndpoint,
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
}).SetupWithManager(ctx, mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "IBMVPCCluster")
os.Exit(1)
}
Expand All @@ -255,7 +261,7 @@ func setupReconcilers(mgr ctrl.Manager, serviceEndpoint []endpoints.ServiceEndpo
Recorder: mgr.GetEventRecorderFor("ibmpowervscluster-controller"),
ServiceEndpoint: serviceEndpoint,
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
}).SetupWithManager(ctx, mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "IBMPowerVSCluster")
os.Exit(1)
}
Expand Down

0 comments on commit b4e0e2a

Please sign in to comment.