diff --git a/internal/controller/dashboard_controller.go b/internal/controller/dashboard_controller.go index d918c5ab..84ed7a53 100644 --- a/internal/controller/dashboard_controller.go +++ b/internal/controller/dashboard_controller.go @@ -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) } diff --git a/internal/controller/grafanaorganization_controller.go b/internal/controller/grafanaorganization_controller.go index 9c10a57c..9ba6fb1a 100644 --- a/internal/controller/grafanaorganization_controller.go +++ b/internal/controller/grafanaorganization_controller.go @@ -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) } diff --git a/pkg/grafana/client/client.go b/pkg/grafana/client/client.go index ea6f426c..0c9a027e 100644 --- a/pkg/grafana/client/client.go +++ b/pkg/grafana/client/client.go @@ -4,6 +4,7 @@ import ( "fmt" "net/url" + "github.com/giantswarm/observability-operator/pkg/config" grafana "github.com/grafana/grafana-openapi-client-go/client" ) @@ -11,10 +12,28 @@ 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) }