mirror of
https://github.com/kubernetes-sigs/prometheus-adapter.git
synced 2026-04-07 10:17:51 +00:00
addressing review comments
addressing review comments, adding run group
This commit is contained in:
parent
3b551fcdd6
commit
bd06a043c5
4 changed files with 41 additions and 24 deletions
|
|
@ -17,6 +17,7 @@ limitations under the License.
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"flag"
|
"flag"
|
||||||
|
|
@ -30,6 +31,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/prometheus/exporter-toolkit/web"
|
"github.com/prometheus/exporter-toolkit/web"
|
||||||
|
"github.com/oklog/run"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
openapinamer "k8s.io/apiserver/pkg/endpoints/openapi"
|
openapinamer "k8s.io/apiserver/pkg/endpoints/openapi"
|
||||||
|
|
@ -94,10 +96,12 @@ type PrometheusAdapter struct {
|
||||||
MetricsRelistInterval time.Duration
|
MetricsRelistInterval time.Duration
|
||||||
// MetricsMaxAge is the period to query available metrics for
|
// MetricsMaxAge is the period to query available metrics for
|
||||||
MetricsMaxAge time.Duration
|
MetricsMaxAge time.Duration
|
||||||
|
// MetricsPort is the port to expose the prometheus adapter self metrics
|
||||||
secureMetricsPort string
|
MetricsPort string
|
||||||
tlsConfig string
|
// TLSConfig is the tls config for exposing the prometheus adapter self metrics
|
||||||
metricsHost string
|
TLSConfig string
|
||||||
|
// MetricsHost is the host to expose the prometheus adapter self metrics
|
||||||
|
MetricsHost string
|
||||||
metricsConfig *adaptercfg.MetricsDiscoveryConfig
|
metricsConfig *adaptercfg.MetricsDiscoveryConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -161,12 +165,9 @@ func (cmd *PrometheusAdapter) addFlags() {
|
||||||
"interval at which to re-list the set of all available metrics from Prometheus")
|
"interval at which to re-list the set of all available metrics from Prometheus")
|
||||||
cmd.Flags().DurationVar(&cmd.MetricsMaxAge, "metrics-max-age", cmd.MetricsMaxAge, ""+
|
cmd.Flags().DurationVar(&cmd.MetricsMaxAge, "metrics-max-age", cmd.MetricsMaxAge, ""+
|
||||||
"period for which to query the set of available metrics from Prometheus")
|
"period for which to query the set of available metrics from Prometheus")
|
||||||
cmd.Flags().StringVar(&cmd.secureMetricsPort, "secure-metrics-port", cmd.secureMetricsPort,
|
cmd.Flags().StringVar(&cmd.MetricsPort, "metrics-port", cmd.MetricsPort, "Port to expose prometheus-adapter self metrics on. (default:8080) ")
|
||||||
"Secure port for metrics")
|
cmd.Flags().StringVar(&cmd.MetricsHost, "metrics-host", cmd.MetricsHost, "Host to expose prometheus-adapter self metrics on. (default:0.0.0.0)")
|
||||||
cmd.Flags().StringVar(&cmd.metricsHost, "metrics-host", cmd.metricsHost,
|
cmd.Flags().StringVar(&cmd.TLSConfig, "tls-config", cmd.TLSConfig, "Optional TLS configuration for metrics server")
|
||||||
"Host for exposing metrics")
|
|
||||||
cmd.Flags().StringVar(&cmd.tlsConfig, "tls-config", cmd.tlsConfig,
|
|
||||||
"TLS configuration for metrics server")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *PrometheusAdapter) loadConfig() error {
|
func (cmd *PrometheusAdapter) loadConfig() error {
|
||||||
|
|
@ -298,6 +299,8 @@ func main() {
|
||||||
cmd := &PrometheusAdapter{
|
cmd := &PrometheusAdapter{
|
||||||
PrometheusURL: "https://localhost",
|
PrometheusURL: "https://localhost",
|
||||||
MetricsRelistInterval: 10 * time.Minute,
|
MetricsRelistInterval: 10 * time.Minute,
|
||||||
|
MetricsHost: "::",
|
||||||
|
MetricsPort: "8080",
|
||||||
}
|
}
|
||||||
cmd.Name = "prometheus-metrics-adapter"
|
cmd.Name = "prometheus-metrics-adapter"
|
||||||
|
|
||||||
|
|
@ -363,8 +366,24 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
apiServerConfig(config)
|
apiServerConfig(config)
|
||||||
cmd.setupMetricsServer()
|
ctx := context.Background()
|
||||||
|
var g run.Group
|
||||||
|
|
||||||
|
metricsServer := cmd.setupMetricsServer()
|
||||||
|
{
|
||||||
|
g.Add(func() error{
|
||||||
|
return web.ListenAndServe(&metricsServer, cmd.TLSConfig, adapterLogger{})
|
||||||
|
}, func(error) {
|
||||||
|
ctxShutDown, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
metricsServer.Shutdown(ctxShutDown)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if err := g.Run(); err != nil {
|
||||||
|
klog.Fatalf("unable to run group : %v", err)
|
||||||
|
}
|
||||||
// run the server
|
// run the server
|
||||||
if err := cmd.Run(stopCh); err != nil {
|
if err := cmd.Run(stopCh); err != nil {
|
||||||
klog.Fatalf("unable to run custom metrics adapter: %v", err)
|
klog.Fatalf("unable to run custom metrics adapter: %v", err)
|
||||||
|
|
@ -375,15 +394,14 @@ func apiServerConfig(config *customexternalmetrics.Config) {
|
||||||
config.GenericConfig.EnableMetrics = false
|
config.GenericConfig.EnableMetrics = false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd PrometheusAdapter) setupMetricsServer() {
|
func (cmd *PrometheusAdapter) setupMetricsServer() http.Server{
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
mux.Handle("/metrics", legacyregistry.Handler())
|
mux.Handle("/metrics", legacyregistry.Handler())
|
||||||
|
|
||||||
serverAddress := net.JoinHostPort(cmd.metricsHost, cmd.secureMetricsPort)
|
serverAddress := net.JoinHostPort(cmd.MetricsHost, cmd.MetricsPort)
|
||||||
metricsServer := http.Server{Handler: mux, Addr: serverAddress}
|
return http.Server{Handler: mux, Addr: serverAddress}
|
||||||
|
|
||||||
go web.ListenAndServe(&metricsServer, cmd.tlsConfig, adapterLogger{})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// makeKubeconfigHTTPClient constructs an HTTP for connecting with the given auth options.
|
// makeKubeconfigHTTPClient constructs an HTTP for connecting with the given auth options.
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ spec:
|
||||||
- --metrics-relist-interval=1m
|
- --metrics-relist-interval=1m
|
||||||
- --v=10
|
- --v=10
|
||||||
- --config=/etc/adapter/config.yaml
|
- --config=/etc/adapter/config.yaml
|
||||||
- --secure-metrics-port=9090
|
- --metrics-port=9090
|
||||||
- --metrics-host=0.0.0.0
|
- --metrics-host=0.0.0.0
|
||||||
- --tls-config=/etc/tls/config.yaml
|
- --tls-config=/etc/tls/config.yaml
|
||||||
ports:
|
ports:
|
||||||
|
|
@ -46,9 +46,6 @@ spec:
|
||||||
- mountPath: /etc/tls
|
- mountPath: /etc/tls
|
||||||
name: metricstlsconfig
|
name: metricstlsconfig
|
||||||
readOnly: false
|
readOnly: false
|
||||||
- mountPath: /etc/certs
|
|
||||||
name: servingcerts
|
|
||||||
readOnly: false
|
|
||||||
volumes:
|
volumes:
|
||||||
- name: volume-serving-cert
|
- name: volume-serving-cert
|
||||||
secret:
|
secret:
|
||||||
|
|
@ -58,9 +55,8 @@ spec:
|
||||||
name: adapter-config
|
name: adapter-config
|
||||||
- name: tmp-vol
|
- name: tmp-vol
|
||||||
emptyDir: {}
|
emptyDir: {}
|
||||||
- configMap:
|
- name: metricstlsconfig
|
||||||
|
configMap:
|
||||||
name: metrics-tls-config
|
name: metrics-tls-config
|
||||||
name: metricstlsconfig
|
|
||||||
- secret:
|
|
||||||
secretName: pa-metrics-serving-certs
|
|
||||||
name: servingcerts
|
|
||||||
|
|
|
||||||
1
go.mod
1
go.mod
|
|
@ -3,6 +3,7 @@ module sigs.k8s.io/prometheus-adapter
|
||||||
go 1.16
|
go 1.16
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/oklog/run v1.1.0
|
||||||
github.com/onsi/ginkgo v1.16.4
|
github.com/onsi/ginkgo v1.16.4
|
||||||
github.com/onsi/gomega v1.15.0
|
github.com/onsi/gomega v1.15.0
|
||||||
github.com/prometheus/client_golang v1.11.0
|
github.com/prometheus/client_golang v1.11.0
|
||||||
|
|
|
||||||
2
go.sum
2
go.sum
|
|
@ -457,6 +457,8 @@ github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
|
||||||
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
|
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
|
||||||
github.com/oklog/oklog v0.3.2/go.mod h1:FCV+B7mhrz4o+ueLpx+KqkyXRGMWOYEvfiXtdGtbWGs=
|
github.com/oklog/oklog v0.3.2/go.mod h1:FCV+B7mhrz4o+ueLpx+KqkyXRGMWOYEvfiXtdGtbWGs=
|
||||||
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
|
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
|
||||||
|
github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA=
|
||||||
|
github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU=
|
||||||
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
|
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
|
||||||
github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
|
github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
|
||||||
github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue