Skip to content

ctr: improve error relative shim path error msg#6519

Merged
kzys merged 1 commit intocontainerd:mainfrom
ginglis13:ctr-runtime-path
Mar 14, 2022
Merged

ctr: improve error relative shim path error msg#6519
kzys merged 1 commit intocontainerd:mainfrom
ginglis13:ctr-runtime-path

Conversation

@ginglis13
Copy link
Copy Markdown
Contributor

addresses #6464

Slice returned by strings.Split("./path/to/shim", ".") is of
length 2 - first element being the empty string "". Check for this to
see if a runtime provided is relative.

With these changes, the example from #6464 becomes:

$ sudo ctr run  --rm --runtime=./containerd-shim-runc-v2 docker.io/library/hello-world:latest hello-world
ctr: failed to start shim: failed to resolve runtime path: invalid runtime name ./containerd-shim-runc-v2, correct runtime name should be either format like `io.containerd.runc.v1` or a full path to the binary: unknown

Additionally adds context to the usage for ctr run --runtime indicating that
absolute path to shim binary must be provided.

Signed-off-by: Gavin Inglis [email protected]

@k8s-ci-robot
Copy link
Copy Markdown

Hi @ginglis13. Thanks for your PR.

I'm waiting for a containerd member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Comment thread runtime/v2/shim/util.go Outdated
// runtime name should format like $prefix.name.version
parts := strings.Split(runtime, ".")
if len(parts) < 2 {
if len(parts) < 3 {
Copy link
Copy Markdown
Member

@AkihiroSuda AkihiroSuda Feb 5, 2022

Choose a reason for hiding this comment

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

This logic doesn’t seem correct.
The right way would be to check filepath.IsAbs in ctr.

cOpts = append(cOpts, containerd.WithRuntime(context.String("runtime"), runtimeOpts))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

My reason for making this change here is to my understanding, the value passed to --runtime is evaluated in ShimManager's resolveRuntimePath method. This method uses filepath.IsAbs to check if the runtime provided is an absolute path to an existing file.

In resolveRuntimePath's fallthrough logic for when the provided value is not an absolute filepath, the BinaryName util is used to check for a value like io.containerd.runc.v1 and return containerd-shim-runc-v1 in that case. A string like ./containerd-shim-runc-v2 passed to BinaryName results in a parts slice ["" "/containerd-shim-runc-v2"] of length 2 and will miss this error check on the parts length.

I'm not too familiar with all the possible runtime name formats - if a format like runc.v1 is acceptable then these changes could be updated to add an additional check for if the first element of parts is ""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

@AkihiroSuda AkihiroSuda left a comment

Choose a reason for hiding this comment

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

Comment thread runtime/v2/shim/util.go
// runtime name should format like $prefix.name.version
parts := strings.Split(runtime, ".")
if len(parts) < 2 {
if len(parts) < 2 || parts[0] == "" {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Still not robust to catch a case like foo/bar.

Probably, we should just do :

runtime := clicontext.String("runtime") 
if strings.Contains(runtime, "/") && !filepath.IsAbs(runtime) {
  return fmt.Errorf("runtime must be a runtime name (e.g., \"io.containerd.runc.v2\") or absolute path to the runtime binary (e.g., \"/usr/local/bin/containerd-shim-runc-v2\"), got %q", runtime)
}

Copy link
Copy Markdown
Contributor Author

@ginglis13 ginglis13 Feb 7, 2022

Choose a reason for hiding this comment

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

From my testing, the existing check len(parts) < 2 already covers this case as strings.Split("foo/bar", ".") will result in a slice of length 1 : ["foo/bar"]

It doesn't however cover foo/bar.baz.foo - I'll go ahead with this suggestion 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Would you like this to go in runtime/v2/manager.go here ? or would it belong better in cmd/ctr/commands/run/run_unix.go ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I prefer to have that under runtime/ rather than cmd/

Copy link
Copy Markdown
Member

@kzys kzys left a comment

Choose a reason for hiding this comment

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

Looks good! Can you write some tests?

@ginglis13
Copy link
Copy Markdown
Contributor Author

Looks good! Can you write some tests?

sure thing - looked around for a bit and didn't see any existing unit tests for v2 ShimManager, or Shim utils. Should I make some tests in a new file manager_test.go , or are there existing tests I can update ?

@mxpv
Copy link
Copy Markdown
Member

mxpv commented Feb 22, 2022

Should I make some tests in a new file manager_test.go, or are there existing tests I can update ?

It's fine to make manager_test.go

Overall LGTM

@theopenlab-ci
Copy link
Copy Markdown

theopenlab-ci Bot commented Mar 1, 2022

Build succeeded.

@ginglis13 ginglis13 marked this pull request as draft March 1, 2022 00:40
@theopenlab-ci
Copy link
Copy Markdown

theopenlab-ci Bot commented Mar 1, 2022

Build succeeded.

@ginglis13 ginglis13 force-pushed the ctr-runtime-path branch 2 times, most recently from d9acb71 to 2effd9f Compare March 3, 2022 02:32
@theopenlab-ci
Copy link
Copy Markdown

theopenlab-ci Bot commented Mar 3, 2022

Build succeeded.

  • containerd-build-arm64 : RETRY_LIMIT in 3m 44s (non-voting)
  • containerd-test-arm64 : RETRY_LIMIT in 5m 53s (non-voting)
  • containerd-integration-test-arm64 : RETRY_LIMIT in 28m 16s (non-voting)

@ginglis13 ginglis13 marked this pull request as ready for review March 3, 2022 04:39
Comment thread runtime/v2/manager_test.go Outdated
return "", err
}

err = os.Setenv("PATH", tempShimDir+":"+os.Getenv("PATH"))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It is bit scary to set the PATH here since it will affect all tests in the same process. How about using t.Setenv?

Comment thread runtime/v2/manager_test.go Outdated
// setupAbsoluteShimPath creates a temporary directory with an empty shim executable file in it.
// This is to test the exec.LookPath branch of resolveRuntimePath
func setupAbsoluteShimPath() (string, error) {
tempShimDir, err := ioutil.TempDir("", "test-resolve-runtime-path")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

  • You need to remove the directory
  • If it is okay to take testing.T, you can use t.TempDir

addresses containerd#6464

Return an error if a runtime provided is relative.

Add context to the usage for `ctr run --runtime` indicating that
absolute path to runtime binary must be provided.

Signed-off-by: Gavin Inglis <[email protected]>
@theopenlab-ci
Copy link
Copy Markdown

theopenlab-ci Bot commented Mar 4, 2022

Build succeeded.

  • containerd-build-arm64 : RETRY_LIMIT in 3m 33s (non-voting)
  • containerd-test-arm64 : RETRY_LIMIT in 5m 47s (non-voting)
  • containerd-integration-test-arm64 : RETRY_LIMIT in 24m 45s (non-voting)

Copy link
Copy Markdown
Member

@kzys kzys left a comment

Choose a reason for hiding this comment

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

/ok-to-test

@kzys
Copy link
Copy Markdown
Member

kzys commented Mar 11, 2022

@mxpv @AkihiroSuda PTAL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants