Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: better error handling for org creation and grafana connection #17

Merged
merged 7 commits into from
May 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions controllers/namespace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,25 +197,32 @@ func (r *NamespaceReconciler) generateGfDataSource(ctx context.Context, name, te
logger := log.FromContext(ctx)

// Connecting to the Grafana API
client, _ := sdk.NewClient(grafanaURL, fmt.Sprintf("%s:%s", grafanaUsername, grafanaPassword), sdk.DefaultHTTPClient)
client, err := sdk.NewClient(grafanaURL, fmt.Sprintf("%s:%s", grafanaUsername, grafanaPassword), sdk.DefaultHTTPClient)
if err != nil {
logger.Error(err, "Unable to create Grafana client")
return nil, err
}

// Retrieving the Organization Info
retrievedOrg, err := client.GetOrgByOrgName(ctx, team)
if err != nil && strings.Contains(err.Error(), "Organization not found") {
logger.Info("Creating organization", "team name is", team)
// Create the organization
neworg := sdk.Org{Name: team}
_, err := client.CreateOrg(ctx, neworg)
if err != nil {
if strings.Contains(err.Error(), "Organization not found") {
logger.Info("Creating organization", "team name is", team)
// Create the organization

if err != nil {
logger.Error(err, "Failed to create organization")
neworg := sdk.Org{Name: team}
_, err := client.CreateOrg(ctx, neworg)

if err != nil {
logger.Error(err, "Failed to create organization")
return nil, err
}
retrievedOrg = neworg
logger.Info("Organization created", "for team", team, "organization name is", retrievedOrg.Name)
} else {
logger.Error(err, "Unable to get organization")
return nil, err
}
retrievedOrg = neworg
logger.Info("Organization created", "for team", team, "organization name is", retrievedOrg.Name)
} else {
logger.Error(err, "Unable to get organization")
return nil, err
}
// Generating the datasource
logger.Info("Start creating Datasource", "Team name:", team, "Team ID:", retrievedOrg.ID, "Namespace", name)
Expand Down