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
19 changes: 9 additions & 10 deletions docs/MachineConfiguration.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ type MachineConfig struct {
}

type MachineConfigSpec struct {
// OSImageURL specifies the remote location that will be used to
// fetch the OS. This must be in the canonical $name@$digest format.
OSImageURL string `json:"osImageURL"`
// Config is a Ignition Config object.
Config ign.Config `json:"config"`
KernelArguments []string `json:"kernelArguments"`
}
```

Expand All @@ -74,13 +72,11 @@ spec:
filesystem: root
mode: 384
path: /root/myfile
osImageURL: quay.io/openshift/rhcos@sha256:...
```

(Notice how it's the usual Ignition config object *inplace*, not as a JSON
string -- also note the `config` and `osImageURL` casing follow the
`json:` markers in the definition above, of course this follows for the
Ignition config keys as well).
Notice how it's the usual Ignition config object *inplace*, not as a JSON
string -- also note the casing follows the `json:` markers in the definition above, of course this follows for the
Ignition config keys as well.

### How to create generated MachineConfig

Expand All @@ -94,8 +90,11 @@ Ignition config keys as well).

* Use the openshift defined Ignition config as base and append all the other Ignition configs in a pre-defined order.

### OSImageURL
### KernelArguments

This extends the host's kernel arguments. Use this for e.g. [nosmt](https://access.redhat.com/solutions/rhel-smt).

The operating system used to first boot a machine is platform dependent. For example, on AWS AMIs are used to bring up EC2Instances. But for day-2 updates of the cluster, the MachineConfigDaemon uses the `OSImageURL` to fetch new operating system during updates. An example for OSImageURL is `quay.io/openshift/$CONTAINER@sha256:$DIGEST`. The digest is required to ensure there are no race conditions.
### OSImageURL

You should not attempt to set this field; it is controlled by the operator and injected directly into the final `rendered-` config.
For more information, see [OSUpgrades.md].
4 changes: 4 additions & 0 deletions lib/resourcemerge/machineconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func EnsureMachineConfigPool(modified *bool, existing *mcfgv1.MachineConfigPool,

func ensureMachineConfigSpec(modified *bool, existing *mcfgv1.MachineConfigSpec, required mcfgv1.MachineConfigSpec) {
setStringIfSet(modified, &existing.OSImageURL, required.OSImageURL)
if !equality.Semantic.DeepEqual(existing.KernelArguments, required.KernelArguments) {
*modified = true
(*existing).KernelArguments = required.KernelArguments
}
if !equality.Semantic.DeepEqual(existing.Config, required.Config) {
*modified = true
(*existing).Config = required.Config
Expand Down
8 changes: 7 additions & 1 deletion pkg/apis/machineconfiguration.openshift.io/v1/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (

// MergeMachineConfigs combines multiple machineconfig objects into one object.
// It sorts all the configs in increasing order of their name.
// It uses the Ign config from first object as base and appends all the rest.
// It uses the Ignition config from first object as base and appends all the rest.
// Kernel arguments are concatenated.
// It uses only the OSImageURL provided by the CVO and ignores any MC provided OSImageURL.
func MergeMachineConfigs(configs []*MachineConfig, osImageURL string) *MachineConfig {
if len(configs) == 0 {
Expand All @@ -23,10 +24,15 @@ func MergeMachineConfigs(configs []*MachineConfig, osImageURL string) *MachineCo
for idx := 1; idx < len(configs); idx++ {
outIgn = ign.Append(outIgn, configs[idx].Spec.Config)
}
kargs := []string{}
for _, cfg := range configs {
kargs = append(kargs, cfg.Spec.KernelArguments...)
}

return &MachineConfig{
Spec: MachineConfigSpec{
OSImageURL: osImageURL,
KernelArguments: kargs,
Config: outIgn,
},
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/machineconfiguration.openshift.io/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ type MachineConfigSpec struct {
OSImageURL string `json:"osImageURL"`
// Config is a Ignition Config object.
Config igntypes.Config `json:"config"`

KernelArguments []string `json:"kernelArguments"`
Comment thread
cgwalters marked this conversation as resolved.
Resolved
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
50 changes: 50 additions & 0 deletions pkg/daemon/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ func (dn *Daemon) update(oldConfig, newConfig *mcfgv1.MachineConfig) (retErr err
}
}()

// kargs
if err := dn.updateKernelArguments(oldConfig, newConfig); err != nil {
return err
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

and I've now found that we need to roll this back if we fail 😄

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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


return dn.updateOSAndReboot(newConfig)
}

Expand All @@ -242,6 +247,7 @@ func (dn *Daemon) update(oldConfig, newConfig *mcfgv1.MachineConfig) (retErr err
// improved logging.
type MachineConfigDiff struct {
osUpdate bool
kargs bool
passwd bool
files bool
units bool
Expand Down Expand Up @@ -355,6 +361,7 @@ func Reconcilable(oldConfig, newConfig *mcfgv1.MachineConfig) (*MachineConfigDif
glog.V(2).Info("Configs are reconcilable")
return &MachineConfigDiff {
osUpdate: oldConfig.Spec.OSImageURL != newConfig.Spec.OSImageURL,
kargs: !reflect.DeepEqual(oldConfig.Spec.KernelArguments, newConfig.Spec.KernelArguments),
passwd: passwdChanged,
files: !reflect.DeepEqual(oldIgn.Storage.Files, newIgn.Storage.Files),
units: !reflect.DeepEqual(oldIgn.Systemd.Units, newIgn.Systemd.Units),
Expand Down Expand Up @@ -382,6 +389,49 @@ func verifyUserFields(pwdUser igntypes.PasswdUser) error {
return nil
}

// generateKargsCommand performs a diff between the old/new MC kernelArguments,
// and generates the command line arguments suitable for `rpm-ostree kargs`.
// Note what we really should be doing though is also looking at the *current*
// kernel arguments in case there was drift. But doing that requires us knowing
// what the "base" arguments are. See https://github.com/ostreedev/ostree/issues/479
func generateKargsCommand(oldConfig, newConfig *mcfgv1.MachineConfig) []string {
Comment thread
cgwalters marked this conversation as resolved.
Resolved
oldKargs := make(map[string]bool)
for _, arg := range oldConfig.Spec.KernelArguments {
oldKargs[arg] = true
}
newKargs := make(map[string]bool)
for _, arg := range newConfig.Spec.KernelArguments {
newKargs[arg] = true
}
cmdArgs := []string{}
for _, arg := range oldConfig.Spec.KernelArguments {
if !newKargs[arg] {
cmdArgs = append(cmdArgs, "--delete=" + arg)
}
}
for _, arg := range newConfig.Spec.KernelArguments {
if !oldKargs[arg] {
cmdArgs = append(cmdArgs, "--append=" + arg)
}
}
return cmdArgs
}

// updateKernelArguments adjusts the kernel args
func (dn *Daemon) updateKernelArguments(oldConfig, newConfig *mcfgv1.MachineConfig) error {
diff := generateKargsCommand(oldConfig, newConfig)
if len(diff) == 0 {
return nil
}
if dn.OperatingSystem != machineConfigDaemonOSRHCOS {
return fmt.Errorf("Updating kargs on non-RHCOS nodes is not supported: %v", diff)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

now that I'm looking at this, should we just return nicely as we do for updateOS 🤔

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

}

args := append([]string{"kargs"}, diff...)
dn.logSystem("Running rpm-ostree %v", args)
return exec.Command("rpm-ostree", args...).Run()
Comment thread
cgwalters marked this conversation as resolved.
Resolved
}

// updateFiles writes files specified by the nodeconfig to disk. it also writes
// systemd units. there is no support for multiple filesystems at this point.
//
Expand Down
89 changes: 89 additions & 0 deletions pkg/daemon/update_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package daemon

import (
"strings"
"time"
"math/rand"
"fmt"
"os/exec"
"testing"
Expand Down Expand Up @@ -223,6 +226,92 @@ func TestReconcilableDiff(t *testing.T) {
assert.Equal(t, diff.files, false)
}

func TestKernelAguments(t *testing.T) {
oldMcfg := &mcfgv1.MachineConfig{
Spec: mcfgv1.MachineConfigSpec{
Config: ctrlcommon.NewIgnConfig(),
KernelArguments: []string {"nosmt", "foo", "baz=test"},
},
}
tests := []struct {
args []string
deletions []string
additions []string
}{
{
args: nil,
deletions: []string{"nosmt", "foo", "baz=test"},
additions: []string{},
},
{
args: oldMcfg.Spec.KernelArguments,
deletions: []string{},
additions: []string{},
},
{
args: append(oldMcfg.Spec.KernelArguments, "hello=world"),
deletions: []string{},
additions: []string{"hello=world"},
},
{
args: []string{"foo", "hello=world"},
deletions: []string{"nosmt", "baz=test"},
additions: []string{"hello=world"},
},
{
args: append([]string{"baz=othertest"}, oldMcfg.Spec.KernelArguments...),
deletions: []string{},
additions: []string{"baz=othertest"},
},
}
rand.Seed(time.Now().UnixNano())
for idx, test := range tests {
t.Run(fmt.Sprintf("case#%d", idx), func(t *testing.T) {
newMcfg := &mcfgv1.MachineConfig{
Spec: mcfgv1.MachineConfigSpec{
Config: ctrlcommon.NewIgnConfig(),
KernelArguments: test.args,
},
}
// Randomize the order of both old/new to sort out ordering issues.
rand.Shuffle(len(test.args), func(i, j int) {
test.args[i], test.args[j] = test.args[j], test.args[i]
})
oldKargs := oldMcfg.Spec.KernelArguments
rand.Shuffle(len(oldMcfg.Spec.KernelArguments), func(i, j int) {
oldKargs[i], oldKargs[j] = oldKargs[j], oldKargs[i]
})
res := generateKargsCommand(oldMcfg, newMcfg)
additionsExpected := make(map[string]bool)
for _, k := range test.additions {
additionsExpected[k] = true
}
deletionsExpected := make(map[string]bool)
for _, k := range test.deletions {
deletionsExpected[k] = true
}
for _, a := range res {
parts := strings.SplitN(a, "=", 2)
if len(parts) != 2 {
t.Fatalf("Bad karg %v", a)
}
arg := parts[1]
if parts[0] == "--append" {
if !additionsExpected[arg] {
t.Errorf("Unexpected addition %s", arg)
}
} else if parts[0] == "--delete" {
if !deletionsExpected[arg] {
t.Errorf("Unexpected deletion %s", arg)
}
} else {
t.Fatalf("Bad karg %s", a)
}
}
})
}
}

func TestReconcilableSSH(t *testing.T) {
// Check that updating SSH Key of user core supported
oldMcfg := &mcfgv1.MachineConfig{
Expand Down
88 changes: 77 additions & 11 deletions test/e2e/mcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/openshift/machine-config-operator/test/e2e/framework"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
Expand Down Expand Up @@ -189,6 +190,26 @@ func bumpPoolMaxUnavailableTo(t *testing.T, cs *framework.ClientSet, max int) {
require.Nil(t, err)
}

func mcdForNode(cs *framework.ClientSet, node *corev1.Node) (*corev1.Pod, error) {
// find the MCD pod that has spec.nodeNAME = node.Name and get its name:
listOptions := metav1.ListOptions{
FieldSelector: fields.SelectorFromSet(fields.Set{"spec.nodeName": node.Name}).String(),
}
listOptions.LabelSelector = labels.SelectorFromSet(labels.Set{"k8s-app": "machine-config-daemon"}).String()

mcdList, err := cs.Pods("openshift-machine-config-operator").List(listOptions)
if err != nil {
return nil, err
}
if len(mcdList.Items) != 1 {
if len(mcdList.Items) == 0 {
return nil, fmt.Errorf("Failed to find MCD for node %s", node.Name)
}
return nil, fmt.Errorf("Too many (%d) MCDs for node %s", len(mcdList.Items), node.Name)
}
return &mcdList.Items[0], nil
}

func TestUpdateSSH(t *testing.T) {
cs := framework.NewClientSet("")
bumpPoolMaxUnavailableTo(t, cs, 3)
Expand Down Expand Up @@ -233,20 +254,11 @@ func TestUpdateSSH(t *testing.T) {
for _, node := range nodes {
assert.Equal(t, node.Annotations[constants.CurrentMachineConfigAnnotationKey], renderedConfig)
assert.Equal(t, node.Annotations[constants.MachineConfigDaemonStateAnnotationKey], constants.MachineConfigDaemonStateDone)
// find the MCD pod that has spec.nodeNAME = node.Name and get its name:
listOptions := metav1.ListOptions{
FieldSelector: fields.SelectorFromSet(fields.Set{"spec.nodeName": node.Name}).String(),
}
listOptions.LabelSelector = labels.SelectorFromSet(labels.Set{"k8s-app": "machine-config-daemon"}).String()

mcdList, err := cs.Pods("openshift-machine-config-operator").List(listOptions)
mcd, err := mcdForNode(cs, &node)
if err != nil {
t.Fatal(err)
}
if len(mcdList.Items) != 1 {
t.Fatalf("Failed to find MCD for node %s", node.Name)
}
mcdName := mcdList.Items[0].ObjectMeta.Name
mcdName := mcd.ObjectMeta.Name

// now rsh into that daemon and grep the authorized key file to check if 1234_test was written
// must do both commands in same shell, combine commands into one exec.Command()
Expand All @@ -262,6 +274,60 @@ func TestUpdateSSH(t *testing.T) {
}
}


func TestKernelArguments(t *testing.T) {
cs := framework.NewClientSet("")
bumpPoolMaxUnavailableTo(t, cs, 3)
kargsMC := &mcv1.MachineConfig{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("kargs-%s", uuid.NewUUID()),
Labels: mcLabelForWorkers(),
},
Spec: mcv1.MachineConfigSpec{
Config: ctrlcommon.NewIgnConfig(),
KernelArguments: []string{"nosmt", "foo=bar"},
},
}

_, err := cs.MachineConfigs().Create(kargsMC)
if err != nil {
t.Fatal(err)
}
t.Logf("Created %s", kargsMC.Name)
renderedConfig, err := waitForRenderedConfig(t, cs, "worker", kargsMC.Name)
if err != nil {
t.Fatal(err)
}
if err := waitForPoolComplete(t, cs, "worker", renderedConfig); err != nil {
t.Fatal(err)
}
nodes, err := getNodesByRole(cs, "worker")
if err != nil {
t.Fatal(err)
}
for _, node := range nodes {
assert.Equal(t, node.Annotations[constants.CurrentMachineConfigAnnotationKey], renderedConfig)
assert.Equal(t, node.Annotations[constants.MachineConfigDaemonStateAnnotationKey], constants.MachineConfigDaemonStateDone)
mcd, err := mcdForNode(cs, &node)
if err != nil {
t.Fatal(err)
}
mcdName := mcd.ObjectMeta.Name
kargsBytes, err := exec.Command("oc", "rsh", "-n", "openshift-machine-config-operator", mcdName,
"cat", "/rootfs/proc/cmdline").CombinedOutput()
if err != nil {
t.Fatal(err)
}
kargs := string(kargsBytes)
for _, v := range kargsMC.Spec.KernelArguments {
if !strings.Contains(kargs, v) {
t.Fatalf("Missing '%s' in kargs", v)
}
}
t.Logf("Node %s has expected kargs", node.Name)
}
}

func getNodesByRole(cs *framework.ClientSet, role string) ([]v1.Node, error) {
listOptions := metav1.ListOptions{
LabelSelector: labels.SelectorFromSet(labels.Set{fmt.Sprintf("node-role.kubernetes.io/%s", role): ""}).String(),
Expand Down