From 349b2ff35a34feb58fd6bdac6e15a1bda3c03ce1 Mon Sep 17 00:00:00 2001 From: Sergiusz Urbaniak Date: Mon, 12 Feb 2024 20:49:50 +0100 Subject: [PATCH] cmd/manager: deprecate subobject deletion flag, add event and log notifications --- cmd/manager/main.go | 46 ++++++++++++++++++++++++++++++++++------ cmd/manager/main_test.go | 2 +- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/cmd/manager/main.go b/cmd/manager/main.go index 9100a41ed0..2e87860170 100644 --- a/cmd/manager/main.go +++ b/cmd/manager/main.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "context" "errors" "flag" "fmt" @@ -25,6 +26,8 @@ import ( "strings" "time" + "sigs.k8s.io/controller-runtime/pkg/manager" + "go.uber.org/zap/zapcore" ctrzap "sigs.k8s.io/controller-runtime/pkg/log/zap" @@ -64,7 +67,11 @@ const ( objectDeletionProtectionEnvVar = "OBJECT_DELETION_PROTECTION" subobjectDeletionProtectionEnvVar = "SUBOBJECT_DELETION_PROTECTION" objectDeletionProtectionDefault = true - subobjectDeletionProtectionDefault = true + subobjectDeletionProtectionDefault = false + + podNamespace = "POD_NAMESPACE" + podName = "POD_NAME" + subobjectDeletionProtectionMessage = "Note: sub-object deletion protection is IGNORED because it does not work deterministically." ) var ( @@ -147,7 +154,7 @@ func main() { EventRecorder: mgr.GetEventRecorderFor("AtlasDeployment"), AtlasProvider: atlasProvider, ObjectDeletionProtection: config.ObjectDeletionProtection, - SubObjectDeletionProtection: config.SubObjectDeletionProtection, + SubObjectDeletionProtection: false, }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "AtlasDeployment") os.Exit(1) @@ -162,7 +169,7 @@ func main() { EventRecorder: mgr.GetEventRecorderFor("AtlasProject"), AtlasProvider: atlasProvider, ObjectDeletionProtection: config.ObjectDeletionProtection, - SubObjectDeletionProtection: config.SubObjectDeletionProtection, + SubObjectDeletionProtection: false, }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "AtlasProject") os.Exit(1) @@ -177,7 +184,7 @@ func main() { AtlasProvider: atlasProvider, GlobalPredicates: globalPredicates, ObjectDeletionProtection: config.ObjectDeletionProtection, - SubObjectDeletionProtection: config.SubObjectDeletionProtection, + SubObjectDeletionProtection: false, FeaturePreviewOIDCAuthEnabled: config.FeatureFlags.IsFeaturePresent(featureflags.FeatureOIDC), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "AtlasDatabaseUser") @@ -193,7 +200,7 @@ func main() { EventRecorder: mgr.GetEventRecorderFor("AtlasDataFederation"), AtlasProvider: atlasProvider, ObjectDeletionProtection: config.ObjectDeletionProtection, - SubObjectDeletionProtection: config.SubObjectDeletionProtection, + SubObjectDeletionProtection: false, }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "AtlasDataFederation") os.Exit(1) @@ -208,7 +215,7 @@ func main() { EventRecorder: mgr.GetEventRecorderFor("AtlasFederatedAuth"), AtlasProvider: atlasProvider, ObjectDeletionProtection: config.ObjectDeletionProtection, - SubObjectDeletionProtection: config.SubObjectDeletionProtection, + SubObjectDeletionProtection: false, }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "AtlasFederatedAuth") os.Exit(1) @@ -225,6 +232,31 @@ func main() { os.Exit(1) } + err = mgr.Add(manager.RunnableFunc(func(ctx context.Context) error { + setupLog.Info(subobjectDeletionProtectionMessage) + + akoPod := &corev1.Pod{} + err = mgr.GetClient().Get(ctx, client.ObjectKey{ + Namespace: os.Getenv(podNamespace), + Name: os.Getenv(podName), + }, akoPod) + + if err == nil { + mgr.GetEventRecorderFor("AtlasKubernetesOperator").Event( + akoPod, + corev1.EventTypeNormal, + "AKOSubObjectDeletionProtection", + subobjectDeletionProtectionMessage, + ) + } + + return nil + })) + if err != nil { + setupLog.Error(err, "unable to set up runnable") + os.Exit(1) + } + setupLog.Info("starting manager") if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { setupLog.Error(err, "problem running manager") @@ -264,7 +296,7 @@ func parseConfiguration() Config { flag.BoolVar(&config.ObjectDeletionProtection, objectDeletionProtectionFlag, objectDeletionProtectionDefault, "Defines if the operator deletes Atlas resource "+ "when a Custom Resource is deleted") flag.BoolVar(&config.SubObjectDeletionProtection, subobjectDeletionProtectionFlag, subobjectDeletionProtectionDefault, "Defines if the operator overwrites "+ - "(and consequently delete) subresources that were not previously created by the operator") + "(and consequently delete) subresources that were not previously created by the operator. "+subobjectDeletionProtectionMessage) appVersion := flag.Bool("v", false, "prints application version") flag.Parse() diff --git a/cmd/manager/main_test.go b/cmd/manager/main_test.go index c8ba6fc149..12c605ff9b 100644 --- a/cmd/manager/main_test.go +++ b/cmd/manager/main_test.go @@ -34,7 +34,7 @@ func Test_configureDeletionProtection(t *testing.T) { t, Config{ ObjectDeletionProtection: true, - SubObjectDeletionProtection: true, + SubObjectDeletionProtection: false, }, config, )