From 0df50ec3b462f1d0913a6c80f860cba22ddd84b2 Mon Sep 17 00:00:00 2001 From: Herve Nicol <12008875+hervenicol@users.noreply.github.com> Date: Wed, 18 Dec 2024 09:30:15 +0100 Subject: [PATCH] make grafana API URL configurable --- .../grafanaorganization_controller.go | 18 ++++++++++-------- main.go | 4 +++- pkg/config/config.go | 1 + pkg/grafana/client/client.go | 15 +++++++-------- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/internal/controller/grafanaorganization_controller.go b/internal/controller/grafanaorganization_controller.go index 184ed2c0..4677f9f3 100644 --- a/internal/controller/grafanaorganization_controller.go +++ b/internal/controller/grafanaorganization_controller.go @@ -54,25 +54,27 @@ type GrafanaOrganizationReconciler struct { GrafanaAPI *grafanaAPI.GrafanaHTTPAPI } -func SetupGrafanaOrganizationReconciler(mgr manager.Manager, environment config.Environment) error { +func SetupGrafanaOrganizationReconciler(mgr manager.Manager, conf config.Config) error { // Generate Grafana client + + grafanaURL := conf.GrafanaURL // Get grafana admin-password and admin-user grafanaAdminCredentials := grafanaclient.AdminCredentials{ - Username: environment.GrafanaAdminUsername, - Password: environment.GrafanaAdminPassword, + Username: conf.Environment.GrafanaAdminUsername, + Password: conf.Environment.GrafanaAdminPassword, } if grafanaAdminCredentials.Username == "" { - return fmt.Errorf("GrafanaAdminUsername not set: %q", environment.GrafanaAdminUsername) + return fmt.Errorf("GrafanaAdminUsername not set: %q", conf.Environment.GrafanaAdminUsername) } if grafanaAdminCredentials.Password == "" { - return fmt.Errorf("GrafanaAdminPassword not set: %q", environment.GrafanaAdminPassword) + return fmt.Errorf("GrafanaAdminPassword not set: %q", conf.Environment.GrafanaAdminPassword) } grafanaTLSConfig := grafanaclient.TLSConfig{ - Cert: environment.GrafanaTLSCertFile, - Key: environment.GrafanaTLSKeyFile, + Cert: conf.Environment.GrafanaTLSCertFile, + Key: conf.Environment.GrafanaTLSKeyFile, } - grafanaAPI, err := grafanaclient.GenerateGrafanaClient(grafanaAdminCredentials, grafanaTLSConfig) + grafanaAPI, err := grafanaclient.GenerateGrafanaClient(grafanaURL, grafanaAdminCredentials, grafanaTLSConfig) if err != nil { return fmt.Errorf("unable to create grafana client: %w", err) } diff --git a/main.go b/main.go index e6afd492..7e61b532 100644 --- a/main.go +++ b/main.go @@ -76,6 +76,8 @@ func main() { "If set, HTTP/2 will be enabled for the metrics and webhook servers") flag.StringVar(&conf.OperatorNamespace, "operator-namespace", "", "The namespace where the observability-operator is running.") + flag.StringVar(&conf.GrafanaURL, "grafana-url", "http://grafana.monitoring.svc.cluster.local", + "grafana URL") // Management cluster configuration flags. flag.StringVar(&conf.ManagementCluster.BaseDomain, "management-cluster-base-domain", "", @@ -187,7 +189,7 @@ func main() { } // Setup controller for the GrafanaOrganization resource. - err = controller.SetupGrafanaOrganizationReconciler(mgr, conf.Environment) + err = controller.SetupGrafanaOrganizationReconciler(mgr, conf) if err != nil { setupLog.Error(err, "unable to setup controller", "controller", "GrafanaOrganizationReconciler") os.Exit(1) diff --git a/pkg/config/config.go b/pkg/config/config.go index 68925239..df67d29d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -12,6 +12,7 @@ type Config struct { SecureMetrics bool EnableHTTP2 bool OperatorNamespace string + GrafanaURL string ManagementCluster common.ManagementCluster diff --git a/pkg/grafana/client/client.go b/pkg/grafana/client/client.go index 47c247d7..99d71208 100644 --- a/pkg/grafana/client/client.go +++ b/pkg/grafana/client/client.go @@ -9,19 +9,18 @@ import ( var grafanaURL *url.URL -func init() { +const ( + clientConfigNumRetries = 3 +) + +func GenerateGrafanaClient(grafanaURLstring string, adminUserCredentials AdminCredentials, tlsConfig TLSConfig) (*grafana.GrafanaHTTPAPI, error) { var err error - grafanaURL, err = url.Parse("http://grafana.monitoring.svc.cluster.local") + + grafanaURL, err = url.Parse(grafanaURLstring) if err != nil { panic(fmt.Sprintf("failed to parse grafana url: %v", err)) } -} - -const ( - clientConfigNumRetries = 3 -) -func GenerateGrafanaClient(adminUserCredentials AdminCredentials, tlsConfig TLSConfig) (*grafana.GrafanaHTTPAPI, error) { grafanaTLSConfig, err := tlsConfig.toTLSConfig() if err != nil { return nil, fmt.Errorf("failed to build tls config: %w", err)