defaults.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. Copyright 2018 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. "path/filepath"
  16. kubeproxyconfigv1alpha1 "k8s.io/kube-proxy/config/v1alpha1"
  17. kubeletconfigv1beta1 "k8s.io/kubelet/config/v1beta1"
  18. kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
  19. kubeadmapiv1beta2 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2"
  20. "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  21. kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
  22. kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config"
  23. utilpointer "k8s.io/utils/pointer"
  24. )
  25. const (
  26. // KubeproxyKubeConfigFileName defines the file name for the kube-proxy's kubeconfig file
  27. KubeproxyKubeConfigFileName = "/var/lib/kube-proxy/kubeconfig.conf"
  28. )
  29. // DefaultKubeProxyConfiguration assigns default values for the kube-proxy ComponentConfig
  30. func DefaultKubeProxyConfiguration(internalcfg *kubeadmapi.ClusterConfiguration) {
  31. externalproxycfg := &kubeproxyconfigv1alpha1.KubeProxyConfiguration{}
  32. // Do a roundtrip to the external version for defaulting
  33. if internalcfg.ComponentConfigs.KubeProxy != nil {
  34. Scheme.Convert(internalcfg.ComponentConfigs.KubeProxy, externalproxycfg, nil)
  35. }
  36. if externalproxycfg.ClusterCIDR == "" && internalcfg.Networking.PodSubnet != "" {
  37. externalproxycfg.ClusterCIDR = internalcfg.Networking.PodSubnet
  38. }
  39. if externalproxycfg.ClientConnection.Kubeconfig == "" {
  40. externalproxycfg.ClientConnection.Kubeconfig = KubeproxyKubeConfigFileName
  41. }
  42. // Run the rest of the kube-proxy defaulting code
  43. Scheme.Default(externalproxycfg)
  44. if internalcfg.ComponentConfigs.KubeProxy == nil {
  45. internalcfg.ComponentConfigs.KubeProxy = &kubeproxyconfig.KubeProxyConfiguration{}
  46. }
  47. // TODO: Figure out how to handle errors in defaulting code
  48. // Go back to the internal version
  49. Scheme.Convert(externalproxycfg, internalcfg.ComponentConfigs.KubeProxy, nil)
  50. }
  51. // DefaultKubeletConfiguration assigns default values for the kubelet ComponentConfig
  52. func DefaultKubeletConfiguration(internalcfg *kubeadmapi.ClusterConfiguration) {
  53. externalkubeletcfg := &kubeletconfigv1beta1.KubeletConfiguration{}
  54. // Do a roundtrip to the external version for defaulting
  55. if internalcfg.ComponentConfigs.Kubelet != nil {
  56. Scheme.Convert(internalcfg.ComponentConfigs.Kubelet, externalkubeletcfg, nil)
  57. }
  58. if externalkubeletcfg.StaticPodPath == "" {
  59. externalkubeletcfg.StaticPodPath = kubeadmapiv1beta2.DefaultManifestsDir
  60. }
  61. if externalkubeletcfg.ClusterDNS == nil {
  62. dnsIP, err := constants.GetDNSIP(internalcfg.Networking.ServiceSubnet)
  63. if err != nil {
  64. externalkubeletcfg.ClusterDNS = []string{kubeadmapiv1beta2.DefaultClusterDNSIP}
  65. } else {
  66. externalkubeletcfg.ClusterDNS = []string{dnsIP.String()}
  67. }
  68. }
  69. if externalkubeletcfg.ClusterDomain == "" {
  70. externalkubeletcfg.ClusterDomain = internalcfg.Networking.DNSDomain
  71. }
  72. // Enforce security-related kubelet options
  73. // Require all clients to the kubelet API to have client certs signed by the cluster CA
  74. externalkubeletcfg.Authentication.X509.ClientCAFile = filepath.Join(internalcfg.CertificatesDir, constants.CACertName)
  75. externalkubeletcfg.Authentication.Anonymous.Enabled = utilpointer.BoolPtr(false)
  76. // On every client request to the kubelet API, execute a webhook (SubjectAccessReview request) to the API server
  77. // and ask it whether the client is authorized to access the kubelet API
  78. externalkubeletcfg.Authorization.Mode = kubeletconfigv1beta1.KubeletAuthorizationModeWebhook
  79. // Let clients using other authentication methods like ServiceAccount tokens also access the kubelet API
  80. externalkubeletcfg.Authentication.Webhook.Enabled = utilpointer.BoolPtr(true)
  81. // Disable the readonly port of the kubelet, in order to not expose unnecessary information
  82. externalkubeletcfg.ReadOnlyPort = 0
  83. // Enables client certificate rotation for the kubelet
  84. externalkubeletcfg.RotateCertificates = true
  85. // Serve a /healthz webserver on localhost:10248 that kubeadm can talk to
  86. externalkubeletcfg.HealthzBindAddress = "127.0.0.1"
  87. externalkubeletcfg.HealthzPort = utilpointer.Int32Ptr(constants.KubeletHealthzPort)
  88. Scheme.Default(externalkubeletcfg)
  89. if internalcfg.ComponentConfigs.Kubelet == nil {
  90. internalcfg.ComponentConfigs.Kubelet = &kubeletconfig.KubeletConfiguration{}
  91. }
  92. // TODO: Figure out how to handle errors in defaulting code
  93. // Go back to the internal version
  94. Scheme.Convert(externalkubeletcfg, internalcfg.ComponentConfigs.Kubelet, nil)
  95. }