Skip to content

Commit 72b6bd7

Browse files
authored
Merge pull request #6013 from chaosi-zju/rt-metrics-provider
expose metrics of karmada-metrics-adapter
2 parents cac020e + c7009b2 commit 72b6bd7

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

artifacts/deploy/karmada-metrics-adapter.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ spec:
2727
command:
2828
- /bin/karmada-metrics-adapter
2929
- --kubeconfig=/etc/karmada/config/karmada.config
30+
- --metrics-bind-address=:8080
3031
- --authentication-kubeconfig=/etc/karmada/config/karmada.config
3132
- --authorization-kubeconfig=/etc/karmada/config/karmada.config
3233
- --client-ca-file=/etc/karmada/pki/server/ca.crt

charts/karmada/templates/karmada-metrics-adapter.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ spec:
4444
command:
4545
- /bin/karmada-metrics-adapter
4646
- --kubeconfig=/etc/kubeconfig
47+
- --metrics-bind-address=:8080
4748
- --authentication-kubeconfig=/etc/kubeconfig
4849
- --authorization-kubeconfig=/etc/kubeconfig
4950
- --tls-cert-file=/etc/kubernetes/pki/karmada.crt

cmd/metrics-adapter/app/options/options.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@ package options
1818

1919
import (
2020
"context"
21+
"net/http"
22+
"os"
23+
"time"
2124

2225
"github.com/spf13/pflag"
2326
openapinamer "k8s.io/apiserver/pkg/endpoints/openapi"
2427
genericapiserver "k8s.io/apiserver/pkg/server"
2528
"k8s.io/client-go/informers"
2629
"k8s.io/client-go/kubernetes"
2730
"k8s.io/client-go/tools/clientcmd"
31+
"k8s.io/component-base/metrics"
32+
"k8s.io/component-base/metrics/legacyregistry"
2833
"k8s.io/klog/v2"
2934
"sigs.k8s.io/custom-metrics-apiserver/pkg/cmd/options"
3035
"sigs.k8s.io/metrics-server/pkg/api"
@@ -39,10 +44,45 @@ import (
3944
"github.com/karmada-io/karmada/pkg/version"
4045
)
4146

47+
const (
48+
// ReadHeaderTimeout is the amount of time allowed to read
49+
// request headers.
50+
// HTTP timeouts are necessary to expire inactive connections
51+
// and failing to do so might make the application vulnerable
52+
// to attacks like slowloris which work by sending data very slow,
53+
// which in case of no timeout will keep the connection active
54+
// eventually leading to a denial-of-service (DoS) attack.
55+
// References:
56+
// - https://en.wikipedia.org/wiki/Slowloris_(computer_security)
57+
ReadHeaderTimeout = 32 * time.Second
58+
// WriteTimeout is the amount of time allowed to write the
59+
// request data.
60+
// HTTP timeouts are necessary to expire inactive connections
61+
// and failing to do so might make the application vulnerable
62+
// to attacks like slowloris which work by sending data very slow,
63+
// which in case of no timeout will keep the connection active
64+
// eventually leading to a denial-of-service (DoS) attack.
65+
WriteTimeout = 5 * time.Minute
66+
// ReadTimeout is the amount of time allowed to read
67+
// response data.
68+
// HTTP timeouts are necessary to expire inactive connections
69+
// and failing to do so might make the application vulnerable
70+
// to attacks like slowloris which work by sending data very slow,
71+
// which in case of no timeout will keep the connection active
72+
// eventually leading to a denial-of-service (DoS) attack.
73+
ReadTimeout = 5 * time.Minute
74+
)
75+
4276
// Options contains everything necessary to create and run metrics-adapter.
4377
type Options struct {
4478
CustomMetricsAdapterServerOptions *options.CustomMetricsAdapterServerOptions
4579

80+
// MetricsBindAddress is the TCP address that the server should bind to
81+
// for serving prometheus metrics.
82+
// It can be set to "0" to disable the metrics serving.
83+
// Defaults to ":8080".
84+
MetricsBindAddress string
85+
4686
KubeConfig string
4787
// ClusterAPIQPS is the QPS to use while talking with cluster kube-apiserver.
4888
ClusterAPIQPS float32
@@ -73,6 +113,7 @@ func (o *Options) Complete() error {
73113
func (o *Options) AddFlags(fs *pflag.FlagSet) {
74114
o.CustomMetricsAdapterServerOptions.AddFlags(fs)
75115
o.ProfileOpts.AddFlags(fs)
116+
fs.StringVar(&o.MetricsBindAddress, "metrics-bind-address", ":8080", "The TCP address that the server should bind to for serving prometheus metrics(e.g. 127.0.0.1:8080, :8080). It can be set to \"0\" to disable the metrics serving. Defaults to 0.0.0.0:8080.")
76117
fs.Float32Var(&o.ClusterAPIQPS, "cluster-api-qps", 40.0, "QPS to use while talking with cluster kube-apiserver.")
77118
fs.IntVar(&o.ClusterAPIBurst, "cluster-api-burst", 60, "Burst to use while talking with cluster kube-apiserver.")
78119
fs.Float32Var(&o.KubeAPIQPS, "kube-api-qps", 40.0, "QPS to use while talking with karmada-apiserver.")
@@ -136,6 +177,9 @@ func (o *Options) Config(stopCh <-chan struct{}) (*metricsadapter.MetricsServer,
136177
// Run runs the metrics-adapter with options. This should never exit.
137178
func (o *Options) Run(ctx context.Context) error {
138179
klog.Infof("karmada-metrics-adapter version: %s", version.Get())
180+
if o.MetricsBindAddress != "0" {
181+
go serveMetrics(o.MetricsBindAddress)
182+
}
139183

140184
profileflag.ListenAndServe(o.ProfileOpts)
141185

@@ -147,3 +191,31 @@ func (o *Options) Run(ctx context.Context) error {
147191

148192
return metricsServer.StartServer(ctx)
149193
}
194+
195+
func serveMetrics(address string) {
196+
mux := http.NewServeMux()
197+
mux.Handle("/metrics", metricsHandler())
198+
serveHTTP(address, mux, "metrics")
199+
}
200+
201+
func metricsHandler() http.Handler {
202+
return metrics.HandlerFor(legacyregistry.DefaultGatherer, metrics.HandlerOpts{
203+
ErrorHandling: metrics.HTTPErrorOnError,
204+
})
205+
}
206+
207+
func serveHTTP(address string, handler http.Handler, name string) {
208+
httpServer := &http.Server{
209+
Addr: address,
210+
Handler: handler,
211+
ReadHeaderTimeout: ReadHeaderTimeout,
212+
WriteTimeout: WriteTimeout,
213+
ReadTimeout: ReadTimeout,
214+
}
215+
216+
klog.Infof("Starting %s server on %s", name, address)
217+
if err := httpServer.ListenAndServe(); err != nil {
218+
klog.Errorf("Failed to serve %s on %s: %v", name, address, err)
219+
os.Exit(1)
220+
}
221+
}

operator/pkg/controlplane/metricsadapter/manifests.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ spec:
4848
command:
4949
- /bin/karmada-metrics-adapter
5050
- --kubeconfig=/etc/karmada/kubeconfig
51+
- --metrics-bind-address=:8080
5152
- --authentication-kubeconfig=/etc/karmada/kubeconfig
5253
- --authorization-kubeconfig=/etc/karmada/kubeconfig
5354
- --client-ca-file=/etc/karmada/pki/ca.crt

pkg/karmadactl/addons/metricsadapter/manifests.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ spec:
5353
command:
5454
- /bin/karmada-metrics-adapter
5555
- --kubeconfig=/etc/kubeconfig
56+
- --metrics-bind-address=:8080
5657
- --authentication-kubeconfig=/etc/kubeconfig
5758
- --authorization-kubeconfig=/etc/kubeconfig
5859
- --client-ca-file=/etc/karmada/pki/ca.crt

0 commit comments

Comments
 (0)