Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(proxy): don't use proxy for internal Grafana instances #1300

Merged
merged 2 commits into from
Nov 8, 2023
Merged
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
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).