Skip to content

Commit

Permalink
controllers: pr issue fix
Browse files Browse the repository at this point in the history
fixed ownership of resources and remove finalizer in deletion

Signed-off-by: Amit Berner <[email protected]>
  • Loading branch information
bernerhat committed Jan 25, 2024
1 parent 34a3c90 commit eb64ba7
Show file tree
Hide file tree
Showing 12 changed files with 1,348 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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{},
)
Expand All @@ -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)
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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)
}

Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ replace (
exclude github.com/kubernetes-incubator/external-storage v0.20.4-openstorage-rc2

require (
github.com/csi-addons/kubernetes-csi-addons v0.8.0
github.com/go-logr/logr v1.3.0
github.com/kubernetes-csi/external-snapshotter/client/v6 v6.3.0
github.com/onsi/ginkgo v1.16.5
Expand Down Expand Up @@ -76,7 +77,7 @@ require (
golang.org/x/time v0.5.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231002182017-d307bd883b97 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2y
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/csi-addons/kubernetes-csi-addons v0.8.0 h1:zvYGp4DM6KdQzEX3dQSYKykqJdLZlxpVBJjtpbaqFjs=
github.com/csi-addons/kubernetes-csi-addons v0.8.0/go.mod h1:dvinzoiXlqdOGDpKkYx8Jxl507BzVEEEO+SI0OmBaRI=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
Expand Down Expand Up @@ -216,8 +218,8 @@ gomodules.xyz/jsonpatch/v2 v2.4.0 h1:Ci3iUJyx9UeRx7CeFN8ARgGbkESwJK+KB9lLcWxY/Zw
gomodules.xyz/jsonpatch/v2 v2.4.0/go.mod h1:AH3dM2RI6uoBZxn3LVrfvJ3E0/9dG4cSrbuBJT4moAY=
google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAsM=
google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231002182017-d307bd883b97 h1:6GQBEOdGkX6MMTLT9V+TjtIRZCw9VPD5Z+yHY9wMgS0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231002182017-d307bd883b97/go.mod h1:v7nGkzlmW8P3n/bKmWBn2WpBjpOEx8Q6gMueudAmKfY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 h1:Jyp0Hsi0bmHXG6k9eATXoYtjd6e2UzZ1SCn/wIupY14=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17/go.mod h1:oQ5rr10WTTMvP4A36n8JpR1OrO1BEiV4f78CneXZxkA=
google.golang.org/grpc v1.60.0 h1:6FQAR0kM31P6MRdeluor2w2gPaS4SVNrD/DNTxrQ15k=
google.golang.org/grpc v1.60.0/go.mod h1:OlCHIeLYqSSsLi6i49B5QGdzaMZK9+M7LXN2FKz4eGM=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ func main() {
os.Exit(1)
}

if err = (&controllers.ClusterVersionReconciler{
if err = (&controllers.OperatorConfigMapReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
OperatorDeployment: operatorDeployment,
OperatorNamespace: utils.GetOperatorNamespace(),
ConsolePort: int32(consolePort),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ClusterVersionReconciler")
setupLog.Error(err, "unable to create controller", "controller", "OperatorConfigMapReconciler")
os.Exit(1)
}

Expand Down
Loading

0 comments on commit eb64ba7

Please sign in to comment.