cleanup kubelet config file: kubeadm-flags.env to remove container-runtime flag

This commit is contained in:
Paco Xu 2022-08-25 16:57:22 +08:00
parent 5974da784c
commit f9643b69f3
3 changed files with 58 additions and 25 deletions

View File

@ -86,6 +86,12 @@ func runKubeletConfigPhase() func(c workflow.RunData) error {
return nil return nil
} }
// TODO: Temporary workaround. Remove in 1.27:
// https://github.com/kubernetes/kubeadm/issues/2626
if err := upgrade.CleanupKubeletDynamicEnvFileContainerRuntime(dryRun); err != nil {
return err
}
fmt.Println("[upgrade] The configuration for this node was successfully updated!") fmt.Println("[upgrade] The configuration for this node was successfully updated!")
fmt.Println("[upgrade] Now you should go ahead and upgrade the kubelet package using your package manager.") fmt.Println("[upgrade] Now you should go ahead and upgrade the kubelet package using your package manager.")
return nil return nil

View File

@ -20,7 +20,9 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath"
"strings" "strings"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -34,7 +36,6 @@ import (
"k8s.io/klog/v2" "k8s.io/klog/v2"
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants" kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
"k8s.io/kubernetes/cmd/kubeadm/app/phases/addons/dns" "k8s.io/kubernetes/cmd/kubeadm/app/phases/addons/dns"
"k8s.io/kubernetes/cmd/kubeadm/app/phases/addons/proxy" "k8s.io/kubernetes/cmd/kubeadm/app/phases/addons/proxy"
@ -69,6 +70,12 @@ func PerformPostUpgradeTasks(client clientset.Interface, cfg *kubeadmapi.InitCon
errs = append(errs, err) errs = append(errs, err)
} }
// TODO: Temporary workaround. Remove in 1.27:
// https://github.com/kubernetes/kubeadm/issues/2626
if err := CleanupKubeletDynamicEnvFileContainerRuntime(dryRun); err != nil {
return err
}
// Annotate the node with the crisocket information, sourced either from the InitConfiguration struct or // Annotate the node with the crisocket information, sourced either from the InitConfiguration struct or
// --cri-socket. // --cri-socket.
// TODO: In the future we want to use something more official like NodeStatus or similar for detecting this properly // TODO: In the future we want to use something more official like NodeStatus or similar for detecting this properly
@ -247,10 +254,32 @@ func RemoveOldControlPlaneTaint(client clientset.Interface) error {
return nil return nil
} }
func updateKubeletDynamicEnvFileWithURLScheme(str string) string { // CleanupKubeletDynamicEnvFileContainerRuntime reads the kubelet dynamic environment file
// from disk, ensure that the container runtime flag is removed.
// TODO: Temporary workaround. Remove in 1.27:
// https://github.com/kubernetes/kubeadm/issues/2626
func CleanupKubeletDynamicEnvFileContainerRuntime(dryRun bool) error {
filePath := filepath.Join(kubeadmconstants.KubeletRunDirectory, kubeadmconstants.KubeletEnvFileName)
if dryRun {
fmt.Printf("[dryrun] Would ensure that %q does not include a --container-runtime flag\n", filePath)
return nil
}
klog.V(2).Infof("Ensuring that %q does not include a --container-runtime flag", filePath)
bytes, err := ioutil.ReadFile(filePath)
if err != nil {
return errors.Wrapf(err, "failed to read kubelet configuration from file %q", filePath)
}
updated := cleanupKubeletDynamicEnvFileContainerRuntime(string(bytes))
if err := ioutil.WriteFile(filePath, []byte(updated), 0644); err != nil {
return errors.Wrapf(err, "failed to write kubelet configuration to the file %q", filePath)
}
return nil
}
func cleanupKubeletDynamicEnvFileContainerRuntime(str string) string {
const ( const (
flag = "container-runtime-endpoint" // `remote` is the only possible value
scheme = kubeadmapiv1.DefaultContainerRuntimeURLScheme + "://" flag = "container-runtime"
) )
// Trim the prefix // Trim the prefix
str = strings.TrimLeft(str, fmt.Sprintf("%s=\"", kubeadmconstants.KubeletEnvFileVariableName)) str = strings.TrimLeft(str, fmt.Sprintf("%s=\"", kubeadmconstants.KubeletEnvFileVariableName))
@ -267,22 +296,21 @@ func updateKubeletDynamicEnvFileWithURLScheme(str string) string {
if len(keyValue) < 2 { if len(keyValue) < 2 {
// Post init/join, the user may have edited the file and has flags that are not // Post init/join, the user may have edited the file and has flags that are not
// followed by "=". If that is the case the next argument must be the value // followed by "=". If that is the case the next argument must be the value
// of the endpoint flag and if its not a flag itself. Update that argument with // of the endpoint flag and if its not a flag itself.
// the scheme instead.
if i+1 < len(split) { if i+1 < len(split) {
nextArg := split[i+1] nextArg := split[i+1]
if !strings.HasPrefix(nextArg, "-") && !strings.HasPrefix(nextArg, scheme) { if strings.HasPrefix(nextArg, "-") {
split[i+1] = scheme + nextArg // remove the flag only
split = append(split[:i], split[i+1:]...)
} else {
// remove the flag and value
split = append(split[:i], split[i+2:]...)
} }
} }
continue continue
} }
if len(keyValue[1]) == 0 || strings.HasPrefix(keyValue[1], scheme) { // remove the flag and value in one
continue // The flag value already has the URL scheme prefix or is empty split = append(split[:i], split[i+1:]...)
}
// Missing prefix. Add it and update the key=value pair
keyValue[1] = scheme + keyValue[1]
split[i] = strings.Join(keyValue, "=")
} }
str = strings.Join(split, " ") str = strings.Join(split, " ")
return fmt.Sprintf("%s=\"%s", kubeadmconstants.KubeletEnvFileVariableName, str) return fmt.Sprintf("%s=\"%s", kubeadmconstants.KubeletEnvFileVariableName, str)

View File

@ -25,7 +25,6 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
"k8s.io/kubernetes/cmd/kubeadm/app/constants" "k8s.io/kubernetes/cmd/kubeadm/app/constants"
testutil "k8s.io/kubernetes/cmd/kubeadm/test" testutil "k8s.io/kubernetes/cmd/kubeadm/test"
) )
@ -104,7 +103,7 @@ func TestRollbackFiles(t *testing.T) {
} }
} }
func TestUpdateKubeletDynamicEnvFileWithURLScheme(t *testing.T) { func TestCleanupKubeletDynamicEnvFileContainerRuntime(t *testing.T) {
tcases := []struct { tcases := []struct {
name string name string
input string input string
@ -117,28 +116,28 @@ func TestUpdateKubeletDynamicEnvFileWithURLScheme(t *testing.T) {
}, },
{ {
name: "add missing URL scheme", name: "add missing URL scheme",
input: fmt.Sprintf("%s=\"--foo=abc --container-runtime-endpoint=/some/endpoint --bar=def\"", constants.KubeletEnvFileVariableName), input: fmt.Sprintf("%s=\"--foo=abc --container-runtime=remote --bar=def\"", constants.KubeletEnvFileVariableName),
expected: fmt.Sprintf("%s=\"--foo=abc --container-runtime-endpoint=%s:///some/endpoint --bar=def\"", constants.KubeletEnvFileVariableName, kubeadmapiv1.DefaultContainerRuntimeURLScheme), expected: fmt.Sprintf("%s=\"--foo=abc --bar=def\"", constants.KubeletEnvFileVariableName),
}, },
{ {
name: "add missing URL scheme if there is no '=' after the flag name", name: "add missing URL scheme if there is no '=' after the flag name",
input: fmt.Sprintf("%s=\"--foo=abc --container-runtime-endpoint /some/endpoint --bar=def\"", constants.KubeletEnvFileVariableName), input: fmt.Sprintf("%s=\"--foo=abc --container-runtime remote --bar=def\"", constants.KubeletEnvFileVariableName),
expected: fmt.Sprintf("%s=\"--foo=abc --container-runtime-endpoint %s:///some/endpoint --bar=def\"", constants.KubeletEnvFileVariableName, kubeadmapiv1.DefaultContainerRuntimeURLScheme), expected: fmt.Sprintf("%s=\"--foo=abc --bar=def\"", constants.KubeletEnvFileVariableName),
}, },
{ {
name: "empty flag of interest value following '='", name: "empty flag of interest value following '='",
input: fmt.Sprintf("%s=\"--foo=abc --container-runtime-endpoint= --bar=def\"", constants.KubeletEnvFileVariableName), input: fmt.Sprintf("%s=\"--foo=abc --container-runtime= --bar=def\"", constants.KubeletEnvFileVariableName),
expected: fmt.Sprintf("%s=\"--foo=abc --container-runtime-endpoint= --bar=def\"", constants.KubeletEnvFileVariableName), expected: fmt.Sprintf("%s=\"--foo=abc --bar=def\"", constants.KubeletEnvFileVariableName),
}, },
{ {
name: "empty flag of interest value without '='", name: "empty flag of interest value without '='",
input: fmt.Sprintf("%s=\"--foo=abc --container-runtime-endpoint --bar=def\"", constants.KubeletEnvFileVariableName), input: fmt.Sprintf("%s=\"--foo=abc --container-runtime --bar=def\"", constants.KubeletEnvFileVariableName),
expected: fmt.Sprintf("%s=\"--foo=abc --container-runtime-endpoint --bar=def\"", constants.KubeletEnvFileVariableName), expected: fmt.Sprintf("%s=\"--foo=abc --bar=def\"", constants.KubeletEnvFileVariableName),
}, },
} }
for _, tt := range tcases { for _, tt := range tcases {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
output := updateKubeletDynamicEnvFileWithURLScheme(tt.input) output := cleanupKubeletDynamicEnvFileContainerRuntime(tt.input)
if output != tt.expected { if output != tt.expected {
t.Errorf("expected output: %q, got: %q", tt.expected, output) t.Errorf("expected output: %q, got: %q", tt.expected, output)
} }