From 581033e28676d2ab7f6f6a59aba3779d699c3798 Mon Sep 17 00:00:00 2001 From: Wen Zhou Date: Fri, 6 Dec 2024 17:14:47 +0100 Subject: [PATCH] feat: add finalizer on each component CR - remove finalizer before delete component CR Signed-off-by: Wen Zhou --- controllers/components/codeflare/codeflare.go | 4 ++++ controllers/components/dashboard/dashboard.go | 5 +++++ .../datasciencepipelines/datasciencepipelines.go | 4 ++++ controllers/components/kserve/kserve.go | 5 +++++ controllers/components/kueue/kueue.go | 5 +++++ controllers/components/modelcontroller/modelcontroller.go | 5 +++++ .../components/modelmeshserving/modelmeshserving.go | 5 +++++ controllers/components/modelregistry/modelregistry.go | 6 ++++++ controllers/components/ray/ray.go | 5 +++++ .../components/trainingoperator/trainingoperator.go | 5 +++++ controllers/components/trustyai/trustyai.go | 5 +++++ controllers/components/workbenches/workbenches.go | 6 ++++++ .../datasciencecluster/datasciencecluster_controller.go | 7 +++++++ 13 files changed, 67 insertions(+) diff --git a/controllers/components/codeflare/codeflare.go b/controllers/components/codeflare/codeflare.go index 59c2dcfbfd0..2c4f9c213d8 100644 --- a/controllers/components/codeflare/codeflare.go +++ b/controllers/components/codeflare/codeflare.go @@ -17,6 +17,7 @@ import ( const ( ComponentName = componentApi.CodeFlareComponentName + finalizerName = "codeflare.components.platform.opendatahub.io/finalizer" ) var ( @@ -39,6 +40,8 @@ func (s *componentHandler) GetManagementState(dsc *dscv1.DataScienceCluster) ope } func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Object { + var finalizerlist []string + finalizerlist = append(finalizerlist, finalizerName) codeflareAnnotations := make(map[string]string) codeflareAnnotations[annotations.ManagementStateAnnotation] = string(s.GetManagementState(dsc)) @@ -50,6 +53,7 @@ func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Obj ObjectMeta: metav1.ObjectMeta{ Name: componentApi.CodeFlareInstanceName, Annotations: codeflareAnnotations, + Finalizers: finalizerlist, }, Spec: componentApi.CodeFlareSpec{ CodeFlareCommonSpec: dsc.Spec.Components.CodeFlare.CodeFlareCommonSpec, diff --git a/controllers/components/dashboard/dashboard.go b/controllers/components/dashboard/dashboard.go index d8103f8e684..35f0927007d 100644 --- a/controllers/components/dashboard/dashboard.go +++ b/controllers/components/dashboard/dashboard.go @@ -15,6 +15,8 @@ import ( "github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/annotations" ) +const finalizerName = "dashboard.components.platform.opendatahub.io/finalizer" + type componentHandler struct{} func init() { //nolint:gochecknoinits @@ -43,6 +45,8 @@ func (s *componentHandler) Init(platform cluster.Platform) error { } func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Object { + var finalizerlist []string + finalizerlist = append(finalizerlist, finalizerName) dashboardAnnotations := make(map[string]string) dashboardAnnotations[annotations.ManagementStateAnnotation] = string(s.GetManagementState(dsc)) @@ -54,6 +58,7 @@ func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Obj ObjectMeta: metav1.ObjectMeta{ Name: componentApi.DashboardInstanceName, Annotations: dashboardAnnotations, + Finalizers: finalizerlist, }, Spec: componentApi.DashboardSpec{ DashboardCommonSpec: dsc.Spec.Components.Dashboard.DashboardCommonSpec, diff --git a/controllers/components/datasciencepipelines/datasciencepipelines.go b/controllers/components/datasciencepipelines/datasciencepipelines.go index 1d3889551f1..3af1fb27ee4 100644 --- a/controllers/components/datasciencepipelines/datasciencepipelines.go +++ b/controllers/components/datasciencepipelines/datasciencepipelines.go @@ -17,6 +17,7 @@ import ( const ( ArgoWorkflowCRD = "workflows.argoproj.io" + finalizerName = "datasciencepipelines.components.platform.opendatahub.io/finalizer" ) type componentHandler struct{} @@ -64,6 +65,8 @@ func (s *componentHandler) Init(platform cluster.Platform) error { } func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Object { + var finalizerlist []string + finalizerlist = append(finalizerlist, finalizerName) dataSciencePipelinesAnnotations := make(map[string]string) dataSciencePipelinesAnnotations[annotations.ManagementStateAnnotation] = string(s.GetManagementState(dsc)) @@ -75,6 +78,7 @@ func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Obj ObjectMeta: metav1.ObjectMeta{ Name: componentApi.DataSciencePipelinesInstanceName, Annotations: dataSciencePipelinesAnnotations, + Finalizers: finalizerlist, }, Spec: componentApi.DataSciencePipelinesSpec{ DataSciencePipelinesCommonSpec: dsc.Spec.Components.DataSciencePipelines.DataSciencePipelinesCommonSpec, diff --git a/controllers/components/kserve/kserve.go b/controllers/components/kserve/kserve.go index 9e266df115c..78e22c2f9eb 100644 --- a/controllers/components/kserve/kserve.go +++ b/controllers/components/kserve/kserve.go @@ -23,6 +23,7 @@ const ( kserveManifestSourcePath = "overlays/odh" odhModelControllerManifestSourcePath = "base" + finalizerName = "kserve.components.platform.opendatahub.io/finalizer" ) type componentHandler struct{} @@ -49,6 +50,9 @@ func (s *componentHandler) GetManagementState(dsc *dscv1.DataScienceCluster) ope // for DSC to get compoment Kserve's CR. func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Object { + var finalizerlist []string + finalizerlist = append(finalizerlist, finalizerName) + kserveAnnotations := make(map[string]string) kserveAnnotations[annotations.ManagementStateAnnotation] = string(s.GetManagementState(dsc)) @@ -60,6 +64,7 @@ func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Obj ObjectMeta: metav1.ObjectMeta{ Name: componentApi.KserveInstanceName, Annotations: kserveAnnotations, + Finalizers: finalizerlist, }, Spec: componentApi.KserveSpec{ KserveCommonSpec: dsc.Spec.Components.Kserve.KserveCommonSpec, diff --git a/controllers/components/kueue/kueue.go b/controllers/components/kueue/kueue.go index ba8d9bfc3ed..2e79117096f 100644 --- a/controllers/components/kueue/kueue.go +++ b/controllers/components/kueue/kueue.go @@ -17,6 +17,7 @@ import ( const ( ComponentName = componentApi.KueueComponentName + finalizerName = "kueue.components.platform.opendatahub.io/finalizer" ) var ( @@ -41,6 +42,9 @@ func (s *componentHandler) GetManagementState(dsc *dscv1.DataScienceCluster) ope } func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Object { + var finalizerlist []string + finalizerlist = append(finalizerlist, finalizerName) + kueueAnnotations := make(map[string]string) kueueAnnotations[annotations.ManagementStateAnnotation] = string(s.GetManagementState(dsc)) return client.Object(&componentApi.Kueue{ @@ -51,6 +55,7 @@ func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Obj ObjectMeta: metav1.ObjectMeta{ Name: componentApi.KueueInstanceName, Annotations: kueueAnnotations, + Finalizers: finalizerlist, }, Spec: componentApi.KueueSpec{ KueueCommonSpec: dsc.Spec.Components.Kueue.KueueCommonSpec, diff --git a/controllers/components/modelcontroller/modelcontroller.go b/controllers/components/modelcontroller/modelcontroller.go index 58353f88510..968cb4f46b0 100644 --- a/controllers/components/modelcontroller/modelcontroller.go +++ b/controllers/components/modelcontroller/modelcontroller.go @@ -17,6 +17,7 @@ import ( const ( ComponentName = componentApi.ModelControllerComponentName + finalizerName = "modelcontroller.components.platform.opendatahub.io/finalizer" ) var DefaultPath = odhdeploy.DefaultManifestPath + "/" + ComponentName + "/base" @@ -39,6 +40,9 @@ func (s *componentHandler) GetManagementState(dsc *dscv1.DataScienceCluster) ope } func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Object { + var finalizerlist []string + finalizerlist = append(finalizerlist, finalizerName) + mcAnnotations := make(map[string]string) mcAnnotations[annotations.ManagementStateAnnotation] = string(s.GetManagementState(dsc)) @@ -61,6 +65,7 @@ func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Obj ObjectMeta: metav1.ObjectMeta{ Name: componentApi.ModelControllerInstanceName, Annotations: mcAnnotations, + Finalizers: finalizerlist, }, Spec: componentApi.ModelControllerSpec{ ModelMeshServing: &componentApi.ModelControllerMMSpec{ diff --git a/controllers/components/modelmeshserving/modelmeshserving.go b/controllers/components/modelmeshserving/modelmeshserving.go index ebe25579b1b..2bb6e33228d 100644 --- a/controllers/components/modelmeshserving/modelmeshserving.go +++ b/controllers/components/modelmeshserving/modelmeshserving.go @@ -17,6 +17,7 @@ import ( const ( ComponentName = componentApi.ModelMeshServingComponentName + finalizerName = "model-mesh.components.platform.opendatahub.io/finalizer" ) type componentHandler struct{} @@ -54,6 +55,9 @@ func (s *componentHandler) Init(platform cluster.Platform) error { // for DSC to get compoment ModelMeshServing's CR. func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Object { + var finalizerlist []string + finalizerlist = append(finalizerlist, finalizerName) + mmAnnotations := make(map[string]string) mmAnnotations[annotations.ManagementStateAnnotation] = string(s.GetManagementState(dsc)) @@ -65,6 +69,7 @@ func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Obj ObjectMeta: metav1.ObjectMeta{ Name: componentApi.ModelMeshServingInstanceName, Annotations: mmAnnotations, + Finalizers: finalizerlist, }, Spec: componentApi.ModelMeshServingSpec{ ModelMeshServingCommonSpec: dsc.Spec.Components.ModelMeshServing.ModelMeshServingCommonSpec, diff --git a/controllers/components/modelregistry/modelregistry.go b/controllers/components/modelregistry/modelregistry.go index a2b28c9f7ff..b19ae06b311 100644 --- a/controllers/components/modelregistry/modelregistry.go +++ b/controllers/components/modelregistry/modelregistry.go @@ -17,6 +17,8 @@ import ( type componentHandler struct{} +const finalizerName = "modelregistry.components.platform.opendatahub.io/finalizer" + func init() { //nolint:gochecknoinits cr.Add(&componentHandler{}) } @@ -51,6 +53,9 @@ func (s *componentHandler) Init(_ cluster.Platform) error { } func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Object { + var finalizerlist []string + finalizerlist = append(finalizerlist, finalizerName) + componentAnnotations := make(map[string]string) componentAnnotations[annotations.ManagementStateAnnotation] = string(s.GetManagementState(dsc)) return client.Object(&componentApi.ModelRegistry{ @@ -61,6 +66,7 @@ func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Obj ObjectMeta: metav1.ObjectMeta{ Name: componentApi.ModelRegistryInstanceName, Annotations: componentAnnotations, + Finalizers: finalizerlist, }, Spec: componentApi.ModelRegistrySpec{ ModelRegistryCommonSpec: dsc.Spec.Components.ModelRegistry.ModelRegistryCommonSpec, diff --git a/controllers/components/ray/ray.go b/controllers/components/ray/ray.go index 266e5df601b..41e3c378420 100644 --- a/controllers/components/ray/ray.go +++ b/controllers/components/ray/ray.go @@ -17,6 +17,7 @@ import ( const ( ComponentName = componentApi.RayComponentName + finalizerName = "ray.components.platform.opendatahub.io/finalizer" ) var ( @@ -40,6 +41,9 @@ func (s *componentHandler) GetManagementState(dsc *dscv1.DataScienceCluster) ope return operatorv1.Removed } func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Object { + var finalizerlist []string + finalizerlist = append(finalizerlist, finalizerName) + rayAnnotations := make(map[string]string) rayAnnotations[annotations.ManagementStateAnnotation] = string(s.GetManagementState(dsc)) @@ -51,6 +55,7 @@ func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Obj ObjectMeta: metav1.ObjectMeta{ Name: componentApi.RayInstanceName, Annotations: rayAnnotations, + Finalizers: finalizerlist, }, Spec: componentApi.RaySpec{ RayCommonSpec: dsc.Spec.Components.Ray.RayCommonSpec, diff --git a/controllers/components/trainingoperator/trainingoperator.go b/controllers/components/trainingoperator/trainingoperator.go index 02cd82036dd..6988bb11e53 100644 --- a/controllers/components/trainingoperator/trainingoperator.go +++ b/controllers/components/trainingoperator/trainingoperator.go @@ -17,6 +17,7 @@ import ( const ( ComponentName = componentApi.TrainingOperatorComponentName + finalizerName = "trainingoperator.components.platform.opendatahub.io/finalizer" ) var ( @@ -40,6 +41,9 @@ func (s *componentHandler) GetManagementState(dsc *dscv1.DataScienceCluster) ope return operatorv1.Removed } func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) k8sclient.Object { + var finalizerlist []string + finalizerlist = append(finalizerlist, finalizerName) + trainingoperatorAnnotations := make(map[string]string) trainingoperatorAnnotations[annotations.ManagementStateAnnotation] = string(s.GetManagementState(dsc)) @@ -51,6 +55,7 @@ func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) k8sclient. ObjectMeta: metav1.ObjectMeta{ Name: componentApi.TrainingOperatorInstanceName, Annotations: trainingoperatorAnnotations, + Finalizers: finalizerlist, }, Spec: componentApi.TrainingOperatorSpec{ TrainingOperatorCommonSpec: dsc.Spec.Components.TrainingOperator.TrainingOperatorCommonSpec, diff --git a/controllers/components/trustyai/trustyai.go b/controllers/components/trustyai/trustyai.go index 40ad9243d80..8153f7f166e 100644 --- a/controllers/components/trustyai/trustyai.go +++ b/controllers/components/trustyai/trustyai.go @@ -18,6 +18,7 @@ import ( const ( ComponentName = componentApi.TrustyAIComponentName ComponentPathName = "trustyai-service-operator" + finalizerName = "trustyai.components.platform.opendatahub.io/finalizer" ) var ( @@ -47,6 +48,9 @@ func (s *componentHandler) GetManagementState(dsc *dscv1.DataScienceCluster) ope } func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Object { + var finalizerlist []string + finalizerlist = append(finalizerlist, finalizerName) + trustyaiAnnotations := make(map[string]string) trustyaiAnnotations[annotations.ManagementStateAnnotation] = string(s.GetManagementState(dsc)) return client.Object(&componentApi.TrustyAI{ @@ -57,6 +61,7 @@ func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Obj ObjectMeta: metav1.ObjectMeta{ Name: componentApi.TrustyAIInstanceName, Annotations: trustyaiAnnotations, + Finalizers: finalizerlist, }, Spec: componentApi.TrustyAISpec{ TrustyAICommonSpec: dsc.Spec.Components.TrustyAI.TrustyAICommonSpec, diff --git a/controllers/components/workbenches/workbenches.go b/controllers/components/workbenches/workbenches.go index 756bc602a5e..089323fb3d0 100644 --- a/controllers/components/workbenches/workbenches.go +++ b/controllers/components/workbenches/workbenches.go @@ -17,6 +17,8 @@ import ( type componentHandler struct{} +const finalizerName = "workbenches.components.platform.opendatahub.io/finalizer" + func init() { //nolint:gochecknoinits cr.Add(&componentHandler{}) } @@ -33,6 +35,9 @@ func (s *componentHandler) GetManagementState(dsc *dscv1.DataScienceCluster) ope } func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Object { + var finalizerlist []string + finalizerlist = append(finalizerlist, finalizerName) + workbenchesAnnotations := make(map[string]string) workbenchesAnnotations[annotations.ManagementStateAnnotation] = string(s.GetManagementState(dsc)) @@ -44,6 +49,7 @@ func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Obj ObjectMeta: metav1.ObjectMeta{ Name: componentApi.WorkbenchesInstanceName, Annotations: workbenchesAnnotations, + Finalizers: finalizerlist, }, Spec: componentApi.WorkbenchesSpec{ WorkbenchesCommonSpec: dsc.Spec.Components.Workbenches.WorkbenchesCommonSpec, diff --git a/controllers/datasciencecluster/datasciencecluster_controller.go b/controllers/datasciencecluster/datasciencecluster_controller.go index 016905f989e..ba8d4d11494 100644 --- a/controllers/datasciencecluster/datasciencecluster_controller.go +++ b/controllers/datasciencecluster/datasciencecluster_controller.go @@ -351,12 +351,19 @@ func (r *DataScienceClusterReconciler) apply(ctx context.Context, dsc *dscv1.Dat managementStateAnn, exists := obj.GetAnnotations()[annotations.ManagementStateAnnotation] if exists && managementStateAnn == string(operatorv1.Removed) { + if len(obj.GetFinalizers()) != 0 { + patch := []byte(`{"metadata":{"finalizers":[]}}`) + if err := r.Client.Patch(ctx, obj, client.RawPatch(types.MergePatchType, patch)); err != nil { + return fmt.Errorf("failed to remove finalizer from component CR: %w", err) + } + } err := r.Client.Delete(ctx, obj) if k8serr.IsNotFound(err) { return nil } return err } + if err := r.Client.Apply(ctx, obj, client.FieldOwner(dsc.Name), client.ForceOwnership); err != nil { return err }