Skip to content

Commit

Permalink
restore basic org sync
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgubler committed Aug 30, 2023
1 parent 10f1da9 commit 6403d04
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 24 deletions.
5 changes: 3 additions & 2 deletions pkg/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ func Reconcile(ctx context.Context, keycloakClient *KeycloakClient, adminOrg str
}
defer grafanaClient.CloseIdleConnections()

/*grafanaOrgsMap, err := reconcileAllOrgs(ctx, organizationAppuioIoClient, grafanaClient, dashboard)
_, err = reconcileAllOrgs(ctx, keycloakOrganizations, grafanaClient, dashboard)
//grafanaOrgsMap, err := reconcileAllOrgs(ctx, keycloakOrganizations, grafanaClient, dashboard)
if err != nil {
return err
}

uidToGrafanaOrgs, adminUids, err := getControlApiUserOrganizationsMap(ctx, appuioIoClient, grafanaOrgsMap, adminOrg)
/*uidToGrafanaOrgs, adminUids, err := getControlApiUserOrganizationsMap(ctx, appuioIoClient, grafanaOrgsMap, adminOrg)
if err != nil {
return err
}
Expand Down
13 changes: 6 additions & 7 deletions pkg/reconcileOrg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@ package controller

import (
"errors"
orgs "github.com/appuio/control-api/apis/organization/v1"
grafana "github.com/grafana/grafana-api-golang-client"
"k8s.io/klog/v2"
"reflect"
"strings"
)

// Sync the basic org. Uses the generic Grafana client.
func reconcileOrgBasic(grafanaOrgLookup map[string]grafana.Org, grafanaClient *GrafanaClient, o orgs.Organization) (*grafana.Org, error) {
displayName := o.Name
if o.Spec.DisplayName != "" {
displayName = o.Spec.DisplayName
func reconcileOrgBasic(grafanaOrgLookup map[string]grafana.Org, grafanaClient *GrafanaClient, keycloakOrganization *KeycloakGroup) (*grafana.Org, error) {
displayName := keycloakOrganization.Name
if keycloakOrganization.GetDisplayNameAttribute() != "" {
displayName = keycloakOrganization.GetDisplayNameAttribute()
}
grafanaOrgDesiredName := o.Name + " - " + displayName
grafanaOrgDesiredName := keycloakOrganization.Name + " - " + displayName

if grafanaOrg, ok := grafanaOrgLookup[o.Name]; ok {
if grafanaOrg, ok := grafanaOrgLookup[keycloakOrganization.Name]; ok {
if grafanaOrg.Name != grafanaOrgDesiredName {
klog.Infof("Organization %d has wrong name: '%s', should be '%s'", grafanaOrg.ID, grafanaOrg.Name, grafanaOrgDesiredName)
err := grafanaClient.UpdateOrg(grafanaOrg.ID, grafanaOrgDesiredName)
Expand Down
21 changes: 6 additions & 15 deletions pkg/reconcileOrgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,13 @@ package controller
import (
"context"
grafana "github.com/grafana/grafana-api-golang-client"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
"strings"
)

func reconcileAllOrgs(ctx context.Context, organizationAppuioIoClient *rest.RESTClient, grafanaClient *GrafanaClient, dashboard map[string]interface{}) (map[string]*grafana.Org, error) {
func reconcileAllOrgs(ctx context.Context, keycloakOrganizations []*KeycloakGroup, grafanaClient *GrafanaClient, dashboard map[string]interface{}) (map[string]*grafana.Org, error) {
grafanaOrgLookupFinal := make(map[string]*grafana.Org)

// Fetch everything we need from the control API.
// This is racy because data can change while we fetch it, making the result inconsistent. This may lead to sync errors,
// but they should disappear with subsequent syncs.
controlApiOrganizationsList, err := getControlApiOrganizations(ctx, organizationAppuioIoClient)
if err != nil {
return nil, err
}

// Get all orgs from Grafana
orgs, err := grafanaClient.Orgs()
if err != nil {
Expand All @@ -36,19 +27,19 @@ func reconcileAllOrgs(ctx context.Context, organizationAppuioIoClient *rest.REST
}

// first make sure that all orgs that need to be present are present
for _, o := range controlApiOrganizationsList {
grafanaOrg, err := reconcileOrgBasic(grafanaOrgLookup, grafanaClient, o)
for _, keycloakOrganization := range keycloakOrganizations {
grafanaOrg, err := reconcileOrgBasic(grafanaOrgLookup, grafanaClient, keycloakOrganization)
if err != nil {
return nil, err
}
delete(grafanaOrgLookup, o.Name)
delete(grafanaOrgLookup, keycloakOrganization.Name)

err = reconcileOrgSettings(grafanaOrg, o.Name, grafanaClient, dashboard)
err = reconcileOrgSettings(grafanaOrg, keycloakOrganization.Name, grafanaClient, dashboard)
if err != nil {
return nil, err
}

grafanaOrgLookupFinal[o.Name] = grafanaOrg
grafanaOrgLookupFinal[keycloakOrganization.Name] = grafanaOrg

// select with a default case is apparently the only way to do a non-blocking read from a channel
select {
Expand Down

0 comments on commit 6403d04

Please sign in to comment.