penguin/monok8s

k8s image for Mono Gateway Dev Kit

clitools/pkg/node/kubeadm_upgrade.go

raw ยท 1038 bytes

package node

import (
	"context"
	"errors"
	"fmt"
	"strings"
	"time"

	"k8s.io/klog/v2"

	system "example.com/monok8s/pkg/system"
)

func RunKubeadmUpgradeApply(ctx context.Context, nctx *NodeContext) error {

	if nctx.BootstrapState == nil {
		return errors.New("BootstrapState is nil. Please run earlier steps first")
	}

	if nctx.BootstrapState.Action != BootstrapActionUpgradeControlPlane {
		klog.V(4).Infof("skipped for %s", nctx.BootstrapState.Action)
		return nil
	}

	if strings.TrimSpace(tmpKubeadmInitConf) == "" {
		return fmt.Errorf("tmp kubeadm config path is empty")
	}

	_, err := nctx.SystemRunner.RunWithOptions(
		ctx,
		"kubeadm",
		[]string{"upgrade", "apply", "-y", nctx.Config.Spec.KubernetesVersion},
		system.RunOptions{
			Timeout: 15 * time.Minute,
			OnStdoutLine: func(line string) {
				klog.Infof("[kubeadm] %s", line)
			},
			OnStderrLine: func(line string) {
				klog.Infof("[kubeadm] %s", line)
			},
		},
	)
	if err != nil {
		return fmt.Errorf("run kubeadm upgrade apply: %w", err)
	}

	return nil
}