Skip to content

Commit

Permalink
fix(proxy): don't use proxy for internal Grafana instances (#1300)
Browse files Browse the repository at this point in the history
* fix(proxy): don't use proxy for internal Grafana instances

* chore(proxy): update docs
  • Loading branch information
weisdd committed Nov 8, 2023
1 parent 6d19d89 commit dddafba
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion controllers/client/grafana_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func NewGrafanaClient(ctx context.Context, c client.Client, grafana *v1beta1.Gra
clientConfig := grapi.Config{
HTTPHeaders: nil,
Client: &http.Client{
Transport: NewInstrumentedRoundTripper(grafana.Name, metrics.GrafanaApiRequests),
Transport: NewInstrumentedRoundTripper(grafana.Name, metrics.GrafanaApiRequests, grafana.IsExternal()),
Timeout: time.Second * timeout,
},
// TODO populate me
Expand Down
10 changes: 8 additions & 2 deletions controllers/client/round_tripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ type instrumentedRoundTripper struct {
metric *prometheus.CounterVec
}

func NewInstrumentedRoundTripper(relatedResource string, metric *prometheus.CounterVec) http.RoundTripper {
transport := http.DefaultTransport.(*http.Transport).Clone()
func NewInstrumentedRoundTripper(relatedResource string, metric *prometheus.CounterVec, useProxy bool) http.RoundTripper {
transport := &http.Transport{}

// Default transport respects proxy settings
if useProxy {
transport = http.DefaultTransport.(*http.Transport).Clone()
}

transport.DisableKeepAlives = true
transport.MaxIdleConnsPerHost = -1
transport.TLSClientConfig.InsecureSkipVerify = true //nolint
Expand Down
2 changes: 1 addition & 1 deletion controllers/fetchers/grafana_com_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func getLatestGrafanaComRevision(dashboard *v1beta1.GrafanaDashboard) (int, erro
return -1, err
}

client := client2.NewInstrumentedRoundTripper(fmt.Sprintf("%v/%v", dashboard.Namespace, dashboard.Name), metrics.GrafanaComApiRevisionRequests)
client := client2.NewInstrumentedRoundTripper(fmt.Sprintf("%v/%v", dashboard.Namespace, dashboard.Name), metrics.GrafanaComApiRevisionRequests, true)
response, err := client.RoundTrip(request)
if err != nil {
return -1, err
Expand Down
2 changes: 1 addition & 1 deletion controllers/fetchers/url_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func FetchDashboardFromUrl(dashboard *v1beta1.GrafanaDashboard) ([]byte, error)
return nil, err
}

client := client2.NewInstrumentedRoundTripper(fmt.Sprintf("%v/%v", dashboard.Namespace, dashboard.Name), metrics.DashboardUrlRequests)
client := client2.NewInstrumentedRoundTripper(fmt.Sprintf("%v/%v", dashboard.Namespace, dashboard.Name), metrics.DashboardUrlRequests, true)
response, err := client.RoundTrip(request)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,5 @@ This is because especially the data sources contain secret information and we do

## Using a proxy server

The Operator can use a proxy server when making requests to Grafana.
The Operator can use a proxy server when fetching URL-based / Grafana.com dashboards or making requests to external Grafana instances.
The proxy settings can be controlled through environment variables as documented [here](https://pkg.go.dev/golang.org/x/net/http/httpproxy#FromEnvironment).

0 comments on commit dddafba

Please sign in to comment.