Skip to content

Commit

Permalink
Merge pull request kubernetes#123096 from neolit123/automated-cherry-…
Browse files Browse the repository at this point in the history
…pick-of-#123077-origin-release-1.29

Automated cherry pick of kubernetes#123077: kubeadm: better checks if the node is CP during "upgrade
  • Loading branch information
k8s-ci-robot committed Feb 9, 2024
2 parents f6b2ccb + c81b482 commit d9cea04
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions cmd/kubeadm/app/cmd/upgrade/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import (
// supported by this api will be exposed as a flag.
type nodeOptions struct {
kubeConfigPath string
isControlPlaneNode bool
etcdUpgrade bool
renewCerts bool
dryRun bool
Expand Down Expand Up @@ -106,26 +105,11 @@ func newCmdNode(out io.Writer) *cobra.Command {

// newNodeOptions returns a struct ready for being used for creating cmd kubeadm upgrade node flags.
func newNodeOptions() *nodeOptions {
kubeConfigPath := constants.GetKubeletKubeConfigPath()

// isControlPlaneNode checks if a node is a control-plane node by looking up
// the kube-apiserver manifest file
isControlPlaneNode := true
filepath := constants.GetStaticPodFilepath(constants.KubeAPIServer, constants.GetStaticPodDirectory())
if _, err := os.Stat(filepath); os.IsNotExist(err) {
isControlPlaneNode = false
}

if isControlPlaneNode {
kubeConfigPath = constants.GetAdminKubeConfigPath()
}

return &nodeOptions{
kubeConfigPath: kubeConfigPath,
isControlPlaneNode: isControlPlaneNode,
dryRun: false,
renewCerts: true,
etcdUpgrade: true,
kubeConfigPath: "", // This is populated in newNodeData() on runtime
dryRun: false,
renewCerts: true,
etcdUpgrade: true,
}
}

Expand All @@ -141,14 +125,28 @@ func addUpgradeNodeFlags(flagSet *flag.FlagSet, nodeOptions *nodeOptions) {
// This func takes care of validating nodeOptions passed to the command, and then it converts
// options into the internal InitConfiguration type that is used as input all the phases in the kubeadm upgrade node workflow
func newNodeData(cmd *cobra.Command, args []string, options *nodeOptions, out io.Writer) (*nodeData, error) {
// Checks if a node is a control-plane node by looking up the kube-apiserver manifest file
isControlPlaneNode := true
filepath := constants.GetStaticPodFilepath(constants.KubeAPIServer, constants.GetStaticPodDirectory())
if _, err := os.Stat(filepath); os.IsNotExist(err) {
isControlPlaneNode = false
}
if len(options.kubeConfigPath) == 0 {
// Update the kubeconfig path depending on whether this is a control plane node or not.
options.kubeConfigPath = constants.GetKubeletKubeConfigPath()
if isControlPlaneNode {
options.kubeConfigPath = constants.GetAdminKubeConfigPath()
}
}

client, err := getClient(options.kubeConfigPath, options.dryRun)
if err != nil {
return nil, errors.Wrapf(err, "couldn't create a Kubernetes client from file %q", options.kubeConfigPath)
}
// Fetches the cluster configuration
// NB in case of control-plane node, we are reading all the info for the node; in case of NOT control-plane node
// (worker node), we are not reading local API address and the CRI socket from the node object
cfg, err := configutil.FetchInitConfigurationFromCluster(client, nil, "upgrade", !options.isControlPlaneNode, false)
cfg, err := configutil.FetchInitConfigurationFromCluster(client, nil, "upgrade", !isControlPlaneNode, false)
if err != nil {
return nil, errors.Wrap(err, "unable to fetch the kubeadm-config ConfigMap")
}
Expand All @@ -165,7 +163,7 @@ func newNodeData(cmd *cobra.Command, args []string, options *nodeOptions, out io
dryRun: options.dryRun,
cfg: cfg,
client: client,
isControlPlaneNode: options.isControlPlaneNode,
isControlPlaneNode: isControlPlaneNode,
patchesDir: options.patchesDir,
ignorePreflightErrors: ignorePreflightErrorsSet,
kubeConfigPath: options.kubeConfigPath,
Expand Down

0 comments on commit d9cea04

Please sign in to comment.