1/*
2Copyright 2015 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package v1alpha1
18
19import (
20	"fmt"
21	"net"
22	"time"
23
24	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25	kruntime "k8s.io/apimachinery/pkg/runtime"
26	kubeproxyconfigv1alpha1 "k8s.io/kube-proxy/config/v1alpha1"
27
28	"k8s.io/kubernetes/pkg/cluster/ports"
29	"k8s.io/kubernetes/pkg/kubelet/qos"
30	proxyutil "k8s.io/kubernetes/pkg/proxy/util"
31	"k8s.io/utils/pointer"
32)
33
34func addDefaultingFuncs(scheme *kruntime.Scheme) error {
35	return RegisterDefaults(scheme)
36}
37
38func SetDefaults_KubeProxyConfiguration(obj *kubeproxyconfigv1alpha1.KubeProxyConfiguration) {
39
40	if len(obj.BindAddress) == 0 {
41		obj.BindAddress = "0.0.0.0"
42	}
43
44	defaultHealthzAddress, defaultMetricsAddress := getDefaultAddresses(obj.BindAddress)
45
46	if obj.HealthzBindAddress == "" {
47		obj.HealthzBindAddress = fmt.Sprintf("%s:%v", defaultHealthzAddress, ports.ProxyHealthzPort)
48	} else {
49		obj.HealthzBindAddress = proxyutil.AppendPortIfNeeded(obj.HealthzBindAddress, ports.ProxyHealthzPort)
50	}
51	if obj.MetricsBindAddress == "" {
52		obj.MetricsBindAddress = fmt.Sprintf("%s:%v", defaultMetricsAddress, ports.ProxyStatusPort)
53	} else {
54		obj.MetricsBindAddress = proxyutil.AppendPortIfNeeded(obj.MetricsBindAddress, ports.ProxyStatusPort)
55	}
56
57	if obj.OOMScoreAdj == nil {
58		temp := int32(qos.KubeProxyOOMScoreAdj)
59		obj.OOMScoreAdj = &temp
60	}
61	if obj.IPTables.SyncPeriod.Duration == 0 {
62		obj.IPTables.SyncPeriod = metav1.Duration{Duration: 30 * time.Second}
63	}
64	if obj.IPTables.MinSyncPeriod.Duration == 0 {
65		obj.IPTables.MinSyncPeriod = metav1.Duration{Duration: 1 * time.Second}
66	}
67	if obj.IPVS.SyncPeriod.Duration == 0 {
68		obj.IPVS.SyncPeriod = metav1.Duration{Duration: 30 * time.Second}
69	}
70	zero := metav1.Duration{}
71	if obj.UDPIdleTimeout == zero {
72		obj.UDPIdleTimeout = metav1.Duration{Duration: 250 * time.Millisecond}
73	}
74
75	if obj.Conntrack.MaxPerCore == nil {
76		obj.Conntrack.MaxPerCore = pointer.Int32Ptr(32 * 1024)
77	}
78	if obj.Conntrack.Min == nil {
79		obj.Conntrack.Min = pointer.Int32Ptr(128 * 1024)
80	}
81
82	if obj.IPTables.MasqueradeBit == nil {
83		temp := int32(14)
84		obj.IPTables.MasqueradeBit = &temp
85	}
86	if obj.Conntrack.TCPEstablishedTimeout == nil {
87		obj.Conntrack.TCPEstablishedTimeout = &metav1.Duration{Duration: 24 * time.Hour} // 1 day (1/5 default)
88	}
89	if obj.Conntrack.TCPCloseWaitTimeout == nil {
90		// See https://github.com/kubernetes/kubernetes/issues/32551.
91		//
92		// CLOSE_WAIT conntrack state occurs when the Linux kernel
93		// sees a FIN from the remote server. Note: this is a half-close
94		// condition that persists as long as the local side keeps the
95		// socket open. The condition is rare as it is typical in most
96		// protocols for both sides to issue a close; this typically
97		// occurs when the local socket is lazily garbage collected.
98		//
99		// If the CLOSE_WAIT conntrack entry expires, then FINs from the
100		// local socket will not be properly SNAT'd and will not reach the
101		// remote server (if the connection was subject to SNAT). If the
102		// remote timeouts for FIN_WAIT* states exceed the CLOSE_WAIT
103		// timeout, then there will be an inconsistency in the state of
104		// the connection and a new connection reusing the SNAT (src,
105		// port) pair may be rejected by the remote side with RST. This
106		// can cause new calls to connect(2) to return with ECONNREFUSED.
107		//
108		// We set CLOSE_WAIT to one hour by default to better match
109		// typical server timeouts.
110		obj.Conntrack.TCPCloseWaitTimeout = &metav1.Duration{Duration: 1 * time.Hour}
111	}
112	if obj.ConfigSyncPeriod.Duration == 0 {
113		obj.ConfigSyncPeriod.Duration = 15 * time.Minute
114	}
115
116	if len(obj.ClientConnection.ContentType) == 0 {
117		obj.ClientConnection.ContentType = "application/vnd.kubernetes.protobuf"
118	}
119	if obj.ClientConnection.QPS == 0.0 {
120		obj.ClientConnection.QPS = 5.0
121	}
122	if obj.ClientConnection.Burst == 0 {
123		obj.ClientConnection.Burst = 10
124	}
125	if obj.FeatureGates == nil {
126		obj.FeatureGates = make(map[string]bool)
127	}
128}
129
130// getDefaultAddresses returns default address of healthz and metrics server
131// based on the given bind address. IPv6 addresses are enclosed in square
132// brackets for appending port.
133func getDefaultAddresses(bindAddress string) (defaultHealthzAddress, defaultMetricsAddress string) {
134	if net.ParseIP(bindAddress).To4() != nil {
135		return "0.0.0.0", "127.0.0.1"
136	}
137	return "[::]", "[::1]"
138}
139