From e9dbed2f9a2a66fe6184e9cbb14831cf03471fcb Mon Sep 17 00:00:00 2001 From: Alice Frosi Date: Thu, 5 Nov 2026 14:59:29 +0000 Subject: [PATCH 1/3] bootc: add bootc --version executor method and parser Adds Executor.Version to run 'bootc --version' on the host, and bootc.ParseVersion to extract the version string from its output. Not yet wired up to anything. Assisted-by: AI Signed-off-by: Alice Frosi --- internal/bootc/executor.go | 14 ++++++++++++ internal/bootc/version.go | 25 +++++++++++++++++++++ internal/bootc/version_test.go | 40 ++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 internal/bootc/version.go create mode 100644 internal/bootc/version_test.go diff --git a/internal/bootc/executor.go b/internal/bootc/executor.go index eaaa9bf7..72059060 100644 --- a/internal/bootc/executor.go +++ b/internal/bootc/executor.go @@ -19,6 +19,7 @@ import ( // PID namespaces. Tests can provide a fake implementation. type Executor interface { Status(ctx context.Context) ([]byte, error) + Version(ctx context.Context) ([]byte, error) Stage(ctx context.Context, image string) error Reboot(ctx context.Context) error ApplyUpdate(ctx context.Context, softReboot bool) error @@ -30,6 +31,10 @@ func bootcStatusArgs() []string { return []string{"bootc", "status", "--json", "--format-version", "1"} } +func bootcVersionArgs() []string { + return []string{"bootc", "--version"} +} + func bootcSwitchArgs(image string) []string { return []string{"bootc", "switch", image} } @@ -76,6 +81,15 @@ func (e *HostExecutor) Status(ctx context.Context) ([]byte, error) { return out, nil } +func (e *HostExecutor) Version(ctx context.Context) ([]byte, error) { + cmd := e.nsenterCmd(ctx, bootcVersionArgs()...) + out, err := cmd.Output() + if err != nil { + return nil, fmt.Errorf("running bootc --version: %w", err) + } + return out, nil +} + // stageUnitName is the systemd transient unit name used for bootc switch. const stageUnitName = "bootc-operator-switch.service" diff --git a/internal/bootc/version.go b/internal/bootc/version.go new file mode 100644 index 00000000..7b3b1c62 --- /dev/null +++ b/internal/bootc/version.go @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: Apache-2.0 + +package bootc + +import ( + "fmt" + "strings" +) + +// maxVersionLen matches the MaxLength of BootcNode status.bootcVersion. +const maxVersionLen = 256 + +// ParseVersion parses raw bootc --version output (e.g. "bootc 1.8.0") +// and returns the version string (e.g. "1.8.0"). Trailing fields are +// ignored to tolerate future output additions. +func ParseVersion(data []byte) (string, error) { + fields := strings.Fields(string(data)) + if len(fields) < 2 || fields[0] != "bootc" { + return "", fmt.Errorf("unexpected bootc --version output: %q", string(data)) + } + if len(fields[1]) > maxVersionLen { + return "", fmt.Errorf("bootc version exceeds %d characters", maxVersionLen) + } + return fields[1], nil +} diff --git a/internal/bootc/version_test.go b/internal/bootc/version_test.go new file mode 100644 index 00000000..c9758ea0 --- /dev/null +++ b/internal/bootc/version_test.go @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: Apache-2.0 + +package bootc + +import ( + "strings" + "testing" + + . "github.com/onsi/gomega" +) + +func TestParseVersion(t *testing.T) { + tests := []struct { + name string + data string + want string + wantErr bool + }{ + {name: "release", data: "bootc 1.8.0\n", want: "1.8.0"}, + {name: "prerelease", data: "bootc 1.9.0-rc.1", want: "1.9.0-rc.1"}, + {name: "empty", data: "", wantErr: true}, + {name: "missing version", data: "bootc\n", wantErr: true}, + {name: "wrong program", data: "ostree 2024.1\n", wantErr: true}, + {name: "extra fields", data: "bootc 1.8.0 (git abc123)\n", want: "1.8.0"}, + {name: "too long", data: "bootc " + strings.Repeat("1", 257), wantErr: true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g := NewWithT(t) + got, err := ParseVersion([]byte(tt.data)) + if tt.wantErr { + g.Expect(err).To(HaveOccurred()) + return + } + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(got).To(Equal(tt.want)) + }) + } +} From dee5d7379798cbb8b3444d1b0305ec0521d8608f Mon Sep 17 00:00:00 2001 From: Alice Frosi Date: Thu, 5 Nov 2026 14:59:39 +0000 Subject: [PATCH 2/3] api: add status.bootcVersion field to BootcNode Reports the version of the bootc binary on the host, as returned by 'bootc --version'. Daemon population comes in a follow-up commit. Signed-off-by: Alice Frosi --- api/v1alpha1/bootcnode_types.go | 6 ++++++ config/crd/bases/node.bootc.dev_bootcnodes.yaml | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/api/v1alpha1/bootcnode_types.go b/api/v1alpha1/bootcnode_types.go index ddeefc1b..ffc5a0d0 100644 --- a/api/v1alpha1/bootcnode_types.go +++ b/api/v1alpha1/bootcnode_types.go @@ -140,6 +140,12 @@ type BootcNodeStatus struct { // +optional ObservedGeneration int64 `json:"observedGeneration,omitempty"` + // bootcVersion is the version of the bootc binary on the host + // (e.g. "1.8.0"), as reported by bootc --version. + // +optional + // +kubebuilder:validation:MaxLength=256 + BootcVersion string `json:"bootcVersion,omitempty"` + // booted describes the currently booted OS image. // +optional Booted *ImageInfo `json:"booted,omitempty"` diff --git a/config/crd/bases/node.bootc.dev_bootcnodes.yaml b/config/crd/bases/node.bootc.dev_bootcnodes.yaml index a29e18ca..2de0e122 100644 --- a/config/crd/bases/node.bootc.dev_bootcnodes.yaml +++ b/config/crd/bases/node.bootc.dev_bootcnodes.yaml @@ -100,6 +100,12 @@ spec: status: description: status defines the observed state, written by the daemon. properties: + bootcVersion: + description: |- + bootcVersion is the version of the bootc binary on the host + (e.g. "1.8.0"), as reported by bootc --version. + maxLength: 256 + type: string booted: description: booted describes the currently booted OS image. properties: From ecbf282cc82ce62285ff0ec663ad9b0b0b403315 Mon Sep 17 00:00:00 2001 From: Alice Frosi Date: Thu, 5 Nov 2026 14:59:46 +0000 Subject: [PATCH 3/3] daemon: report bootc version in BootcNode status Reads 'bootc --version' via the watcher, caches it since the host binary only changes across a reboot (which also restarts the daemon), and writes it into status.bootcVersion on reconcile. A failure to read the version is logged but does not mark the node Degraded, since the field is purely informational. Assisted-by: AI Signed-off-by: Alice Frosi --- docs/design/ARCHITECTURE.md | 1 + internal/daemon/fake_test.go | 14 ++++++ internal/daemon/reconciler.go | 9 ++++ internal/daemon/reconciler_test.go | 31 +++++++++++++ internal/daemon/suite_test.go | 10 +++-- internal/daemon/watcher.go | 25 +++++++++++ internal/daemon/watcher_test.go | 70 ++++++++++++++++++++++++++++++ 7 files changed, 157 insertions(+), 3 deletions(-) diff --git a/docs/design/ARCHITECTURE.md b/docs/design/ARCHITECTURE.md index cf52b47d..3fe43044 100644 --- a/docs/design/ARCHITECTURE.md +++ b/docs/design/ARCHITECTURE.md @@ -174,6 +174,7 @@ spec: # ← written by controller namespace: bootc-operator pullSecretHash: sha256:e3b0c4... status: # ← written by daemon + bootcVersion: "1.8.0" # host bootc binary version booted: image: quay.io/example/myos@sha256:old789 imageDigest: sha256:old789 diff --git a/internal/daemon/fake_test.go b/internal/daemon/fake_test.go index fba756bd..0bef581f 100644 --- a/internal/daemon/fake_test.go +++ b/internal/daemon/fake_test.go @@ -18,6 +18,10 @@ type fakeExecutor struct { status bootc.Status statusErr error + version string + versionErr error + versionCalls int + stageErr error stageImg string stageHook func() @@ -40,6 +44,16 @@ func (f *fakeExecutor) Status(_ context.Context) ([]byte, error) { return data, nil } +func (f *fakeExecutor) Version(_ context.Context) ([]byte, error) { + f.mu.Lock() + defer f.mu.Unlock() + f.versionCalls++ + if f.versionErr != nil { + return nil, f.versionErr + } + return []byte(f.version), nil +} + func (f *fakeExecutor) Stage(_ context.Context, image string) error { f.mu.Lock() f.stageImg = image diff --git a/internal/daemon/reconciler.go b/internal/daemon/reconciler.go index 7a873336..7c88147a 100644 --- a/internal/daemon/reconciler.go +++ b/internal/daemon/reconciler.go @@ -374,6 +374,15 @@ func (r *BootcNodeReconciler) populateBootcFields( bn.Status.Staged = convertBootEntry(status.Status.Staged) bn.Status.Rollback = convertBootEntry(status.Status.Rollback) + // The version is informational; failing to read it must not block + // updates, so keep the last reported value and carry on. + version, err := r.StatusWatcher.GetVersion(ctx) + if err != nil { + logf.FromContext(ctx).Error(err, "Failed to get bootc version") + } else { + bn.Status.BootcVersion = version + } + return nil } diff --git a/internal/daemon/reconciler_test.go b/internal/daemon/reconciler_test.go index 040d9397..4ac80c6b 100644 --- a/internal/daemon/reconciler_test.go +++ b/internal/daemon/reconciler_test.go @@ -74,6 +74,7 @@ func TestReconcilePopulatesStatus(t *testing.T) { err := k8sClient.Get(ctx, client.ObjectKeyFromObject(bn), &got) return got.Status, err }).Should(And( + HaveField("BootcVersion", testBootcVersion), HaveField("Booted", And( Not(BeNil()), HaveField("Image", testutil.ImageTaggedRef), @@ -107,6 +108,36 @@ func TestReconcilePopulatesStatus(t *testing.T) { )) } +func TestReconcileBootcVersionErrorNotDegraded(t *testing.T) { + g := NewWithT(t) + g.SetDefaultEventuallyTimeout(pollTimeout) + g.SetDefaultEventuallyPollingInterval(pollInterval) + ctx := context.Background() + + fake := newTestEnv() + fake.status = newBootcStatus(testutil.DigestA) + fake.versionErr = errors.New("bootc --version failed") + + bn := testutil.NewBootcNode(testNodeName, testutil.ImageDigestRefA) + g.Expect(k8sClient.Create(ctx, bn)).To(Succeed()) + t.Cleanup(func() { + _ = k8sClient.Delete(ctx, bn) + }) + + g.Eventually(func() (bootcv1alpha1.BootcNodeStatus, error) { + var got bootcv1alpha1.BootcNode + err := k8sClient.Get(ctx, client.ObjectKeyFromObject(bn), &got) + return got.Status, err + }).Should(And( + HaveField("BootcVersion", BeEmpty()), + HaveField("Booted", HaveField("ImageDigest", testutil.DigestA)), + HaveField("Conditions", ContainElement(And( + HaveField("Type", bootcv1alpha1.NodeDegraded), + HaveField("Status", metav1.ConditionFalse), + ))), + )) +} + func TestReconcileBootcStatusError(t *testing.T) { g := NewWithT(t) g.SetDefaultEventuallyTimeout(pollTimeout) diff --git a/internal/daemon/suite_test.go b/internal/daemon/suite_test.go index a8393c93..629b9c36 100644 --- a/internal/daemon/suite_test.go +++ b/internal/daemon/suite_test.go @@ -20,7 +20,11 @@ import ( bootcv1alpha1 "github.com/bootc-dev/bootc-operator/api/v1alpha1" ) -const testNodeName = "test-node" +const ( + testNodeName = "test-node" + testBootcVersion = "1.8.0" + testBootcVersionOutput = "bootc " + testBootcVersion + "\n" +) var ( testEnv *envtest.Environment @@ -64,7 +68,7 @@ func TestMain(m *testing.M) { os.Exit(1) } - fake := &fakeExecutor{} + fake := &fakeExecutor{version: testBootcVersionOutput} testReconciler = &BootcNodeReconciler{ Client: mgr.GetClient(), Scheme: mgr.GetScheme(), @@ -111,7 +115,7 @@ func TestMain(m *testing.M) { // newTestEnv creates a fresh fakeExecutor and StatusWatcher for a test, // wiring them into the shared reconciler. Each test gets its own cache. func newTestEnv() *fakeExecutor { - fake := &fakeExecutor{} + fake := &fakeExecutor{version: testBootcVersionOutput} testReconciler.Executor = fake testReconciler.rebootIssued = false testReconciler.inflight.reset() diff --git a/internal/daemon/watcher.go b/internal/daemon/watcher.go index af6e2b66..4888e835 100644 --- a/internal/daemon/watcher.go +++ b/internal/daemon/watcher.go @@ -39,6 +39,31 @@ type StatusWatcher struct { mu sync.RWMutex cached *bootc.Status started bool + + versionMu sync.Mutex + versionDone bool + version string + versionErr error +} + +// GetVersion returns the host bootc version. The bootc binary lives in +// the read-only host /usr and only changes across a (soft) reboot, +// which also restarts the daemon (barring usroverlay/hotfix), so the +// result of the first successful run, including a parse failure, is +// cached. Failures to run bootc are retried on the next call. +func (w *StatusWatcher) GetVersion(ctx context.Context) (string, error) { + w.versionMu.Lock() + defer w.versionMu.Unlock() + if w.versionDone { + return w.version, w.versionErr + } + data, err := w.Executor.Version(ctx) + if err != nil { + return "", err + } + w.version, w.versionErr = bootc.ParseVersion(data) + w.versionDone = true + return w.version, w.versionErr } // GetStatus returns the cached bootc status when the watcher loop is diff --git a/internal/daemon/watcher_test.go b/internal/daemon/watcher_test.go index 9fe13f57..ba0e7996 100644 --- a/internal/daemon/watcher_test.go +++ b/internal/daemon/watcher_test.go @@ -4,6 +4,7 @@ package daemon import ( "context" + "errors" "os" "path/filepath" "testing" @@ -249,3 +250,72 @@ func TestWatcherNoPollWhenFsnotifyHealthy(t *testing.T) { t.Fatalf("watcher returned error: %v", err) } } + +func TestWatcherGetVersion(t *testing.T) { + tests := []struct { + name string + output string + execErr error + wantVer string + wantErr bool + wantCalls int + }{ + { + name: "success is cached", + output: testBootcVersionOutput, + wantVer: testBootcVersion, + wantCalls: 1, + }, + { + name: "parse failure is cached", + output: "garbage", + wantErr: true, + wantCalls: 1, + }, + { + name: "exec failure is retried", + execErr: errors.New("nsenter failed"), + wantErr: true, + wantCalls: 2, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + f := &fakeExecutor{version: tt.output, versionErr: tt.execErr} + w := &StatusWatcher{Executor: f} + + for range 2 { + got, err := w.GetVersion(context.Background()) + if tt.wantErr != (err != nil) { + t.Fatalf("GetVersion() error = %v, wantErr %v", err, tt.wantErr) + } + if got != tt.wantVer { + t.Fatalf("GetVersion() = %q, want %q", got, tt.wantVer) + } + } + if f.versionCalls != tt.wantCalls { + t.Fatalf("Version() calls = %d, want %d", f.versionCalls, tt.wantCalls) + } + }) + } +} + +func TestWatcherGetVersionRecovers(t *testing.T) { + f := &fakeExecutor{versionErr: errors.New("nsenter failed")} + w := &StatusWatcher{Executor: f} + + if _, err := w.GetVersion(context.Background()); err == nil { + t.Fatal("expected error") + } + + f.versionErr = nil + f.version = testBootcVersionOutput + got, err := w.GetVersion(context.Background()) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if got != testBootcVersion { + t.Fatalf("GetVersion() = %q, want %q", got, testBootcVersion) + } +}