Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mcbs] Validate rpm-ostree version is new enough #2859

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
4 changes: 4 additions & 0 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,10 @@ func (dn *Daemon) LogSystemData() {
} else {
glog.Info("systemd service state: OK")
}

if err := checkNodeRpmOstreeVersion(); err != nil {
glog.Errorf("Failed to check rpm-ostree version: %v", err)
}
}

const (
Expand Down
69 changes: 69 additions & 0 deletions pkg/daemon/rpm-ostree.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"time"

"github.com/containers/image/v5/types"
yaml "github.com/ghodss/yaml"
"github.com/golang/glog"
"github.com/opencontainers/go-digest"
pivotutils "github.com/openshift/machine-config-operator/pkg/daemon/pivot/utils"
Expand All @@ -20,8 +22,21 @@ const (
numRetriesNetCommands = 5
// Pull secret. Written by the machine-config-operator
kubeletAuthFile = "/var/lib/kubelet/config.json"

// rpmOstreeVersionMinimum is the minimum required version
rpmOstreeVersionMinimum = "2021.14"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice idea for sanity check (aside from immediate mcbs use) 1 q from that pov: would hardcoding directly in file might make it go stale over time? how often would we expect this to change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we wouldn't change this too often. In practice, we'll just know out of band when we're using a new feature - it'll have landed in rhel 8.z or fast tracked into OCP.

I honestly don't have a really strong opinion on actually merging this patch. We can drop it too when we look at merging the mcbs branch.

But for now it'll be a sanity check.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense to me!

)

// rpmOstreeVersionOuter is YAML output by `rpm-ostree --version`
type rpmOstreeVersionOuter struct {
Root rpmOstreeVersionData `json:"rpm-ostree"`
}

type rpmOstreeVersionData struct {
Version string `json:"Version"`
Features []string `json:"Features"`
}

// rpmOstreeState houses zero or more RpmOstreeDeployments
// Subset of `rpm-ostree status --json`
// https://github.com/projectatomic/rpm-ostree/blob/bce966a9812df141d38e3290f845171ec745aa4e/src/daemon/rpmostreed-deployment-utils.c#L227
Expand Down Expand Up @@ -100,6 +115,60 @@ func (r *RpmOstreeClient) loadStatus() (*rpmOstreeState, error) {
return &rosState, nil
}

func parseVer(s string) ([]int, error) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rust has totally spoiled me. I find it soooo hard to write code like this. In Rust it's just that simple and elegant: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3709150890266cfee1f09a35c1f7391f

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

colin we're not rewriting openshift in rust. at least... not yet. ;)

r := []int{}
for _, s := range strings.Split(s, ".") {
n, err := strconv.Atoi(s)
if err != nil {
return nil, fmt.Errorf("Failed to parse %s: %v", s, err)
}
r = append(r, n)
}
return r, nil
}

func validateVersion(current rpmOstreeVersionOuter) error {
requiredVer, err := parseVer(rpmOstreeVersionMinimum)
if err != nil {
return err
}
curVer, err := parseVer(current.Root.Version)
if err != nil {
return err
}
if len(curVer) < len(requiredVer) {
return fmt.Errorf("Too few components in %s, expected to match %s", current.Root.Version, rpmOstreeVersionMinimum)
}
for i, v := range requiredVer {
if curVer[i] < v {
return fmt.Errorf("Too old %s, expected to match %s", current.Root.Version, rpmOstreeVersionMinimum)
}
// Shortcut for e.g. 2022 > 2021
if curVer[i] > v {
return nil
}
}

return nil
}

func checkNodeRpmOstreeVersion() error {
versionBytes, err := runGetOut("rpm-ostree", "--version")
if err != nil {
return err
}
var versionData rpmOstreeVersionOuter
if err := yaml.Unmarshal(versionBytes, &versionData); err != nil {
return fmt.Errorf("failed to parse rpm-ostree --version as YAML: %v", err)
}

if err := validateVersion(versionData); err != nil {
return fmt.Errorf("Too old rpm-ostree: %v", err)
}

return nil
}

func (r *RpmOstreeClient) Initialize() error {
// This replicates https://github.com/coreos/rpm-ostree/pull/2945
// and can be removed when we have a new enough rpm-ostree with
Expand Down
44 changes: 44 additions & 0 deletions pkg/daemon/rpm-ostree_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package daemon

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/util/yaml"
)

/*
* This file contains test code for the rpm-ostree client. It is meant to be used when
* testing the daemon and mocking the responses that would normally be executed by the
Expand Down Expand Up @@ -46,3 +54,39 @@ func (r RpmOstreeClientMock) GetStatus() (string, error) {
func (r RpmOstreeClientMock) GetBootedDeployment() (*RpmOstreeDeployment, error) {
return &RpmOstreeDeployment{}, nil
}

func TestParseVersion(t *testing.T) {
s := `
rpm-ostree:
Version: '2021.14'
Git: v2021.14
Features:
- bin-unit-tests
- compose
- rust
- fedora-integration
`
var outer rpmOstreeVersionOuter
assert.Nil(t, yaml.Unmarshal([]byte(s), &outer))
fmt.Printf("%v", outer)
assert.Equal(t, outer.Root.Version, "2021.14")
}

func TestValidateVersion(t *testing.T) {
for _, old := range []string{"2019.5", "2021.6"} {
v := rpmOstreeVersionOuter{
Root: rpmOstreeVersionData{
Version: old,
},
}
assert.NotNil(t, validateVersion(v))
}
for _, newver := range []string{"2021.14", "2021.15", "2022.1"} {
v := rpmOstreeVersionOuter{
Root: rpmOstreeVersionData{
Version: newver,
},
}
assert.Nil(t, validateVersion(v))
}
}