Skip to content

Commit

Permalink
make grafana API URL configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
hervenicol committed Dec 18, 2024
1 parent 1c1f39a commit 0df50ec
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
18 changes: 10 additions & 8 deletions internal/controller/grafanaorganization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", "",
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Config struct {
SecureMetrics bool
EnableHTTP2 bool
OperatorNamespace string
GrafanaURL string

ManagementCluster common.ManagementCluster

Expand Down
15 changes: 7 additions & 8 deletions pkg/grafana/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0df50ec

Please sign in to comment.