constants.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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 constants
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "net"
  18. "os"
  19. "path"
  20. "path/filepath"
  21. "strings"
  22. "time"
  23. "github.com/pkg/errors"
  24. v1 "k8s.io/api/core/v1"
  25. "k8s.io/apimachinery/pkg/util/version"
  26. bootstrapapi "k8s.io/cluster-bootstrap/token/api"
  27. kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
  28. utilnet "k8s.io/utils/net"
  29. )
  30. const (
  31. // KubernetesDir is the directory Kubernetes owns for storing various configuration files
  32. KubernetesDir = "/etc/kubernetes"
  33. // ManifestsSubDirName defines directory name to store manifests
  34. ManifestsSubDirName = "manifests"
  35. // TempDirForKubeadm defines temporary directory for kubeadm
  36. // should be joined with KubernetesDir.
  37. TempDirForKubeadm = "tmp"
  38. // CertificateValidity defines the validity for all the signed certificates generated by kubeadm
  39. CertificateValidity = time.Hour * 24 * 365
  40. // CACertAndKeyBaseName defines certificate authority base name
  41. CACertAndKeyBaseName = "ca"
  42. // CACertName defines certificate name
  43. CACertName = "ca.crt"
  44. // CAKeyName defines certificate name
  45. CAKeyName = "ca.key"
  46. // APIServerCertAndKeyBaseName defines API's server certificate and key base name
  47. APIServerCertAndKeyBaseName = "apiserver"
  48. // APIServerCertName defines API's server certificate name
  49. APIServerCertName = "apiserver.crt"
  50. // APIServerKeyName defines API's server key name
  51. APIServerKeyName = "apiserver.key"
  52. // APIServerCertCommonName defines API's server certificate common name (CN)
  53. APIServerCertCommonName = "kube-apiserver"
  54. // APIServerKubeletClientCertAndKeyBaseName defines kubelet client certificate and key base name
  55. APIServerKubeletClientCertAndKeyBaseName = "apiserver-kubelet-client"
  56. // APIServerKubeletClientCertName defines kubelet client certificate name
  57. APIServerKubeletClientCertName = "apiserver-kubelet-client.crt"
  58. // APIServerKubeletClientKeyName defines kubelet client key name
  59. APIServerKubeletClientKeyName = "apiserver-kubelet-client.key"
  60. // APIServerKubeletClientCertCommonName defines kubelet client certificate common name (CN)
  61. APIServerKubeletClientCertCommonName = "kube-apiserver-kubelet-client"
  62. // EtcdCACertAndKeyBaseName defines etcd's CA certificate and key base name
  63. EtcdCACertAndKeyBaseName = "etcd/ca"
  64. // EtcdCACertName defines etcd's CA certificate name
  65. EtcdCACertName = "etcd/ca.crt"
  66. // EtcdCAKeyName defines etcd's CA key name
  67. EtcdCAKeyName = "etcd/ca.key"
  68. // EtcdServerCertAndKeyBaseName defines etcd's server certificate and key base name
  69. EtcdServerCertAndKeyBaseName = "etcd/server"
  70. // EtcdServerCertName defines etcd's server certificate name
  71. EtcdServerCertName = "etcd/server.crt"
  72. // EtcdServerKeyName defines etcd's server key name
  73. EtcdServerKeyName = "etcd/server.key"
  74. // EtcdListenClientPort defines the port etcd listen on for client traffic
  75. EtcdListenClientPort = 2379
  76. // EtcdMetricsPort is the port at which to obtain etcd metrics and health status
  77. EtcdMetricsPort = 2381
  78. // EtcdPeerCertAndKeyBaseName defines etcd's peer certificate and key base name
  79. EtcdPeerCertAndKeyBaseName = "etcd/peer"
  80. // EtcdPeerCertName defines etcd's peer certificate name
  81. EtcdPeerCertName = "etcd/peer.crt"
  82. // EtcdPeerKeyName defines etcd's peer key name
  83. EtcdPeerKeyName = "etcd/peer.key"
  84. // EtcdListenPeerPort defines the port etcd listen on for peer traffic
  85. EtcdListenPeerPort = 2380
  86. // EtcdHealthcheckClientCertAndKeyBaseName defines etcd's healthcheck client certificate and key base name
  87. EtcdHealthcheckClientCertAndKeyBaseName = "etcd/healthcheck-client"
  88. // EtcdHealthcheckClientCertName defines etcd's healthcheck client certificate name
  89. EtcdHealthcheckClientCertName = "etcd/healthcheck-client.crt"
  90. // EtcdHealthcheckClientKeyName defines etcd's healthcheck client key name
  91. EtcdHealthcheckClientKeyName = "etcd/healthcheck-client.key"
  92. // EtcdHealthcheckClientCertCommonName defines etcd's healthcheck client certificate common name (CN)
  93. EtcdHealthcheckClientCertCommonName = "kube-etcd-healthcheck-client"
  94. // APIServerEtcdClientCertAndKeyBaseName defines apiserver's etcd client certificate and key base name
  95. APIServerEtcdClientCertAndKeyBaseName = "apiserver-etcd-client"
  96. // APIServerEtcdClientCertName defines apiserver's etcd client certificate name
  97. APIServerEtcdClientCertName = "apiserver-etcd-client.crt"
  98. // APIServerEtcdClientKeyName defines apiserver's etcd client key name
  99. APIServerEtcdClientKeyName = "apiserver-etcd-client.key"
  100. // APIServerEtcdClientCertCommonName defines apiserver's etcd client certificate common name (CN)
  101. APIServerEtcdClientCertCommonName = "kube-apiserver-etcd-client"
  102. // ServiceAccountKeyBaseName defines SA key base name
  103. ServiceAccountKeyBaseName = "sa"
  104. // ServiceAccountPublicKeyName defines SA public key base name
  105. ServiceAccountPublicKeyName = "sa.pub"
  106. // ServiceAccountPrivateKeyName defines SA private key base name
  107. ServiceAccountPrivateKeyName = "sa.key"
  108. // FrontProxyCACertAndKeyBaseName defines front proxy CA certificate and key base name
  109. FrontProxyCACertAndKeyBaseName = "front-proxy-ca"
  110. // FrontProxyCACertName defines front proxy CA certificate name
  111. FrontProxyCACertName = "front-proxy-ca.crt"
  112. // FrontProxyCAKeyName defines front proxy CA key name
  113. FrontProxyCAKeyName = "front-proxy-ca.key"
  114. // FrontProxyClientCertAndKeyBaseName defines front proxy certificate and key base name
  115. FrontProxyClientCertAndKeyBaseName = "front-proxy-client"
  116. // FrontProxyClientCertName defines front proxy certificate name
  117. FrontProxyClientCertName = "front-proxy-client.crt"
  118. // FrontProxyClientKeyName defines front proxy key name
  119. FrontProxyClientKeyName = "front-proxy-client.key"
  120. // FrontProxyClientCertCommonName defines front proxy certificate common name
  121. FrontProxyClientCertCommonName = "front-proxy-client" //used as subject.commonname attribute (CN)
  122. // AdminKubeConfigFileName defines name for the kubeconfig aimed to be used by the superuser/admin of the cluster
  123. AdminKubeConfigFileName = "admin.conf"
  124. // KubeletBootstrapKubeConfigFileName defines the file name for the kubeconfig that the kubelet will use to do
  125. // the TLS bootstrap to get itself an unique credential
  126. KubeletBootstrapKubeConfigFileName = "bootstrap-kubelet.conf"
  127. // KubeletKubeConfigFileName defines the file name for the kubeconfig that the control-plane kubelet will use for talking
  128. // to the API server
  129. KubeletKubeConfigFileName = "kubelet.conf"
  130. // ControllerManagerKubeConfigFileName defines the file name for the controller manager's kubeconfig file
  131. ControllerManagerKubeConfigFileName = "controller-manager.conf"
  132. // SchedulerKubeConfigFileName defines the file name for the scheduler's kubeconfig file
  133. SchedulerKubeConfigFileName = "scheduler.conf"
  134. // Some well-known users and groups in the core Kubernetes authorization system
  135. // ControllerManagerUser defines the well-known user the controller-manager should be authenticated as
  136. ControllerManagerUser = "system:kube-controller-manager"
  137. // SchedulerUser defines the well-known user the scheduler should be authenticated as
  138. SchedulerUser = "system:kube-scheduler"
  139. // SystemPrivilegedGroup defines the well-known group for the apiservers. This group is also superuser by default
  140. // (i.e. bound to the cluster-admin ClusterRole)
  141. SystemPrivilegedGroup = "system:masters"
  142. // NodesGroup defines the well-known group for all nodes.
  143. NodesGroup = "system:nodes"
  144. // NodesUserPrefix defines the user name prefix as requested by the Node authorizer.
  145. NodesUserPrefix = "system:node:"
  146. // NodesClusterRoleBinding defines the well-known ClusterRoleBinding which binds the too permissive system:node
  147. // ClusterRole to the system:nodes group. Since kubeadm is using the Node Authorizer, this ClusterRoleBinding's
  148. // system:nodes group subject is removed if present.
  149. NodesClusterRoleBinding = "system:node"
  150. // APICallRetryInterval defines how long kubeadm should wait before retrying a failed API operation
  151. APICallRetryInterval = 500 * time.Millisecond
  152. // DiscoveryRetryInterval specifies how long kubeadm should wait before retrying to connect to the control-plane when doing discovery
  153. DiscoveryRetryInterval = 5 * time.Second
  154. // PatchNodeTimeout specifies how long kubeadm should wait for applying the label and taint on the control-plane before timing out
  155. PatchNodeTimeout = 2 * time.Minute
  156. // TLSBootstrapTimeout specifies how long kubeadm should wait for the kubelet to perform the TLS Bootstrap
  157. TLSBootstrapTimeout = 2 * time.Minute
  158. // PullImageRetry specifies how many times ContainerRuntime retries when pulling image failed
  159. PullImageRetry = 5
  160. // PrepullImagesInParallelTimeout specifies how long kubeadm should wait for prepulling images in parallel before timing out
  161. PrepullImagesInParallelTimeout = 10 * time.Second
  162. // DefaultControlPlaneTimeout specifies the default control plane (actually API Server) timeout for use by kubeadm
  163. DefaultControlPlaneTimeout = 4 * time.Minute
  164. // MinimumAddressesInServiceSubnet defines minimum amount of nodes the Service subnet should allow.
  165. // We need at least ten, because the DNS service is always at the tenth cluster clusterIP
  166. MinimumAddressesInServiceSubnet = 10
  167. // DefaultTokenDuration specifies the default amount of time that a bootstrap token will be valid
  168. // Default behaviour is 24 hours
  169. DefaultTokenDuration = 24 * time.Hour
  170. // DefaultCertTokenDuration specifies the default amount of time that the token used by upload certs will be valid
  171. // Default behaviour is 2 hours
  172. DefaultCertTokenDuration = 2 * time.Hour
  173. // CertificateKeySize specifies the size of the key used to encrypt certificates on uploadcerts phase
  174. CertificateKeySize = 32
  175. // LabelNodeRoleMaster specifies that a node is a control-plane
  176. // This is a duplicate definition of the constant in pkg/controller/service/controller.go
  177. LabelNodeRoleMaster = "node-role.kubernetes.io/master"
  178. // AnnotationKubeadmCRISocket specifies the annotation kubeadm uses to preserve the crisocket information given to kubeadm at
  179. // init/join time for use later. kubeadm annotates the node object with this information
  180. AnnotationKubeadmCRISocket = "kubeadm.alpha.kubernetes.io/cri-socket"
  181. // KubeadmConfigConfigMap specifies in what ConfigMap in the kube-system namespace the `kubeadm init` configuration should be stored
  182. KubeadmConfigConfigMap = "kubeadm-config"
  183. // ClusterConfigurationConfigMapKey specifies in what ConfigMap key the cluster configuration should be stored
  184. ClusterConfigurationConfigMapKey = "ClusterConfiguration"
  185. // ClusterStatusConfigMapKey specifies in what ConfigMap key the cluster status should be stored
  186. ClusterStatusConfigMapKey = "ClusterStatus"
  187. // KubeProxyConfigMap specifies in what ConfigMap in the kube-system namespace the kube-proxy configuration should be stored
  188. KubeProxyConfigMap = "kube-proxy"
  189. // KubeProxyConfigMapKey specifies in what ConfigMap key the component config of kube-proxy should be stored
  190. KubeProxyConfigMapKey = "config.conf"
  191. // KubeletBaseConfigurationConfigMapPrefix specifies in what ConfigMap in the kube-system namespace the initial remote configuration of kubelet should be stored
  192. KubeletBaseConfigurationConfigMapPrefix = "kubelet-config-"
  193. // KubeletBaseConfigurationConfigMapKey specifies in what ConfigMap key the initial remote configuration of kubelet should be stored
  194. KubeletBaseConfigurationConfigMapKey = "kubelet"
  195. // KubeletBaseConfigMapRolePrefix defines the base kubelet configuration ConfigMap.
  196. KubeletBaseConfigMapRolePrefix = "kubeadm:kubelet-config-"
  197. // KubeletRunDirectory specifies the directory where the kubelet runtime information is stored.
  198. KubeletRunDirectory = "/var/lib/kubelet"
  199. // KubeletConfigurationFileName specifies the file name on the node which stores initial remote configuration of kubelet
  200. // This file should exist under KubeletRunDirectory
  201. KubeletConfigurationFileName = "config.yaml"
  202. // DynamicKubeletConfigurationDirectoryName specifies the directory which stores the dynamic configuration checkpoints for the kubelet
  203. // This directory should exist under KubeletRunDirectory
  204. DynamicKubeletConfigurationDirectoryName = "dynamic-config"
  205. // KubeletEnvFileName is a file "kubeadm init" writes at runtime. Using that interface, kubeadm can customize certain
  206. // kubelet flags conditionally based on the environment at runtime. Also, parameters given to the configuration file
  207. // might be passed through this file. "kubeadm init" writes one variable, with the name ${KubeletEnvFileVariableName}.
  208. // This file should exist under KubeletRunDirectory
  209. KubeletEnvFileName = "kubeadm-flags.env"
  210. // KubeletEnvFileVariableName specifies the shell script variable name "kubeadm init" should write a value to in KubeletEnvFile
  211. KubeletEnvFileVariableName = "KUBELET_KUBEADM_ARGS"
  212. // KubeletHealthzPort is the port of the kubelet healthz endpoint
  213. KubeletHealthzPort = 10248
  214. // MinExternalEtcdVersion indicates minimum external etcd version which kubeadm supports
  215. MinExternalEtcdVersion = "3.2.18"
  216. // DefaultEtcdVersion indicates the default etcd version that kubeadm uses
  217. DefaultEtcdVersion = "3.4.3-0"
  218. // PauseVersion indicates the default pause image version for kubeadm
  219. PauseVersion = "3.1"
  220. // Etcd defines variable used internally when referring to etcd component
  221. Etcd = "etcd"
  222. // KubeAPIServer defines variable used internally when referring to kube-apiserver component
  223. KubeAPIServer = "kube-apiserver"
  224. // KubeControllerManager defines variable used internally when referring to kube-controller-manager component
  225. KubeControllerManager = "kube-controller-manager"
  226. // KubeScheduler defines variable used internally when referring to kube-scheduler component
  227. KubeScheduler = "kube-scheduler"
  228. // KubeProxy defines variable used internally when referring to kube-proxy component
  229. KubeProxy = "kube-proxy"
  230. // HyperKube defines variable used internally when referring to the hyperkube image
  231. HyperKube = "hyperkube"
  232. // SelfHostingPrefix describes the prefix workloads that are self-hosted by kubeadm has
  233. SelfHostingPrefix = "self-hosted-"
  234. // KubeCertificatesVolumeName specifies the name for the Volume that is used for injecting certificates to control plane components (can be both a hostPath volume or a projected, all-in-one volume)
  235. KubeCertificatesVolumeName = "k8s-certs"
  236. // KubeConfigVolumeName specifies the name for the Volume that is used for injecting the kubeconfig to talk securely to the api server for a control plane component if applicable
  237. KubeConfigVolumeName = "kubeconfig"
  238. // NodeBootstrapTokenAuthGroup specifies which group a Node Bootstrap Token should be authenticated in
  239. NodeBootstrapTokenAuthGroup = "system:bootstrappers:kubeadm:default-node-token"
  240. // DefaultCIImageRepository points to image registry where CI uploads images from ci-cross build job
  241. DefaultCIImageRepository = "gcr.io/kubernetes-ci-images"
  242. // CoreDNSConfigMap specifies in what ConfigMap in the kube-system namespace the CoreDNS config should be stored
  243. CoreDNSConfigMap = "coredns"
  244. // CoreDNSDeploymentName specifies the name of the Deployment for CoreDNS add-on
  245. CoreDNSDeploymentName = "coredns"
  246. // CoreDNSImageName specifies the name of the image for CoreDNS add-on
  247. CoreDNSImageName = "coredns"
  248. // KubeDNSConfigMap specifies in what ConfigMap in the kube-system namespace the kube-dns config should be stored
  249. KubeDNSConfigMap = "kube-dns"
  250. // KubeDNSDeploymentName specifies the name of the Deployment for kube-dns add-on
  251. KubeDNSDeploymentName = "kube-dns"
  252. // KubeDNSKubeDNSImageName specifies the name of the image for the kubedns container in the kube-dns add-on
  253. KubeDNSKubeDNSImageName = "k8s-dns-kube-dns"
  254. // KubeDNSSidecarImageName specifies the name of the image for the sidecar container in the kube-dns add-on
  255. KubeDNSSidecarImageName = "k8s-dns-sidecar"
  256. // KubeDNSDnsMasqNannyImageName specifies the name of the image for the dnsmasq container in the kube-dns add-on
  257. KubeDNSDnsMasqNannyImageName = "k8s-dns-dnsmasq-nanny"
  258. // AuditPolicyDir is the directory that will contain the audit policy
  259. AuditPolicyDir = "audit"
  260. // AuditPolicyFile is the name of the audit policy file itself
  261. AuditPolicyFile = "audit.yaml"
  262. // StaticPodAuditPolicyLogDir is the name of the directory in the static pod that will have the audit logs
  263. StaticPodAuditPolicyLogDir = "/var/log/kubernetes/audit"
  264. // LeaseEndpointReconcilerType will select a storage based reconciler
  265. // Copied from pkg/master/reconcilers to avoid pulling extra dependencies
  266. // TODO: Import this constant from a consts only package, that does not pull any further dependencies.
  267. LeaseEndpointReconcilerType = "lease"
  268. // KubeDNSVersion is the version of kube-dns to be deployed if it is used
  269. KubeDNSVersion = "1.14.13"
  270. // CoreDNSVersion is the version of CoreDNS to be deployed if it is used
  271. CoreDNSVersion = "1.6.5"
  272. // ClusterConfigurationKind is the string kind value for the ClusterConfiguration struct
  273. ClusterConfigurationKind = "ClusterConfiguration"
  274. // InitConfigurationKind is the string kind value for the InitConfiguration struct
  275. InitConfigurationKind = "InitConfiguration"
  276. // JoinConfigurationKind is the string kind value for the JoinConfiguration struct
  277. JoinConfigurationKind = "JoinConfiguration"
  278. // YAMLDocumentSeparator is the separator for YAML documents
  279. // TODO: Find a better place for this constant
  280. YAMLDocumentSeparator = "---\n"
  281. // DefaultAPIServerBindAddress is the default bind address for the API Server
  282. DefaultAPIServerBindAddress = "0.0.0.0"
  283. // ControlPlaneNumCPU is the number of CPUs required on control-plane
  284. ControlPlaneNumCPU = 2
  285. // KubeadmCertsSecret specifies in what Secret in the kube-system namespace the certificates should be stored
  286. KubeadmCertsSecret = "kubeadm-certs"
  287. // KubeletPort is the default port for the kubelet server on each host machine.
  288. // May be overridden by a flag at startup.
  289. KubeletPort = 10250
  290. // KubeSchedulerPort is the default port for the scheduler status server.
  291. // May be overridden by a flag at startup.
  292. KubeSchedulerPort = 10259
  293. // KubeControllerManagerPort is the default port for the controller manager status server.
  294. // May be overridden by a flag at startup.
  295. KubeControllerManagerPort = 10257
  296. // Mode* constants were copied from pkg/kubeapiserver/authorizer/modes
  297. // to avoid kubeadm dependency on the internal module
  298. // TODO: share Mode* constants in component config
  299. // ModeAlwaysAllow is the mode to set all requests as authorized
  300. ModeAlwaysAllow string = "AlwaysAllow"
  301. // ModeAlwaysDeny is the mode to set no requests as authorized
  302. ModeAlwaysDeny string = "AlwaysDeny"
  303. // ModeABAC is the mode to use Attribute Based Access Control to authorize
  304. ModeABAC string = "ABAC"
  305. // ModeWebhook is the mode to make an external webhook call to authorize
  306. ModeWebhook string = "Webhook"
  307. // ModeRBAC is the mode to use Role Based Access Control to authorize
  308. ModeRBAC string = "RBAC"
  309. // ModeNode is an authorization mode that authorizes API requests made by kubelets.
  310. ModeNode string = "Node"
  311. )
  312. var (
  313. // ControlPlaneTaint is the taint to apply on the PodSpec for being able to run that Pod on the control-plane
  314. ControlPlaneTaint = v1.Taint{
  315. Key: LabelNodeRoleMaster,
  316. Effect: v1.TaintEffectNoSchedule,
  317. }
  318. // ControlPlaneToleration is the toleration to apply on the PodSpec for being able to run that Pod on the control-plane
  319. ControlPlaneToleration = v1.Toleration{
  320. Key: LabelNodeRoleMaster,
  321. Effect: v1.TaintEffectNoSchedule,
  322. }
  323. // DefaultTokenUsages specifies the default functions a token will get
  324. DefaultTokenUsages = bootstrapapi.KnownTokenUsages
  325. // DefaultTokenGroups specifies the default groups that this token will authenticate as when used for authentication
  326. DefaultTokenGroups = []string{NodeBootstrapTokenAuthGroup}
  327. // ControlPlaneComponents defines the control-plane component names
  328. ControlPlaneComponents = []string{KubeAPIServer, KubeControllerManager, KubeScheduler}
  329. // MinimumControlPlaneVersion specifies the minimum control plane version kubeadm can deploy
  330. MinimumControlPlaneVersion = version.MustParseSemantic("v1.16.0")
  331. // MinimumKubeletVersion specifies the minimum version of kubelet which kubeadm supports
  332. MinimumKubeletVersion = version.MustParseSemantic("v1.16.0")
  333. // CurrentKubernetesVersion specifies current Kubernetes version supported by kubeadm
  334. CurrentKubernetesVersion = version.MustParseSemantic("v1.17.0")
  335. // SupportedEtcdVersion lists officially supported etcd versions with corresponding Kubernetes releases
  336. SupportedEtcdVersion = map[uint8]string{
  337. 13: "3.2.24",
  338. 14: "3.3.10",
  339. 15: "3.3.10",
  340. 16: "3.3.17-0",
  341. 17: "3.4.3-0",
  342. 18: "3.4.3-0",
  343. }
  344. // KubeadmCertsClusterRoleName sets the name for the ClusterRole that allows
  345. // the bootstrap tokens to access the kubeadm-certs Secret during the join of a new control-plane
  346. KubeadmCertsClusterRoleName = fmt.Sprintf("kubeadm:%s", KubeadmCertsSecret)
  347. )
  348. // EtcdSupportedVersion returns officially supported version of etcd for a specific Kubernetes release
  349. // if passed version is not listed, the function returns nil and an error
  350. func EtcdSupportedVersion(versionString string) (*version.Version, error) {
  351. kubernetesVersion, err := version.ParseSemantic(versionString)
  352. if err != nil {
  353. return nil, err
  354. }
  355. if etcdStringVersion, ok := SupportedEtcdVersion[uint8(kubernetesVersion.Minor())]; ok {
  356. etcdVersion, err := version.ParseSemantic(etcdStringVersion)
  357. if err != nil {
  358. return nil, err
  359. }
  360. return etcdVersion, nil
  361. }
  362. return nil, errors.Errorf("unsupported or unknown Kubernetes version(%v)", kubernetesVersion)
  363. }
  364. // GetStaticPodDirectory returns the location on the disk where the Static Pod should be present
  365. func GetStaticPodDirectory() string {
  366. return filepath.Join(KubernetesDir, ManifestsSubDirName)
  367. }
  368. // GetStaticPodFilepath returns the location on the disk where the Static Pod should be present
  369. func GetStaticPodFilepath(componentName, manifestsDir string) string {
  370. return filepath.Join(manifestsDir, componentName+".yaml")
  371. }
  372. // GetAdminKubeConfigPath returns the location on the disk where admin kubeconfig is located by default
  373. func GetAdminKubeConfigPath() string {
  374. return filepath.Join(KubernetesDir, AdminKubeConfigFileName)
  375. }
  376. // GetBootstrapKubeletKubeConfigPath returns the location on the disk where bootstrap kubelet kubeconfig is located by default
  377. func GetBootstrapKubeletKubeConfigPath() string {
  378. return filepath.Join(KubernetesDir, KubeletBootstrapKubeConfigFileName)
  379. }
  380. // GetKubeletKubeConfigPath returns the location on the disk where kubelet kubeconfig is located by default
  381. func GetKubeletKubeConfigPath() string {
  382. return filepath.Join(KubernetesDir, KubeletKubeConfigFileName)
  383. }
  384. // AddSelfHostedPrefix adds the self-hosted- prefix to the component name
  385. func AddSelfHostedPrefix(componentName string) string {
  386. return fmt.Sprintf("%s%s", SelfHostingPrefix, componentName)
  387. }
  388. // CreateTempDirForKubeadm is a function that creates a temporary directory under /etc/kubernetes/tmp (not using /tmp as that would potentially be dangerous)
  389. func CreateTempDirForKubeadm(kubernetesDir, dirName string) (string, error) {
  390. tempDir := path.Join(KubernetesDir, TempDirForKubeadm)
  391. if len(kubernetesDir) != 0 {
  392. tempDir = path.Join(kubernetesDir, TempDirForKubeadm)
  393. }
  394. // creates target folder if not already exists
  395. if err := os.MkdirAll(tempDir, 0700); err != nil {
  396. return "", errors.Wrapf(err, "failed to create directory %q", tempDir)
  397. }
  398. tempDir, err := ioutil.TempDir(tempDir, dirName)
  399. if err != nil {
  400. return "", errors.Wrap(err, "couldn't create a temporary directory")
  401. }
  402. return tempDir, nil
  403. }
  404. // CreateTimestampDirForKubeadm is a function that creates a temporary directory under /etc/kubernetes/tmp formatted with the current date
  405. func CreateTimestampDirForKubeadm(kubernetesDir, dirName string) (string, error) {
  406. tempDir := path.Join(KubernetesDir, TempDirForKubeadm)
  407. if len(kubernetesDir) != 0 {
  408. tempDir = path.Join(kubernetesDir, TempDirForKubeadm)
  409. }
  410. // creates target folder if not already exists
  411. if err := os.MkdirAll(tempDir, 0700); err != nil {
  412. return "", errors.Wrapf(err, "failed to create directory %q", tempDir)
  413. }
  414. timestampDirName := fmt.Sprintf("%s-%s", dirName, time.Now().Format("2006-01-02-15-04-05"))
  415. timestampDir := path.Join(tempDir, timestampDirName)
  416. if err := os.Mkdir(timestampDir, 0700); err != nil {
  417. return "", errors.Wrap(err, "could not create timestamp directory")
  418. }
  419. return timestampDir, nil
  420. }
  421. // GetDNSIP returns a dnsIP, which is 10th IP in svcSubnet CIDR range
  422. func GetDNSIP(svcSubnetList string, isDualStack bool) (net.IP, error) {
  423. // Get the service subnet CIDR
  424. svcSubnetCIDR, err := GetKubernetesServiceCIDR(svcSubnetList, isDualStack)
  425. if err != nil {
  426. return nil, errors.Wrapf(err, "unable to get internal Kubernetes Service IP from the given service CIDR (%s)", svcSubnetList)
  427. }
  428. // Selects the 10th IP in service subnet CIDR range as dnsIP
  429. dnsIP, err := utilnet.GetIndexedIP(svcSubnetCIDR, 10)
  430. if err != nil {
  431. return nil, errors.Wrap(err, "unable to get internal Kubernetes Service IP from the given service CIDR")
  432. }
  433. return dnsIP, nil
  434. }
  435. // GetKubernetesServiceCIDR returns the default Service CIDR for the Kubernetes internal service
  436. func GetKubernetesServiceCIDR(svcSubnetList string, isDualStack bool) (*net.IPNet, error) {
  437. if isDualStack {
  438. // The default service address family for the cluster is the address family of the first
  439. // service cluster IP range configured via the `--service-cluster-ip-range` flag
  440. // of the kube-controller-manager and kube-apiserver.
  441. svcSubnets, err := utilnet.ParseCIDRs(strings.Split(svcSubnetList, ","))
  442. if err != nil {
  443. return nil, errors.Wrapf(err, "unable to parse ServiceSubnet %v", svcSubnetList)
  444. }
  445. if len(svcSubnets) == 0 {
  446. return nil, errors.New("received empty ServiceSubnet for dual-stack")
  447. }
  448. return svcSubnets[0], nil
  449. }
  450. // internal IP address for the API server
  451. _, svcSubnet, err := net.ParseCIDR(svcSubnetList)
  452. if err != nil {
  453. return nil, errors.Wrapf(err, "unable to parse ServiceSubnet %v", svcSubnetList)
  454. }
  455. return svcSubnet, nil
  456. }
  457. // GetAPIServerVirtualIP returns the IP of the internal Kubernetes API service
  458. func GetAPIServerVirtualIP(svcSubnetList string, isDualStack bool) (net.IP, error) {
  459. svcSubnet, err := GetKubernetesServiceCIDR(svcSubnetList, isDualStack)
  460. if err != nil {
  461. return nil, errors.Wrap(err, "unable to get internal Kubernetes Service IP from the given service CIDR")
  462. }
  463. internalAPIServerVirtualIP, err := utilnet.GetIndexedIP(svcSubnet, 1)
  464. if err != nil {
  465. return nil, errors.Wrapf(err, "unable to get the first IP address from the given CIDR: %s", svcSubnet.String())
  466. }
  467. return internalAPIServerVirtualIP, nil
  468. }
  469. // GetStaticPodAuditPolicyFile returns the path to the audit policy file within a static pod
  470. func GetStaticPodAuditPolicyFile() string {
  471. return filepath.Join(KubernetesDir, AuditPolicyDir, AuditPolicyFile)
  472. }
  473. // GetDNSVersion is a handy function that returns the DNS version by DNS type
  474. func GetDNSVersion(dnsType kubeadmapi.DNSAddOnType) string {
  475. switch dnsType {
  476. case kubeadmapi.KubeDNS:
  477. return KubeDNSVersion
  478. default:
  479. return CoreDNSVersion
  480. }
  481. }
  482. // GetKubeletConfigMapName returns the right ConfigMap name for the right branch of k8s
  483. func GetKubeletConfigMapName(k8sVersion *version.Version) string {
  484. return fmt.Sprintf("%s%d.%d", KubeletBaseConfigurationConfigMapPrefix, k8sVersion.Major(), k8sVersion.Minor())
  485. }