diff --git a/internal/controller/grafanaorganization_controller.go b/internal/controller/grafanaorganization_controller.go index 4677f9f3..10a188de 100644 --- a/internal/controller/grafanaorganization_controller.go +++ b/internal/controller/grafanaorganization_controller.go @@ -57,7 +57,6 @@ type GrafanaOrganizationReconciler struct { 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: conf.Environment.GrafanaAdminUsername, @@ -74,7 +73,7 @@ func SetupGrafanaOrganizationReconciler(mgr manager.Manager, conf config.Config) Cert: conf.Environment.GrafanaTLSCertFile, Key: conf.Environment.GrafanaTLSKeyFile, } - grafanaAPI, err := grafanaclient.GenerateGrafanaClient(grafanaURL, grafanaAdminCredentials, grafanaTLSConfig) + grafanaAPI, err := grafanaclient.GenerateGrafanaClient(conf.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 7e61b532..693c74c3 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ import ( "crypto/tls" "flag" "fmt" + "net/url" "os" "time" @@ -63,6 +64,9 @@ func init() { } func main() { + var grafanaURLString string + var err error + flag.StringVar(&conf.MetricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.") flag.StringVar(&conf.ProbeAddr, "health-probe-bind-address", ":8081", @@ -76,7 +80,7 @@ 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", + flag.StringVar(&grafanaURLString, "grafana-url", "http://grafana.monitoring.svc.cluster.local", "grafana URL") // Management cluster configuration flags. @@ -112,7 +116,7 @@ func main() { "The version of Prometheus Agents to deploy.") flag.DurationVar(&conf.Monitoring.WALTruncateFrequency, "monitoring-wal-truncate-frequency", 2*time.Hour, "Configures how frequently the Write-Ahead Log (WAL) truncates segments.") - flag.StringVar(&conf.Monitoring.MetricsQueryURL, "metrics-query-url", "http://mimir-gateway.mimir.svc/prometheus", + flag.StringVar(&conf.Monitoring.MetricsQueryURL, "monitoring-metrics-query-url", "http://mimir-gateway.mimir.svc/prometheus", "URL to query for cluster metrics") opts := zap.Options{ Development: false, @@ -121,10 +125,16 @@ func main() { opts.BindFlags(flag.CommandLine) flag.Parse() + // parse grafana URL + conf.GrafanaURL, err = url.Parse(grafanaURLString) + if err != nil { + panic(fmt.Sprintf("failed to parse grafana url: %v", err)) + } + ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) // Load environment variables. - _, err := env.UnmarshalFromEnviron(&conf.Environment) + _, err = env.UnmarshalFromEnviron(&conf.Environment) if err != nil { setupLog.Error(err, "failed to unmarshal environment variables") os.Exit(1) diff --git a/pkg/config/config.go b/pkg/config/config.go index df67d29d..31ca0a26 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -1,6 +1,8 @@ package config import ( + "net/url" + "github.com/giantswarm/observability-operator/pkg/common" "github.com/giantswarm/observability-operator/pkg/monitoring" ) @@ -12,7 +14,7 @@ type Config struct { SecureMetrics bool EnableHTTP2 bool OperatorNamespace string - GrafanaURL string + GrafanaURL *url.URL ManagementCluster common.ManagementCluster diff --git a/pkg/grafana/client/client.go b/pkg/grafana/client/client.go index 99d71208..ea6f426c 100644 --- a/pkg/grafana/client/client.go +++ b/pkg/grafana/client/client.go @@ -7,20 +7,13 @@ import ( grafana "github.com/grafana/grafana-openapi-client-go/client" ) -var grafanaURL *url.URL - const ( clientConfigNumRetries = 3 ) -func GenerateGrafanaClient(grafanaURLstring string, adminUserCredentials AdminCredentials, tlsConfig TLSConfig) (*grafana.GrafanaHTTPAPI, error) { +func GenerateGrafanaClient(grafanaURL *url.URL, adminUserCredentials AdminCredentials, tlsConfig TLSConfig) (*grafana.GrafanaHTTPAPI, error) { var err error - grafanaURL, err = url.Parse(grafanaURLstring) - if err != nil { - panic(fmt.Sprintf("failed to parse grafana url: %v", err)) - } - grafanaTLSConfig, err := tlsConfig.toTLSConfig() if err != nil { return nil, fmt.Errorf("failed to build tls config: %w", err)