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

Handle indentation when parsing sandbox_image #1119

Merged
merged 1 commit into from
Dec 10, 2022
Merged

Conversation

cartermckinnon
Copy link
Member

@cartermckinnon cartermckinnon commented Dec 8, 2022

Issue #, if available:

Resolves #767

Description of changes:

Handles indentation on the sandbox_image key of the containerd config file. Works by just converting this line to valid bash and source-ing it. TOML comments are ignored because they're valid bash comments.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Testing Done

Executed:

#!/usr/bin/env bash

INDENTED=$(mktemp)

echo '[plugins]
  [plugins."io.containerd.grpc.v1.cri"]
    # sandbox_image comment about the sandbox_image
    sandbox_image = "one"' >> $INDENTED

FLAT=$(mktemp)

echo '[plugins]
[plugins."io.containerd.grpc.v1.cri"]
# sandbox_image comment about the sandbox_image
sandbox_image = "two"' >> $FLAT

source <(grep "sandbox_image" $INDENTED | tr -d ' ')
echo "$sandbox_image"

source <(grep "sandbox_image" $FLAT | tr -d ' ')
echo "$sandbox_image"

Output:

one
two

@@ -1,7 +1,7 @@
#!/usr/bin/env bash
set -euo pipefail

sandbox_image="$(awk -F'[ ="]+' '$1 == "sandbox_image" { print $2 }' /etc/containerd/config.toml)"
source <(grep "sandbox_image" /etc/containerd/config.toml | tr -d ' ')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I though it's a danger more to use "grep" here.

Imagine there's a comment line with "sandbox_image" but it just a comment?

Copy link
Member Author

@cartermckinnon cartermckinnon Dec 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TOML comments use a hash symbol (#) like bash, so it "just works". There's an example of this to my testing section in the description.

https://github.com/toml-lang/toml/blob/main/toml.md#user-content-comment

Copy link
Contributor

@bwagner5 bwagner5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very clever! LGTM

@cartermckinnon cartermckinnon merged commit f1bb10b into master Dec 10, 2022
@cartermckinnon cartermckinnon deleted the resolves-767 branch December 10, 2022 00:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sandbox image script does not work if containerd config is indented
3 participants