-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed ownership of resources and remove finalizer in deletion Signed-off-by: Amit Berner <[email protected]>
- Loading branch information
Showing
12 changed files
with
1,348 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,12 +25,14 @@ import ( | |
"github.com/red-hat-storage/ocs-client-operator/pkg/csi" | ||
"github.com/red-hat-storage/ocs-client-operator/pkg/templates" | ||
|
||
csiaddonsv1alpha1 "github.com/csi-addons/kubernetes-csi-addons/apis/csiaddons/v1alpha1" | ||
"github.com/go-logr/logr" | ||
configv1 "github.com/openshift/api/config/v1" | ||
secv1 "github.com/openshift/api/security/v1" | ||
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
k8serrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
|
@@ -57,8 +59,8 @@ const ( | |
clusterVersionName = "version" | ||
) | ||
|
||
// ClusterVersionReconciler reconciles a ClusterVersion object | ||
type ClusterVersionReconciler struct { | ||
// OperatorConfigMapReconciler reconciles a ClusterVersion object | ||
type OperatorConfigMapReconciler struct { | ||
client.Client | ||
OperatorDeployment *appsv1.Deployment | ||
OperatorNamespace string | ||
|
@@ -76,7 +78,7 @@ type ClusterVersionReconciler struct { | |
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (c *ClusterVersionReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
func (c *OperatorConfigMapReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
clusterVersionPredicates := builder.WithPredicates( | ||
predicate.GenerationChangedPredicate{}, | ||
) | ||
|
@@ -91,19 +93,19 @@ func (c *ClusterVersionReconciler) SetupWithManager(mgr ctrl.Manager) error { | |
), | ||
) | ||
// Reconcile the ClusterVersion object when the operator config map is updated | ||
enqueueClusterVersionRequest := handler.EnqueueRequestsFromMapFunc( | ||
enqueueConfigMapRequest := handler.EnqueueRequestsFromMapFunc( | ||
func(_ context.Context, client client.Object) []reconcile.Request { | ||
return []reconcile.Request{{ | ||
NamespacedName: types.NamespacedName{ | ||
Name: clusterVersionName, | ||
Name: operatorConfigMapName, | ||
}, | ||
}} | ||
}, | ||
) | ||
|
||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&configv1.ClusterVersion{}, clusterVersionPredicates). | ||
Watches(&corev1.ConfigMap{}, enqueueClusterVersionRequest, configMapPredicates). | ||
For(&corev1.ConfigMap{}, configMapPredicates). | ||
Watches(&configv1.ClusterVersion{}, enqueueConfigMapRequest, clusterVersionPredicates). | ||
Complete(c) | ||
} | ||
|
||
|
@@ -123,23 +125,30 @@ func (c *ClusterVersionReconciler) SetupWithManager(mgr ctrl.Manager) error { | |
|
||
// For more details, check Reconcile and its Result here: | ||
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile | ||
func (c *ClusterVersionReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
func (c *OperatorConfigMapReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
var err error | ||
c.ctx = ctx | ||
c.log = log.FromContext(ctx, "ClusterVersion", req) | ||
c.log.Info("Reconciling ClusterVersion") | ||
c.log = log.FromContext(ctx, "OperatorConfigMap", req) | ||
c.log.Info("Reconciling OperatorConfigMap") | ||
|
||
if err := c.ensureConsolePlugin(); err != nil { | ||
c.log.Error(err, "unable to deploy client console") | ||
return ctrl.Result{}, err | ||
} | ||
|
||
instance := configv1.ClusterVersion{} | ||
instance := corev1.ConfigMap{} | ||
if err = c.Client.Get(context.TODO(), req.NamespacedName, &instance); err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
|
||
if err := csi.InitializeSidecars(instance.Status.Desired.Version); err != nil { | ||
clusterVersion := &configv1.ClusterVersion{} | ||
err = c.Client.Get(c.ctx, types.NamespacedName{Name: clusterVersionName}, clusterVersion) | ||
if err != nil { | ||
c.log.Error(err, "failed to get the clusterVersion version of the OCP cluster") | ||
return reconcile.Result{}, err | ||
} | ||
|
||
if err := csi.InitializeSidecars(clusterVersion.Status.Desired.Version); err != nil { | ||
c.log.Error(err, "unable to initialize sidecars") | ||
return ctrl.Result{}, err | ||
} | ||
|
@@ -210,10 +219,10 @@ func (c *ClusterVersionReconciler) Reconcile(ctx context.Context, req ctrl.Reque | |
}, | ||
} | ||
err = c.createOrUpdate(c.cephFSDeployment, func() error { | ||
csi.GetCephFSDeployment(c.OperatorNamespace).DeepCopyInto(c.cephFSDeployment) | ||
if err := c.own(c.cephFSDeployment); err != nil { | ||
return err | ||
} | ||
csi.GetCephFSDeployment(c.OperatorNamespace).DeepCopyInto(c.cephFSDeployment) | ||
return nil | ||
}) | ||
if err != nil { | ||
|
@@ -228,10 +237,10 @@ func (c *ClusterVersionReconciler) Reconcile(ctx context.Context, req ctrl.Reque | |
}, | ||
} | ||
err = c.createOrUpdate(c.cephFSDaemonSet, func() error { | ||
csi.GetCephFSDaemonSet(c.OperatorNamespace).DeepCopyInto(c.cephFSDaemonSet) | ||
if err := c.own(c.cephFSDaemonSet); err != nil { | ||
return err | ||
} | ||
csi.GetCephFSDaemonSet(c.OperatorNamespace).DeepCopyInto(c.cephFSDaemonSet) | ||
return nil | ||
}) | ||
if err != nil { | ||
|
@@ -246,10 +255,10 @@ func (c *ClusterVersionReconciler) Reconcile(ctx context.Context, req ctrl.Reque | |
}, | ||
} | ||
err = c.createOrUpdate(c.rbdDeployment, func() error { | ||
csi.GetRBDDeployment(c.OperatorNamespace).DeepCopyInto(c.rbdDeployment) | ||
if err := c.own(c.rbdDeployment); err != nil { | ||
return err | ||
} | ||
csi.GetRBDDeployment(c.OperatorNamespace).DeepCopyInto(c.rbdDeployment) | ||
return nil | ||
}) | ||
if err != nil { | ||
|
@@ -264,10 +273,10 @@ func (c *ClusterVersionReconciler) Reconcile(ctx context.Context, req ctrl.Reque | |
}, | ||
} | ||
err = c.createOrUpdate(c.rbdDaemonSet, func() error { | ||
csi.GetRBDDaemonSet(c.OperatorNamespace).DeepCopyInto(c.rbdDaemonSet) | ||
if err := c.own(c.rbdDaemonSet); err != nil { | ||
return err | ||
} | ||
csi.GetRBDDaemonSet(c.OperatorNamespace).DeepCopyInto(c.rbdDaemonSet) | ||
return nil | ||
}) | ||
if err != nil { | ||
|
@@ -300,14 +309,10 @@ func (c *ClusterVersionReconciler) Reconcile(ctx context.Context, req ctrl.Reque | |
return ctrl.Result{}, err | ||
} | ||
|
||
operatorConfig, err := c.getOperatorConfig() | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
prometheusRule.SetNamespace(c.OperatorNamespace) | ||
|
||
err = c.createOrUpdate(prometheusRule, func() error { | ||
applyLabels(operatorConfig.Data["OCS_METRICS_LABELS"], &prometheusRule.ObjectMeta) | ||
applyLabels(instance.Data["OCS_METRICS_LABELS"], &prometheusRule.ObjectMeta) | ||
return c.own(prometheusRule) | ||
}) | ||
if err != nil { | ||
|
@@ -317,10 +322,35 @@ func (c *ClusterVersionReconciler) Reconcile(ctx context.Context, req ctrl.Reque | |
|
||
c.log.Info("prometheus rules deployed", "prometheusRule", klog.KRef(prometheusRule.Namespace, prometheusRule.Name)) | ||
|
||
// deletion phase | ||
if !instance.GetDeletionTimestamp().IsZero() { | ||
err = c.deletionPhase(&instance, &req) | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
} | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
func (c *ClusterVersionReconciler) createOrUpdate(obj client.Object, f controllerutil.MutateFn) error { | ||
func (c *OperatorConfigMapReconciler) deletionPhase(instance *corev1.ConfigMap, req *ctrl.Request) error { | ||
csiAddonsNode := &csiaddonsv1alpha1.CSIAddonsNode{} | ||
err := c.Client.Get(c.ctx, req.NamespacedName, csiAddonsNode) | ||
if err != nil { | ||
if apierrors.IsNotFound(err) { | ||
// Request object not found, could have been deleted before reconcile request. | ||
c.log.Info("CSIAddonsNode resource not found") | ||
return nil | ||
} | ||
return err | ||
} | ||
csiAddonsNode.Finalizers = nil | ||
if err := c.Client.Update(c.ctx, csiAddonsNode); err != nil { | ||
c.log.Error(err, "Failed to remove finalizer from csiAddonsNode") | ||
return err | ||
} | ||
return nil | ||
} | ||
func (c *OperatorConfigMapReconciler) createOrUpdate(obj client.Object, f controllerutil.MutateFn) error { | ||
result, err := controllerutil.CreateOrUpdate(c.ctx, c.Client, obj, f) | ||
if err != nil { | ||
return err | ||
|
@@ -329,11 +359,11 @@ func (c *ClusterVersionReconciler) createOrUpdate(obj client.Object, f controlle | |
return nil | ||
} | ||
|
||
func (c *ClusterVersionReconciler) own(obj client.Object) error { | ||
func (c *OperatorConfigMapReconciler) own(obj client.Object) error { | ||
return controllerutil.SetControllerReference(c.OperatorDeployment, obj, c.Client.Scheme()) | ||
} | ||
|
||
func (c *ClusterVersionReconciler) create(obj client.Object) error { | ||
func (c *OperatorConfigMapReconciler) create(obj client.Object) error { | ||
return c.Client.Create(c.ctx, obj) | ||
} | ||
|
||
|
@@ -357,16 +387,7 @@ func applyLabels(label string, t *metav1.ObjectMeta) { | |
t.Labels = promLabel | ||
} | ||
|
||
func (c *ClusterVersionReconciler) getOperatorConfig() (*corev1.ConfigMap, error) { | ||
cm := &corev1.ConfigMap{} | ||
err := c.Client.Get(c.ctx, types.NamespacedName{Name: operatorConfigMapName, Namespace: c.OperatorNamespace}, cm) | ||
if err != nil && !k8serrors.IsNotFound(err) { | ||
return nil, err | ||
} | ||
return cm, nil | ||
} | ||
|
||
func (c *ClusterVersionReconciler) ensureConsolePlugin() error { | ||
func (c *OperatorConfigMapReconciler) ensureConsolePlugin() error { | ||
c.consoleDeployment = &appsv1.Deployment{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: console.DeploymentName, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.