config.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. Copyright 2017 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 kubelet
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "os"
  18. "path/filepath"
  19. "github.com/pkg/errors"
  20. v1 "k8s.io/api/core/v1"
  21. rbac "k8s.io/api/rbac/v1"
  22. apierrors "k8s.io/apimachinery/pkg/api/errors"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. "k8s.io/apimachinery/pkg/util/version"
  25. clientset "k8s.io/client-go/kubernetes"
  26. kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
  27. "k8s.io/kubernetes/cmd/kubeadm/app/componentconfigs"
  28. kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  29. "k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
  30. )
  31. // WriteConfigToDisk writes the kubelet config object down to a file
  32. // Used at "kubeadm init" and "kubeadm upgrade" time
  33. func WriteConfigToDisk(kubeletCfg kubeadmapi.ComponentConfig, kubeletDir string) error {
  34. kubeletBytes, err := kubeletCfg.Marshal()
  35. if err != nil {
  36. return err
  37. }
  38. return writeConfigBytesToDisk(kubeletBytes, kubeletDir)
  39. }
  40. // CreateConfigMap creates a ConfigMap with the generic kubelet configuration.
  41. // Used at "kubeadm init" and "kubeadm upgrade" time
  42. func CreateConfigMap(cfg *kubeadmapi.ClusterConfiguration, client clientset.Interface) error {
  43. k8sVersion, err := version.ParseSemantic(cfg.KubernetesVersion)
  44. if err != nil {
  45. return err
  46. }
  47. configMapName := kubeadmconstants.GetKubeletConfigMapName(k8sVersion)
  48. fmt.Printf("[kubelet] Creating a ConfigMap %q in namespace %s with the configuration for the kubelets in the cluster\n", configMapName, metav1.NamespaceSystem)
  49. kubeletCfg, ok := cfg.ComponentConfigs[componentconfigs.KubeletGroup]
  50. if !ok {
  51. return errors.New("no kubelet component config found in the active component config set")
  52. }
  53. kubeletBytes, err := kubeletCfg.Marshal()
  54. if err != nil {
  55. return err
  56. }
  57. if err := apiclient.CreateOrUpdateConfigMap(client, &v1.ConfigMap{
  58. ObjectMeta: metav1.ObjectMeta{
  59. Name: configMapName,
  60. Namespace: metav1.NamespaceSystem,
  61. },
  62. Data: map[string]string{
  63. kubeadmconstants.KubeletBaseConfigurationConfigMapKey: string(kubeletBytes),
  64. },
  65. }); err != nil {
  66. return err
  67. }
  68. if err := createConfigMapRBACRules(client, k8sVersion); err != nil {
  69. return errors.Wrap(err, "error creating kubelet configuration configmap RBAC rules")
  70. }
  71. return nil
  72. }
  73. // createConfigMapRBACRules creates the RBAC rules for exposing the base kubelet ConfigMap in the kube-system namespace to unauthenticated users
  74. func createConfigMapRBACRules(client clientset.Interface, k8sVersion *version.Version) error {
  75. if err := apiclient.CreateOrUpdateRole(client, &rbac.Role{
  76. ObjectMeta: metav1.ObjectMeta{
  77. Name: configMapRBACName(k8sVersion),
  78. Namespace: metav1.NamespaceSystem,
  79. },
  80. Rules: []rbac.PolicyRule{
  81. {
  82. Verbs: []string{"get"},
  83. APIGroups: []string{""},
  84. Resources: []string{"configmaps"},
  85. ResourceNames: []string{kubeadmconstants.GetKubeletConfigMapName(k8sVersion)},
  86. },
  87. },
  88. }); err != nil {
  89. return err
  90. }
  91. return apiclient.CreateOrUpdateRoleBinding(client, &rbac.RoleBinding{
  92. ObjectMeta: metav1.ObjectMeta{
  93. Name: configMapRBACName(k8sVersion),
  94. Namespace: metav1.NamespaceSystem,
  95. },
  96. RoleRef: rbac.RoleRef{
  97. APIGroup: rbac.GroupName,
  98. Kind: "Role",
  99. Name: configMapRBACName(k8sVersion),
  100. },
  101. Subjects: []rbac.Subject{
  102. {
  103. Kind: rbac.GroupKind,
  104. Name: kubeadmconstants.NodesGroup,
  105. },
  106. {
  107. Kind: rbac.GroupKind,
  108. Name: kubeadmconstants.NodeBootstrapTokenAuthGroup,
  109. },
  110. },
  111. })
  112. }
  113. // DownloadConfig downloads the kubelet configuration from a ConfigMap and writes it to disk.
  114. // Used at "kubeadm join" time
  115. func DownloadConfig(client clientset.Interface, kubeletVersion *version.Version, kubeletDir string) error {
  116. // Download the ConfigMap from the cluster based on what version the kubelet is
  117. configMapName := kubeadmconstants.GetKubeletConfigMapName(kubeletVersion)
  118. fmt.Printf("[kubelet-start] Downloading configuration for the kubelet from the %q ConfigMap in the %s namespace\n",
  119. configMapName, metav1.NamespaceSystem)
  120. kubeletCfg, err := apiclient.GetConfigMapWithRetry(client, metav1.NamespaceSystem, configMapName)
  121. // If the ConfigMap wasn't found and the kubelet version is v1.10.x, where we didn't support the config file yet
  122. // just return, don't error out
  123. if apierrors.IsNotFound(err) && kubeletVersion.Minor() == 10 {
  124. return nil
  125. }
  126. if err != nil {
  127. return err
  128. }
  129. return writeConfigBytesToDisk([]byte(kubeletCfg.Data[kubeadmconstants.KubeletBaseConfigurationConfigMapKey]), kubeletDir)
  130. }
  131. // configMapRBACName returns the name for the Role/RoleBinding for the kubelet config configmap for the right branch of k8s
  132. func configMapRBACName(k8sVersion *version.Version) string {
  133. return fmt.Sprintf("%s%d.%d", kubeadmconstants.KubeletBaseConfigMapRolePrefix, k8sVersion.Major(), k8sVersion.Minor())
  134. }
  135. // writeConfigBytesToDisk writes a byte slice down to disk at the specific location of the kubelet config file
  136. func writeConfigBytesToDisk(b []byte, kubeletDir string) error {
  137. configFile := filepath.Join(kubeletDir, kubeadmconstants.KubeletConfigurationFileName)
  138. fmt.Printf("[kubelet-start] Writing kubelet configuration to file %q\n", configFile)
  139. // creates target folder if not already exists
  140. if err := os.MkdirAll(kubeletDir, 0700); err != nil {
  141. return errors.Wrapf(err, "failed to create directory %q", kubeletDir)
  142. }
  143. if err := ioutil.WriteFile(configFile, b, 0644); err != nil {
  144. return errors.Wrapf(err, "failed to write kubelet configuration to the file %q", configFile)
  145. }
  146. return nil
  147. }