mirror of
https://github.com/kubernetes-sigs/prometheus-adapter.git
synced 2026-04-05 17:27:51 +00:00
Support setting headers on requests to Prometheus
This commit is contained in:
parent
93450fc29f
commit
d84340cc85
3 changed files with 64 additions and 5 deletions
|
|
@ -25,6 +25,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
basecmd "github.com/kubernetes-sigs/custom-metrics-apiserver/pkg/cmd"
|
basecmd "github.com/kubernetes-sigs/custom-metrics-apiserver/pkg/cmd"
|
||||||
|
|
@ -72,6 +73,8 @@ type PrometheusAdapter struct {
|
||||||
PrometheusClientTLSKeyFile string
|
PrometheusClientTLSKeyFile string
|
||||||
// PrometheusTokenFile points to the file that contains the bearer token when connecting with Prometheus
|
// PrometheusTokenFile points to the file that contains the bearer token when connecting with Prometheus
|
||||||
PrometheusTokenFile string
|
PrometheusTokenFile string
|
||||||
|
// PrometheusHeaders is a k=v list of headers to set on requests to PrometheusURL
|
||||||
|
PrometheusHeaders []string
|
||||||
// AdapterConfigFile points to the file containing the metrics discovery configuration.
|
// AdapterConfigFile points to the file containing the metrics discovery configuration.
|
||||||
AdapterConfigFile string
|
AdapterConfigFile string
|
||||||
// MetricsRelistInterval is the interval at which to relist the set of available metrics
|
// MetricsRelistInterval is the interval at which to relist the set of available metrics
|
||||||
|
|
@ -113,8 +116,7 @@ func (cmd *PrometheusAdapter) makePromClient() (prom.Client, error) {
|
||||||
}
|
}
|
||||||
httpClient.Transport = transport.NewBearerAuthRoundTripper(string(data), httpClient.Transport)
|
httpClient.Transport = transport.NewBearerAuthRoundTripper(string(data), httpClient.Transport)
|
||||||
}
|
}
|
||||||
|
genericPromClient := prom.NewGenericAPIClient(httpClient, baseURL, parseHeaderArgs(cmd.PrometheusHeaders))
|
||||||
genericPromClient := prom.NewGenericAPIClient(httpClient, baseURL)
|
|
||||||
instrumentedGenericPromClient := mprom.InstrumentGenericAPIClient(genericPromClient, baseURL.String())
|
instrumentedGenericPromClient := mprom.InstrumentGenericAPIClient(genericPromClient, baseURL.String())
|
||||||
return prom.NewClientForAPI(instrumentedGenericPromClient), nil
|
return prom.NewClientForAPI(instrumentedGenericPromClient), nil
|
||||||
}
|
}
|
||||||
|
|
@ -134,6 +136,8 @@ func (cmd *PrometheusAdapter) addFlags() {
|
||||||
"Optional client TLS key file to use when connecting with Prometheus, auto-renewal is not supported")
|
"Optional client TLS key file to use when connecting with Prometheus, auto-renewal is not supported")
|
||||||
cmd.Flags().StringVar(&cmd.PrometheusTokenFile, "prometheus-token-file", cmd.PrometheusTokenFile,
|
cmd.Flags().StringVar(&cmd.PrometheusTokenFile, "prometheus-token-file", cmd.PrometheusTokenFile,
|
||||||
"Optional file containing the bearer token to use when connecting with Prometheus")
|
"Optional file containing the bearer token to use when connecting with Prometheus")
|
||||||
|
cmd.Flags().StringArrayVar(&cmd.PrometheusHeaders, "prometheus-header", cmd.PrometheusHeaders,
|
||||||
|
"Optional header to set on requests to prometheus-url. Can be repeated")
|
||||||
cmd.Flags().StringVar(&cmd.AdapterConfigFile, "config", cmd.AdapterConfigFile,
|
cmd.Flags().StringVar(&cmd.AdapterConfigFile, "config", cmd.AdapterConfigFile,
|
||||||
"Configuration file containing details of how to transform between Prometheus metrics "+
|
"Configuration file containing details of how to transform between Prometheus metrics "+
|
||||||
"and custom metrics API resources")
|
"and custom metrics API resources")
|
||||||
|
|
@ -406,3 +410,16 @@ func makePrometheusCAClient(caFilePath string, tlsCertFilePath string, tlsKeyFil
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseHeaderArgs(args []string) http.Header {
|
||||||
|
headers := make(http.Header, len(args))
|
||||||
|
for _, h := range args {
|
||||||
|
parts := strings.SplitN(h, "=", 2)
|
||||||
|
value := ""
|
||||||
|
if len(parts) > 1 {
|
||||||
|
value = parts[1]
|
||||||
|
}
|
||||||
|
headers.Add(parts[0], value)
|
||||||
|
}
|
||||||
|
return headers
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -146,3 +147,41 @@ func TestMakePrometheusCAClient(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParseHeaderArgs(t *testing.T) {
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
args []string
|
||||||
|
headers http.Header
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
headers: http.Header{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
args: []string{"foo=bar"},
|
||||||
|
headers: http.Header{
|
||||||
|
"Foo": []string{"bar"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
args: []string{"foo"},
|
||||||
|
headers: http.Header{
|
||||||
|
"Foo": []string{""},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
args: []string{"foo=bar", "foo=baz", "bux=baz=23"},
|
||||||
|
headers: http.Header{
|
||||||
|
"Foo": []string{"bar", "baz"},
|
||||||
|
"Bux": []string{"baz=23"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
got := parseHeaderArgs(test.args)
|
||||||
|
if !reflect.DeepEqual(got, test.headers) {
|
||||||
|
t.Errorf("Expected %#v but got %#v", test.headers, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,7 @@ type GenericAPIClient interface {
|
||||||
type httpAPIClient struct {
|
type httpAPIClient struct {
|
||||||
client *http.Client
|
client *http.Client
|
||||||
baseURL *url.URL
|
baseURL *url.URL
|
||||||
|
headers http.Header
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *httpAPIClient) Do(ctx context.Context, verb, endpoint string, query url.Values) (APIResponse, error) {
|
func (c *httpAPIClient) Do(ctx context.Context, verb, endpoint string, query url.Values) (APIResponse, error) {
|
||||||
|
|
@ -58,6 +59,7 @@ func (c *httpAPIClient) Do(ctx context.Context, verb, endpoint string, query url
|
||||||
return APIResponse{}, fmt.Errorf("error constructing HTTP request to Prometheus: %v", err)
|
return APIResponse{}, fmt.Errorf("error constructing HTTP request to Prometheus: %v", err)
|
||||||
}
|
}
|
||||||
req.WithContext(ctx)
|
req.WithContext(ctx)
|
||||||
|
req.Header = c.headers
|
||||||
|
|
||||||
resp, err := c.client.Do(req)
|
resp, err := c.client.Do(req)
|
||||||
defer func() {
|
defer func() {
|
||||||
|
|
@ -113,10 +115,11 @@ func (c *httpAPIClient) Do(ctx context.Context, verb, endpoint string, query url
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewGenericAPIClient builds a new generic Prometheus API client for the given base URL and HTTP Client.
|
// NewGenericAPIClient builds a new generic Prometheus API client for the given base URL and HTTP Client.
|
||||||
func NewGenericAPIClient(client *http.Client, baseURL *url.URL) GenericAPIClient {
|
func NewGenericAPIClient(client *http.Client, baseURL *url.URL, headers http.Header) GenericAPIClient {
|
||||||
return &httpAPIClient{
|
return &httpAPIClient{
|
||||||
client: client,
|
client: client,
|
||||||
baseURL: baseURL,
|
baseURL: baseURL,
|
||||||
|
headers: headers,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -139,8 +142,8 @@ func NewClientForAPI(client GenericAPIClient) Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient creates a Client for the given HTTP client and base URL (the location of the Prometheus server).
|
// NewClient creates a Client for the given HTTP client and base URL (the location of the Prometheus server).
|
||||||
func NewClient(client *http.Client, baseURL *url.URL) Client {
|
func NewClient(client *http.Client, baseURL *url.URL, headers http.Header) Client {
|
||||||
genericClient := NewGenericAPIClient(client, baseURL)
|
genericClient := NewGenericAPIClient(client, baseURL, headers)
|
||||||
return NewClientForAPI(genericClient)
|
return NewClientForAPI(genericClient)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue