kubeproxy.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. Copyright 2019 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 componentconfigs
  14. import (
  15. "net"
  16. clientset "k8s.io/client-go/kubernetes"
  17. kubeproxyconfig "k8s.io/kube-proxy/config/v1alpha1"
  18. kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
  19. kubeadmapiv1beta2 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2"
  20. kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  21. "k8s.io/kubernetes/cmd/kubeadm/app/features"
  22. )
  23. const (
  24. // KubeProxyGroup is a pointer to the used API group name for the kube-proxy config
  25. KubeProxyGroup = kubeproxyconfig.GroupName
  26. // kubeproxyKubeConfigFileName is used during defaulting. It's here so it can be accessed from the tests.
  27. kubeproxyKubeConfigFileName = "/var/lib/kube-proxy/kubeconfig.conf"
  28. )
  29. // kubeProxyHandler is the handler instance for the kube-proxy component config
  30. var kubeProxyHandler = handler{
  31. GroupVersion: kubeproxyconfig.SchemeGroupVersion,
  32. AddToScheme: kubeproxyconfig.AddToScheme,
  33. CreateEmpty: func() kubeadmapi.ComponentConfig {
  34. return &kubeProxyConfig{}
  35. },
  36. fromCluster: kubeProxyConfigFromCluster,
  37. }
  38. func kubeProxyConfigFromCluster(h *handler, clientset clientset.Interface, _ *kubeadmapi.ClusterConfiguration) (kubeadmapi.ComponentConfig, error) {
  39. return h.fromConfigMap(clientset, kubeadmconstants.KubeProxyConfigMap, kubeadmconstants.KubeProxyConfigMapKey, false)
  40. }
  41. // kubeProxyConfig implements the kubeadmapi.ComponentConfig interface for kube-proxy
  42. type kubeProxyConfig struct {
  43. config kubeproxyconfig.KubeProxyConfiguration
  44. }
  45. func (kp *kubeProxyConfig) DeepCopy() kubeadmapi.ComponentConfig {
  46. result := &kubeProxyConfig{}
  47. kp.config.DeepCopyInto(&result.config)
  48. return result
  49. }
  50. func (kp *kubeProxyConfig) Marshal() ([]byte, error) {
  51. return kubeProxyHandler.Marshal(&kp.config)
  52. }
  53. func (kp *kubeProxyConfig) Unmarshal(docmap kubeadmapi.DocumentMap) error {
  54. return kubeProxyHandler.Unmarshal(docmap, &kp.config)
  55. }
  56. func kubeProxyDefaultBindAddress(localAdvertiseAddress string) string {
  57. ip := net.ParseIP(localAdvertiseAddress)
  58. if ip.To4() != nil {
  59. return kubeadmapiv1beta2.DefaultProxyBindAddressv4
  60. }
  61. return kubeadmapiv1beta2.DefaultProxyBindAddressv6
  62. }
  63. func (kp *kubeProxyConfig) Default(cfg *kubeadmapi.ClusterConfiguration, localAPIEndpoint *kubeadmapi.APIEndpoint) {
  64. const kind = "KubeProxyConfiguration"
  65. // The below code is necessary because while KubeProxy may be defined, the user may not
  66. // have defined any feature-gates, thus FeatureGates will be nil and the later insertion
  67. // of any feature-gates (e.g. IPv6DualStack) will cause a panic.
  68. if kp.config.FeatureGates == nil {
  69. kp.config.FeatureGates = map[string]bool{}
  70. }
  71. defaultBindAddress := kubeProxyDefaultBindAddress(localAPIEndpoint.AdvertiseAddress)
  72. if kp.config.BindAddress == "" {
  73. kp.config.BindAddress = defaultBindAddress
  74. } else if kp.config.BindAddress != defaultBindAddress {
  75. warnDefaultComponentConfigValue(kind, "bindAddress", kp.config.BindAddress, defaultBindAddress)
  76. }
  77. if kp.config.ClusterCIDR == "" && cfg.Networking.PodSubnet != "" {
  78. kp.config.ClusterCIDR = cfg.Networking.PodSubnet
  79. } else if cfg.Networking.PodSubnet != "" && kp.config.ClusterCIDR != cfg.Networking.PodSubnet {
  80. warnDefaultComponentConfigValue(kind, "clusterCIDR", cfg.Networking.PodSubnet, kp.config.ClusterCIDR)
  81. }
  82. if kp.config.ClientConnection.Kubeconfig == "" {
  83. kp.config.ClientConnection.Kubeconfig = kubeproxyKubeConfigFileName
  84. } else if kp.config.ClientConnection.Kubeconfig != kubeproxyKubeConfigFileName {
  85. warnDefaultComponentConfigValue(kind, "clientConnection.kubeconfig", kubeproxyKubeConfigFileName, kp.config.ClientConnection.Kubeconfig)
  86. }
  87. // TODO: The following code should be removed after dual-stack is GA.
  88. // Note: The user still retains the ability to explicitly set feature-gates and that value will overwrite this base value.
  89. if enabled, present := cfg.FeatureGates[features.IPv6DualStack]; present {
  90. kp.config.FeatureGates[features.IPv6DualStack] = enabled
  91. }
  92. }