-
Notifications
You must be signed in to change notification settings - Fork 242
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
Implement core scripts and logic for running upgrades #211
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package upgrade | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
|
||
"github.com/kubermatic/kubeone/pkg/config" | ||
"github.com/kubermatic/kubeone/pkg/installer/util" | ||
) | ||
|
||
const ( | ||
upgradeKubeadmDebianCommand = ` | ||
source /etc/os-release | ||
source /etc/kubeone/proxy-env | ||
|
||
sudo apt-get update | ||
|
||
kube_ver=$(apt-cache madison kubelet | grep "{{ .KUBERNETES_VERSION }}" | head -1 | awk '{print $3}') | ||
|
||
sudo apt-mark unhold kubeadm | ||
sudo apt-get install kubeadm=${kube_ver} | ||
sudo apt-mark hold kubeadm | ||
` | ||
upgradeKubeadmCentOSCommand = ` | ||
source /etc/kubeone/proxy-env | ||
|
||
sudo yum install -y --disableexcludes=kubernetes \ | ||
kubeadm-{{ .KUBERNETES_VERSION }}-0 | ||
` | ||
upgradeKubeadmCoreOSCommand = ` | ||
source /etc/kubeone/proxy-env | ||
|
||
RELEASE="v{{ .KUBERNETES_VERSION }}" | ||
|
||
sudo mkdir -p /opt/bin | ||
cd /opt/bin | ||
sudo curl -L --remote-name-all \ | ||
https://storage.googleapis.com/kubernetes-release/release/${RELEASE}/bin/linux/amd64/kubeadm | ||
sudo chmod +x kubeadm | ||
` | ||
) | ||
|
||
func upgradeKubeadm(ctx *util.Context, node *config.HostConfig) error { | ||
var err error | ||
|
||
switch node.OperatingSystem { | ||
case "ubuntu", "debian": | ||
err = upgradeKubeadmDebian(ctx) | ||
|
||
case "coreos": | ||
err = upgradeKubeadmCoreOS(ctx) | ||
|
||
case "centos": | ||
err = upgradeKubeadmCentOS(ctx) | ||
|
||
default: | ||
err = errors.Errorf("'%s' is not a supported operating system", node.OperatingSystem) | ||
} | ||
|
||
return err | ||
} | ||
|
||
func upgradeKubeadmDebian(ctx *util.Context) error { | ||
_, _, err := ctx.Runner.Run(upgradeKubeadmDebianCommand, util.TemplateVariables{ | ||
"KUBERNETES_VERSION": ctx.Cluster.Versions.Kubernetes, | ||
}) | ||
|
||
return errors.WithStack(err) | ||
} | ||
|
||
func upgradeKubeadmCentOS(ctx *util.Context) error { | ||
_, _, err := ctx.Runner.Run(upgradeKubeadmCentOSCommand, util.TemplateVariables{ | ||
"KUBERNETES_VERSION": ctx.Cluster.Versions.Kubernetes, | ||
}) | ||
|
||
return errors.WithStack(err) | ||
} | ||
|
||
func upgradeKubeadmCoreOS(ctx *util.Context) error { | ||
_, _, err := ctx.Runner.Run(upgradeKubeadmCoreOSCommand, util.TemplateVariables{ | ||
"KUBERNETES_VERSION": ctx.Cluster.Versions.Kubernetes, | ||
}) | ||
|
||
return errors.WithStack(err) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package upgrade | ||
|
||
import ( | ||
"github.com/kubermatic/kubeone/pkg/installer/util" | ||
) | ||
|
||
const ( | ||
kubeadmUpgradeLeaderCommand = ` | ||
if [[ -f /etc/kubernetes/kubelet.conf ]]; then exit 0; fi | ||
sudo kubeadm upgrade {{ .VERSION }} | ||
` | ||
kubeadmUpgradeFollowerCommand = ` | ||
if [[ -f /etc/kubernetes/kubelet.conf ]]; then exit 0; fi | ||
sudo kubeadm upgrade node experimental-control-plane | ||
` | ||
) | ||
|
||
func upgradeLeaderControlPlane(ctx *util.Context) error { | ||
_, _, err := ctx.Runner.Run(kubeadmUpgradeLeaderCommand, util.TemplateVariables{ | ||
"VERSION": ctx.Cluster.Versions.Kubernetes, | ||
}) | ||
return err | ||
} | ||
|
||
func upgradeFollowerControlPlane(ctx *util.Context) error { | ||
_, _, err := ctx.Runner.Run(kubeadmUpgradeFollowerCommand, util.TemplateVariables{}) | ||
return err | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package upgrade | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
|
||
"github.com/kubermatic/kubeone/pkg/config" | ||
"github.com/kubermatic/kubeone/pkg/installer/util" | ||
) | ||
|
||
const ( | ||
upgradeKubeletDebianCommand = ` | ||
source /etc/os-release | ||
source /etc/kubeone/proxy-env | ||
|
||
sudo apt-get update | ||
|
||
kube_ver=$(apt-cache madison kubelet | grep "{{ .KUBERNETES_VERSION }}" | head -1 | awk '{print $3}') | ||
|
||
sudo apt-mark unhold kubelet | ||
sudo apt-get install kubelet=${kube_ver} | ||
sudo apt-mark hold kubelet | ||
` | ||
upgradeKubeletCentOSCommand = ` | ||
source /etc/kubeone/proxy-env | ||
|
||
sudo yum install -y --disableexcludes=kubernetes \ | ||
kubelet-{{ .KUBERNETES_VERSION }}-0 | ||
` | ||
upgradeKubeletCoreOSCommand = ` | ||
source /etc/kubeone/proxy-env | ||
|
||
RELEASE="v{{ .KUBERNETES_VERSION }}" | ||
|
||
sudo mkdir -p /opt/bin | ||
cd /opt/bin | ||
sudo curl -L --remote-name-all \ | ||
https://storage.googleapis.com/kubernetes-release/release/${RELEASE}/bin/linux/amd64/kubelet | ||
sudo chmod +x kubelet | ||
` | ||
) | ||
|
||
func upgradeKubelet(ctx *util.Context, node *config.HostConfig) error { | ||
var err error | ||
|
||
switch node.OperatingSystem { | ||
case "ubuntu", "debian": | ||
err = upgradeKubeletDebian(ctx) | ||
|
||
case "coreos": | ||
err = upgradeKubeletCoreOS(ctx) | ||
|
||
case "centos": | ||
err = upgradeKubeletCentOS(ctx) | ||
|
||
default: | ||
err = errors.Errorf("'%s' is not a supported operating system", node.OperatingSystem) | ||
} | ||
|
||
return err | ||
} | ||
|
||
func upgradeKubeletDebian(ctx *util.Context) error { | ||
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. As mentioned in a comment above, this could be a single function for both |
||
_, _, err := ctx.Runner.Run(upgradeKubeletDebianCommand, util.TemplateVariables{ | ||
"KUBERNETES_VERSION": ctx.Cluster.Versions.Kubernetes, | ||
}) | ||
|
||
return errors.WithStack(err) | ||
} | ||
|
||
func upgradeKubeletCentOS(ctx *util.Context) error { | ||
_, _, err := ctx.Runner.Run(upgradeKubeletCentOSCommand, util.TemplateVariables{ | ||
"KUBERNETES_VERSION": ctx.Cluster.Versions.Kubernetes, | ||
}) | ||
|
||
return errors.WithStack(err) | ||
} | ||
|
||
func upgradeKubeletCoreOS(ctx *util.Context) error { | ||
_, _, err := ctx.Runner.Run(upgradeKubeletCoreOSCommand, util.TemplateVariables{ | ||
"KUBERNETES_VERSION": ctx.Cluster.Versions.Kubernetes, | ||
}) | ||
|
||
return errors.WithStack(err) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,18 +7,30 @@ import ( | |
) | ||
|
||
const ( | ||
labelUpgradeLock = "kubeone.io/upgrading-in-process" | ||
labelUpgradeLock = "kubeone.io/upgrade-in-progress" | ||
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. If we decide to go with this change, the proposal should be updated as well (can be done in this PR) |
||
labelControlPlaneNode = "node-role.kubernetes.io/master" | ||
) | ||
|
||
// Upgrade performs all the steps required to upgrade Kubernetes on | ||
// cluster provisioned using KubeOne | ||
func Upgrade(ctx *util.Context) error { | ||
if err := util.BuildKubernetesClientset(ctx); err != nil { | ||
return errors.Wrap(err, "unable to build kubernetes clientset") | ||
// commonSteps are same for all worker nodes and they are safe to be run in parallel | ||
commonSteps := []struct { | ||
fn func(ctx *util.Context) error | ||
errMsg string | ||
}{ | ||
{fn: util.BuildKubernetesClientset, errMsg: "unable to build kubernetes clientset"}, | ||
{fn: determineHostname, errMsg: "unable to determine hostname"}, | ||
{fn: determineOS, errMsg: "unable to determine operating system"}, | ||
{fn: runPreflightChecks, errMsg: "preflight checks failed"}, | ||
{fn: upgradeLeader, errMsg: "unable to upgrade leader control plane"}, | ||
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 was thinking a lot how to handle this but still unsure is this the right way.. I have two big functions in In case of the leader, it would be quite easy, as we have only leader and just listing tasks would do the job. This might not be a case for followers. I believe that if we started upgrading one follower, we should fully finish upgrade there before proceeding to the next follower. If I'd add task by task here instead of using one big function, it might not be easy to ensure that multiple tasks will finish on the same node before doing same for another. Of course, it's possible to use different approach, but I wanted to be consistent and not expand on too much. |
||
{fn: upgradeFollower, errMsg: "unable to upgrade follower control plane"}, | ||
} | ||
if err := runPreflightChecks(ctx); err != nil { | ||
return errors.Wrap(err, "preflight checks failed") | ||
|
||
for _, step := range commonSteps { | ||
if err := step.fn(ctx); err != nil { | ||
return errors.Wrap(err, step.errMsg) | ||
} | ||
} | ||
|
||
return nil | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package upgrade | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
|
||
"github.com/kubermatic/kubeone/pkg/config" | ||
"github.com/kubermatic/kubeone/pkg/installer/util" | ||
"github.com/kubermatic/kubeone/pkg/ssh" | ||
) | ||
|
||
func upgradeFollower(ctx *util.Context) error { | ||
return ctx.RunTaskOnFollowers(upgradeFollowerExecutor, false) | ||
} | ||
|
||
func upgradeFollowerExecutor(ctx *util.Context, node *config.HostConfig, conn ssh.Connection) error { | ||
ctx.Logger.Infoln("Labeling follower control plane…") | ||
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.
|
||
err := labelNode(ctx.Clientset.CoreV1().Nodes(), node) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to label leader control plane node") | ||
} | ||
|
||
ctx.Logger.Infoln("Upgrading kubeadm on follower control plane…") | ||
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. Similar as for leader, do we need |
||
err = upgradeKubeadm(ctx, node) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to upgrade kubeadm on follower control plane") | ||
} | ||
|
||
ctx.Logger.Infoln("Running 'kubeadm upgrade' on the follower control plane node…") | ||
err = upgradeFollowerControlPlane(ctx) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to upgrade follower control plane") | ||
} | ||
|
||
ctx.Logger.Infoln("Upgrading kubelet…") | ||
err = upgradeKubelet(ctx, node) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to upgrade kubelet") | ||
} | ||
|
||
ctx.Logger.Infoln("Unlabeling follower control plane…") | ||
err = unlabelNode(ctx.Clientset.CoreV1().Nodes(), node) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to unlabel follower control plane node") | ||
} | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package upgrade | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
|
||
"github.com/kubermatic/kubeone/pkg/config" | ||
"github.com/kubermatic/kubeone/pkg/installer/util" | ||
"github.com/kubermatic/kubeone/pkg/ssh" | ||
) | ||
|
||
func upgradeLeader(ctx *util.Context) error { | ||
return ctx.RunTaskOnLeader(upgradeLeaderExecutor) | ||
} | ||
|
||
func upgradeLeaderExecutor(ctx *util.Context, node *config.HostConfig, conn ssh.Connection) error { | ||
logger := ctx.Logger.WithField("node", node.PublicAddress) | ||
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. Not sure is this needed, but I thought it would be nice to have on what node the task is executed. |
||
|
||
logger.Infoln("Labeling leader control plane…") | ||
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. Do we need |
||
err := labelNode(ctx.Clientset.CoreV1().Nodes(), node) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to label leader control plane node") | ||
} | ||
|
||
logger.Infoln("Upgrading kubeadm on leader control plane…") | ||
err = upgradeKubeadm(ctx, node) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to upgrade kubeadm on leader control plane") | ||
} | ||
|
||
logger.Infoln("Running 'kubeadm upgrade' on leader control plane node…") | ||
err = upgradeLeaderControlPlane(ctx) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to run 'kubeadm upgrade' on leader control plane") | ||
} | ||
|
||
logger.Infoln("Upgrading kubelet on leader control plane…") | ||
err = upgradeKubelet(ctx, node) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to upgrade kubelet on leader control plane") | ||
} | ||
|
||
logger.Infoln("Unlabeling leader control plane…") | ||
err = unlabelNode(ctx.Clientset.CoreV1().Nodes(), node) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to unlabel leader control plane node") | ||
} | ||
|
||
return nil | ||
} |
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.
This function could actually be a single function like
upgradeKubeadmExecutor(*util.Context, string)
. Actually, it could be reused forkubelet
upgrade as well as they have the same variables and basically do the same thing.We have similar situation in the
installer
package as well.