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

OCPVE-382: fix: add default state for crio config #700

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -530,6 +530,12 @@ func renderManagementCPUPinningConfig(cpuv2 *performancev2.CPU, src string) ([]b
return pinningConfigData.Bytes(), nil
}

// BootstrapWorkloadPinningMC creates an initial state MachineConfig resource that establishes an empty CPU set for both
// CRIO config and Kubelet config. The purpose is provide empty state initialization for both CRIO and Kubelet so that the nodes
// always start and join the cluster in a Workload Pinning configuration.
//
// When a performance profiles is created, they will override the config files in this MC with the desired CPU Set. If that performance
// profile were to be deleted later on, this initial MC will be the fallback that MCO re-renders and maintain a workload pinning configuration.
func BootstrapWorkloadPinningMC(role string, pinningMode *apiconfigv1.CPUPartitioningMode) (*machineconfigv1.MachineConfig, error) {
if pinningMode == nil {
return nil, fmt.Errorf("can not generate configs, CPUPartitioningMode is nil")
Expand All @@ -540,7 +546,28 @@ func BootstrapWorkloadPinningMC(role string, pinningMode *apiconfigv1.CPUPartiti
}

mode := 420
source := "data:text/plain;charset=utf-8;base64,ewogICJtYW5hZ2VtZW50IjogewogICAgImNwdXNldCI6ICIiCiAgfQp9Cg=="
/*
Contents for emptyKubeletConfig:
{
"management": {
"cpuset": ""
}
}

Contents for emptyCrioConfig:
[crio.runtime.workloads.management]
activation_annotation = "target.workload.openshift.io/management"
annotation_prefix = "resources.workload.openshift.io"
resources = { "cpushares" = 0, "cpuset" = "" }
*/
emptyKubeletConfig := "ewogICJtYW5hZ2VtZW50IjogewogICAgImNwdXNldCI6ICIiCiAgfQp9Cg=="
emptyCrioConfig := "W2NyaW8ucnVudGltZS53b3JrbG9hZHMubWFuYWdlbWVudF0KYWN0aXZhdGlvbl9hbm5vdGF0aW9uID0gInRhcmdldC53b3JrbG9hZC5vcGVuc2hpZnQuaW8vbWFuYWdlbWVudCIKYW5ub3RhdGlvbl9wcmVmaXggPSAicmVzb3VyY2VzLndvcmtsb2FkLm9wZW5zaGlmdC5pbyIKcmVzb3VyY2VzID0geyAiY3B1c2hhcmVzIiA9IDAsICJjcHVzZXQiID0gIiIgfQo="
eggfoobar marked this conversation as resolved.
Show resolved Hide resolved
emptyKubeletSource := fmt.Sprintf("%s,%s", defaultIgnitionContentSource, emptyKubeletConfig)
emptyCrioSource := fmt.Sprintf("%s,%s", defaultIgnitionContentSource, emptyCrioConfig)

// We add lower hierarchical config file name so as to not disturb any
// pre-existing files that users might have for workload pinning
crioDefaultPartitioningConfig := "01-workload-pinning-default.conf"

mc := &machineconfigv1.MachineConfig{
TypeMeta: metav1.TypeMeta{
Expand All @@ -561,17 +588,30 @@ func BootstrapWorkloadPinningMC(role string, pinningMode *apiconfigv1.CPUPartiti
Version: igntypes.MaxVersion.String(),
},
Storage: igntypes.Storage{
Files: []igntypes.File{{
Node: igntypes.Node{
Path: "/etc/kubernetes/openshift-workload-pinning",
Files: []igntypes.File{
{
Node: igntypes.Node{
Path: filepath.Join(kubernetesConfDir, ocpPartitioningConfig),
},
FileEmbedded1: igntypes.FileEmbedded1{
Contents: igntypes.Resource{
Source: &emptyKubeletSource,
},
Mode: &mode,
},
},
FileEmbedded1: igntypes.FileEmbedded1{
Contents: igntypes.Resource{
Source: &source,
{
Node: igntypes.Node{
Path: filepath.Join(crioConfd, crioDefaultPartitioningConfig),
},
FileEmbedded1: igntypes.FileEmbedded1{
Contents: igntypes.Resource{
Source: &emptyCrioSource,
},
Mode: &mode,
},
Mode: &mode,
},
}},
},
},
}

Expand Down
@@ -1,15 +1,19 @@
package machineconfig

import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"regexp"

"k8s.io/utils/pointer"

igntypes "github.com/coreos/ignition/v2/config/v3_2/types"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/types"
configv1 "github.com/openshift/api/config/v1"
performancev2 "github.com/openshift/cluster-node-tuning-operator/pkg/apis/performanceprofile/v2"
"sigs.k8s.io/yaml"

Expand Down Expand Up @@ -224,6 +228,71 @@ var _ = Describe("Pinning Config", func() {
})
})

var _ = Describe("Bootstrap Pinning Config", func() {

allNodes := configv1.CPUPartitioningAllNodes
none := configv1.CPUPartitioningNone
defaultEmptyCrio := []byte(`[crio.runtime.workloads.management]
activation_annotation = "target.workload.openshift.io/management"
annotation_prefix = "resources.workload.openshift.io"
resources = { "cpushares" = 0, "cpuset" = "" }
`)
defaultEmptyKubelet := []byte(`{
"management": {
"cpuset": ""
}
}
`)

expected := map[string][]byte{
"/etc/kubernetes/openshift-workload-pinning": defaultEmptyKubelet,
"/etc/crio/crio.conf.d/01-workload-pinning-default.conf": defaultEmptyCrio,
}

Context("should generate config", func() {
It("when cpu partitioning set", func() {
mc, err := BootstrapWorkloadPinningMC("master", &allNodes)
Expect(err).ToNot(HaveOccurred())
Expect(mc).ToNot(BeNil())
})

It("with correct ignition configs", func() {
mc, err := BootstrapWorkloadPinningMC("master", &allNodes)
Expect(err).ToNot(HaveOccurred())

result := igntypes.Config{}

err = json.Unmarshal(mc.Spec.Config.Raw, &result)
Expect(err).ToNot(HaveOccurred())
Expect(result.Storage.Files).To(HaveLen(2))

for _, f := range result.Storage.Files {
nonEncodedContent, ok := expected[f.Node.Path]
Expect(ok).To(BeTrue(), "path %s is not present in expected map", f.Node.Path)
encoded := base64.StdEncoding.EncodeToString(nonEncodedContent)
Expect(f.Contents.Source).ToNot(BeNil())
Expect(*f.Contents.Source).To(
Equal(fmt.Sprintf("%s,%s", defaultIgnitionContentSource, encoded)),
"path %s contains content mismatch", f.Node.Path)
}
})
})

Context("should not be generated", func() {
It("with cpu partitioning set to none", func() {
mc, err := BootstrapWorkloadPinningMC("master", &none)
Expect(err).ToNot(HaveOccurred())
Expect(mc).To(BeNil())
})

It("with nil being provided", func() {
mc, err := BootstrapWorkloadPinningMC("master", nil)
Expect(err).To(HaveOccurred())
Expect(mc).To(BeNil())
})
})
})

func removeAllWhiteSpace(str string) string {
return spaceRegex.ReplaceAllString(str, "")
}
Expand Down