defaults.go 4.6 KB

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