Skip to content

Commit

Permalink
feat: add finalizer on each component CR
Browse files Browse the repository at this point in the history
- remove finalizer before delete component CR

Signed-off-by: Wen Zhou <[email protected]>
  • Loading branch information
zdtsw committed Dec 6, 2024
1 parent b27759b commit 581033e
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 0 deletions.
4 changes: 4 additions & 0 deletions controllers/components/codeflare/codeflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

const (
ComponentName = componentApi.CodeFlareComponentName
finalizerName = "codeflare.components.platform.opendatahub.io/finalizer"
)

var (
Expand All @@ -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))

Expand All @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions controllers/components/dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))

Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

const (
ArgoWorkflowCRD = "workflows.argoproj.io"
finalizerName = "datasciencepipelines.components.platform.opendatahub.io/finalizer"
)

type componentHandler struct{}
Expand Down Expand Up @@ -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))

Expand All @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions controllers/components/kserve/kserve.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (

kserveManifestSourcePath = "overlays/odh"
odhModelControllerManifestSourcePath = "base"
finalizerName = "kserve.components.platform.opendatahub.io/finalizer"
)

type componentHandler struct{}
Expand All @@ -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))

Expand All @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions controllers/components/kueue/kueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

const (
ComponentName = componentApi.KueueComponentName
finalizerName = "kueue.components.platform.opendatahub.io/finalizer"
)

var (
Expand All @@ -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{
Expand All @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions controllers/components/modelcontroller/modelcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

const (
ComponentName = componentApi.ModelControllerComponentName
finalizerName = "modelcontroller.components.platform.opendatahub.io/finalizer"
)

var DefaultPath = odhdeploy.DefaultManifestPath + "/" + ComponentName + "/base"
Expand All @@ -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))

Expand All @@ -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{
Expand Down
5 changes: 5 additions & 0 deletions controllers/components/modelmeshserving/modelmeshserving.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

const (
ComponentName = componentApi.ModelMeshServingComponentName
finalizerName = "model-mesh.components.platform.opendatahub.io/finalizer"
)

type componentHandler struct{}
Expand Down Expand Up @@ -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))

Expand All @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions controllers/components/modelregistry/modelregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (

type componentHandler struct{}

const finalizerName = "modelregistry.components.platform.opendatahub.io/finalizer"

func init() { //nolint:gochecknoinits
cr.Add(&componentHandler{})
}
Expand Down Expand Up @@ -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{
Expand All @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions controllers/components/ray/ray.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

const (
ComponentName = componentApi.RayComponentName
finalizerName = "ray.components.platform.opendatahub.io/finalizer"
)

var (
Expand All @@ -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))

Expand All @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions controllers/components/trainingoperator/trainingoperator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

const (
ComponentName = componentApi.TrainingOperatorComponentName
finalizerName = "trainingoperator.components.platform.opendatahub.io/finalizer"
)

var (
Expand All @@ -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))

Expand All @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions controllers/components/trustyai/trustyai.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
const (
ComponentName = componentApi.TrustyAIComponentName
ComponentPathName = "trustyai-service-operator"
finalizerName = "trustyai.components.platform.opendatahub.io/finalizer"
)

var (
Expand Down Expand Up @@ -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{
Expand All @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions controllers/components/workbenches/workbenches.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (

type componentHandler struct{}

const finalizerName = "workbenches.components.platform.opendatahub.io/finalizer"

func init() { //nolint:gochecknoinits
cr.Add(&componentHandler{})
}
Expand All @@ -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))

Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 581033e

Please sign in to comment.