Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions api/v1alpha1/bootcnode_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
6 changes: 6 additions & 0 deletions config/crd/bases/node.bootc.dev_bootcnodes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions docs/design/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions internal/bootc/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
}
Expand Down Expand Up @@ -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"

Expand Down
25 changes: 25 additions & 0 deletions internal/bootc/version.go
Original file line number Diff line number Diff line change
@@ -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
}
40 changes: 40 additions & 0 deletions internal/bootc/version_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
}
}
14 changes: 14 additions & 0 deletions internal/daemon/fake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ type fakeExecutor struct {
status bootc.Status
statusErr error

version string
versionErr error
versionCalls int

stageErr error
stageImg string
stageHook func()
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions internal/daemon/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
31 changes: 31 additions & 0 deletions internal/daemon/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions internal/daemon/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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()
Expand Down
25 changes: 25 additions & 0 deletions internal/daemon/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,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
Expand Down
70 changes: 70 additions & 0 deletions internal/daemon/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package daemon

import (
"context"
"errors"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -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)
}
}
Loading