Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cmd/datasource-syncer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The corresponding flag is named grafana-api-token. The env variable name should be GRAFANA_API_TOKEN, for consistency.

- `GRAFANA_API_ENDPOINT`
- `PROJECT_ID`
- `TLS_CERT_FILE`
- `TLS_KEY_FILE`
- `TLS_CA_FILE`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise, I think naming the TLS environment variables consistently with the flags would be preferred.

59 changes: 49 additions & 10 deletions cmd/datasource-syncer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down