Skip to content

refactor: Group setup by feature flag #3647

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
281 changes: 144 additions & 137 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,217 +174,224 @@ func main() {
externalResourceGC = true
}

if feature.Gates.Enabled(feature.BootstrapFormatIgnition) {
setupLog.Info("Enabling Ignition support for machine bootstrap data")
}

// Parse service endpoints.
AWSServiceEndpoints, err := endpoints.ParseFlag(serviceEndpoints)
awsServiceEndpoints, err := endpoints.ParseFlag(serviceEndpoints)
if err != nil {
setupLog.Error(err, "unable to parse service endpoints", "controller", "AWSCluster")
os.Exit(1)
}

if err = (&controllers.AWSMachineReconciler{
setupReconcilersAndWebhooks(ctx, mgr, awsServiceEndpoints, externalResourceGC)
if feature.Gates.Enabled(feature.EKS) {
setupEKSReconcilersAndWebhooks(ctx, mgr, awsServiceEndpoints, externalResourceGC)
}

// +kubebuilder:scaffold:builder

if err := mgr.AddReadyzCheck("webhook", mgr.GetWebhookServer().StartedChecker()); err != nil {
setupLog.Error(err, "unable to create ready check")
os.Exit(1)
}

if err := mgr.AddHealthzCheck("webhook", mgr.GetWebhookServer().StartedChecker()); err != nil {
setupLog.Error(err, "unable to create health check")
os.Exit(1)
}

setupLog.Info("starting manager", "version", version.Get().String())
if err := mgr.Start(ctx); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}

func setupReconcilersAndWebhooks(ctx context.Context, mgr ctrl.Manager, awsServiceEndpoints []scope.ServiceEndpoint,
externalResourceGC bool,
) {
if err := (&controllers.AWSMachineReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("AWSMachine"),
Recorder: mgr.GetEventRecorderFor("awsmachine-controller"),
Endpoints: AWSServiceEndpoints,
Endpoints: awsServiceEndpoints,
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: awsMachineConcurrency, RecoverPanic: true}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AWSMachine")
os.Exit(1)
}
if err = (&controllers.AWSClusterReconciler{

if err := (&controllers.AWSClusterReconciler{
Client: mgr.GetClient(),
Recorder: mgr.GetEventRecorderFor("awscluster-controller"),
Endpoints: AWSServiceEndpoints,
Endpoints: awsServiceEndpoints,
WatchFilterValue: watchFilterValue,
ExternalResourceGC: externalResourceGC,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: awsClusterConcurrency, RecoverPanic: true}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AWSCluster")
os.Exit(1)
}
enableGates(ctx, mgr, AWSServiceEndpoints, externalResourceGC)

if err = (&infrav1.AWSMachineTemplate{}).SetupWebhookWithManager(mgr); err != nil {
if feature.Gates.Enabled(feature.MachinePool) {
setupLog.V(2).Info("enabling machine pool controller and webhook")
if err := (&expcontrollers.AWSMachinePoolReconciler{
Client: mgr.GetClient(),
Recorder: mgr.GetEventRecorderFor("awsmachinepool-controller"),
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: instanceStateConcurrency, RecoverPanic: true}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AWSMachinePool")
os.Exit(1)
}

if err := (&expinfrav1.AWSMachinePool{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "AWSMachinePool")
os.Exit(1)
}
}

if feature.Gates.Enabled(feature.EventBridgeInstanceState) {
setupLog.Info("EventBridge notifications enabled. enabling AWSInstanceStateController")
if err := (&instancestate.AwsInstanceStateReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("AWSInstanceStateController"),
Endpoints: awsServiceEndpoints,
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: instanceStateConcurrency, RecoverPanic: true}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AWSInstanceStateController")
os.Exit(1)
}
}

if feature.Gates.Enabled(feature.AutoControllerIdentityCreator) {
setupLog.Info("AutoControllerIdentityCreator enabled")
if err := (&controlleridentitycreator.AWSControllerIdentityReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("AWSControllerIdentity"),
Endpoints: awsServiceEndpoints,
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: awsClusterConcurrency, RecoverPanic: true}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AWSControllerIdentity")
os.Exit(1)
}
}

if err := (&infrav1.AWSMachineTemplate{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "AWSMachineTemplate")
os.Exit(1)
}
if err = (&infrav1.AWSCluster{}).SetupWebhookWithManager(mgr); err != nil {
if err := (&infrav1.AWSCluster{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "AWSCluster")
os.Exit(1)
}
if err = (&infrav1.AWSClusterTemplate{}).SetupWebhookWithManager(mgr); err != nil {
if err := (&infrav1.AWSClusterTemplate{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "AWSClusterTemplate")
os.Exit(1)
}
if err = (&infrav1.AWSClusterControllerIdentity{}).SetupWebhookWithManager(mgr); err != nil {
if err := (&infrav1.AWSClusterControllerIdentity{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "AWSClusterControllerIdentity")
os.Exit(1)
}
if err = (&infrav1.AWSClusterRoleIdentity{}).SetupWebhookWithManager(mgr); err != nil {
if err := (&infrav1.AWSClusterRoleIdentity{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "AWSClusterRoleIdentity")
os.Exit(1)
}
if err = (&infrav1.AWSClusterStaticIdentity{}).SetupWebhookWithManager(mgr); err != nil {
if err := (&infrav1.AWSClusterStaticIdentity{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "AWSClusterStaticIdentity")
os.Exit(1)
}
if err = (&infrav1.AWSMachine{}).SetupWebhookWithManager(mgr); err != nil {
if err := (&infrav1.AWSMachine{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "AWSMachine")
os.Exit(1)
}
if feature.Gates.Enabled(feature.EKS) {
setupLog.Info("enabling EKS webhooks")
if err := (&ekscontrolplanev1.AWSManagedControlPlane{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "AWSManagedControlPlane")
os.Exit(1)
}
if feature.Gates.Enabled(feature.EKSFargate) {
if err = (&expinfrav1.AWSFargateProfile{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "AWSFargateProfile")
os.Exit(1)
}
}
if feature.Gates.Enabled(feature.MachinePool) {
if err = (&expinfrav1.AWSManagedMachinePool{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "AWSManagedMachinePool")
os.Exit(1)
}
}
}
if feature.Gates.Enabled(feature.MachinePool) {
setupLog.Info("enabling webhook for AWSMachinePool")
if err = (&expinfrav1.AWSMachinePool{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "AWSMachinePool")
os.Exit(1)
}
}
}

// +kubebuilder:scaffold:builder
func setupEKSReconcilersAndWebhooks(ctx context.Context, mgr ctrl.Manager, awsServiceEndpoints []scope.ServiceEndpoint,
externalResourceGC bool,
) {
setupLog.Info("enabling EKS controllers and webhooks")

if err := mgr.AddReadyzCheck("webhook", mgr.GetWebhookServer().StartedChecker()); err != nil {
setupLog.Error(err, "unable to create ready check")
if syncPeriod > maxEKSSyncPeriod {
setupLog.Error(errMaxSyncPeriodExceeded, "failed to enable EKS", "max-sync-period", maxEKSSyncPeriod, "syn-period", syncPeriod)
os.Exit(1)
}

if err := mgr.AddHealthzCheck("webhook", mgr.GetWebhookServer().StartedChecker()); err != nil {
setupLog.Error(err, "unable to create health check")
enableIAM := feature.Gates.Enabled(feature.EKSEnableIAM)
allowAddRoles := feature.Gates.Enabled(feature.EKSAllowAddRoles)
setupLog.V(2).Info("EKS IAM role creation", "enabled", enableIAM)
setupLog.V(2).Info("EKS IAM additional roles", "enabled", allowAddRoles)
if allowAddRoles && !enableIAM {
setupLog.Error(errEKSInvalidFlags, "cannot use EKSAllowAddRoles flag without EKSEnableIAM")
os.Exit(1)
}

setupLog.Info("starting manager", "version", version.Get().String())
if err := mgr.Start(ctx); err != nil {
setupLog.Error(err, "problem running manager")
setupLog.V(2).Info("enabling EKS control plane controller")
if err := (&ekscontrolplanecontrollers.AWSManagedControlPlaneReconciler{
Client: mgr.GetClient(),
EnableIAM: enableIAM,
AllowAdditionalRoles: allowAddRoles,
Endpoints: awsServiceEndpoints,
WatchFilterValue: watchFilterValue,
ExternalResourceGC: externalResourceGC,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: awsClusterConcurrency, RecoverPanic: true}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AWSManagedControlPlane")
os.Exit(1)
}
}

func enableGates(ctx context.Context, mgr ctrl.Manager, awsServiceEndpoints []scope.ServiceEndpoint, externalResourceGC bool) {
if feature.Gates.Enabled(feature.EKS) {
setupLog.Info("enabling EKS controllers")
setupLog.V(2).Info("enabling EKS bootstrap controller")
if err := (&eksbootstrapcontrollers.EKSConfigReconciler{
Client: mgr.GetClient(),
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: awsClusterConcurrency, RecoverPanic: true}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "EKSConfig")
os.Exit(1)
}

if syncPeriod > maxEKSSyncPeriod {
setupLog.Error(errMaxSyncPeriodExceeded, "failed to enable EKS", "max-sync-period", maxEKSSyncPeriod, "syn-period", syncPeriod)
os.Exit(1)
if feature.Gates.Enabled(feature.EKSFargate) {
setupLog.V(2).Info("enabling EKS fargate profile controller")
if err := (&expcontrollers.AWSFargateProfileReconciler{
Client: mgr.GetClient(),
Recorder: mgr.GetEventRecorderFor("awsfargateprofile-reconciler"),
EnableIAM: enableIAM,
Endpoints: awsServiceEndpoints,
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: awsClusterConcurrency, RecoverPanic: true}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AWSFargateProfile")
}

enableIAM := feature.Gates.Enabled(feature.EKSEnableIAM)
allowAddRoles := feature.Gates.Enabled(feature.EKSAllowAddRoles)
setupLog.V(2).Info("EKS IAM role creation", "enabled", enableIAM)
setupLog.V(2).Info("EKS IAM additional roles", "enabled", allowAddRoles)
if allowAddRoles && !enableIAM {
setupLog.Error(errEKSInvalidFlags, "cannot use EKSAllowAddRoles flag without EKSEnableIAM")
if err := (&expinfrav1.AWSFargateProfile{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "AWSFargateProfile")
os.Exit(1)
}
}

setupLog.V(2).Info("enabling EKS control plane controller")
if err := (&ekscontrolplanecontrollers.AWSManagedControlPlaneReconciler{
if feature.Gates.Enabled(feature.MachinePool) {
setupLog.V(2).Info("enabling EKS managed machine pool controller")
if err := (&expcontrollers.AWSManagedMachinePoolReconciler{
AllowAdditionalRoles: allowAddRoles,
Client: mgr.GetClient(),
EnableIAM: enableIAM,
AllowAdditionalRoles: allowAddRoles,
Endpoints: awsServiceEndpoints,
Recorder: mgr.GetEventRecorderFor("awsmanagedmachinepool-reconciler"),
WatchFilterValue: watchFilterValue,
ExternalResourceGC: externalResourceGC,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: awsClusterConcurrency, RecoverPanic: true}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AWSManagedControlPlane")
os.Exit(1)
}

setupLog.V(2).Info("enabling EKS bootstrap controller")
if err := (&eksbootstrapcontrollers.EKSConfigReconciler{
Client: mgr.GetClient(),
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: awsClusterConcurrency, RecoverPanic: true}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "EKSConfig")
os.Exit(1)
}

if feature.Gates.Enabled(feature.EKSFargate) {
setupLog.V(2).Info("enabling EKS fargate profile controller")
if err := (&expcontrollers.AWSFargateProfileReconciler{
Client: mgr.GetClient(),
Recorder: mgr.GetEventRecorderFor("awsfargateprofile-reconciler"),
EnableIAM: enableIAM,
Endpoints: awsServiceEndpoints,
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: awsClusterConcurrency, RecoverPanic: true}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AWSFargateProfile")
}
}

if feature.Gates.Enabled(feature.MachinePool) {
setupLog.V(2).Info("enabling EKS managed machine pool controller")
if err := (&expcontrollers.AWSManagedMachinePoolReconciler{
AllowAdditionalRoles: allowAddRoles,
Client: mgr.GetClient(),
EnableIAM: enableIAM,
Endpoints: awsServiceEndpoints,
Recorder: mgr.GetEventRecorderFor("awsmanagedmachinepool-reconciler"),
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: instanceStateConcurrency, RecoverPanic: true}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AWSManagedMachinePool")
os.Exit(1)
}
}
}
if feature.Gates.Enabled(feature.MachinePool) {
setupLog.V(2).Info("enabling machine pool controller")
if err := (&expcontrollers.AWSMachinePoolReconciler{
Client: mgr.GetClient(),
Recorder: mgr.GetEventRecorderFor("awsmachinepool-controller"),
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: instanceStateConcurrency, RecoverPanic: true}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AWSMachinePool")
os.Exit(1)
}
}
if feature.Gates.Enabled(feature.EventBridgeInstanceState) {
setupLog.Info("EventBridge notifications enabled. enabling AWSInstanceStateController")
if err := (&instancestate.AwsInstanceStateReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("AWSInstanceStateController"),
Endpoints: awsServiceEndpoints,
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: instanceStateConcurrency, RecoverPanic: true}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AWSInstanceStateController")
setupLog.Error(err, "unable to create controller", "controller", "AWSManagedMachinePool")
os.Exit(1)
}
}
if feature.Gates.Enabled(feature.AutoControllerIdentityCreator) {
setupLog.Info("AutoControllerIdentityCreator enabled")
if err := (&controlleridentitycreator.AWSControllerIdentityReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("AWSControllerIdentity"),
Endpoints: awsServiceEndpoints,
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: awsClusterConcurrency, RecoverPanic: true}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AWSControllerIdentity")

if err := (&expinfrav1.AWSManagedMachinePool{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "AWSManagedMachinePool")
os.Exit(1)
}
}

if feature.Gates.Enabled(feature.BootstrapFormatIgnition) {
setupLog.Info("Enabling Ignition support for machine bootstrap data")
if err := (&ekscontrolplanev1.AWSManagedControlPlane{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "AWSManagedControlPlane")
os.Exit(1)
}
}

func initFlags(fs *pflag.FlagSet) {
fs.StringVar(
&metricsBindAddr,
Expand Down