penguin/monok8s

k8s image for Mono Gateway Dev Kit

clitools/pkg/controller/osupgrade/handler.go

raw ยท 1994 bytes

package osupgrade

import (
	"context"
	"fmt"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/klog/v2"

	monov1alpha1 "example.com/monok8s/pkg/apis/monok8s/v1alpha1"
	"example.com/monok8s/pkg/buildinfo"
	"example.com/monok8s/pkg/catalog"
	"example.com/monok8s/pkg/kube"
)

func HandleOSUpgrade(
	ctx context.Context,
	clients *kube.Clients,
	namespace string,
	nodeName string,
	osu *monov1alpha1.OSUpgrade,
) error {
	osup, err := ensureProgressHeartbeat(ctx, clients, namespace, nodeName, osu)
	if err != nil {
		return err
	}

	klog.InfoS("handling osupgrade",
		"name", osu.Name,
		"node", nodeName,
		"desiredVersion", osu.Spec.DesiredVersion,
	)

	kata, err := catalog.ResolveCatalog(ctx, clients.Kubernetes, namespace, osu.Spec.Catalog)
	if err != nil {
		return failProgress(ctx, clients, osup, "resolve catalog", err)
	}

	plan, err := PlanUpgrade(buildinfo.KubeVersion, osu, kata)
	if err != nil {
		return failProgress(ctx, clients, osup, "plan upgrade", err)
	}

	if len(plan.Path) == 0 {
		osup.Status.CurrentVersion = buildinfo.KubeVersion
		osup.Status.TargetVersion = buildinfo.KubeVersion
		if err := markProgressCompleted(ctx, clients, osup, "already at target version"); err != nil {
			return err
		}
		klog.InfoS("osupgrade already satisfied",
			"name", osu.Name,
			"node", nodeName,
			"target", plan.ResolvedTarget,
		)
		return nil
	}

	first := plan.Path[0]

	osup.Status.TargetVersion = plan.ResolvedTarget
	osup.Status.Phase = monov1alpha1.OSUpgradeProgressPhaseDownloading
	osup.Status.Message = fmt.Sprintf("downloading image: %s", first.URL)
	now := metav1.Now()
	osup.Status.LastUpdatedAt = &now

	if _, err := updateProgressStatus(ctx, clients, osup_gvr, osup); err != nil {
		return fmt.Errorf("update progress status: %w", err)
	}

	klog.InfoS("planned osupgrade",
		"name", osu.Name,
		"node", nodeName,
		"resolvedTarget", plan.ResolvedTarget,
		"steps", len(plan.Path),
		"firstVersion", first.Version,
		"firstURL", first.URL,
	)

	return nil
}