-
Notifications
You must be signed in to change notification settings - Fork 38.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
kubeadm: reduce the number of fetch configuration when reset #74410
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ import ( | |
"k8s.io/apimachinery/pkg/util/sets" | ||
clientset "k8s.io/client-go/kubernetes" | ||
"k8s.io/klog" | ||
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" | ||
kubeadmapiv1beta1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta1" | ||
"k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/validation" | ||
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options" | ||
|
@@ -68,15 +69,20 @@ func NewCmdReset(in io.Reader, out io.Writer) *cobra.Command { | |
kubeadmutil.CheckErr(err) | ||
} | ||
|
||
cfg, err := configutil.FetchInitConfigurationFromCluster(client, os.Stdout, "reset", false) | ||
if err != nil { | ||
klog.Warningf("[reset] Unable to fetch the kubeadm-config ConfigMap from cluster: %v", err) | ||
} | ||
|
||
if criSocketPath == "" { | ||
criSocketPath, err = resetDetectCRISocket(client) | ||
criSocketPath, err = resetDetectCRISocket(cfg) | ||
kubeadmutil.CheckErr(err) | ||
klog.V(1).Infof("[reset] detected and using CRI socket: %s", criSocketPath) | ||
} | ||
|
||
r, err := NewReset(in, ignorePreflightErrorsSet, forceReset, certsDir, criSocketPath) | ||
kubeadmutil.CheckErr(err) | ||
kubeadmutil.CheckErr(r.Run(out, client)) | ||
kubeadmutil.CheckErr(r.Run(out, client, cfg)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i don't think we should call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We not call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, i guess that's true. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I check the currently code, I find There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so we should run all the code that doesn't depend on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, you said right. I considered it, maybe better to check I update the code in my local development environment, below is code: cfg, err := configutil.FetchInitConfigurationFromCluster(client, os.Stdout, "reset", false)
if err != nil {
kubeadmutil.CheckErr(errors.Errorf("[reset] Unable to fetch the kubeadm-config ConfigMap from cluster: %v\n", err))
} If we check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
}, | ||
} | ||
|
||
|
@@ -131,17 +137,19 @@ func NewReset(in io.Reader, ignorePreflightErrors sets.String, forceReset bool, | |
} | ||
|
||
// Run reverts any changes made to this host by "kubeadm init" or "kubeadm join". | ||
func (r *Reset) Run(out io.Writer, client clientset.Interface) error { | ||
func (r *Reset) Run(out io.Writer, client clientset.Interface, cfg *kubeadmapi.InitConfiguration) error { | ||
var dirsToClean []string | ||
// Only clear etcd data when using local etcd. | ||
etcdManifestPath := filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.ManifestsSubDirName, "etcd.yaml") | ||
|
||
klog.V(1).Infof("[reset] checking for etcd config") | ||
etcdDataDir, err := getEtcdDataDir(etcdManifestPath, client) | ||
etcdDataDir, err := getEtcdDataDir(etcdManifestPath, cfg) | ||
if err == nil { | ||
dirsToClean = append(dirsToClean, etcdDataDir) | ||
if err := removeEtcdMember(client); err != nil { | ||
klog.Warningf("[reset] failed to remove etcd member: %v\n.Please manually remove this etcd member using etcdctl", err) | ||
if cfg != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add check. Because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice catch. |
||
if err := etcdphase.RemoveStackedEtcdMemberFromCluster(client, cfg); err != nil { | ||
klog.Warningf("[reset] failed to remove etcd member: %v\n.Please manually remove this etcd member using etcdctl", err) | ||
} | ||
} | ||
} else { | ||
fmt.Println("[reset] no etcd config found. Assuming external etcd") | ||
|
@@ -209,25 +217,14 @@ func (r *Reset) Run(out io.Writer, client clientset.Interface) error { | |
return nil | ||
} | ||
|
||
func removeEtcdMember(client clientset.Interface) error { | ||
cfg, err := configutil.FetchInitConfigurationFromCluster(client, os.Stdout, "reset", false) | ||
if err != nil { | ||
return err | ||
} | ||
return etcdphase.RemoveStackedEtcdMemberFromCluster(client, cfg) | ||
} | ||
|
||
func getEtcdDataDir(manifestPath string, client clientset.Interface) (string, error) { | ||
func getEtcdDataDir(manifestPath string, cfg *kubeadmapi.InitConfiguration) (string, error) { | ||
const etcdVolumeName = "etcd-data" | ||
var dataDir string | ||
|
||
if client != nil { | ||
cfg, err := configutil.FetchInitConfigurationFromCluster(client, os.Stdout, "reset", false) | ||
if err == nil && cfg.Etcd.Local != nil { | ||
return cfg.Etcd.Local.DataDir, nil | ||
} | ||
klog.Warningf("[reset] Unable to fetch the kubeadm-config ConfigMap, using etcd pod spec as fallback: %v", err) | ||
if cfg != nil && cfg.Etcd.Local != nil { | ||
return cfg.Etcd.Local.DataDir, nil | ||
} | ||
klog.Warningln("[reset] No kubeadm config, using etcd pod spec to get data directory") | ||
|
||
etcdPod, err := utilstaticpod.ReadStaticPodFromDisk(manifestPath) | ||
if err != nil { | ||
|
@@ -311,13 +308,10 @@ func resetConfigDir(configPathDir, pkiPathDir string) { | |
} | ||
} | ||
|
||
func resetDetectCRISocket(client clientset.Interface) (string, error) { | ||
if client != nil { | ||
// first try to connect to the cluster for the CRI socket | ||
cfg, err := configutil.FetchInitConfigurationFromCluster(client, os.Stdout, "reset", false) | ||
if err == nil { | ||
return cfg.NodeRegistration.CRISocket, nil | ||
} | ||
func resetDetectCRISocket(cfg *kubeadmapi.InitConfiguration) (string, error) { | ||
if cfg != nil { | ||
// first try to get the CRI socket from the cluster configuration | ||
return cfg.NodeRegistration.CRISocket, nil | ||
} | ||
|
||
// if this fails, try to detect it | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to check if
cfg
is notnil
here.