mirror of
https://github.com/openfaas/faas.git
synced 2025-06-08 16:26:47 +00:00
Enable basic auth for service query / scaling on provider
- this is a blocking issue for auth with Docker Swarm fixes #879 Signed-off-by: Alex Ellis (VMware) <alexellis2@gmail.com>
This commit is contained in:
parent
e7cfaa1b9b
commit
3598da2e51
@ -14,6 +14,7 @@ import (
|
||||
|
||||
"log"
|
||||
|
||||
"github.com/openfaas/faas-provider/auth"
|
||||
"github.com/openfaas/faas/gateway/requests"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
@ -22,13 +23,15 @@ import (
|
||||
type Exporter struct {
|
||||
metricOptions MetricOptions
|
||||
services []requests.Function
|
||||
credentials *auth.BasicAuthCredentials
|
||||
}
|
||||
|
||||
// NewExporter creates a new exporter for the OpenFaaS gateway metrics
|
||||
func NewExporter(options MetricOptions) *Exporter {
|
||||
func NewExporter(options MetricOptions, credentials *auth.BasicAuthCredentials) *Exporter {
|
||||
return &Exporter{
|
||||
metricOptions: options,
|
||||
services: []requests.Function{},
|
||||
credentials: credentials,
|
||||
}
|
||||
}
|
||||
|
||||
@ -77,6 +80,9 @@ func (e *Exporter) StartServiceWatcher(endpointURL url.URL, metricsOptions Metri
|
||||
case <-ticker.C:
|
||||
|
||||
get, _ := http.NewRequest(http.MethodGet, endpointURL.String()+"system/functions", nil)
|
||||
if e.credentials != nil {
|
||||
get.SetBasicAuth(e.credentials.User, e.credentials.Password)
|
||||
}
|
||||
|
||||
services := []requests.Function{}
|
||||
res, err := proxyClient.Do(get)
|
||||
|
@ -33,7 +33,7 @@ func readGauge(g prometheus.Metric) metricResult {
|
||||
|
||||
func Test_Describe_DescribesThePrometheusMetrics(t *testing.T) {
|
||||
metricsOptions := BuildMetricsOptions()
|
||||
exporter := NewExporter(metricsOptions)
|
||||
exporter := NewExporter(metricsOptions, nil)
|
||||
|
||||
ch := make(chan *prometheus.Desc)
|
||||
defer close(ch)
|
||||
@ -62,7 +62,7 @@ func Test_Describe_DescribesThePrometheusMetrics(t *testing.T) {
|
||||
|
||||
func Test_Collect_CollectsTheNumberOfReplicasOfAService(t *testing.T) {
|
||||
metricsOptions := BuildMetricsOptions()
|
||||
exporter := NewExporter(metricsOptions)
|
||||
exporter := NewExporter(metricsOptions, nil)
|
||||
|
||||
expectedService := requests.Function{
|
||||
Name: "function_with_two_replica",
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// PrometheusQuery a PrometheusQuery
|
||||
// PrometheusQuery represents parameters for querying Prometheus
|
||||
type PrometheusQuery struct {
|
||||
Port int
|
||||
Host string
|
||||
|
@ -17,12 +17,13 @@ import (
|
||||
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/openfaas/faas-provider/auth"
|
||||
"github.com/openfaas/faas/gateway/handlers"
|
||||
"github.com/openfaas/faas/gateway/requests"
|
||||
)
|
||||
|
||||
// NewExternalServiceQuery proxies service queries to external plugin via HTTP
|
||||
func NewExternalServiceQuery(externalURL url.URL) handlers.ServiceQuery {
|
||||
func NewExternalServiceQuery(externalURL url.URL, credentials *auth.BasicAuthCredentials) handlers.ServiceQuery {
|
||||
proxyClient := http.Client{
|
||||
Transport: &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
@ -40,6 +41,7 @@ func NewExternalServiceQuery(externalURL url.URL) handlers.ServiceQuery {
|
||||
return ExternalServiceQuery{
|
||||
URL: externalURL,
|
||||
ProxyClient: proxyClient,
|
||||
Credentials: credentials,
|
||||
}
|
||||
}
|
||||
|
||||
@ -47,6 +49,7 @@ func NewExternalServiceQuery(externalURL url.URL) handlers.ServiceQuery {
|
||||
type ExternalServiceQuery struct {
|
||||
URL url.URL
|
||||
ProxyClient http.Client
|
||||
Credentials *auth.BasicAuthCredentials
|
||||
}
|
||||
|
||||
// ScaleServiceRequest request scaling of replica
|
||||
@ -65,10 +68,13 @@ func (s ExternalServiceQuery) GetReplicas(serviceName string) (handlers.ServiceQ
|
||||
|
||||
req, _ := http.NewRequest(http.MethodGet, urlPath, nil)
|
||||
|
||||
if s.Credentials != nil {
|
||||
req.SetBasicAuth(s.Credentials.User, s.Credentials.Password)
|
||||
}
|
||||
|
||||
res, err := s.ProxyClient.Do(req)
|
||||
|
||||
if err != nil {
|
||||
|
||||
log.Println(urlPath, err)
|
||||
} else {
|
||||
|
||||
@ -129,6 +135,11 @@ func (s ExternalServiceQuery) SetReplicas(serviceName string, count uint64) erro
|
||||
|
||||
urlPath := fmt.Sprintf("%ssystem/scale-function/%s", s.URL.String(), serviceName)
|
||||
req, _ := http.NewRequest(http.MethodPost, urlPath, bytes.NewReader(requestBody))
|
||||
|
||||
if s.Credentials != nil {
|
||||
req.SetBasicAuth(s.Credentials.User, s.Credentials.Password)
|
||||
}
|
||||
|
||||
defer req.Body.Close()
|
||||
res, err := s.ProxyClient.Do(req)
|
||||
|
||||
|
@ -53,7 +53,7 @@ func main() {
|
||||
servicePollInterval := time.Second * 5
|
||||
|
||||
metricsOptions := metrics.BuildMetricsOptions()
|
||||
exporter := metrics.NewExporter(metricsOptions)
|
||||
exporter := metrics.NewExporter(metricsOptions, credentials)
|
||||
exporter.StartServiceWatcher(*config.FunctionsProviderURL, metricsOptions, "func", servicePollInterval)
|
||||
metrics.RegisterExporter(exporter)
|
||||
|
||||
@ -89,7 +89,7 @@ func main() {
|
||||
faasHandlers.QueryFunction = handlers.MakeForwardingProxyHandler(reverseProxy, forwardingNotifiers, urlResolver, nilURLTransformer)
|
||||
faasHandlers.InfoHandler = handlers.MakeInfoHandler(handlers.MakeForwardingProxyHandler(reverseProxy, forwardingNotifiers, urlResolver, nilURLTransformer))
|
||||
|
||||
alertHandler := plugin.NewExternalServiceQuery(*config.FunctionsProviderURL)
|
||||
alertHandler := plugin.NewExternalServiceQuery(*config.FunctionsProviderURL, credentials)
|
||||
faasHandlers.Alert = handlers.MakeAlertHandler(alertHandler)
|
||||
|
||||
if config.UseNATS() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user