Skip to content
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

Migration uninstall configmap from DSC to setup controller #1438

Merged
merged 12 commits into from
Dec 19, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ metadata:
categories: AI/Machine Learning, Big Data
certified: "False"
containerImage: quay.io/opendatahub/opendatahub-operator:v2.21.0
createdAt: "2024-11-22T19:16:14Z"
createdAt: "2024-12-16T13:35:31Z"
olm.skipRange: '>=1.0.0 <2.21.0'
operators.operatorframework.io/builder: operator-sdk-v1.31.0
operators.operatorframework.io/internal-objects: '["featuretrackers.features.opendatahub.io",
Expand Down Expand Up @@ -649,6 +649,7 @@ spec:
verbs:
- create
- delete
- deletecollection
- get
- list
- patch
Expand Down
1 change: 1 addition & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ rules:
verbs:
- create
- delete
- deletecollection
- get
- list
- patch
Expand Down
77 changes: 0 additions & 77 deletions controllers/datasciencecluster/datasciencecluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,14 @@ import (
corev1 "k8s.io/api/core/v1"
k8serr "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/opendatahub-io/opendatahub-operator/v2/apis/common"
Expand All @@ -52,7 +49,6 @@ import (
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/handlers"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/predicates"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/predicates/dependent"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/upgrade"
)

// DataScienceClusterReconciler reconciles a DataScienceCluster object.
Expand All @@ -76,53 +72,24 @@ const (
func (r *DataScienceClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := logf.FromContext(ctx).WithName("DataScienceCluster")
log.Info("Reconciling DataScienceCluster resources", "Request.Name", req.Name)

instance := &dscv1.DataScienceCluster{}
err := r.Client.Get(ctx, req.NamespacedName, instance)
biswassri marked this conversation as resolved.
Show resolved Hide resolved

switch {
case k8serr.IsNotFound(err):
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected.
// For additional cleanup logic use operatorUninstall function.
// Return and don't requeue
if upgrade.HasDeleteConfigMap(ctx, r.Client) {
if uninstallErr := upgrade.OperatorUninstall(ctx, r.Client, cluster.GetRelease().Name); uninstallErr != nil {
return ctrl.Result{}, fmt.Errorf("error while operator uninstall: %w", uninstallErr)
}
}

return ctrl.Result{}, nil
case err != nil:
return ctrl.Result{}, err
}

// We don't need finalizer anymore, remove it if present to handle the
// upgrade case
if controllerutil.RemoveFinalizer(instance, finalizerName) {
biswassri marked this conversation as resolved.
Show resolved Hide resolved
if err := r.Client.Update(ctx, instance); err != nil {
return ctrl.Result{}, err
}
}

// If DSC CR exist and deletion CM exist
// delete DSC CR and let reconcile requeue
if upgrade.HasDeleteConfigMap(ctx, r.Client) {
err := r.Client.Delete(ctx, instance, client.PropagationPolicy(metav1.DeletePropagationForeground))
if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}

return ctrl.Result{}, nil
}

if !instance.ObjectMeta.DeletionTimestamp.IsZero() {
log.Info("Finalization DataScienceCluster start deleting instance", "name", instance.Name)

if upgrade.HasDeleteConfigMap(ctx, r.Client) {
return ctrl.Result{Requeue: true}, nil
}

return ctrl.Result{}, nil
}

Expand Down Expand Up @@ -327,53 +294,9 @@ func (r *DataScienceClusterReconciler) SetupWithManager(_ context.Context, mgr c
Watches(
&dsciv1.DSCInitialization{},
handlers.Fn(r.watchDataScienceClusters)).
Watches(
&corev1.ConfigMap{},
handlers.Fn(r.watchDataScienceClusters),
builder.WithPredicates(r.filterDeleteConfigMap())).
Complete(r)
}

func (r *DataScienceClusterReconciler) filterDeleteConfigMap() predicate.Funcs {
filter := func(obj client.Object) bool {
cm, ok := obj.(*corev1.ConfigMap)
if !ok {
return false
}

// Trigger reconcile function when uninstall configmap is created
operatorNs, err := cluster.GetOperatorNamespace()
if err != nil {
return false
}

if cm.Namespace != operatorNs {
return false
}

if cm.Labels[upgrade.DeleteConfigMapLabel] != "true" {
return false
}

return true
}

return predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return filter(e.Object)
},
UpdateFunc: func(e event.UpdateEvent) bool {
return filter(e.ObjectNew)
},
DeleteFunc: func(e event.DeleteEvent) bool {
return false
},
GenericFunc: func(e event.GenericEvent) bool {
return false
},
}
}

func (r *DataScienceClusterReconciler) watchDataScienceClusters(ctx context.Context, _ client.Object) []reconcile.Request {
instanceList := &dscv1.DataScienceClusterList{}
err := r.Client.List(ctx, instanceList)
Expand Down
2 changes: 1 addition & 1 deletion controllers/datasciencecluster/kubebuilder_rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package datasciencecluster

// +kubebuilder:rbac:groups="datasciencecluster.opendatahub.io",resources=datascienceclusters/status,verbs=get;update;patch
// +kubebuilder:rbac:groups="datasciencecluster.opendatahub.io",resources=datascienceclusters/finalizers,verbs=update;patch
// +kubebuilder:rbac:groups="datasciencecluster.opendatahub.io",resources=datascienceclusters,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="datasciencecluster.opendatahub.io",resources=datascienceclusters,verbs=get;list;watch;create;update;patch;delete;deletecollection
zdtsw marked this conversation as resolved.
Show resolved Hide resolved

// +kubebuilder:rbac:groups="authentication.k8s.io",resources=tokenreviews,verbs=create;get
// +kubebuilder:rbac:groups="authorization.k8s.io",resources=subjectaccessreviews,verbs=create;get
Expand Down
83 changes: 83 additions & 0 deletions controllers/setupcontroller/setup_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package setupcontroller

import (
"context"
"fmt"

corev1 "k8s.io/api/core/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"

"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
odhClient "github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/client"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/upgrade"
)

type SetupControllerReconciler struct {
*odhClient.Client
}

func (r *SetupControllerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
biswassri marked this conversation as resolved.
Show resolved Hide resolved
log := logf.FromContext(ctx).WithName("SetupController")
log.Info("Reconciling setup controller")

if !upgrade.HasDeleteConfigMap(ctx, r.Client) {
return ctrl.Result{}, nil
}

Check warning on line 30 in controllers/setupcontroller/setup_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/setupcontroller/setup_controller.go#L24-L30

Added lines #L24 - L30 were not covered by tests

if err := upgrade.OperatorUninstall(ctx, r.Client, cluster.GetRelease().Name); err != nil {
return ctrl.Result{}, fmt.Errorf("operator uninstall failed : %w", err)
}

Check warning on line 34 in controllers/setupcontroller/setup_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/setupcontroller/setup_controller.go#L32-L34

Added lines #L32 - L34 were not covered by tests

return ctrl.Result{}, nil

Check warning on line 36 in controllers/setupcontroller/setup_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/setupcontroller/setup_controller.go#L36

Added line #L36 was not covered by tests
}

func (r *SetupControllerReconciler) SetupWithManager(mgr ctrl.Manager) error {
operatorNs, err := cluster.GetOperatorNamespace()

if err != nil {
return fmt.Errorf("failed to get operator namespace: %w", err)
}
return ctrl.NewControllerManagedBy(mgr).
For(&corev1.ConfigMap{}, builder.WithPredicates(r.filterDeleteConfigMap(operatorNs))).
Complete(r)

Check warning on line 47 in controllers/setupcontroller/setup_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/setupcontroller/setup_controller.go#L39-L47

Added lines #L39 - L47 were not covered by tests
}

func (r *SetupControllerReconciler) filterDeleteConfigMap(operatorNs string) predicate.Funcs {
filter := func(obj client.Object) bool {
cm, ok := obj.(*corev1.ConfigMap)

if !ok {
return false
}

Check warning on line 56 in controllers/setupcontroller/setup_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/setupcontroller/setup_controller.go#L50-L56

Added lines #L50 - L56 were not covered by tests

if cm.Namespace != operatorNs {
return false
}

Check warning on line 60 in controllers/setupcontroller/setup_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/setupcontroller/setup_controller.go#L58-L60

Added lines #L58 - L60 were not covered by tests

if cm.Labels[upgrade.DeleteConfigMapLabel] != "true" {
return false
}

Check warning on line 64 in controllers/setupcontroller/setup_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/setupcontroller/setup_controller.go#L62-L64

Added lines #L62 - L64 were not covered by tests

return true

Check warning on line 66 in controllers/setupcontroller/setup_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/setupcontroller/setup_controller.go#L66

Added line #L66 was not covered by tests
}

return predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return filter(e.Object)
},
UpdateFunc: func(e event.UpdateEvent) bool {
return filter(e.ObjectNew)
},
DeleteFunc: func(e event.DeleteEvent) bool {
return false
},
GenericFunc: func(e event.GenericEvent) bool {
return false
},

Check warning on line 81 in controllers/setupcontroller/setup_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/setupcontroller/setup_controller.go#L69-L81

Added lines #L69 - L81 were not covered by tests
}
}
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import (
dscctrl "github.com/opendatahub-io/opendatahub-operator/v2/controllers/datasciencecluster"
dscictrl "github.com/opendatahub-io/opendatahub-operator/v2/controllers/dscinitialization"
"github.com/opendatahub-io/opendatahub-operator/v2/controllers/secretgenerator"
"github.com/opendatahub-io/opendatahub-operator/v2/controllers/setupcontroller"
"github.com/opendatahub-io/opendatahub-operator/v2/controllers/webhook"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster/gvk"
Expand Down Expand Up @@ -301,6 +302,13 @@ func main() { //nolint:funlen,maintidx
os.Exit(1)
}

if err = (&setupcontroller.SetupControllerReconciler{
Client: oc,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "SetupController")
os.Exit(1)
}

if err = (&secretgenerator.SecretGeneratorReconciler{
Client: oc,
Scheme: mgr.GetScheme(),
Expand Down
17 changes: 17 additions & 0 deletions pkg/upgrade/uninstallation.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
"github.com/hashicorp/go-multierror"
corev1 "k8s.io/api/core/v1"
k8serr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"

dscv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/datasciencecluster/v1"
dsciv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/labels"
Expand All @@ -27,6 +29,11 @@
// This includes DSCI, namespace created by operator (but not workbench or MR's), subscription and CSV.
func OperatorUninstall(ctx context.Context, cli client.Client, platform cluster.Platform) error {
log := logf.FromContext(ctx)

if err := removeDSC(ctx, cli); err != nil {
return err
}

Check warning on line 35 in pkg/upgrade/uninstallation.go

View check run for this annotation

Codecov / codecov/patch

pkg/upgrade/uninstallation.go#L32-L35

Added lines #L32 - L35 were not covered by tests

if err := removeDSCInitialization(ctx, cli); err != nil {
return err
}
Expand Down Expand Up @@ -102,6 +109,16 @@
return multiErr.ErrorOrNil()
}

func removeDSC(ctx context.Context, cli client.Client) error {
instance := &dscv1.DataScienceCluster{}

if err := cli.DeleteAllOf(ctx, instance, client.PropagationPolicy(metav1.DeletePropagationForeground)); err != nil {
biswassri marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("failure deleting DSC: %w", err)
}

Check warning on line 117 in pkg/upgrade/uninstallation.go

View check run for this annotation

Codecov / codecov/patch

pkg/upgrade/uninstallation.go#L112-L117

Added lines #L112 - L117 were not covered by tests

return nil

Check warning on line 119 in pkg/upgrade/uninstallation.go

View check run for this annotation

Codecov / codecov/patch

pkg/upgrade/uninstallation.go#L119

Added line #L119 was not covered by tests
}

// HasDeleteConfigMap returns true if delete configMap is added to the operator namespace by managed-tenants repo.
// It returns false in all other cases.
func HasDeleteConfigMap(ctx context.Context, c client.Client) bool {
Expand Down
Loading