constants.go 24 KB

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