From 27ea418989bb99cbbc1f52d5a312a68d99c60617 Mon Sep 17 00:00:00 2001 From: yangxg <53515649+yangxggo@users.noreply.github.com> Date: Fri, 30 Jun 2023 17:29:36 +0800 Subject: [PATCH] support old version of kubelet-config (#3447) Signed-off-by: yangxg --- pkg/client-go/kubernetes/expansion.go | 46 ++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/pkg/client-go/kubernetes/expansion.go b/pkg/client-go/kubernetes/expansion.go index f810b1cd1cf..3ad97ef0922 100644 --- a/pkg/client-go/kubernetes/expansion.go +++ b/pkg/client-go/kubernetes/expansion.go @@ -19,14 +19,16 @@ package kubernetes import ( "context" "fmt" - - "github.com/labring/sealos/pkg/utils/iputils" - - ckubeadm "k8s.io/kubernetes/cmd/kubeadm/app/constants" + str "strings" v1 "k8s.io/api/core/v1" + kerrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" clientset "k8s.io/client-go/kubernetes" + ckubeadm "k8s.io/kubernetes/cmd/kubeadm/app/constants" + + "github.com/labring/sealos/pkg/utils/iputils" + "github.com/labring/sealos/pkg/utils/logger" ) var ( @@ -76,6 +78,17 @@ func (ke *kubeExpansion) UpdateKubeadmConfig(ctx context.Context, clusterConfig func (ke *kubeExpansion) FetchKubeletConfig(ctx context.Context) (string, error) { cm, err := ke.client.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(ctx, ckubeadm.KubeletBaseConfigurationConfigMap, metav1.GetOptions{}) if err != nil { + if kerrors.IsNotFound(err) { + logger.Info("cannot find configMap %q, try to detect older versions", ckubeadm.KubeletBaseConfigurationConfigMap) + data, err := ke.fetchOldKubeletConfig(ctx) + if err != nil { + return "", err + } + if err = ke.cloneOldKubeletConfig(ctx, data); err != nil { + return "", err + } + return data, nil + } return "", err } return cm.Data[ckubeadm.KubeletBaseConfigurationConfigMapKey], nil @@ -106,3 +119,28 @@ func (ke *kubeExpansion) FetchHostNameFromInternalIP(ctx context.Context, nodeIP } return "", fmt.Errorf("cannot find host with internal ip %v", ip) } + +func (ke *kubeExpansion) fetchOldKubeletConfig(ctx context.Context) (string, error) { + kubeletBaseConfigurationConfigMapPrefix := fmt.Sprintf("%s-", ckubeadm.KubeletBaseConfigurationConfigMap) + cms, err := ke.client.CoreV1().ConfigMaps(metav1.NamespaceSystem).List(ctx, metav1.ListOptions{}) + if err != nil { + return "", err + } + for _, cm := range cms.Items { + if str.HasPrefix(cm.Name, kubeletBaseConfigurationConfigMapPrefix) { + return cm.Data[ckubeadm.KubeletBaseConfigurationConfigMapKey], nil + } + } + return "", fmt.Errorf("cannot find config map with prefix %q", kubeletBaseConfigurationConfigMapPrefix) +} + +func (ke *kubeExpansion) cloneOldKubeletConfig(ctx context.Context, data string) error { + cm := &v1.ConfigMap{Data: map[string]string{}} + cm.Name = ckubeadm.KubeletBaseConfigurationConfigMap + cm.Data[ckubeadm.KubeletBaseConfigurationConfigMapKey] = data + _, err := ke.client.CoreV1().ConfigMaps(metav1.NamespaceSystem).Create(ctx, cm, metav1.CreateOptions{}) + if err != nil { + return err + } + return nil +}