Skip to content

Commit

Permalink
address theo's remarks, round 1: easy and obvious fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hervenicol committed Dec 20, 2024
1 parent caee054 commit 3449953
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 34 deletions.
49 changes: 17 additions & 32 deletions internal/controller/dashboard_controller.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
/*
Copyright 2024.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controller

import (
Expand Down Expand Up @@ -41,6 +25,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/giantswarm/observability-operator/pkg/common/organization"
"github.com/giantswarm/observability-operator/pkg/config"
"github.com/giantswarm/observability-operator/pkg/grafana"
grafanaclient "github.com/giantswarm/observability-operator/pkg/grafana/client"
Expand All @@ -56,8 +41,9 @@ type DashboardReconciler struct {
}

const (
DashboardFinalizer = "observability.giantswarm.io/grafanadashboard"
DashboardTeamAnnotation = "giantswarm.io/organization"
DashboardFinalizer = "observability.giantswarm.io/grafanadashboard"
DashboardSelectorLabelName = "app.giantswarm.io/kind"
DashboardSelectorLabelValue = "dashboard"
)

func SetupDashboardReconciler(mgr manager.Manager, conf config.Config) error {
Expand Down Expand Up @@ -130,12 +116,13 @@ func (r *DashboardReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
// SetupWithManager sets up the controller with the Manager.
func (r *DashboardReconciler) SetupWithManager(mgr ctrl.Manager) error {

labelSelectorPredicate, err := predicate.LabelSelectorPredicate(metav1.LabelSelector{MatchLabels: map[string]string{"app.giantswarm.io/kind": "dashboard"}})
labelSelectorPredicate, err := predicate.LabelSelectorPredicate(metav1.LabelSelector{MatchLabels: map[string]string{DashboardSelectorLabelName: DashboardSelectorLabelValue}})
if err != nil {
return errors.WithStack(err)
}

return ctrl.NewControllerManagedBy(mgr).
Named("dashboards").
For(&v1.ConfigMap{}, builder.WithPredicates(labelSelectorPredicate)).
// Watch for grafana pod's status changes
Watches(
Expand All @@ -155,7 +142,8 @@ func (r *DashboardReconciler) SetupWithManager(mgr ctrl.Manager) error {
for _, dashboard := range dashboards.Items {
requests = append(requests, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: dashboard.Name,
Name: dashboard.Name,
Namespace: dashboard.Namespace,
},
})
}
Expand Down Expand Up @@ -199,10 +187,10 @@ func (r DashboardReconciler) reconcileCreate(ctx context.Context, dashboard *v1.
return ctrl.Result{}, nil
}

func getDashboardUID(jsonDashboard string) (UID string, err error) {
func getDashboardUID(jsonDashboard string) (string, error) {

var Dashboard map[string]interface{}
err = json.Unmarshal([]byte(jsonDashboard), &Dashboard)
err := json.Unmarshal([]byte(jsonDashboard), &Dashboard)
if err != nil {
return "", err
}
Expand All @@ -214,26 +202,25 @@ func getDashboardUID(jsonDashboard string) (UID string, err error) {
return UID, nil
}

func getDashboardCMOrg(dashboard *v1.ConfigMap) (Org string, err error) {
func getDashboardCMOrg(dashboard *v1.ConfigMap) (string, error) {

// Try to look for an annotation first
orgName := dashboard.GetAnnotations()[DashboardTeamAnnotation]
if orgName != "" {
orgName, ok := dashboard.GetAnnotations()[organization.OrganizationLabel]
if ok && orgName != "" {
return orgName, nil
}

// Then look for a label
orgName = dashboard.GetLabels()[DashboardTeamAnnotation]

if orgName != "" {
orgName, ok = dashboard.GetLabels()[organization.OrganizationLabel]
if ok && orgName != "" {
return orgName, nil
}

// Return an error if no label was found
return "", errors.New("No organization label found in configmap")
}

func (r DashboardReconciler) configureDashboard(ctx context.Context, dashboardCM *v1.ConfigMap) (err error) {
func (r DashboardReconciler) configureDashboard(ctx context.Context, dashboardCM *v1.ConfigMap) error {
logger := log.FromContext(ctx)

dashboardOrg, err := getDashboardCMOrg(dashboardCM)
Expand Down Expand Up @@ -261,7 +248,6 @@ func (r DashboardReconciler) configureDashboard(ctx context.Context, dashboardCM
logger.Error(err, "failed to find organization", "organization", dashboardOrg)
return errors.WithStack(err)
}
logger.Info("Found dashboard org", "Organization", organization)

if _, err = r.GrafanaAPI.SignedInUser.UserSetUsingOrg(organization.ID); err != nil {
logger.Error(err, "failed to change current org for signed in user")
Expand Down Expand Up @@ -294,8 +280,7 @@ func (r DashboardReconciler) configureDashboard(ctx context.Context, dashboardCM
logger.Info("updated dashboard", "Dashboard UID", dashboardUID, "Dashboard Org", dashboardOrg)
}

// return nil
return errors.New("All good, but not implemented yet")
return nil
}

// reconcileDelete deletes the grafana dashboard.
Expand Down
4 changes: 2 additions & 2 deletions pkg/common/organization/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

const (
organizationLabel string = "giantswarm.io/organization"
OrganizationLabel string = "giantswarm.io/organization"
)

type OrganizationRepository interface {
Expand All @@ -32,7 +32,7 @@ func (r NamespaceOrganizationRepository) Read(ctx context.Context, cluster *clus
return "", err
}

if organization, ok := namespace.Labels[organizationLabel]; ok {
if organization, ok := namespace.Labels[OrganizationLabel]; ok {
return organization, nil
}
return "", errors.New("cluster namespace missing organization label")
Expand Down

0 comments on commit 3449953

Please sign in to comment.