...

Source file src/k8s.io/kubernetes/cmd/kubeadm/app/componentconfigs/kubeproxy.go

Documentation: k8s.io/kubernetes/cmd/kubeadm/app/componentconfigs

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package componentconfigs
    18  
    19  import (
    20  	"github.com/pkg/errors"
    21  	clientset "k8s.io/client-go/kubernetes"
    22  	"k8s.io/klog/v2"
    23  	kubeproxyconfig "k8s.io/kube-proxy/config/v1alpha1"
    24  	netutils "k8s.io/utils/net"
    25  
    26  	kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
    27  	kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
    28  	kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
    29  )
    30  
    31  const (
    32  	// KubeProxyGroup is a pointer to the used API group name for the kube-proxy config
    33  	KubeProxyGroup = kubeproxyconfig.GroupName
    34  
    35  	// kubeproxyKubeConfigFileName is used during defaulting. It's here so it can be accessed from the tests.
    36  	kubeproxyKubeConfigFileName = "/var/lib/kube-proxy/kubeconfig.conf"
    37  )
    38  
    39  // kubeProxyHandler is the handler instance for the kube-proxy component config
    40  var kubeProxyHandler = handler{
    41  	GroupVersion: kubeproxyconfig.SchemeGroupVersion,
    42  	AddToScheme:  kubeproxyconfig.AddToScheme,
    43  	CreateEmpty: func() kubeadmapi.ComponentConfig {
    44  		return &kubeProxyConfig{
    45  			configBase: configBase{
    46  				GroupVersion: kubeproxyconfig.SchemeGroupVersion,
    47  			},
    48  		}
    49  	},
    50  	fromCluster: kubeProxyConfigFromCluster,
    51  }
    52  
    53  func kubeProxyConfigFromCluster(h *handler, clientset clientset.Interface, _ *kubeadmapi.ClusterConfiguration) (kubeadmapi.ComponentConfig, error) {
    54  	configMapName := kubeadmconstants.KubeProxyConfigMap
    55  	klog.V(1).Infof("attempting to download the KubeProxyConfiguration from ConfigMap %q", configMapName)
    56  	cm, err := h.fromConfigMap(clientset, configMapName, kubeadmconstants.KubeProxyConfigMapKey, false)
    57  	if err != nil {
    58  		return nil, errors.Wrapf(err, "could not download the kube-proxy configuration from ConfigMap %q",
    59  			configMapName)
    60  	}
    61  	return cm, nil
    62  }
    63  
    64  // kubeProxyConfig implements the kubeadmapi.ComponentConfig interface for kube-proxy
    65  type kubeProxyConfig struct {
    66  	configBase
    67  	config kubeproxyconfig.KubeProxyConfiguration
    68  }
    69  
    70  func (kp *kubeProxyConfig) DeepCopy() kubeadmapi.ComponentConfig {
    71  	result := &kubeProxyConfig{}
    72  	kp.configBase.DeepCopyInto(&result.configBase)
    73  	kp.config.DeepCopyInto(&result.config)
    74  	return result
    75  }
    76  
    77  func (kp *kubeProxyConfig) Marshal() ([]byte, error) {
    78  	return kp.configBase.Marshal(&kp.config)
    79  }
    80  
    81  func (kp *kubeProxyConfig) Unmarshal(docmap kubeadmapi.DocumentMap) error {
    82  	return kp.configBase.Unmarshal(docmap, &kp.config)
    83  }
    84  
    85  func kubeProxyDefaultBindAddress(localAdvertiseAddress string) string {
    86  	ip := netutils.ParseIPSloppy(localAdvertiseAddress)
    87  	if ip.To4() != nil {
    88  		return kubeadmapiv1.DefaultProxyBindAddressv4
    89  	}
    90  	return kubeadmapiv1.DefaultProxyBindAddressv6
    91  }
    92  
    93  func (kp *kubeProxyConfig) Get() interface{} {
    94  	return &kp.config
    95  }
    96  
    97  func (kp *kubeProxyConfig) Set(cfg interface{}) {
    98  	kp.config = *cfg.(*kubeproxyconfig.KubeProxyConfiguration)
    99  }
   100  
   101  func (kp *kubeProxyConfig) Default(cfg *kubeadmapi.ClusterConfiguration, localAPIEndpoint *kubeadmapi.APIEndpoint, _ *kubeadmapi.NodeRegistrationOptions) {
   102  	const kind = "KubeProxyConfiguration"
   103  
   104  	// The below code is necessary because while KubeProxy may be defined, the user may not
   105  	// have defined any feature-gates, thus FeatureGates will be nil and the later insertion
   106  	// of any feature-gates will cause a panic.
   107  	if kp.config.FeatureGates == nil {
   108  		kp.config.FeatureGates = map[string]bool{}
   109  	}
   110  
   111  	defaultBindAddress := kubeProxyDefaultBindAddress(localAPIEndpoint.AdvertiseAddress)
   112  	if kp.config.BindAddress == "" {
   113  		kp.config.BindAddress = defaultBindAddress
   114  	} else if kp.config.BindAddress != defaultBindAddress {
   115  		warnDefaultComponentConfigValue(kind, "bindAddress", defaultBindAddress, kp.config.BindAddress)
   116  	}
   117  
   118  	if kp.config.ClusterCIDR == "" && cfg.Networking.PodSubnet != "" {
   119  		kp.config.ClusterCIDR = cfg.Networking.PodSubnet
   120  	} else if cfg.Networking.PodSubnet != "" && kp.config.ClusterCIDR != cfg.Networking.PodSubnet {
   121  		warnDefaultComponentConfigValue(kind, "clusterCIDR", cfg.Networking.PodSubnet, kp.config.ClusterCIDR)
   122  	}
   123  
   124  	if kp.config.ClientConnection.Kubeconfig == "" {
   125  		kp.config.ClientConnection.Kubeconfig = kubeproxyKubeConfigFileName
   126  	} else if kp.config.ClientConnection.Kubeconfig != kubeproxyKubeConfigFileName {
   127  		warnDefaultComponentConfigValue(kind, "clientConnection.kubeconfig", kubeproxyKubeConfigFileName, kp.config.ClientConnection.Kubeconfig)
   128  	}
   129  }
   130  
   131  // Mutate is NOP for the kube-proxy config
   132  func (kp *kubeProxyConfig) Mutate() error {
   133  	return nil
   134  }
   135  

View as plain text