mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-12 13:31:52 +00:00
Implement kubeadm reset
This commit is contained in:
parent
c47eaa88b1
commit
d3ea4d3d9a
@ -20,6 +20,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -34,12 +36,15 @@ import (
|
|||||||
"k8s.io/client-go/tools/clientcmd"
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
certutil "k8s.io/client-go/util/cert"
|
certutil "k8s.io/client-go/util/cert"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
|
kubeletconfig "k8s.io/kubelet/config/v1beta1"
|
||||||
|
"sigs.k8s.io/yaml"
|
||||||
|
|
||||||
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
||||||
kubeadmscheme "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/scheme"
|
kubeadmscheme "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/scheme"
|
||||||
kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta4"
|
kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta4"
|
||||||
"k8s.io/kubernetes/cmd/kubeadm/app/componentconfigs"
|
"k8s.io/kubernetes/cmd/kubeadm/app/componentconfigs"
|
||||||
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
|
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
|
||||||
|
"k8s.io/kubernetes/cmd/kubeadm/app/features"
|
||||||
"k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
|
"k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
|
||||||
"k8s.io/kubernetes/cmd/kubeadm/app/util/config/strict"
|
"k8s.io/kubernetes/cmd/kubeadm/app/util/config/strict"
|
||||||
"k8s.io/kubernetes/cmd/kubeadm/app/util/output"
|
"k8s.io/kubernetes/cmd/kubeadm/app/util/output"
|
||||||
@ -111,7 +116,7 @@ func getInitConfigurationFromCluster(kubeconfigDir string, client clientset.Inte
|
|||||||
if !newControlPlane {
|
if !newControlPlane {
|
||||||
// gets the nodeRegistration for the current from the node object
|
// gets the nodeRegistration for the current from the node object
|
||||||
kubeconfigFile := filepath.Join(kubeconfigDir, constants.KubeletKubeConfigFileName)
|
kubeconfigFile := filepath.Join(kubeconfigDir, constants.KubeletKubeConfigFileName)
|
||||||
if err := GetNodeRegistration(kubeconfigFile, client, &initcfg.NodeRegistration); err != nil {
|
if err := GetNodeRegistration(kubeconfigFile, client, &initcfg.NodeRegistration, &initcfg.ClusterConfiguration); err != nil {
|
||||||
return nil, errors.Wrap(err, "failed to get node registration")
|
return nil, errors.Wrap(err, "failed to get node registration")
|
||||||
}
|
}
|
||||||
// gets the APIEndpoint for the current node
|
// gets the APIEndpoint for the current node
|
||||||
@ -123,7 +128,7 @@ func getInitConfigurationFromCluster(kubeconfigDir string, client clientset.Inte
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetNodeRegistration returns the nodeRegistration for the current node
|
// GetNodeRegistration returns the nodeRegistration for the current node
|
||||||
func GetNodeRegistration(kubeconfigFile string, client clientset.Interface, nodeRegistration *kubeadmapi.NodeRegistrationOptions) error {
|
func GetNodeRegistration(kubeconfigFile string, client clientset.Interface, nodeRegistration *kubeadmapi.NodeRegistrationOptions, clusterCfg *kubeadmapi.ClusterConfiguration) error {
|
||||||
// gets the name of the current node
|
// gets the name of the current node
|
||||||
nodeName, err := getNodeNameFromKubeletConfig(kubeconfigFile)
|
nodeName, err := getNodeNameFromKubeletConfig(kubeconfigFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -136,9 +141,30 @@ func GetNodeRegistration(kubeconfigFile string, client clientset.Interface, node
|
|||||||
return errors.Wrap(err, "failed to get corresponding node")
|
return errors.Wrap(err, "failed to get corresponding node")
|
||||||
}
|
}
|
||||||
|
|
||||||
criSocket, ok := node.ObjectMeta.Annotations[constants.AnnotationKubeadmCRISocket]
|
var (
|
||||||
|
criSocket string
|
||||||
|
ok bool
|
||||||
|
missingAnnotationError = errors.Errorf("node %s doesn't have %s annotation", nodeName, constants.AnnotationKubeadmCRISocket)
|
||||||
|
)
|
||||||
|
if features.Enabled(clusterCfg.FeatureGates, features.NodeLocalCRISocket) {
|
||||||
|
_, err = os.Stat(filepath.Join(constants.KubeletRunDirectory, constants.KubeletInstanceConfigurationFileName))
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
criSocket, ok = node.ObjectMeta.Annotations[constants.AnnotationKubeadmCRISocket]
|
||||||
if !ok {
|
if !ok {
|
||||||
return errors.Errorf("node %s doesn't have %s annotation", nodeName, constants.AnnotationKubeadmCRISocket)
|
return missingAnnotationError
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
kubeletConfig, err := readKubeletConfig(constants.KubeletRunDirectory, constants.KubeletInstanceConfigurationFileName)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "node %q does not have a kubelet instance configuration", nodeName)
|
||||||
|
}
|
||||||
|
criSocket = kubeletConfig.ContainerRuntimeEndpoint
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
criSocket, ok = node.ObjectMeta.Annotations[constants.AnnotationKubeadmCRISocket]
|
||||||
|
if !ok {
|
||||||
|
return missingAnnotationError
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns the nodeRegistration attributes
|
// returns the nodeRegistration attributes
|
||||||
@ -253,3 +279,20 @@ func getRawAPIEndpointFromPodAnnotationWithoutRetry(ctx context.Context, client
|
|||||||
}
|
}
|
||||||
return "", errors.Errorf("API server pod for node name %q hasn't got a %q annotation, cannot retrieve API endpoint", nodeName, constants.KubeAPIServerAdvertiseAddressEndpointAnnotationKey)
|
return "", errors.Errorf("API server pod for node name %q hasn't got a %q annotation, cannot retrieve API endpoint", nodeName, constants.KubeAPIServerAdvertiseAddressEndpointAnnotationKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// readKubeletConfig reads a KubeletConfiguration from the specified file.
|
||||||
|
func readKubeletConfig(kubeletDir, fileName string) (*kubeletconfig.KubeletConfiguration, error) {
|
||||||
|
kubeletFile := path.Join(kubeletDir, fileName)
|
||||||
|
|
||||||
|
data, err := os.ReadFile(kubeletFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrapf(err, "could not read kubelet configuration file %q", kubeletFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
var config kubeletconfig.KubeletConfiguration
|
||||||
|
if err := yaml.Unmarshal(data, &config); err != nil {
|
||||||
|
return nil, errors.Wrapf(err, "could not parse kubelet configuration file %q", kubeletFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &config, nil
|
||||||
|
}
|
||||||
|
@ -335,7 +335,7 @@ func TestGetNodeRegistration(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cfg := &kubeadmapi.InitConfiguration{}
|
cfg := &kubeadmapi.InitConfiguration{}
|
||||||
err = GetNodeRegistration(cfgPath, client, &cfg.NodeRegistration)
|
err = GetNodeRegistration(cfgPath, client, &cfg.NodeRegistration, &cfg.ClusterConfiguration)
|
||||||
if rt.expectedError != (err != nil) {
|
if rt.expectedError != (err != nil) {
|
||||||
t.Errorf("unexpected return err from getNodeRegistration: %v", err)
|
t.Errorf("unexpected return err from getNodeRegistration: %v", err)
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user