Skip to content

Commit

Permalink
feat: upgrade grafana-operator to 4.1.0
Browse files Browse the repository at this point in the history
This commit upgrades the Grafana Operator to 4.1.0 and configures the
operator to scan dashboards from all namespace. This change was
introduced in 4.0.2 [1].

grafana/grafana-operator#579
  • Loading branch information
fpetkovski committed Dec 14, 2021
1 parent c13d605 commit 3a741ee
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,6 @@ kind-cluster: $(OPERATOR_SDK)
kind create cluster --config hack/kind/config.yaml
$(OPERATOR_SDK) olm install
kubectl apply -f hack/kind/registry.yaml -n operators
kubectl create -k deploy/crds/kubernetes/
kubectl create -k deploy/dependencies

46 changes: 36 additions & 10 deletions pkg/controllers/grafana-operator/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const (
subscriptionName = "monitoring-stack-operator-grafana-operator"
operatorGroupName = "monitoring-stack-operator-grafana-operator"
grafanaName = "monitoring-stack-operator-grafana"
grafanaCSV = "grafana-operator.v4.0.1"
grafanaCSV = "grafana-operator.v4.1.0"
)

type reconciler struct {
Expand Down Expand Up @@ -162,7 +162,7 @@ func RegisterWithManager(mgr ctrl.Manager) error {
go installPlanInformer.Run(nil)
if err := c.Watch(&source.Informer{
Informer: installPlanInformer,
}, &handler.EnqueueRequestForObject{}, predicate.GenerationChangedPredicate{}); err != nil {
}, &handler.EnqueueRequestForObject{}, installPlanFilter{}); err != nil {
return err
}

Expand All @@ -173,7 +173,6 @@ func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
logger := r.logger.WithValues("req", req)

result := func(r reconcileResult) (ctrl.Result, error) {

if r.err != nil {
logger.Error(r.err, "End Reconcile - creation or updation error")
} else if r.Result.Requeue || r.Result.RequeueAfter != 0 {
Expand Down Expand Up @@ -228,7 +227,6 @@ func (r *reconciler) reconcileNamespace(ctx context.Context) reconcileResult {
return creationResult(err)
}

///
// requeue if namespace is marked for deletion
// TODO(sthaha): decide if want to use finalizers to prevent deletion but
// we also need to solve how to properly cleanup / uninstall operator
Expand Down Expand Up @@ -286,13 +284,35 @@ func (r *reconciler) reconcileSubscription(ctx context.Context) reconcileResult
return creationResult(err)
}

r.logger.Info("Updating Grafana Operator Subscription")
subscription.Spec.StartingCSV = desired.Spec.StartingCSV
return updationResult(r.k8sClient.Update(ctx, &subscription))
if subscription.Spec.StartingCSV == desired.Spec.StartingCSV {
return next()
}

r.logger.WithValues("Name", subscription.Name).Info("Deleting Subscription")
if err := r.k8sClient.Delete(ctx, &subscription); err != nil {
return reconcileError(err)
}

r.logger.WithValues("Name", subscription.Status.InstalledCSV).Info("Deleting CSV")
csv := v1alpha1.ClusterServiceVersion{
TypeMeta: metav1.TypeMeta{
APIVersion: v1alpha1.SchemeGroupVersion.String(),
Kind: "ClusterServiceVersion",
},
ObjectMeta: metav1.ObjectMeta{
Name: subscription.Status.InstalledCSV,
Namespace: Namespace,
},
}
if err := r.k8sClient.Delete(ctx, &csv); err != nil {
return reconcileError(err)
}

r.logger.WithValues("Name", subscription.Name).Info("Creating Subscription")
return creationResult(r.k8sClient.Create(ctx, &subscription))
}

func (r *reconciler) approveInstallPlan(ctx context.Context) reconcileResult {

var installPlans v1alpha1.InstallPlanList
err := r.k8sClient.List(ctx, &installPlans, client.InNamespace(Namespace))
if err != nil {
Expand All @@ -305,7 +325,6 @@ func (r *reconciler) approveInstallPlan(ctx context.Context) reconcileResult {
}

var approvePlan *v1alpha1.InstallPlan

for _, installPlan := range installPlans.Items {
csv := installPlan.Spec.ClusterServiceVersionNames[0]
// ignore all but the install matching the Grafana version
Expand Down Expand Up @@ -346,7 +365,6 @@ func (r *reconciler) reconcileGrafana(ctx context.Context) reconcileResult {
var grafana integreatlyv1alpha1.Grafana
err := r.k8sClient.Get(ctx, key, &grafana)
if err != nil && !errors.IsNotFound(err) {

// Ignore error and requeue if the errors are related to CRD not present
if meta.IsNoMatchError(err) || errors.IsMethodNotSupported(err) {
r.logger.V(10).Info("Grafana CRD does not exist - NoMatchError")
Expand Down Expand Up @@ -406,6 +424,14 @@ func NewSubscription() *v1alpha1.Subscription {
Channel: "v4",
InstallPlanApproval: v1alpha1.ApprovalManual,
StartingCSV: grafanaCSV,
Config: &v1alpha1.SubscriptionConfig{
Env: []corev1.EnvVar{
{
Name: "DASHBOARD_NAMESPACES_ALL",
Value: "true",
},
},
},
},
}
}
Expand Down
27 changes: 27 additions & 0 deletions pkg/controllers/grafana-operator/filters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package grafana_operator

import (
"github.com/operator-framework/api/pkg/operators/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)

// installPlanFilter filter events on v1alpha1.InstallPlan resources
type installPlanFilter struct {
predicate.Funcs
}

// Update returns true only when the v1alpha1.Status.BundleLookup field
// is set by OLM.
func (f installPlanFilter) Update(e event.UpdateEvent) bool {
previous, ok := e.ObjectOld.(*v1alpha1.InstallPlan)
if !ok {
return false
}
current, ok := e.ObjectNew.(*v1alpha1.InstallPlan)
if !ok {
return false
}

return len(previous.Status.BundleLookups) == 0 && len(current.Status.BundleLookups) != 0
}

0 comments on commit 3a741ee

Please sign in to comment.