Skip to content
Closed
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
1 change: 1 addition & 0 deletions cmd/machine-config-controller/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func createControllers(ctx *controllercommon.ControllerContext) []controllercomm
// The node controller consumes data written by the above
node.New(
ctx.InformerFactory.Machineconfiguration().V1().MachineConfigPools(),
ctx.InformerFactory.Machineconfiguration().V1().MachineConfigs(),
ctx.KubeInformerFactory.Core().V1().Nodes(),
ctx.ClientBuilder.KubeClientOrDie("node-update-controller"),
ctx.ClientBuilder.MachineConfigClientOrDie("node-update-controller"),
Expand Down
8 changes: 8 additions & 0 deletions lib/resourcemerge/machineconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ func ensureMachineConfigSpec(modified *bool, existing *mcfgv1.MachineConfigSpec,
*modified = true
(*existing).Config = required.Config
}
if !equality.Semantic.DeepEqual(existing.Labels, required.Labels) {
*modified = true
(*existing).Labels = required.Labels
}
if !equality.Semantic.DeepEqual(existing.Taints, required.Taints) {
*modified = true
(*existing).Taints = required.Taints
}
}

func ensureControllerConfigSpec(modified *bool, existing *mcfgv1.ControllerConfigSpec, required mcfgv1.ControllerConfigSpec) {
Expand Down
76 changes: 76 additions & 0 deletions pkg/apis/machineconfiguration.openshift.io/v1/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,76 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// simplifyLabels takes a slice of MachineConfigLabels and returns a slice. The input
// slice may contain an arbitrary number of labels and may have multiple exist and notexist
// entries. The output slice will container 0, 1, or 2 entries and no more than a single
// exists label set and a single notexist label set.
func simplifyLabels(labels []MachineConfigLabels) []MachineConfigLabels {
exist := map[string]string{}
notexist := map[string]string{}

for _, mcl := range labels {
for key, value := range mcl.Labels {
if mcl.Exist {
exist[key] = value
if _, ok := notexist[key]; ok {
delete(notexist, key)
}
} else {
notexist[key] = value
if _, ok := exist[key]; ok {
delete(exist, key)
}
}
}
}
out := []MachineConfigLabels{}
if len(exist) > 0 {
out = append(out, MachineConfigLabels{
Exist: true,
Labels: exist,
})
}
if len(notexist) > 0 {
out = append(out, MachineConfigLabels{
Exist: false,
Labels: notexist,
})
}
return out
}

func mergeLabels(l1 []MachineConfigLabels, l2 []MachineConfigLabels) []MachineConfigLabels {
out := append(l1, l2...)
return simplifyLabels(out)
}

func removeTaint(mcts []MachineConfigTaint, taint MachineConfigTaint) []MachineConfigTaint {
for i, mct := range mcts {
if mct.Taint.Key == taint.Taint.Key {
return append(mcts[:i], mcts[i+1:]...)
}
}
return mcts
}

// Create a new list of taints, one by one, removing any previous taints with the
// same key.
func simplifyTaints(mcts []MachineConfigTaint) []MachineConfigTaint {
out := []MachineConfigTaint{}
for i, _ := range mcts {
taint := &mcts[i]
out = removeTaint(out, *taint)
out = append(out, *taint)
}
return out
}

func mergeTaints(t1 []MachineConfigTaint, t2 []MachineConfigTaint) []MachineConfigTaint {
out := append(t1, t2...)
return simplifyTaints(out)
}

// MergeMachineConfigs combines multiple machineconfig objects into one object.
// It sorts all the configs in increasing order of their name.
// It uses the Ignition config from first object as base and appends all the rest.
Expand All @@ -25,14 +95,20 @@ func MergeMachineConfigs(configs []*MachineConfig, osImageURL string) *MachineCo
outIgn = ign.Append(outIgn, configs[idx].Spec.Config)
}
kargs := []string{}
labels := []MachineConfigLabels{}
taints := []MachineConfigTaint{}
for _, cfg := range configs {
kargs = append(kargs, cfg.Spec.KernelArguments...)
labels = mergeLabels(labels, cfg.Spec.Labels)
taints = mergeTaints(taints, cfg.Spec.Taints)
}

return &MachineConfig{
Spec: MachineConfigSpec{
OSImageURL: osImageURL,
KernelArguments: kargs,
Labels: labels,
Taints: taints,
Config: outIgn,
},
}
Expand Down
155 changes: 155 additions & 0 deletions pkg/apis/machineconfiguration.openshift.io/v1/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,158 @@ func TestIsControllerConfigCompleted(t *testing.T) {
})
}
}

func TestMergeLabels(t *testing.T) {
in1 := []MachineConfigLabels{
{
Labels: map[string]string{
"existkey1": "existvalue1",
"noexistkey1": "shouldnotexist",
},
Exist: true,
},
{
Labels: map[string]string{
"noexistkey1": "noexistvalue1",
"existkey5": "shouldexist",
},
Exist: false,
},
{
Labels: map[string]string{
"existkey2": "existvalue2",
},
Exist: true,
},
{
Labels: map[string]string{
"noexistkey2": "noexistvalue2",
},
Exist: false,
},
}
in2 := []MachineConfigLabels{
{
Labels: map[string]string{
"existkey3": "existvalue3",
"existkey4": "existvalue4",
},
Exist: true,
},
{
Labels: map[string]string{
"noexistkey3": "noexistvalue3",
"noexistkey4": "noexistvalue4",
},
Exist: false,
},
{
Labels: map[string]string{
"existkey1": "overwriteexistkey1",
"existkey5": "existvalue5",
},
Exist: true,
},
{
Labels: map[string]string{
"noexistkey5": "",
},
Exist: false,
},
}
expected := []MachineConfigLabels{
{
Labels: map[string]string{
"existkey1": "overwriteexistkey1",
"existkey2": "existvalue2",
"existkey3": "existvalue3",
"existkey4": "existvalue4",
"existkey5": "existvalue5",
},
Exist: true,
},
{
Labels: map[string]string{
"noexistkey1": "noexistvalue1",
"noexistkey2": "noexistvalue2",
"noexistkey3": "noexistvalue3",
"noexistkey4": "noexistvalue4",
"noexistkey5": "",
},
Exist: false,
},
}
found := mergeLabels(in1, in2)
if !reflect.DeepEqual(found, expected) {
t.Fatalf("expected: %v got %v", expected, found)
}
}

func TestMergeTaints(t *testing.T) {
in1 := []MachineConfigTaint{
{
Taint: corev1.Taint{
Key: "existkey1",
Value: "value1",
Effect: "NoSchedule",
},
Exist: true,
},
{
Taint: corev1.Taint{
Key: "noexistkey1",
Value: "value1",
Effect: "NoSchedule",
},
Exist: false,
},
{
Taint: corev1.Taint{
Key: "noexistkey2",
Value: "invalidvalue",
Effect: "invalideffect",
},
Exist: true,
},
}
in2 := []MachineConfigTaint{
{
Taint: corev1.Taint{
Key: "noexistkey2",
Value: "value2",
Effect: "NoSchedule",
},
Exist: false,
},
}
expected := []MachineConfigTaint{
{
Taint: corev1.Taint{
Key: "existkey1",
Value: "value1",
Effect: "NoSchedule",
},
Exist: true,
},
{
Taint: corev1.Taint{
Key: "noexistkey1",
Value: "value1",
Effect: "NoSchedule",
},
Exist: false,
},
{
Taint: corev1.Taint{
Key: "noexistkey2",
Value: "value2",
Effect: "NoSchedule",
},
Exist: false,
},
}
found := mergeTaints(in1, in2)
if !reflect.DeepEqual(found, expected) {
t.Fatalf("expected: %v got %v", expected, found)
}
}
14 changes: 14 additions & 0 deletions pkg/apis/machineconfiguration.openshift.io/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,23 @@ type MachineConfigSpec struct {
// Config is a Ignition Config object.
Config igntypes.Config `json:"config"`

// Labels and taints to use when registering the node with and to continuously apply
Labels []MachineConfigLabels `json:labels`
Taints []MachineConfigTaint `json:taints`

KernelArguments []string `json:"kernelArguments"`
}

type MachineConfigLabels struct {
Labels map[string]string `json:"labels"`
Exist bool `json:"exist"`
}

type MachineConfigTaint struct {
Taint corev1.Taint `json:taint`
Exist bool `json:"exist"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// MachineConfigList is a list of MachineConfig resources
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions pkg/controller/kubelet-config/kubelet_config_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,13 @@ func (ctrl *Controller) syncKubeletConfig(key string) error {
// Get MachineConfig
managedKey := getManagedKubeletConfigKey(pool)
mc, err := ctrl.client.MachineconfigurationV1().MachineConfigs().Get(managedKey, metav1.GetOptions{})
if err != nil && !macherrors.IsNotFound(err) {
isNotFound := macherrors.IsNotFound(err)
if err != nil && !isNotFound {
return ctrl.syncStatusOnly(cfg, err, "could not find MachineConfig: %v", managedKey)
} else if isNotFound {
ignConfig := ctrlcommon.NewIgnConfig()
mc = mtmpl.MachineConfigFromIgnConfig(role, managedKey, &ignConfig)
}
isNotFound := macherrors.IsNotFound(err)
// Generate the original KubeletConfig
originalKubeletIgn, err := ctrl.generateOriginalKubeletConfig(role)
if err != nil {
Expand Down Expand Up @@ -465,10 +468,6 @@ func (ctrl *Controller) syncKubeletConfig(key string) error {
if err != nil {
return ctrl.syncStatusOnly(cfg, err, "could not encode YAML: %v", err)
}
if isNotFound {
ignConfig := ctrlcommon.NewIgnConfig()
mc = mtmpl.MachineConfigFromIgnConfig(role, managedKey, &ignConfig)
}
mc.Spec.Config = createNewKubeletIgnition(cfgYAML)
mc.SetAnnotations(map[string]string{
ctrlcommon.GeneratedByControllerVersionAnnotationKey: version.Hash,
Expand Down
Loading