Skip to content

Commit d3ea4d3

Browse files
committed
Implement kubeadm reset
1 parent c47eaa8 commit d3ea4d3

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

cmd/kubeadm/app/util/config/cluster.go

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"context"
2121
"crypto/x509"
2222
"fmt"
23+
"os"
24+
"path"
2325
"path/filepath"
2426
"strings"
2527
"time"
@@ -34,12 +36,15 @@ import (
3436
"k8s.io/client-go/tools/clientcmd"
3537
certutil "k8s.io/client-go/util/cert"
3638
"k8s.io/klog/v2"
39+
kubeletconfig "k8s.io/kubelet/config/v1beta1"
40+
"sigs.k8s.io/yaml"
3741

3842
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
3943
kubeadmscheme "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/scheme"
4044
kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta4"
4145
"k8s.io/kubernetes/cmd/kubeadm/app/componentconfigs"
4246
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
47+
"k8s.io/kubernetes/cmd/kubeadm/app/features"
4348
"k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
4449
"k8s.io/kubernetes/cmd/kubeadm/app/util/config/strict"
4550
"k8s.io/kubernetes/cmd/kubeadm/app/util/output"
@@ -111,7 +116,7 @@ func getInitConfigurationFromCluster(kubeconfigDir string, client clientset.Inte
111116
if !newControlPlane {
112117
// gets the nodeRegistration for the current from the node object
113118
kubeconfigFile := filepath.Join(kubeconfigDir, constants.KubeletKubeConfigFileName)
114-
if err := GetNodeRegistration(kubeconfigFile, client, &initcfg.NodeRegistration); err != nil {
119+
if err := GetNodeRegistration(kubeconfigFile, client, &initcfg.NodeRegistration, &initcfg.ClusterConfiguration); err != nil {
115120
return nil, errors.Wrap(err, "failed to get node registration")
116121
}
117122
// gets the APIEndpoint for the current node
@@ -123,7 +128,7 @@ func getInitConfigurationFromCluster(kubeconfigDir string, client clientset.Inte
123128
}
124129

125130
// GetNodeRegistration returns the nodeRegistration for the current node
126-
func GetNodeRegistration(kubeconfigFile string, client clientset.Interface, nodeRegistration *kubeadmapi.NodeRegistrationOptions) error {
131+
func GetNodeRegistration(kubeconfigFile string, client clientset.Interface, nodeRegistration *kubeadmapi.NodeRegistrationOptions, clusterCfg *kubeadmapi.ClusterConfiguration) error {
127132
// gets the name of the current node
128133
nodeName, err := getNodeNameFromKubeletConfig(kubeconfigFile)
129134
if err != nil {
@@ -136,9 +141,30 @@ func GetNodeRegistration(kubeconfigFile string, client clientset.Interface, node
136141
return errors.Wrap(err, "failed to get corresponding node")
137142
}
138143

139-
criSocket, ok := node.ObjectMeta.Annotations[constants.AnnotationKubeadmCRISocket]
140-
if !ok {
141-
return errors.Errorf("node %s doesn't have %s annotation", nodeName, constants.AnnotationKubeadmCRISocket)
144+
var (
145+
criSocket string
146+
ok bool
147+
missingAnnotationError = errors.Errorf("node %s doesn't have %s annotation", nodeName, constants.AnnotationKubeadmCRISocket)
148+
)
149+
if features.Enabled(clusterCfg.FeatureGates, features.NodeLocalCRISocket) {
150+
_, err = os.Stat(filepath.Join(constants.KubeletRunDirectory, constants.KubeletInstanceConfigurationFileName))
151+
if os.IsNotExist(err) {
152+
criSocket, ok = node.ObjectMeta.Annotations[constants.AnnotationKubeadmCRISocket]
153+
if !ok {
154+
return missingAnnotationError
155+
}
156+
} else {
157+
kubeletConfig, err := readKubeletConfig(constants.KubeletRunDirectory, constants.KubeletInstanceConfigurationFileName)
158+
if err != nil {
159+
return errors.Wrapf(err, "node %q does not have a kubelet instance configuration", nodeName)
160+
}
161+
criSocket = kubeletConfig.ContainerRuntimeEndpoint
162+
}
163+
} else {
164+
criSocket, ok = node.ObjectMeta.Annotations[constants.AnnotationKubeadmCRISocket]
165+
if !ok {
166+
return missingAnnotationError
167+
}
142168
}
143169

144170
// returns the nodeRegistration attributes
@@ -253,3 +279,20 @@ func getRawAPIEndpointFromPodAnnotationWithoutRetry(ctx context.Context, client
253279
}
254280
return "", errors.Errorf("API server pod for node name %q hasn't got a %q annotation, cannot retrieve API endpoint", nodeName, constants.KubeAPIServerAdvertiseAddressEndpointAnnotationKey)
255281
}
282+
283+
// readKubeletConfig reads a KubeletConfiguration from the specified file.
284+
func readKubeletConfig(kubeletDir, fileName string) (*kubeletconfig.KubeletConfiguration, error) {
285+
kubeletFile := path.Join(kubeletDir, fileName)
286+
287+
data, err := os.ReadFile(kubeletFile)
288+
if err != nil {
289+
return nil, errors.Wrapf(err, "could not read kubelet configuration file %q", kubeletFile)
290+
}
291+
292+
var config kubeletconfig.KubeletConfiguration
293+
if err := yaml.Unmarshal(data, &config); err != nil {
294+
return nil, errors.Wrapf(err, "could not parse kubelet configuration file %q", kubeletFile)
295+
}
296+
297+
return &config, nil
298+
}

cmd/kubeadm/app/util/config/cluster_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ func TestGetNodeRegistration(t *testing.T) {
335335
}
336336

337337
cfg := &kubeadmapi.InitConfiguration{}
338-
err = GetNodeRegistration(cfgPath, client, &cfg.NodeRegistration)
338+
err = GetNodeRegistration(cfgPath, client, &cfg.NodeRegistration, &cfg.ClusterConfiguration)
339339
if rt.expectedError != (err != nil) {
340340
t.Errorf("unexpected return err from getNodeRegistration: %v", err)
341341
return

0 commit comments

Comments
 (0)