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
8 changes: 6 additions & 2 deletions pkg/apis/machineconfiguration.openshift.io/v1/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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 only uses the OSImageURL from first object and ignores it from rest.
// It uses the first non-empty OSImageURL.
func MergeMachineConfigs(configs []*MachineConfig) *MachineConfig {
if len(configs) == 0 {
return nil
Expand All @@ -21,7 +21,11 @@ func MergeMachineConfigs(configs []*MachineConfig) *MachineConfig {
outOSImageURL := configs[0].Spec.OSImageURL
outIgn := configs[0].Spec.Config
for idx := 1; idx < len(configs); idx++ {
outIgn = ignv2_2.Append(outIgn, configs[idx].Spec.Config)
config := configs[idx]
if outOSImageURL == "" {
outOSImageURL = config.Spec.OSImageURL
}
outIgn = ignv2_2.Append(outIgn, config.Spec.Config)
}

return &MachineConfig{
Expand Down
45 changes: 45 additions & 0 deletions pkg/apis/machineconfiguration.openshift.io/v1/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var (
Expand Down Expand Up @@ -139,3 +140,47 @@ func TestRemoveMachineConfigPoolCondition(t *testing.T) {
})
}
}

func TestMergeMachineConfigs(t *testing.T) {
var configs []*MachineConfig
configs = append(configs, &MachineConfig{
ObjectMeta: metav1.ObjectMeta{Name: "4"},
Spec: MachineConfigSpec{
OSImageURL: "",
},
})

configs = append(configs, &MachineConfig{
ObjectMeta: metav1.ObjectMeta{Name: "3"},
Spec: MachineConfigSpec{
OSImageURL:"example.com/os@sha256:notthetarget",
},
})

configs = append(configs, &MachineConfig{
ObjectMeta: metav1.ObjectMeta{Name: "0"},
Spec: MachineConfigSpec{
OSImageURL: "",
},
})

targetOSImageURL := "example.com/os@sha256:thetarget"
configs = append(configs, &MachineConfig{
ObjectMeta: metav1.ObjectMeta{Name: "1"},
Spec: MachineConfigSpec{
OSImageURL: targetOSImageURL,
},
})

configs = append(configs, &MachineConfig{
ObjectMeta: metav1.ObjectMeta{Name: "2"},
Spec: MachineConfigSpec{
OSImageURL: "",
},
})

merged := MergeMachineConfigs(configs)
if merged.Spec.OSImageURL != targetOSImageURL {
t.Errorf("OSImageURL expected: %s, received: %s", targetOSImageURL, merged.Spec.OSImageURL)
}
}