Skip to content

Commit e632600

Browse files
committed
kube-proxy: internal config: refactor Healthz and Metrics Address
Refactor Healthz with Metrics Address for internal configuration of kube-proxy adhering to the v1alpha2 version specifications as detailed in https://kep.k8s.io/784. Signed-off-by: Daman Arora <[email protected]>
1 parent 48f1356 commit e632600

File tree

15 files changed

+446
-256
lines changed

15 files changed

+446
-256
lines changed

cmd/kube-proxy/app/options.go

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ package app
1919
import (
2020
"context"
2121
"fmt"
22+
"net"
2223
"os"
24+
"strconv"
2325
"strings"
2426
"time"
2527

@@ -33,15 +35,14 @@ import (
3335
logsapi "k8s.io/component-base/logs/api/v1"
3436
"k8s.io/klog/v2"
3537
"k8s.io/kube-proxy/config/v1alpha1"
36-
"k8s.io/kubernetes/pkg/cluster/ports"
3738
"k8s.io/kubernetes/pkg/kubelet/qos"
3839
kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config"
3940
proxyconfigscheme "k8s.io/kubernetes/pkg/proxy/apis/config/scheme"
4041
kubeproxyconfigv1alpha1 "k8s.io/kubernetes/pkg/proxy/apis/config/v1alpha1"
4142
"k8s.io/kubernetes/pkg/proxy/apis/config/validation"
42-
proxyutil "k8s.io/kubernetes/pkg/proxy/util"
4343
"k8s.io/kubernetes/pkg/util/filesystem"
4444
utilflag "k8s.io/kubernetes/pkg/util/flag"
45+
netutils "k8s.io/utils/net"
4546
"k8s.io/utils/ptr"
4647
)
4748

@@ -71,10 +72,6 @@ type Options struct {
7172

7273
// master is used to override the kubeconfig's URL to the apiserver.
7374
master string
74-
// healthzPort is the port to be used by the healthz server.
75-
healthzPort int32
76-
// metricsPort is the port to be used by the metrics server.
77-
metricsPort int32
7875

7976
// hostnameOverride, if set from the command line flag, takes precedence over the `HostnameOverride` value from the config file
8077
hostnameOverride string
@@ -88,6 +85,8 @@ type Options struct {
8885
ipvsSyncPeriod time.Duration
8986
ipvsMinSyncPeriod time.Duration
9087
clusterCIDRs string
88+
healthzBindAddress string
89+
metricsBindAddress string
9190
}
9291

9392
// AddFlags adds flags to fs and binds them to options.
@@ -111,8 +110,8 @@ func (o *Options) AddFlags(fs *pflag.FlagSet) {
111110

112111
fs.StringVar(&o.hostnameOverride, "hostname-override", o.hostnameOverride, "If non-empty, will be used as the name of the Node that kube-proxy is running on. If unset, the node name is assumed to be the same as the node's hostname.")
113112
fs.Var(&utilflag.IPVar{Val: &o.config.BindAddress}, "bind-address", "Overrides kube-proxy's idea of what its node's primary IP is. Note that the name is a historical artifact, and kube-proxy does not actually bind any sockets to this IP. This parameter is ignored if a config file is specified by --config.")
114-
fs.Var(&utilflag.IPPortVar{Val: &o.config.HealthzBindAddress}, "healthz-bind-address", "The IP address and port for the health check server to serve on, defaulting to \"0.0.0.0:10256\". This parameter is ignored if a config file is specified by --config.")
115-
fs.Var(&utilflag.IPPortVar{Val: &o.config.MetricsBindAddress}, "metrics-bind-address", "The IP address and port for the metrics server to serve on, defaulting to \"127.0.0.1:10249\". (Set to \"0.0.0.0:10249\" / \"[::]:10249\" to bind on all interfaces.) Set empty to disable. This parameter is ignored if a config file is specified by --config.")
113+
fs.Var(&utilflag.IPPortVar{Val: &o.healthzBindAddress}, "healthz-bind-address", "The IP address and port for the health check server to serve on, defaulting to \"0.0.0.0:10256\". This parameter is ignored if a config file is specified by --config.")
114+
fs.Var(&utilflag.IPPortVar{Val: &o.metricsBindAddress}, "metrics-bind-address", "The IP address and port for the metrics server to serve on, defaulting to \"127.0.0.1:10249\". (Set to \"0.0.0.0:10249\" / \"[::]:10249\" to bind on all interfaces.) Set empty to disable. This parameter is ignored if a config file is specified by --config.")
116115
fs.BoolVar(&o.config.BindAddressHardFail, "bind-address-hard-fail", o.config.BindAddressHardFail, "If true kube-proxy will treat failure to bind to a port as fatal and exit")
117116
fs.BoolVar(&o.config.EnableProfiling, "profiling", o.config.EnableProfiling, "If true enables profiling via web interface on /debug/pprof handler. This parameter is ignored if a config file is specified by --config.")
118117
fs.StringVar(&o.config.ShowHiddenMetricsForVersion, "show-hidden-metrics-for-version", o.config.ShowHiddenMetricsForVersion,
@@ -166,11 +165,6 @@ func (o *Options) AddFlags(fs *pflag.FlagSet) {
166165
fs.DurationVar(&o.config.Linux.Conntrack.UDPStreamTimeout.Duration, "conntrack-udp-timeout-stream", o.config.Linux.Conntrack.UDPStreamTimeout.Duration, "Idle timeout for ASSURED UDP connections (0 to leave as-is)")
167166
fs.DurationVar(&o.config.ConfigSyncPeriod.Duration, "config-sync-period", o.config.ConfigSyncPeriod.Duration, "How often configuration from the apiserver is refreshed. Must be greater than 0.")
168167

169-
fs.Int32Var(&o.healthzPort, "healthz-port", o.healthzPort, "The port to bind the health check server. Use 0 to disable.")
170-
_ = fs.MarkDeprecated("healthz-port", "This flag is deprecated and will be removed in a future release. Please use --healthz-bind-address instead.")
171-
fs.Int32Var(&o.metricsPort, "metrics-port", o.metricsPort, "The port to bind the metrics server. Use 0 to disable.")
172-
_ = fs.MarkDeprecated("metrics-port", "This flag is deprecated and will be removed in a future release. Please use --metrics-bind-address instead.")
173-
174168
logsapi.AddFlags(&o.config.Logging, fs)
175169
}
176170

@@ -189,21 +183,14 @@ func newKubeProxyConfiguration() *kubeproxyconfig.KubeProxyConfiguration {
189183
// NewOptions returns initialized Options
190184
func NewOptions() *Options {
191185
return &Options{
192-
config: newKubeProxyConfiguration(),
193-
healthzPort: ports.ProxyHealthzPort,
194-
metricsPort: ports.ProxyStatusPort,
195-
errCh: make(chan error),
196-
logger: klog.FromContext(context.Background()),
186+
config: newKubeProxyConfiguration(),
187+
errCh: make(chan error),
188+
logger: klog.FromContext(context.Background()),
197189
}
198190
}
199191

200192
// Complete completes all the required options.
201193
func (o *Options) Complete(fs *pflag.FlagSet) error {
202-
if len(o.ConfigFile) == 0 && len(o.WriteConfigTo) == 0 {
203-
o.config.HealthzBindAddress = addressFromDeprecatedFlags(o.config.HealthzBindAddress, o.healthzPort)
204-
o.config.MetricsBindAddress = addressFromDeprecatedFlags(o.config.MetricsBindAddress, o.metricsPort)
205-
}
206-
207194
// Load the config file here in Complete, so that Validate validates the fully-resolved config.
208195
if len(o.ConfigFile) > 0 {
209196
c, err := o.loadConfigFromFile(o.ConfigFile)
@@ -328,6 +315,32 @@ func (o *Options) processV1Alpha1Flags(fs *pflag.FlagSet) {
328315
if fs.Changed("cluster-cidr") {
329316
o.config.DetectLocal.ClusterCIDRs = strings.Split(o.clusterCIDRs, ",")
330317
}
318+
if fs.Changed("healthz-bind-address") {
319+
host, port, _ := net.SplitHostPort(o.healthzBindAddress)
320+
ip := netutils.ParseIPSloppy(host)
321+
if ip.IsUnspecified() {
322+
o.config.HealthzBindAddresses = []string{fmt.Sprintf("%s/0", host)}
323+
} else if netutils.IsIPv4(ip) {
324+
o.config.HealthzBindAddresses = []string{fmt.Sprintf("%s/32", host)}
325+
} else {
326+
o.config.HealthzBindAddresses = []string{fmt.Sprintf("%s/128", host)}
327+
}
328+
intPort, _ := strconv.Atoi(port)
329+
o.config.HealthzBindPort = int32(intPort)
330+
}
331+
if fs.Changed("metrics-bind-address") {
332+
host, port, _ := net.SplitHostPort(o.metricsBindAddress)
333+
ip := netutils.ParseIPSloppy(host)
334+
if ip.IsUnspecified() {
335+
o.config.MetricsBindAddresses = []string{fmt.Sprintf("%s/0", host)}
336+
} else if netutils.IsIPv4(ip) {
337+
o.config.MetricsBindAddresses = []string{fmt.Sprintf("%s/32", host)}
338+
} else {
339+
o.config.MetricsBindAddresses = []string{fmt.Sprintf("%s/128", host)}
340+
}
341+
intPort, _ := strconv.Atoi(port)
342+
o.config.MetricsBindPort = int32(intPort)
343+
}
331344
}
332345

333346
// Validate validates all the required options.
@@ -416,17 +429,6 @@ func (o *Options) writeConfigFile() (err error) {
416429
return nil
417430
}
418431

419-
// addressFromDeprecatedFlags returns server address from flags
420-
// passed on the command line based on the following rules:
421-
// 1. If port is 0, disable the server (e.g. set address to empty).
422-
// 2. Otherwise, set the port portion of the config accordingly.
423-
func addressFromDeprecatedFlags(addr string, port int32) string {
424-
if port == 0 {
425-
return ""
426-
}
427-
return proxyutil.AppendPortIfNeeded(addr, port)
428-
}
429-
430432
// newLenientSchemeAndCodecs returns a scheme that has only v1alpha1 registered into
431433
// it and a CodecFactory with strict decoding disabled.
432434
func newLenientSchemeAndCodecs() (*runtime.Scheme, *serializer.CodecFactory, error) {

0 commit comments

Comments
 (0)