From 1e2ef4940c9e549d075e8e74eb02777ace6c9758 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E6=B4=AA=E8=B4=9E?= Date: Fri, 6 Jan 2023 16:19:45 +0800 Subject: [PATCH] add enable-metrics arg to disable metrics --- cmd/controller/controller.go | 4 +++- cmd/daemon/cniserver.go | 4 +++- cmd/ovn_monitor/ovn_monitor.go | 6 ++++-- cmd/pinger/pinger.go | 3 ++- pkg/controller/config.go | 3 +++ pkg/daemon/config.go | 3 +++ pkg/ovnmonitor/config.go | 3 +++ pkg/pinger/config.go | 3 +++ 8 files changed, 24 insertions(+), 5 deletions(-) diff --git a/cmd/controller/controller.go b/cmd/controller/controller.go index cddb593931e..15e7fa3bfec 100644 --- a/cmd/controller/controller.go +++ b/cmd/controller/controller.go @@ -57,7 +57,9 @@ func CmdMain() { go loopOvnNbctlDaemon(config) go func() { mux := http.NewServeMux() - mux.Handle("/metrics", promhttp.Handler()) + if config.EnableMetrics { + mux.Handle("/metrics", promhttp.Handler()) + } if config.EnablePprof { mux.HandleFunc("/debug/pprof/", pprof.Index) mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) diff --git a/cmd/daemon/cniserver.go b/cmd/daemon/cniserver.go index d7ac2415a2f..bfffd736943 100644 --- a/cmd/daemon/cniserver.go +++ b/cmd/daemon/cniserver.go @@ -87,7 +87,9 @@ func CmdMain() { } mux := http.NewServeMux() - mux.Handle("/metrics", promhttp.Handler()) + if config.EnableMetrics { + mux.Handle("/metrics", promhttp.Handler()) + } if config.EnablePprof { mux.HandleFunc("/debug/pprof/", pprof.Index) mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) diff --git a/cmd/ovn_monitor/ovn_monitor.go b/cmd/ovn_monitor/ovn_monitor.go index 4c68acea01f..26cf8be59a9 100644 --- a/cmd/ovn_monitor/ovn_monitor.go +++ b/cmd/ovn_monitor/ovn_monitor.go @@ -32,8 +32,10 @@ func CmdMain() { } exporter.StartOvnMetrics() - http.Handle(config.MetricsPath, promhttp.Handler()) - klog.Infoln("Listening on", config.ListenAddress) + if config.EnableMetrics { + http.Handle(config.MetricsPath, promhttp.Handler()) + klog.Infoln("Listening on", config.ListenAddress) + } // conform to Gosec G114 // https://github.com/securego/gosec#available-rules diff --git a/cmd/pinger/pinger.go b/cmd/pinger/pinger.go index b43b183d784..23063590d4f 100644 --- a/cmd/pinger/pinger.go +++ b/cmd/pinger/pinger.go @@ -24,8 +24,9 @@ func CmdMain() { if err != nil { util.LogFatalAndExit(err, "failed to parse config") } - if config.Mode == "server" { + if config.Mode == "server" && config.EnableMetrics { http.Handle("/metrics", promhttp.Handler()) + go func() { // conform to Gosec G114 // https://github.com/securego/gosec#available-rules diff --git a/pkg/controller/config.go b/pkg/controller/config.go index a0b9881f4f4..80dda4af9ab 100644 --- a/pkg/controller/config.go +++ b/pkg/controller/config.go @@ -84,6 +84,7 @@ type Configuration struct { EnableEcmp bool EnableKeepVmIP bool EnableLbSvc bool + EnableMetrics bool ExternalGatewaySwitch string ExternalGatewayConfigNS string @@ -147,6 +148,7 @@ func ParseFlags() (*Configuration, error) { argEnableEcmp = pflag.Bool("enable-ecmp", false, "Enable ecmp route for centralized subnet") argKeepVmIP = pflag.Bool("keep-vm-ip", false, "Whether to keep ip for kubevirt pod when pod is rebuild") argEnableLbSvc = pflag.Bool("enable-lb-svc", false, "Whether to support loadbalancer service") + argEnableMetrics = pflag.Bool("enable-metrics", true, "Whether to support metrics query") argExternalGatewayConfigNS = pflag.String("external-gateway-config-ns", "kube-system", "The namespace of configmap external-gateway-config, default: kube-system") argExternalGatewaySwitch = pflag.String("external-gateway-switch", "external", "The name of the external gateway switch which is a ovs bridge to provide external network, default: external") @@ -226,6 +228,7 @@ func ParseFlags() (*Configuration, error) { GCInterval: *argGCInterval, InspectInterval: *argInspectInterval, EnableLbSvc: *argEnableLbSvc, + EnableMetrics: *argEnableMetrics, } if config.NetworkType == util.NetworkTypeVlan && config.DefaultHostInterface == "" { diff --git a/pkg/daemon/config.go b/pkg/daemon/config.go index 5e80ef7352e..653697d44fb 100644 --- a/pkg/daemon/config.go +++ b/pkg/daemon/config.go @@ -56,6 +56,7 @@ type Configuration struct { DefaultInterfaceName string ExternalGatewayConfigNS string ExternalGatewaySwitch string // provider network underlay vlan subnet + EnableMetrics bool } // ParseFlags will parse cmd args then init kubeClient and configuration @@ -86,6 +87,7 @@ func ParseFlags() *Configuration { argsDefaultInterfaceName = pflag.String("default-interface-name", "", "The default host interface name in the vlan/vxlan type") argExternalGatewayConfigNS = pflag.String("external-gateway-config-ns", "kube-system", "The namespace of configmap external-gateway-config, default: kube-system") argExternalGatewaySwitch = pflag.String("external-gateway-switch", "external", "The name of the external gateway switch which is a ovs bridge to provide external network, default: external") + argEnableMetrics = pflag.Bool("enable-metrics", true, "Whether to support metrics query") ) // mute info log for ipset lib @@ -133,6 +135,7 @@ func ParseFlags() *Configuration { DefaultInterfaceName: *argsDefaultInterfaceName, ExternalGatewayConfigNS: *argExternalGatewayConfigNS, ExternalGatewaySwitch: *argExternalGatewaySwitch, + EnableMetrics: *argEnableMetrics, } return config } diff --git a/pkg/ovnmonitor/config.go b/pkg/ovnmonitor/config.go index b67116babca..71163f26931 100644 --- a/pkg/ovnmonitor/config.go +++ b/pkg/ovnmonitor/config.go @@ -40,6 +40,7 @@ type Configuration struct { ServiceVswitchdFilePidPath string ServiceNorthdFileLogPath string ServiceNorthdFilePidPath string + EnableMetrics bool } // ParseFlags get parameters information. @@ -49,6 +50,7 @@ func ParseFlags() (*Configuration, error) { argMetricsPath = pflag.String("telemetry-path", "/metrics", "Path under which to expose metrics.") argPollTimeout = pflag.Int("ovs.timeout", 2, "Timeout on JSON-RPC requests to OVN.") argPollInterval = pflag.Int("ovs.poll-interval", 30, "The minimum interval (in seconds) between collections from OVN server.") + argEnableMetrics = pflag.Bool("enable-metrics", true, "Whether to support metrics query") argSystemRunDir = pflag.String("system.run.dir", "/var/run/openvswitch", "OVS default run directory.") argDatabaseVswitchName = pflag.String("database.vswitch.name", "Open_vSwitch", "The name of OVS db.") @@ -121,6 +123,7 @@ func ParseFlags() (*Configuration, error) { ServiceVswitchdFilePidPath: *argServiceVswitchdFilePidPath, ServiceNorthdFileLogPath: *argServiceNorthdFileLogPath, ServiceNorthdFilePidPath: *argServiceNorthdFilePidPath, + EnableMetrics: *argEnableMetrics, } klog.Infof("ovn monitor config is %+v", config) diff --git a/pkg/pinger/config.go b/pkg/pinger/config.go index 712e23bfbe4..204d3bb7c9e 100644 --- a/pkg/pinger/config.go +++ b/pkg/pinger/config.go @@ -34,6 +34,7 @@ type Configuration struct { PodProtocols []string ExternalAddress string NetworkMode string + EnableMetrics bool // Used for OVS Monitor PollTimeout int @@ -64,6 +65,7 @@ func ParseFlags() (*Configuration, error) { argExternalDns = pflag.String("external-dns", "", "check external dns resolve from pod") argExternalAddress = pflag.String("external-address", "", "check ping connection to an external address, default: 114.114.114.114") argNetworkMode = pflag.String("network-mode", "kube-ovn", "The cni plugin current cluster used, default: kube-ovn") + argEnableMetrics = pflag.Bool("enable-metrics", true, "Whether to support metrics query") argPollTimeout = pflag.Int("ovs.timeout", 2, "Timeout on JSON-RPC requests to OVS.") argPollInterval = pflag.Int("ovs.poll-interval", 15, "The minimum interval (in seconds) between collections from OVS server.") @@ -115,6 +117,7 @@ func ParseFlags() (*Configuration, error) { PodName: os.Getenv("POD_NAME"), ExternalAddress: *argExternalAddress, NetworkMode: *argNetworkMode, + EnableMetrics: *argEnableMetrics, // OVS Monitor PollTimeout: *argPollTimeout,