-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Support Intel RDT #4830
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
Support Intel RDT #4830
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| package rdt | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io/ioutil" | ||
|
|
||
| "github.com/pkg/errors" | ||
| "github.com/sirupsen/logrus" | ||
| "sigs.k8s.io/yaml" | ||
|
|
||
| "github.com/intel/goresctrl/pkg/rdt" | ||
| ) | ||
|
|
||
| const ( | ||
| // DefaultRdtConfigFile is the default value for RDT config file path | ||
| DefaultRdtConfigFile = "" | ||
| // ResctrlPrefix is the prefix used for class/closid directories under the resctrl filesystem | ||
| ResctrlPrefix = "" | ||
| ) | ||
|
|
||
| type Config struct { | ||
| supported bool | ||
| enabled bool | ||
| config *rdt.Config | ||
| } | ||
|
|
||
| // New creates a new RDT config instance | ||
| func New() *Config { | ||
| c := &Config{ | ||
| supported: true, | ||
| config: &rdt.Config{}, | ||
| } | ||
|
|
||
| rdt.SetLogger(logrus.StandardLogger()) | ||
|
|
||
| if err := rdt.Initialize(ResctrlPrefix); err != nil { | ||
| logrus.Infof("RDT is not enabled: %v", err) | ||
| c.supported = false | ||
| } | ||
| return c | ||
| } | ||
|
|
||
| // Supported returns true if RDT is enabled in the host system | ||
| func (c *Config) Supported() bool { | ||
| return c.supported | ||
| } | ||
|
|
||
| // Enabled returns true if RDT is enabled in CRI-O | ||
| func (c *Config) Enabled() bool { | ||
| return c.enabled | ||
| } | ||
|
|
||
| // Load loads and validates RDT config | ||
| func (c *Config) Load(path string) error { | ||
| c.enabled = false | ||
|
|
||
| if !c.Supported() { | ||
| logrus.Info("RDT not available in the host system") | ||
| return nil | ||
| } | ||
|
|
||
| if path == "" { | ||
| logrus.Info("No RDT config file specified, RDT not enabled") | ||
| return nil | ||
| } | ||
|
|
||
| tmpCfg, err := loadConfigFile(path) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := rdt.SetConfig(tmpCfg, true); err != nil { | ||
| return errors.Wrap(err, "configuring RDT failed") | ||
| } | ||
|
|
||
| logrus.Infof("RDT enabled, config successfully loaded from %q", path) | ||
| c.enabled = true | ||
| c.config = tmpCfg | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func loadConfigFile(path string) (*rdt.Config, error) { | ||
| data, err := ioutil.ReadFile(path) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. G304: Potential file inclusion via variable
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd ignore this as the config from sysadmin is supposed to be trusted input
marquiz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| return nil, errors.Wrap(err, "reading rdt config file failed") | ||
| } | ||
|
|
||
| c := &rdt.Config{} | ||
| if err = yaml.Unmarshal(data, c); err != nil { | ||
| return nil, errors.Wrap(err, "parsing RDT config failed") | ||
| } | ||
|
|
||
| return c, nil | ||
| } | ||
|
|
||
| func (c *Config) ContainerClassFromAnnotations(containerName string, containerAnnotations, podAnnotations map[string]string) (string, error) { | ||
| cls, err := rdt.ContainerClassFromAnnotations(containerName, containerAnnotations, podAnnotations) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| if cls != "" && !c.Enabled() { | ||
| return "", fmt.Errorf("RDT disabled, refusing to set RDT class of container %q to %q", containerName, cls) | ||
| } | ||
| return cls, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package rdt | ||
|
|
||
| import ( | ||
| "io/ioutil" | ||
|
|
||
| . "github.com/onsi/ginkgo" | ||
| . "github.com/onsi/gomega" | ||
| ) | ||
|
|
||
| func tempFileWithData(data string) string { | ||
| f := t.MustTempFile("") | ||
| Expect(ioutil.WriteFile(f, []byte(data), 0o644)).To(BeNil()) | ||
| return f | ||
| } | ||
|
|
||
| // The actual test suite | ||
| var _ = t.Describe("When parsing RDT config file", func() { | ||
| t.Describe("non-existent file", func() { | ||
| It("should return an error", func() { | ||
| _, err := loadConfigFile("non-existent-file") | ||
| Expect(err).NotTo(BeNil()) | ||
| }) | ||
| }) | ||
|
|
||
| t.Describe("invalid file format", func() { | ||
| It("should return an error", func() { | ||
| f := tempFileWithData(`partitions: | ||
| - foo | ||
| `) | ||
| _, err := loadConfigFile(f) | ||
| Expect(err).NotTo(BeNil()) | ||
| }) | ||
| }) | ||
|
|
||
| t.Describe("correct file format", func() { | ||
| It("should not return an error", func() { | ||
| f := tempFileWithData(`partitions: | ||
| default: | ||
| l3Allocation: 100% | ||
| classes: | ||
| default: | ||
| `) | ||
| _, err := loadConfigFile(f) | ||
| Expect(err).To(BeNil()) | ||
| }) | ||
| }) | ||
| }) |
Uh oh!
There was an error while loading. Please reload this page.