flags.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 kubelet
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "os"
  18. "path/filepath"
  19. "strings"
  20. "github.com/pkg/errors"
  21. "k8s.io/klog"
  22. kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
  23. "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  24. "k8s.io/kubernetes/cmd/kubeadm/app/images"
  25. kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
  26. nodeutil "k8s.io/kubernetes/pkg/util/node"
  27. "k8s.io/kubernetes/pkg/util/procfs"
  28. utilsexec "k8s.io/utils/exec"
  29. )
  30. type kubeletFlagsOpts struct {
  31. nodeRegOpts *kubeadmapi.NodeRegistrationOptions
  32. featureGates map[string]bool
  33. pauseImage string
  34. registerTaintsUsingFlags bool
  35. execer utilsexec.Interface
  36. pidOfFunc func(string) ([]int, error)
  37. defaultHostname string
  38. }
  39. // WriteKubeletDynamicEnvFile writes an environment file with dynamic flags to the kubelet.
  40. // Used at "kubeadm init" and "kubeadm join" time.
  41. func WriteKubeletDynamicEnvFile(cfg *kubeadmapi.ClusterConfiguration, nodeReg *kubeadmapi.NodeRegistrationOptions, registerTaintsUsingFlags bool, kubeletDir string) error {
  42. hostName, err := nodeutil.GetHostname("")
  43. if err != nil {
  44. return err
  45. }
  46. flagOpts := kubeletFlagsOpts{
  47. nodeRegOpts: nodeReg,
  48. featureGates: cfg.FeatureGates,
  49. pauseImage: images.GetPauseImage(cfg),
  50. registerTaintsUsingFlags: registerTaintsUsingFlags,
  51. execer: utilsexec.New(),
  52. pidOfFunc: procfs.PidOf,
  53. defaultHostname: hostName,
  54. }
  55. stringMap := buildKubeletArgMap(flagOpts)
  56. argList := kubeadmutil.BuildArgumentListFromMap(stringMap, nodeReg.KubeletExtraArgs)
  57. envFileContent := fmt.Sprintf("%s=%q\n", constants.KubeletEnvFileVariableName, strings.Join(argList, " "))
  58. return writeKubeletFlagBytesToDisk([]byte(envFileContent), kubeletDir)
  59. }
  60. // buildKubeletArgMap takes a kubeletFlagsOpts object and builds based on that a string-string map with flags
  61. // that should be given to the local kubelet daemon.
  62. func buildKubeletArgMap(opts kubeletFlagsOpts) map[string]string {
  63. kubeletFlags := map[string]string{}
  64. if opts.nodeRegOpts.CRISocket == constants.DefaultDockerCRISocket {
  65. // These flags should only be set when running docker
  66. kubeletFlags["network-plugin"] = "cni"
  67. driver, err := kubeadmutil.GetCgroupDriverDocker(opts.execer)
  68. if err != nil {
  69. klog.Warningf("cannot automatically assign a '--cgroup-driver' value when starting the Kubelet: %v\n", err)
  70. } else {
  71. kubeletFlags["cgroup-driver"] = driver
  72. }
  73. if opts.pauseImage != "" {
  74. kubeletFlags["pod-infra-container-image"] = opts.pauseImage
  75. }
  76. } else {
  77. kubeletFlags["container-runtime"] = "remote"
  78. kubeletFlags["container-runtime-endpoint"] = opts.nodeRegOpts.CRISocket
  79. }
  80. if opts.registerTaintsUsingFlags && opts.nodeRegOpts.Taints != nil && len(opts.nodeRegOpts.Taints) > 0 {
  81. taintStrs := []string{}
  82. for _, taint := range opts.nodeRegOpts.Taints {
  83. taintStrs = append(taintStrs, taint.ToString())
  84. }
  85. kubeletFlags["register-with-taints"] = strings.Join(taintStrs, ",")
  86. }
  87. if pids, _ := opts.pidOfFunc("systemd-resolved"); len(pids) > 0 {
  88. // procfs.PidOf only returns an error if the regex is empty or doesn't compile, so we can ignore it
  89. kubeletFlags["resolv-conf"] = "/run/systemd/resolve/resolv.conf"
  90. }
  91. // Make sure the node name we're passed will work with Kubelet
  92. if opts.nodeRegOpts.Name != "" && opts.nodeRegOpts.Name != opts.defaultHostname {
  93. klog.V(1).Infof("setting kubelet hostname-override to %q", opts.nodeRegOpts.Name)
  94. kubeletFlags["hostname-override"] = opts.nodeRegOpts.Name
  95. }
  96. // TODO: Conditionally set `--cgroup-driver` to either `systemd` or `cgroupfs` for CRI other than Docker
  97. return kubeletFlags
  98. }
  99. // writeKubeletFlagBytesToDisk writes a byte slice down to disk at the specific location of the kubelet flag overrides file
  100. func writeKubeletFlagBytesToDisk(b []byte, kubeletDir string) error {
  101. kubeletEnvFilePath := filepath.Join(kubeletDir, constants.KubeletEnvFileName)
  102. fmt.Printf("[kubelet-start] Writing kubelet environment file with flags to file %q\n", kubeletEnvFilePath)
  103. // creates target folder if not already exists
  104. if err := os.MkdirAll(kubeletDir, 0700); err != nil {
  105. return errors.Wrapf(err, "failed to create directory %q", kubeletDir)
  106. }
  107. if err := ioutil.WriteFile(kubeletEnvFilePath, b, 0644); err != nil {
  108. return errors.Wrapf(err, "failed to write kubelet configuration to the file %q", kubeletEnvFilePath)
  109. }
  110. return nil
  111. }