defaults.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.ResourceContainer == "" {
  49. obj.ResourceContainer = "/kube-proxy"
  50. }
  51. if obj.IPTables.SyncPeriod.Duration == 0 {
  52. obj.IPTables.SyncPeriod = metav1.Duration{Duration: 30 * time.Second}
  53. }
  54. if obj.IPVS.SyncPeriod.Duration == 0 {
  55. obj.IPVS.SyncPeriod = metav1.Duration{Duration: 30 * time.Second}
  56. }
  57. zero := metav1.Duration{}
  58. if obj.UDPIdleTimeout == zero {
  59. obj.UDPIdleTimeout = metav1.Duration{Duration: 250 * time.Millisecond}
  60. }
  61. if obj.Conntrack.MaxPerCore == nil {
  62. obj.Conntrack.MaxPerCore = pointer.Int32Ptr(32 * 1024)
  63. }
  64. if obj.Conntrack.Min == nil {
  65. obj.Conntrack.Min = pointer.Int32Ptr(128 * 1024)
  66. }
  67. if obj.IPTables.MasqueradeBit == nil {
  68. temp := int32(14)
  69. obj.IPTables.MasqueradeBit = &temp
  70. }
  71. if obj.Conntrack.TCPEstablishedTimeout == nil {
  72. obj.Conntrack.TCPEstablishedTimeout = &metav1.Duration{Duration: 24 * time.Hour} // 1 day (1/5 default)
  73. }
  74. if obj.Conntrack.TCPCloseWaitTimeout == nil {
  75. // See https://github.com/kubernetes/kubernetes/issues/32551.
  76. //
  77. // CLOSE_WAIT conntrack state occurs when the Linux kernel
  78. // sees a FIN from the remote server. Note: this is a half-close
  79. // condition that persists as long as the local side keeps the
  80. // socket open. The condition is rare as it is typical in most
  81. // protocols for both sides to issue a close; this typically
  82. // occurs when the local socket is lazily garbage collected.
  83. //
  84. // If the CLOSE_WAIT conntrack entry expires, then FINs from the
  85. // local socket will not be properly SNAT'd and will not reach the
  86. // remote server (if the connection was subject to SNAT). If the
  87. // remote timeouts for FIN_WAIT* states exceed the CLOSE_WAIT
  88. // timeout, then there will be an inconsistency in the state of
  89. // the connection and a new connection reusing the SNAT (src,
  90. // port) pair may be rejected by the remote side with RST. This
  91. // can cause new calls to connect(2) to return with ECONNREFUSED.
  92. //
  93. // We set CLOSE_WAIT to one hour by default to better match
  94. // typical server timeouts.
  95. obj.Conntrack.TCPCloseWaitTimeout = &metav1.Duration{Duration: 1 * time.Hour}
  96. }
  97. if obj.ConfigSyncPeriod.Duration == 0 {
  98. obj.ConfigSyncPeriod.Duration = 15 * time.Minute
  99. }
  100. if len(obj.ClientConnection.ContentType) == 0 {
  101. obj.ClientConnection.ContentType = "application/vnd.kubernetes.protobuf"
  102. }
  103. if obj.ClientConnection.QPS == 0.0 {
  104. obj.ClientConnection.QPS = 5.0
  105. }
  106. if obj.ClientConnection.Burst == 0 {
  107. obj.ClientConnection.Burst = 10
  108. }
  109. if obj.FeatureGates == nil {
  110. obj.FeatureGates = make(map[string]bool)
  111. }
  112. }
  113. // getDefaultAddresses returns default address of healthz and metrics server
  114. // based on the given bind address. IPv6 addresses are enclosed in square
  115. // brackets for appending port.
  116. func getDefaultAddresses(bindAddress string) (defaultHealthzAddress, defaultMetricsAddress string) {
  117. if net.ParseIP(bindAddress).To4() != nil {
  118. return "0.0.0.0", "127.0.0.1"
  119. }
  120. return "[::]", "[::1]"
  121. }