-
Notifications
You must be signed in to change notification settings - Fork 410
[no-relnote] Refactor AddRuntime #1279
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
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 |
---|---|---|
|
@@ -28,29 +28,37 @@ func (c *Config) AddRuntime(name string, path string, setAsDefault bool) error { | |
if c == nil || c.Tree == nil { | ||
return fmt.Errorf("config is nil") | ||
} | ||
config := *c.Tree | ||
|
||
config.Set("version", c.Version) | ||
defaultRuntimeOptions := c.GetDefaultRuntimeOptions() | ||
return c.AddRuntimeWithOptions(name, path, setAsDefault, defaultRuntimeOptions) | ||
} | ||
|
||
func (c *Config) GetDefaultRuntimeOptions() interface{} { | ||
runtimeNamesForConfig := engine.GetLowLevelRuntimes(c) | ||
for _, r := range runtimeNamesForConfig { | ||
options := config.GetSubtreeByPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", r}) | ||
if options == nil { | ||
continue | ||
options := c.GetSubtreeByPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", r}) | ||
if options != nil { | ||
c.Logger.Debugf("Using options from runtime %v: %v", r, options) | ||
return options.Copy() | ||
} | ||
c.Logger.Debugf("using options from runtime %v: %v", r, options) | ||
config.SetPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", name}, options.Copy()) | ||
break | ||
} | ||
c.Logger.Warningf("Could not infer options from runtimes %v", runtimeNamesForConfig) | ||
options, _ := toml.TreeFromMap(map[string]interface{}{ | ||
"runtime_type": c.RuntimeType, | ||
"runtime_root": "", | ||
"runtime_engine": "", | ||
"privileged_without_host_devices": false, | ||
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 wonder if, on this fallback, we should add the 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. The intent of this change is not to add additional behaviour. It is a refactor. |
||
}) | ||
return options | ||
} | ||
|
||
if config.GetPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", name}) == nil { | ||
c.Logger.Warningf("could not infer options from runtimes %v; using defaults", runtimeNamesForConfig) | ||
config.SetPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", name, "runtime_type"}, c.RuntimeType) | ||
config.SetPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", name, "runtime_root"}, "") | ||
config.SetPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", name, "runtime_engine"}, "") | ||
config.SetPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", name, "privileged_without_host_devices"}, false) | ||
} | ||
func (c *Config) AddRuntimeWithOptions(name string, path string, setAsDefault bool, options interface{}) error { | ||
config := *c.Tree | ||
|
||
config.Set("version", c.Version) | ||
|
||
if options != nil { | ||
config.SetPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", name}, options) | ||
} | ||
if len(c.ContainerAnnotations) > 0 { | ||
annotations, err := c.getRuntimeAnnotations([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", name, "container_annotations"}) | ||
if err != nil { | ||
|
@@ -65,7 +73,6 @@ func (c *Config) AddRuntime(name string, path string, setAsDefault bool) error { | |
if setAsDefault { | ||
config.SetPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "default_runtime_name"}, name) | ||
} | ||
|
||
*c.Tree = config | ||
return nil | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,59 +33,36 @@ func (c *ConfigV1) AddRuntime(name string, path string, setAsDefault bool) error | |
if c == nil || c.Tree == nil { | ||
return fmt.Errorf("config is nil") | ||
} | ||
defaultRuntimeOptions := c.GetDefaultRuntimeOptions() | ||
return c.AddRuntimeWithOptions(name, path, setAsDefault, defaultRuntimeOptions) | ||
} | ||
|
||
config := *c.Tree | ||
|
||
config.Set("version", int64(1)) | ||
|
||
runtimeNamesForConfig := engine.GetLowLevelRuntimes(c) | ||
for _, r := range runtimeNamesForConfig { | ||
options := config.GetSubtreeByPath([]string{"plugins", "cri", "containerd", "runtimes", r}) | ||
if options == nil { | ||
continue | ||
} | ||
c.Logger.Debugf("using options from runtime %v: %v", r, options) | ||
config.SetPath([]string{"plugins", "cri", "containerd", "runtimes", name}, options.Copy()) | ||
break | ||
|
||
} | ||
|
||
if config.GetPath([]string{"plugins", "cri", "containerd", "runtimes", name}) == nil { | ||
c.Logger.Warningf("could not infer options from runtimes %v; using defaults", runtimeNamesForConfig) | ||
config.SetPath([]string{"plugins", "cri", "containerd", "runtimes", name, "runtime_type"}, c.RuntimeType) | ||
config.SetPath([]string{"plugins", "cri", "containerd", "runtimes", name, "runtime_root"}, "") | ||
config.SetPath([]string{"plugins", "cri", "containerd", "runtimes", name, "runtime_engine"}, "") | ||
config.SetPath([]string{"plugins", "cri", "containerd", "runtimes", name, "privileged_without_host_devices"}, false) | ||
} | ||
func (c *ConfigV1) GetDefaultRuntimeOptions() interface{} { | ||
return (*Config)(c).GetDefaultRuntimeOptions() | ||
} | ||
|
||
if len(c.ContainerAnnotations) > 0 { | ||
annotations, err := (*Config)(c).getRuntimeAnnotations([]string{"plugins", "cri", "containerd", "runtimes", name, "container_annotations"}) | ||
if err != nil { | ||
return err | ||
} | ||
annotations = append(c.ContainerAnnotations, annotations...) | ||
config.SetPath([]string{"plugins", "cri", "containerd", "runtimes", name, "container_annotations"}, annotations) | ||
func (c *ConfigV1) AddRuntimeWithOptions(name string, path string, setAsDefault bool, options interface{}) error { | ||
if err := (*Config)(c).AddRuntimeWithOptions(name, path, setAsDefault && !c.UseLegacyConfig, options); err != nil { | ||
return err | ||
} | ||
|
||
config.SetPath([]string{"plugins", "cri", "containerd", "runtimes", name, "options", "BinaryName"}, path) | ||
config := *c.Tree | ||
config.SetPath([]string{"plugins", "cri", "containerd", "runtimes", name, "options", "Runtime"}, path) | ||
*c.Tree = config | ||
|
||
if setAsDefault { | ||
if !c.UseLegacyConfig { | ||
config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime_name"}, name) | ||
} else { | ||
// Note: This is deprecated in containerd 1.4.0 and will be removed in 1.5.0 | ||
if config.GetPath([]string{"plugins", "cri", "containerd", "default_runtime"}) == nil { | ||
config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime", "runtime_type"}, c.RuntimeType) | ||
config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime", "runtime_root"}, "") | ||
config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime", "runtime_engine"}, "") | ||
config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime", "privileged_without_host_devices"}, false) | ||
} | ||
config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime", "options", "BinaryName"}, path) | ||
config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime", "options", "Runtime"}, path) | ||
} | ||
if !c.UseLegacyConfig || !setAsDefault { | ||
return nil | ||
} | ||
|
||
config = *c.Tree | ||
// Note: This is deprecated in containerd 1.4.0 and will be removed in 1.5.0 | ||
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. Should we edit the note, now that 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. The intent of this change is not to add additional behaviour. It is a refactor. |
||
if config.GetPath([]string{"plugins", "cri", "containerd", "default_runtime"}) == nil { | ||
config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime", "runtime_type"}, c.RuntimeType) | ||
config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime", "runtime_root"}, "") | ||
config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime", "runtime_engine"}, "") | ||
config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime", "privileged_without_host_devices"}, false) | ||
} | ||
config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime", "options", "BinaryName"}, path) | ||
config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime", "options", "Runtime"}, path) | ||
*c.Tree = config | ||
return nil | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -74,31 +74,42 @@ func New(opts ...Option) (engine.Interface, error) { | |||||||||
return &cfg, nil | ||||||||||
} | ||||||||||
|
||||||||||
// AddRuntime adds a new runtime to the crio config | ||||||||||
// AddRuntime adds a new runtime to the crio config. | ||||||||||
// The runtime options are extracted from the default runtime and the applicable | ||||||||||
// settings are overridden. | ||||||||||
func (c *Config) AddRuntime(name string, path string, setAsDefault bool) error { | ||||||||||
if c == nil { | ||||||||||
if c == nil || c.Tree == nil { | ||||||||||
return fmt.Errorf("config is nil") | ||||||||||
} | ||||||||||
defaultRuntimeOptions := c.GetDefaultRuntimeOptions() | ||||||||||
return c.AddRuntimeWithOptions(name, path, setAsDefault, defaultRuntimeOptions) | ||||||||||
Comment on lines
+84
to
+85
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. [nitpick] The variable name
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||
} | ||||||||||
|
||||||||||
config := *c.Tree | ||||||||||
|
||||||||||
func (c *Config) GetDefaultRuntimeOptions() interface{} { | ||||||||||
runtimeNamesForConfig := engine.GetLowLevelRuntimes(c) | ||||||||||
for _, r := range runtimeNamesForConfig { | ||||||||||
if options, ok := config.GetPath([]string{"crio", "runtime", "runtimes", r}).(*toml.Tree); ok { | ||||||||||
c.Logger.Debugf("using options from runtime %v: %v", r, options.String()) | ||||||||||
options, _ = toml.Load(options.String()) | ||||||||||
config.SetPath([]string{"crio", "runtime", "runtimes", name}, options) | ||||||||||
break | ||||||||||
options := c.GetSubtreeByPath([]string{"crio", "runtime", "runtimes", r}) | ||||||||||
if options != nil { | ||||||||||
c.Logger.Debugf("Using options from runtime %v: %v", r, options) | ||||||||||
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. [nitpick] The log message capitalization should be consistent with other log messages in the codebase. Consider using lowercase 'using' to match the pattern in the original code.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||
return options.Copy() | ||||||||||
} | ||||||||||
} | ||||||||||
c.Logger.Warningf("Could not infer options from runtimes %v", runtimeNamesForConfig) | ||||||||||
return nil | ||||||||||
} | ||||||||||
|
||||||||||
func (c *Config) AddRuntimeWithOptions(name string, path string, setAsDefault bool, options interface{}) error { | ||||||||||
config := *c.Tree | ||||||||||
|
||||||||||
if options != nil { | ||||||||||
config.SetPath([]string{"crio", "runtime", "runtimes", name}, options) | ||||||||||
} | ||||||||||
config.SetPath([]string{"crio", "runtime", "runtimes", name, "runtime_path"}, path) | ||||||||||
config.SetPath([]string{"crio", "runtime", "runtimes", name, "runtime_type"}, "oci") | ||||||||||
|
||||||||||
if setAsDefault { | ||||||||||
config.SetPath([]string{"crio", "runtime", "default_runtime"}, name) | ||||||||||
} | ||||||||||
|
||||||||||
*c.Tree = config | ||||||||||
return nil | ||||||||||
} | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The log message capitalization should be consistent. Consider using lowercase 'could' to match the pattern in the original code which used 'could not infer options'.
Copilot uses AI. Check for mistakes.