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

Support to get the metric data from a prometheus metric api with basic auth enabled #3737

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions docs/design/usage-based-scheduling.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ metrics: # metrics server related configuration
type: prometheus # Optional, The metrics source type, prometheus by default, support "prometheus", "prometheus_adapt" and "elasticsearch"
address: http://192.168.0.10:9090 # Mandatory, The metrics source address
interval: 30s # Optional, The scheduler pull metrics from Prometheus with this interval, 30s by default
basicauth: "false" # Optional, The basic authentication configuration,false by default
username: "" # Optional, The prometheus username
password: "" # Optional, The prometheus password
tls: # Optional, The tls configuration
insecureSkipVerify: "false" # Optional, Skip the certificate verification, false by default
elasticsearch: # Optional, The elasticsearch configuration
Expand Down
23 changes: 23 additions & 0 deletions pkg/scheduler/metrics/source/basicauth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package source

import (
"net/http"
"encoding/base64"
"fmt"
)

type BasicAuthTransport struct {
Username string
Password string
}

func (bat BasicAuthTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("Authorization", fmt.Sprintf("Basic %s",
base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s",
bat.Username, bat.Password)))))
return http.DefaultTransport.RoundTrip(req)
}

func (bat *BasicAuthTransport) Client() *http.Client {
return &http.Client{Transport: bat}
}
29 changes: 21 additions & 8 deletions pkg/scheduler/metrics/source/metrics_client_prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,29 @@ func (p *PrometheusMetricsClient) NodeMetricsAvg(ctx context.Context, nodeName s
klog.V(4).Infof("Get node metrics from Prometheus: %s", p.address)
var client api.Client
var err error
basicauth := p.conf["basicauth"] == "true"
insecureSkipVerify := p.conf["tls.insecureSkipVerify"] == "true"
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: insecureSkipVerify,
},
if basicauth {
bat := &BasicAuthTransport{
Username: p.conf["username"],
Password: p.conf["password"],
}
client, err = api.NewClient(api.Config{
Address: p.address,
RoundTripper: bat,
})
} else {
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: insecureSkipVerify,
},
}
client, err = api.NewClient(api.Config{
Address: p.address,
RoundTripper: tr,
})
}
client, err = api.NewClient(api.Config{
Address: p.address,
RoundTripper: tr,
})

if err != nil {
return nil, err
}
Expand Down
Loading