Skip to content

Commit

Permalink
move more logic to GenerateGrafanaClient
Browse files Browse the repository at this point in the history
  • Loading branch information
hervenicol committed Jan 7, 2025
1 parent 9296e55 commit cdbea5e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 39 deletions.
19 changes: 1 addition & 18 deletions internal/controller/dashboard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,7 @@ const (
)

func SetupDashboardReconciler(mgr manager.Manager, conf config.Config) error {
// Generate Grafana client
// Get grafana admin-password and admin-user
grafanaAdminCredentials := grafanaclient.AdminCredentials{
Username: conf.Environment.GrafanaAdminUsername,
Password: conf.Environment.GrafanaAdminPassword,
}
if grafanaAdminCredentials.Username == "" {
return fmt.Errorf("GrafanaAdminUsername not set: %q", conf.Environment.GrafanaAdminUsername)
}
if grafanaAdminCredentials.Password == "" {
return fmt.Errorf("GrafanaAdminPassword not set: %q", conf.Environment.GrafanaAdminPassword)
}

grafanaTLSConfig := grafanaclient.TLSConfig{
Cert: conf.Environment.GrafanaTLSCertFile,
Key: conf.Environment.GrafanaTLSKeyFile,
}
grafanaAPI, err := grafanaclient.GenerateGrafanaClient(conf.GrafanaURL, grafanaAdminCredentials, grafanaTLSConfig)
grafanaAPI, err := grafanaclient.GenerateGrafanaClient(conf.GrafanaURL, conf)
if err != nil {
return fmt.Errorf("unable to create grafana client: %w", err)
}
Expand Down
20 changes: 1 addition & 19 deletions internal/controller/grafanaorganization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,7 @@ type GrafanaOrganizationReconciler struct {
}

func SetupGrafanaOrganizationReconciler(mgr manager.Manager, conf config.Config) error {
// Generate Grafana client

// Get grafana admin-password and admin-user
grafanaAdminCredentials := grafanaclient.AdminCredentials{
Username: conf.Environment.GrafanaAdminUsername,
Password: conf.Environment.GrafanaAdminPassword,
}
if grafanaAdminCredentials.Username == "" {
return fmt.Errorf("GrafanaAdminUsername not set: %q", conf.Environment.GrafanaAdminUsername)
}
if grafanaAdminCredentials.Password == "" {
return fmt.Errorf("GrafanaAdminPassword not set: %q", conf.Environment.GrafanaAdminPassword)
}

grafanaTLSConfig := grafanaclient.TLSConfig{
Cert: conf.Environment.GrafanaTLSCertFile,
Key: conf.Environment.GrafanaTLSKeyFile,
}
grafanaAPI, err := grafanaclient.GenerateGrafanaClient(conf.GrafanaURL, grafanaAdminCredentials, grafanaTLSConfig)
grafanaAPI, err := grafanaclient.GenerateGrafanaClient(conf.GrafanaURL, conf)
if err != nil {
return fmt.Errorf("unable to create grafana client: %w", err)
}
Expand Down
23 changes: 21 additions & 2 deletions pkg/grafana/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,36 @@ import (
"fmt"
"net/url"

"github.com/giantswarm/observability-operator/pkg/config"
grafana "github.com/grafana/grafana-openapi-client-go/client"
)

const (
clientConfigNumRetries = 3
)

func GenerateGrafanaClient(grafanaURL *url.URL, adminUserCredentials AdminCredentials, tlsConfig TLSConfig) (*grafana.GrafanaHTTPAPI, error) {
func GenerateGrafanaClient(grafanaURL *url.URL, conf config.Config) (*grafana.GrafanaHTTPAPI, error) {
var err error

grafanaTLSConfig, err := tlsConfig.toTLSConfig()
// Generate Grafana client
// Get grafana admin-password and admin-user
adminUserCredentials := AdminCredentials{
Username: conf.Environment.GrafanaAdminUsername,
Password: conf.Environment.GrafanaAdminPassword,
}
if adminUserCredentials.Username == "" {
return nil, fmt.Errorf("GrafanaAdminUsername not set: %q", conf.Environment.GrafanaAdminUsername)

}
if adminUserCredentials.Password == "" {
return nil, fmt.Errorf("GrafanaAdminPassword not set: %q", conf.Environment.GrafanaAdminPassword)

}

grafanaTLSConfig, err := TLSConfig{
Cert: conf.Environment.GrafanaTLSCertFile,
Key: conf.Environment.GrafanaTLSKeyFile,
}.toTLSConfig()
if err != nil {
return nil, fmt.Errorf("failed to build tls config: %w", err)
}
Expand Down

0 comments on commit cdbea5e

Please sign in to comment.