Skip to content

Commit a35bca9

Browse files
authored
Merge pull request kubernetes#127811 from aojea/revert-126889-kube-proxy-refactor-healthz-metrics-address
Revert "kube-proxy: internal config: refactor HealthzAddress and MetricsAddress "
2 parents 37004f8 + 7c4c7b1 commit a35bca9

20 files changed

+318
-521
lines changed

cmd/kube-proxy/app/options.go

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

@@ -35,14 +33,15 @@ import (
3533
logsapi "k8s.io/component-base/logs/api/v1"
3634
"k8s.io/klog/v2"
3735
"k8s.io/kube-proxy/config/v1alpha1"
36+
"k8s.io/kubernetes/pkg/cluster/ports"
3837
"k8s.io/kubernetes/pkg/kubelet/qos"
3938
kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config"
4039
proxyconfigscheme "k8s.io/kubernetes/pkg/proxy/apis/config/scheme"
4140
kubeproxyconfigv1alpha1 "k8s.io/kubernetes/pkg/proxy/apis/config/v1alpha1"
4241
"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"
4645
"k8s.io/utils/ptr"
4746
)
4847

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

7372
// master is used to override the kubeconfig's URL to the apiserver.
7473
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
7578

7679
// hostnameOverride, if set from the command line flag, takes precedence over the `HostnameOverride` value from the config file
7780
hostnameOverride string
@@ -85,8 +88,6 @@ type Options struct {
8588
ipvsSyncPeriod time.Duration
8689
ipvsMinSyncPeriod time.Duration
8790
clusterCIDRs string
88-
healthzBindAddress string
89-
metricsBindAddress string
9091
}
9192

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

111112
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.")
112113
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.")
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.")
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.")
115116
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")
116117
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.")
117118
fs.StringVar(&o.config.ShowHiddenMetricsForVersion, "show-hidden-metrics-for-version", o.config.ShowHiddenMetricsForVersion,
@@ -165,6 +166,11 @@ func (o *Options) AddFlags(fs *pflag.FlagSet) {
165166
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)")
166167
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.")
167168

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+
168174
logsapi.AddFlags(&o.config.Logging, fs)
169175
}
170176

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

192200
// Complete completes all the required options.
193201
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+
194207
// Load the config file here in Complete, so that Validate validates the fully-resolved config.
195208
if len(o.ConfigFile) > 0 {
196209
c, err := o.loadConfigFromFile(o.ConfigFile)
@@ -315,32 +328,6 @@ func (o *Options) processV1Alpha1Flags(fs *pflag.FlagSet) {
315328
if fs.Changed("cluster-cidr") {
316329
o.config.DetectLocal.ClusterCIDRs = strings.Split(o.clusterCIDRs, ",")
317330
}
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-
}
344331
}
345332

346333
// Validate validates all the required options.
@@ -429,6 +416,17 @@ func (o *Options) writeConfigFile() (err error) {
429416
return nil
430417
}
431418

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+
432430
// newLenientSchemeAndCodecs returns a scheme that has only v1alpha1 registered into
433431
// it and a CodecFactory with strict decoding disabled.
434432
func newLenientSchemeAndCodecs() (*runtime.Scheme, *serializer.CodecFactory, error) {

0 commit comments

Comments
 (0)