kubeadm: cleanup the temporary workaround about kubelet --container-runtime flag

This commit is contained in:
SataQiu 2022-12-11 15:52:12 +08:00
parent f1f2d51891
commit 76bb3364d2
3 changed files with 0 additions and 127 deletions

View File

@ -86,12 +86,6 @@ func runKubeletConfigPhase() func(c workflow.RunData) error {
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] Now you should go ahead and upgrade the kubelet package using your package manager.")
return nil

View File

@ -18,11 +18,8 @@ package upgrade
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
@ -66,12 +63,6 @@ func PerformPostUpgradeTasks(client clientset.Interface, cfg *kubeadmapi.InitCon
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
// --cri-socket.
// TODO: In the future we want to use something more official like NodeStatus or similar for detecting this properly
@ -210,67 +201,3 @@ func rollbackFiles(files map[string]string, originalErr error) error {
}
return errors.Errorf("couldn't move these files: %v. Got errors: %v", files, errorsutil.NewAggregate(errs))
}
// 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 := os.ReadFile(filePath)
if err != nil {
return errors.Wrapf(err, "failed to read kubelet configuration from file %q", filePath)
}
updated := cleanupKubeletDynamicEnvFileContainerRuntime(string(bytes))
if err := os.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 (
// `remote` is the only possible value
containerRuntimeFlag = "container-runtime"
endpointFlag = "container-runtime-endpoint"
)
// Trim the prefix
str = strings.TrimLeft(str, fmt.Sprintf("%s=\"", kubeadmconstants.KubeletEnvFileVariableName))
// Flags are managed by kubeadm as pairs of key=value separated by space.
// Split them, find the one containing the flag of interest and update
// its value to have the scheme prefix.
split := strings.Split(str, " ")
for i, s := range split {
if !(strings.Contains(s, containerRuntimeFlag) && !strings.Contains(s, endpointFlag)) {
continue
}
keyValue := strings.Split(s, "=")
if len(keyValue) < 2 {
// 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
// of the endpoint flag and if its not a flag itself.
if i+1 < len(split) {
nextArg := split[i+1]
if strings.HasPrefix(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
}
// remove the flag and value in one
split = append(split[:i], split[i+1:]...)
}
str = strings.Join(split, " ")
return fmt.Sprintf("%s=\"%s", kubeadmconstants.KubeletEnvFileVariableName, str)
}

View File

@ -17,7 +17,6 @@ limitations under the License.
package upgrade
import (
"fmt"
"os"
"path/filepath"
"strings"
@ -102,50 +101,3 @@ func TestRollbackFiles(t *testing.T) {
t.Fatalf("Expected error contains %q, got %v", errString, err)
}
}
func TestCleanupKubeletDynamicEnvFileContainerRuntime(t *testing.T) {
tcases := []struct {
name string
input string
expected string
}{
{
name: "common flag",
input: fmt.Sprintf("%s=\"--container-runtime=remote --container-runtime-endpoint=unix:///var/run/containerd/containerd.sock --pod-infra-container-image=registry.k8s.io/pause:3.9\"", constants.KubeletEnvFileVariableName),
expected: fmt.Sprintf("%s=\"--container-runtime-endpoint=unix:///var/run/containerd/containerd.sock --pod-infra-container-image=registry.k8s.io/pause:3.9\"", constants.KubeletEnvFileVariableName),
},
{
name: "missing flag of interest",
input: fmt.Sprintf("%s=\"--foo=abc --bar=def --container-runtime-endpoint=unix:///var/run/containerd/containerd.sock\"", constants.KubeletEnvFileVariableName),
expected: fmt.Sprintf("%s=\"--foo=abc --bar=def --container-runtime-endpoint=unix:///var/run/containerd/containerd.sock\"", constants.KubeletEnvFileVariableName),
},
{
name: "add missing URL scheme",
input: fmt.Sprintf("%s=\"--foo=abc --container-runtime=remote --bar=def\"", constants.KubeletEnvFileVariableName),
expected: fmt.Sprintf("%s=\"--foo=abc --bar=def\"", constants.KubeletEnvFileVariableName),
},
{
name: "add missing URL scheme if there is no '=' after the flag name",
input: fmt.Sprintf("%s=\"--foo=abc --container-runtime remote --container-runtime-endpoint=unix:///var/run/containerd/containerd.sock --bar=def\"", constants.KubeletEnvFileVariableName),
expected: fmt.Sprintf("%s=\"--foo=abc --container-runtime-endpoint=unix:///var/run/containerd/containerd.sock --bar=def\"", constants.KubeletEnvFileVariableName),
},
{
name: "empty flag of interest value following '='",
input: fmt.Sprintf("%s=\"--foo=abc --container-runtime= --container-runtime-endpoint unix:///var/run/containerd/containerd.sock --bar=def\"", constants.KubeletEnvFileVariableName),
expected: fmt.Sprintf("%s=\"--foo=abc --container-runtime-endpoint unix:///var/run/containerd/containerd.sock --bar=def\"", constants.KubeletEnvFileVariableName),
},
{
name: "empty flag of interest value without '='",
input: fmt.Sprintf("%s=\"--foo=abc --container-runtime --bar=def\"", constants.KubeletEnvFileVariableName),
expected: fmt.Sprintf("%s=\"--foo=abc --bar=def\"", constants.KubeletEnvFileVariableName),
},
}
for _, tt := range tcases {
t.Run(tt.name, func(t *testing.T) {
output := cleanupKubeletDynamicEnvFileContainerRuntime(tt.input)
if output != tt.expected {
t.Errorf("expected output: %q, got: %q", tt.expected, output)
}
})
}
}