Initial upgrade flow for control-plane

This commit is contained in:
2026-04-06 23:49:39 +08:00
parent c6f89651ce
commit 578b3e6a6f
19 changed files with 563 additions and 42 deletions

View File

@@ -2,9 +2,14 @@ package uboot
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
monov1alpha1 "example.com/monok8s/pkg/apis/monok8s/v1alpha1"
"example.com/monok8s/pkg/controller/osimage"
"example.com/monok8s/pkg/node"
)
@@ -15,13 +20,67 @@ func ConfigureABBoot(ctx context.Context, nctx *node.NodeContext) error {
return fmt.Errorf("get current executable path: %w", err)
}
doWrite := false
writer := NewFWEnvWriter(HostFWEnvCfgPath, exePath)
// TODO: configurable from cluster.env
return writer.EnsureBootEnv(ctx, BootEnvConfig{
BootSource: "usb",
BootPart: "A",
})
bootPart, err := writer.GetEnv(ctx, "boot_part")
if err != nil {
if errors.Is(err, ErrEnvNotFound) {
state, err := osimage.ReadBootState()
if err != nil {
return fmt.Errorf("read boot state: %w", err)
}
bootPart := state["BOOT_PART"]
if bootPart == "" {
return fmt.Errorf("BOOT_PART missing")
}
doWrite = true
} else {
return fmt.Errorf("get boot_part: %w", err)
}
}
bootSource, err := writer.GetEnv(ctx, "boot_source")
if err != nil {
if errors.Is(err, ErrEnvNotFound) {
target, err := os.Readlink(monov1alpha1.AltPartDeviceLink)
if err != nil {
return fmt.Errorf("readlink %q: %w", monov1alpha1.AltPartDeviceLink, err)
}
if !filepath.IsAbs(target) {
target = filepath.Join(filepath.Dir(monov1alpha1.AltPartDeviceLink), target)
}
resolved, err := filepath.EvalSymlinks(target)
if err != nil {
return fmt.Errorf("resolve %q: %w", target, err)
}
base := filepath.Base(resolved)
if strings.HasPrefix(base, "mmcblk") {
bootSource = "emmc"
} else {
bootSource = "usb"
}
doWrite = true
} else {
return fmt.Errorf("get boot_source: %w", err)
}
}
if doWrite {
return writer.EnsureBootEnv(ctx, BootEnvConfig{
BootSource: bootSource,
BootPart: bootPart,
})
}
return nil
}
// This is called by the agent controller/osupgrade/handler.go

View File

@@ -2,6 +2,7 @@ package uboot
import (
"context"
"errors"
"fmt"
"strings"
"time"
@@ -11,6 +12,8 @@ import (
"example.com/monok8s/pkg/system"
)
var ErrEnvNotFound = errors.New("fw env not found")
func NewFWEnvWriter(configPath string, ctlPath string) *FWEnvWriter {
return &FWEnvWriter{
Runner: system.NewRunner(system.RunnerConfig{
@@ -38,8 +41,12 @@ func (w *FWEnvWriter) GetEnv(ctx context.Context, key string) (string, error) {
res, err := w.Runner.RunWithOptions(ctx, w.CtlPath, args, system.RunOptions{Quiet: true})
if err != nil {
if res != nil {
return "", fmt.Errorf("fw-printenv %q: %w (stdout=%q stderr=%q)",
key, err, strings.TrimSpace(res.Stdout), strings.TrimSpace(res.Stderr))
if strings.Contains(res.Stderr, "not defined") {
return "", ErrEnvNotFound
} else {
return "", fmt.Errorf("fw-printenv %q: %w (stdout=%q stderr=%q)",
key, err, strings.TrimSpace(res.Stdout), strings.TrimSpace(res.Stderr))
}
}
return "", fmt.Errorf("fw-printenv %q: %w", key, err)
}