-
Notifications
You must be signed in to change notification settings - Fork 1.7k
add limited support for version aware containerd config patching #4042
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
base: main
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -29,18 +29,33 @@ import ( | |
| "sigs.k8s.io/kind/pkg/errors" | ||
| ) | ||
|
|
||
| // TOML patches toPatch with the patches (should be TOML merge patches) and patches6902 (should be JSON 6902 patches) | ||
| func TOML(toPatch string, patches []string, patches6902 []string) (string, error) { | ||
| // ContainerdTOML patches toPatch with the patches (should be TOML merge patches) and patches6902 (should be JSON 6902 patches) | ||
| func ContainerdTOML(toPatch string, patches []string, patches6902 []string) (string, error) { | ||
| // convert to JSON for patching | ||
| j, err := tomlToJSON([]byte(toPatch)) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| version, err := containerdConfigVersion(toPatch) | ||
| if err != nil { | ||
| return "", errors.WithStack(err) | ||
| } | ||
| if version == 0 { | ||
| return "", errors.New("failed to detect containerd config version") | ||
| } | ||
| // apply merge patches | ||
| for _, patch := range patches { | ||
| pj, err := tomlToJSON([]byte(patch)) | ||
| if err != nil { | ||
| return "", err | ||
| return "", errors.WithStack(err) | ||
| } | ||
| patchVersion, err := containerdConfigVersion(patch) | ||
| if err != nil { | ||
| return "", errors.WithStack(err) | ||
| } | ||
| // skip if patch sets version and version does not match | ||
| if patchVersion != 0 && patchVersion != version { | ||
| continue | ||
| } | ||
| patched, err := jsonpatch.MergePatch(j, pj) | ||
| if err != nil { | ||
|
|
@@ -64,6 +79,17 @@ func TOML(toPatch string, patches []string, patches6902 []string) (string, error | |
| return jsonToTOMLString(j) | ||
| } | ||
|
|
||
| func containerdConfigVersion(configTOML string) (int, error) { | ||
| type version struct { | ||
| Version int `toml:"version,omitempty"` | ||
| } | ||
| v := version{} | ||
| if err := toml.Unmarshal([]byte(configTOML), &v); err != nil { | ||
|
Contributor
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 default to version 1 instead of error?
Member
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. version 1 isn't the default in containerd as far as I know, but the default would depend on containerd version and that seems fragile. This is the file to be patched, we are always specifying the version ourselves, so if it doesn't have the version or we can't parse it, something is wrong. since this is in an internal package already, I didn't think it was worth designing for other use cases (hence the rename to Containerd and the assumption about the
Member
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. Ah also, maybe this wasn't obvious: We won't error if there is no version, error would mean it fails to parse as toml or it has a version key with a different data type or something like that. (You can also confirm from the existing test cases without version specified continuing to work, in additon to the updated test cases) We have version field as omitempty, so it defaults to 0 if not specified |
||
| return 0, errors.WithStack(err) | ||
| } | ||
| return v.Version, nil | ||
| } | ||
|
|
||
| // tomlToJSON converts arbitrary TOML to JSON | ||
| func tomlToJSON(t []byte) ([]byte, error) { | ||
| // we use github.com.pelletier/go-toml here to unmarshal arbitrary TOML to JSON | ||
|
|
||
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.
there is an assumption that we always get a valid version from the base configuration here, we could error if this is not non-zero here, or below we could always match if it is zero similar to how we ignore match for patches with version = 0
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.
I think failing to read this one should be an error, it should only happen if we changed our base config and didn't update this properly (because version parsing changed in containerd config?) or because someone has forked kind with a custom containerd base config that doesn't set version, which I don't really care to guard against.
Uh oh!
There was an error while loading. Please reload this page.
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.
updated to require detecting the version in the config to be patched