config.go 6.0 KB

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