
If a Node name in the cluster is already taken and this Node is Ready, prevent TLS bootsrap on "kubeadm join" and exit early. This change requires that a new ClusterRole is granted to the "system:bootstrappers:kubeadm:default-node-token" group to be able get Nodes in the cluster. The same group already has access to obtain objects such as the KubeletConfiguration and kubeadm's ClusterConfiguration. The motivation of this change is to prevent undefined behavior and the potential control-plane breakdown if such a cluster is racing to have two nodes with the same name for long periods of time. The following values are validated in the following precedence from lower to higher: - actual hostname - NodeRegistration.Name (or "--node-name") from JoinConfiguration - "--hostname-override" passed via kubeletExtraArgs If the user decides to not let kubeadm know about a custom node name and to instead override the hostname from a kubelet systemd unit file, kubeadm will not be able to detect the problem.
162 lines
6.1 KiB
Go
162 lines
6.1 KiB
Go
/*
|
|
Copyright 2018 The Kubernetes Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package kubelet
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/pkg/errors"
|
|
"k8s.io/klog"
|
|
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
|
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
|
|
"k8s.io/kubernetes/cmd/kubeadm/app/features"
|
|
"k8s.io/kubernetes/cmd/kubeadm/app/images"
|
|
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
|
|
"k8s.io/kubernetes/cmd/kubeadm/app/util/initsystem"
|
|
utilsexec "k8s.io/utils/exec"
|
|
)
|
|
|
|
type kubeletFlagsOpts struct {
|
|
nodeRegOpts *kubeadmapi.NodeRegistrationOptions
|
|
featureGates map[string]bool
|
|
pauseImage string
|
|
registerTaintsUsingFlags bool
|
|
execer utilsexec.Interface
|
|
isServiceActiveFunc func(string) (bool, error)
|
|
}
|
|
|
|
// GetNodeNameAndHostname obtains the name for this Node using the following precedence
|
|
// (from lower to higher):
|
|
// - actual hostname
|
|
// - NodeRegistrationOptions.Name (same as "--node-name" passed to "kubeadm init/join")
|
|
// - "hostname-overide" flag in NodeRegistrationOptions.KubeletExtraArgs
|
|
// It also returns the hostname or an error if getting the hostname failed.
|
|
func GetNodeNameAndHostname(cfg *kubeadmapi.NodeRegistrationOptions) (string, string, error) {
|
|
hostname, err := kubeadmutil.GetHostname("")
|
|
nodeName := hostname
|
|
if cfg.Name != "" {
|
|
nodeName = cfg.Name
|
|
}
|
|
if name, ok := cfg.KubeletExtraArgs["hostname-override"]; ok {
|
|
nodeName = name
|
|
}
|
|
return nodeName, hostname, err
|
|
}
|
|
|
|
// WriteKubeletDynamicEnvFile writes an environment file with dynamic flags to the kubelet.
|
|
// Used at "kubeadm init" and "kubeadm join" time.
|
|
func WriteKubeletDynamicEnvFile(cfg *kubeadmapi.ClusterConfiguration, nodeReg *kubeadmapi.NodeRegistrationOptions, registerTaintsUsingFlags bool, kubeletDir string) error {
|
|
flagOpts := kubeletFlagsOpts{
|
|
nodeRegOpts: nodeReg,
|
|
featureGates: cfg.FeatureGates,
|
|
pauseImage: images.GetPauseImage(cfg),
|
|
registerTaintsUsingFlags: registerTaintsUsingFlags,
|
|
execer: utilsexec.New(),
|
|
isServiceActiveFunc: func(name string) (bool, error) {
|
|
initSystem, err := initsystem.GetInitSystem()
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return initSystem.ServiceIsActive(name), nil
|
|
},
|
|
}
|
|
stringMap := buildKubeletArgMap(flagOpts)
|
|
argList := kubeadmutil.BuildArgumentListFromMap(stringMap, nodeReg.KubeletExtraArgs)
|
|
envFileContent := fmt.Sprintf("%s=%q\n", constants.KubeletEnvFileVariableName, strings.Join(argList, " "))
|
|
|
|
return writeKubeletFlagBytesToDisk([]byte(envFileContent), kubeletDir)
|
|
}
|
|
|
|
// buildKubeletArgMap takes a kubeletFlagsOpts object and builds based on that a string-string map with flags
|
|
// that should be given to the local kubelet daemon.
|
|
func buildKubeletArgMap(opts kubeletFlagsOpts) map[string]string {
|
|
kubeletFlags := map[string]string{}
|
|
|
|
if opts.nodeRegOpts.CRISocket == constants.DefaultDockerCRISocket {
|
|
// These flags should only be set when running docker
|
|
kubeletFlags["network-plugin"] = "cni"
|
|
driver, err := kubeadmutil.GetCgroupDriverDocker(opts.execer)
|
|
if err != nil {
|
|
klog.Warningf("cannot automatically assign a '--cgroup-driver' value when starting the Kubelet: %v\n", err)
|
|
} else {
|
|
kubeletFlags["cgroup-driver"] = driver
|
|
}
|
|
if opts.pauseImage != "" {
|
|
kubeletFlags["pod-infra-container-image"] = opts.pauseImage
|
|
}
|
|
} else {
|
|
kubeletFlags["container-runtime"] = "remote"
|
|
kubeletFlags["container-runtime-endpoint"] = opts.nodeRegOpts.CRISocket
|
|
}
|
|
|
|
if opts.registerTaintsUsingFlags && opts.nodeRegOpts.Taints != nil && len(opts.nodeRegOpts.Taints) > 0 {
|
|
taintStrs := []string{}
|
|
for _, taint := range opts.nodeRegOpts.Taints {
|
|
taintStrs = append(taintStrs, taint.ToString())
|
|
}
|
|
|
|
kubeletFlags["register-with-taints"] = strings.Join(taintStrs, ",")
|
|
}
|
|
|
|
ok, err := opts.isServiceActiveFunc("systemd-resolved")
|
|
if err != nil {
|
|
klog.Warningf("cannot determine if systemd-resolved is active: %v\n", err)
|
|
}
|
|
if ok {
|
|
kubeletFlags["resolv-conf"] = "/run/systemd/resolve/resolv.conf"
|
|
}
|
|
|
|
// Pass the "--hostname-override" flag to the kubelet only if it's different from the hostname
|
|
nodeName, hostname, err := GetNodeNameAndHostname(opts.nodeRegOpts)
|
|
if err != nil {
|
|
klog.Warning(err)
|
|
}
|
|
if nodeName != hostname {
|
|
klog.V(1).Infof("setting kubelet hostname-override to %q", nodeName)
|
|
kubeletFlags["hostname-override"] = nodeName
|
|
}
|
|
|
|
// TODO: Conditionally set `--cgroup-driver` to either `systemd` or `cgroupfs` for CRI other than Docker
|
|
|
|
// TODO: The following code should be removed after dual-stack is GA.
|
|
// Note: The user still retains the ability to explicitly set feature-gates and that value will overwrite this base value.
|
|
if enabled, present := opts.featureGates[features.IPv6DualStack]; present {
|
|
kubeletFlags["feature-gates"] = fmt.Sprintf("%s=%t", features.IPv6DualStack, enabled)
|
|
}
|
|
|
|
return kubeletFlags
|
|
}
|
|
|
|
// writeKubeletFlagBytesToDisk writes a byte slice down to disk at the specific location of the kubelet flag overrides file
|
|
func writeKubeletFlagBytesToDisk(b []byte, kubeletDir string) error {
|
|
kubeletEnvFilePath := filepath.Join(kubeletDir, constants.KubeletEnvFileName)
|
|
fmt.Printf("[kubelet-start] Writing kubelet environment file with flags to file %q\n", kubeletEnvFilePath)
|
|
|
|
// creates target folder if not already exists
|
|
if err := os.MkdirAll(kubeletDir, 0700); err != nil {
|
|
return errors.Wrapf(err, "failed to create directory %q", kubeletDir)
|
|
}
|
|
if err := ioutil.WriteFile(kubeletEnvFilePath, b, 0644); err != nil {
|
|
return errors.Wrapf(err, "failed to write kubelet configuration to the file %q", kubeletEnvFilePath)
|
|
}
|
|
return nil
|
|
}
|