From ac8a54738caf06b125cafd94394a02ce63bcc567 Mon Sep 17 00:00:00 2001 From: abhisek Date: Sat, 1 Feb 2025 01:14:49 +0530 Subject: [PATCH] feat: add support for fetching config from env Signed-off-by: abhisek --- cmd/datasource-syncer/README.md | 12 +++++++ cmd/datasource-syncer/main.go | 59 +++++++++++++++++++++++++++------ 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/cmd/datasource-syncer/README.md b/cmd/datasource-syncer/README.md index a8040e11d3..aa18dfbdee 100644 --- a/cmd/datasource-syncer/README.md +++ b/cmd/datasource-syncer/README.md @@ -38,3 +38,15 @@ Usage of datasource-syncer: -tls-key string Path to the server TLS key. ``` + +Configuration can also be set via environment variables however command line +flags take precedence. Following configuration are available through environment +variables: + +- `DATASOURCE_UIDS` +- `GRAFANA_SERVICE_ACCOUNT_TOKEN` +- `GRAFANA_API_ENDPOINT` +- `PROJECT_ID` +- `TLS_CERT_FILE` +- `TLS_KEY_FILE` +- `TLS_CA_FILE` diff --git a/cmd/datasource-syncer/main.go b/cmd/datasource-syncer/main.go index 5019d040ff..a726a6145a 100644 --- a/cmd/datasource-syncer/main.go +++ b/cmd/datasource-syncer/main.go @@ -63,10 +63,15 @@ func main() { logger = log.With(logger, "ts", log.DefaultTimestampUTC) logger = log.With(logger, "caller", log.DefaultCaller) - if len(*datasourceUIDList) == 0 { - //nolint:errcheck - level.Error(logger).Log("msg", "--datasource-uid must be set") - os.Exit(1) + if *datasourceUIDList == "" { + envDatasourceUIDs := os.Getenv("DATASOURCE_UIDS") + if envDatasourceUIDs == "" { + //nolint:errcheck + level.Error(logger).Log("msg", "--datasource-uid must be set") + os.Exit(1) + } + + datasourceUIDList = &envDatasourceUIDs } if *grafanaAPIToken == "" { @@ -76,18 +81,51 @@ func main() { level.Error(logger).Log("msg", "--grafana-api-token or the environment variable GRAFANA_SERVICE_ACCOUNT_TOKEN must be set") os.Exit(1) } + grafanaAPIToken = &envToken } + if *grafanaEndpoint == "" { - //nolint:errcheck - level.Error(logger).Log("msg", "--grafana-api-endpoint must be set") - os.Exit(1) + envEndpoint := os.Getenv("GRAFANA_API_ENDPOINT") + if envEndpoint == "" { + //nolint:errcheck + level.Error(logger).Log("msg", "--grafana-api-endpoint must be set") + os.Exit(1) + } + + grafanaEndpoint = &envEndpoint } if *projectID == "" { - //nolint:errcheck - level.Error(logger).Log("msg", "--project-id must be set") - os.Exit(1) + envProjectID := os.Getenv("PROJECT_ID") + if envProjectID == "" { + //nolint:errcheck + level.Error(logger).Log("msg", "--project-id must be set") + os.Exit(1) + } + + projectID = &envProjectID + } + + if *certFile == "" { + envCertFile := os.Getenv("TLS_CERT_FILE") + if envCertFile != "" { + certFile = &envCertFile + } + } + + if *keyFile == "" { + envKeyFile := os.Getenv("TLS_KEY_FILE") + if envKeyFile != "" { + keyFile = &envKeyFile + } + } + + if *caFile == "" { + envCAFile := os.Getenv("TLS_CA_FILE") + if envCAFile != "" { + caFile = &envCAFile + } } client, err := getTLSClient(*certFile, *keyFile, *caFile, *insecureSkipVerify) @@ -215,6 +253,7 @@ func buildUpdateDataSourceRequest(dataSource grafana.DataSource, token string) ( if dataSource.Type != "prometheus" { return nil, fmt.Errorf("datasource type is not prometheus") } + if *gcmEndpointOverride != "" { dataSource.URL = *gcmEndpointOverride } else {