Skip to content

Conversation

Shubhranshu153
Copy link
Contributor

Currently when we set "BUILDKIT_HOST" as env variable, nerdctl doesnt take that into consideration for system prune.

[root@lima-finch nerdctl]# export BUILDKIT_HOST=unix:///tmp/buildkit-env.sock 
[root@lima-finch nerdctl]# nerdctl system prune --all
INFO[0000] Using buildkit host from command line flag or environment variable. 
WARN[0000] BuildKit is not running. Build caches will not be pruned.  error="flag accessed but not defined: buildkit-host"

With the change:

[root@lima-finch nerdctl]# export BUILDKIT_HOST=unix:///tmp/buildkit-env.sock 
[root@lima-finch nerdctl]# nerdctl system prune --all
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all images without at least one container associated to them
  - all build cache
Are you sure you want to continue? [y/N] y

Note: helpers.AddStringFlag(cmd, "buildkit-host", nil, "", "BUILDKIT_HOST", "BuildKit address") this masks the logic for build commands but for prune command it doesn't work correctly

Would like to refactor not have the helper commands to parse it as this only works when we have the specific flag but for buildctl config BUILDKIT_HOST can be also set for commands which may not have the buildkit-host flag specifically: eg: system prune. Of we add other options like du etc for buildctl this will be also relevant

@apostasie
Copy link
Contributor

@Shubhranshu153
A few nits on test + implementing something to ease your env manipulation.

Also, do we need to keep calling helpers.AddStringFlag( instead of a plain cmd.Flags().String?

@Shubhranshu153
Copy link
Contributor Author

helpers.AddStringFlag -> This one i want to remove from all places as i feel we are manipulating behavior artificially.
will refactor it.

@Shubhranshu153
Copy link
Contributor Author

As the GetBuildkitHost() is kind of high level function used in 3 different places do you think we should move it to inside buildkitutils?

@Shubhranshu153 Shubhranshu153 force-pushed the fix-buildkit-host branch 3 times, most recently from 1c5f8d4 to 29a42b6 Compare April 17, 2025 19:02
@apostasie
Copy link
Contributor

apostasie commented Apr 17, 2025

As the GetBuildkitHost() is kind of high level function used in 3 different places do you think we should move it to inside buildkitutils?

Personal opinion:
We already have one in buildkitutil.
As for the part that processes flags and environment variable, I would rather keep that (somewhere) under ./cmd (eg: that is cli concern, not library concern IMO)

@apostasie
Copy link
Contributor

Linking latest failures to their tickets:

Copy link
Contributor

@apostasie apostasie left a comment

Choose a reason for hiding this comment

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

One last comment on a test, but otherwise looks fine.

cc @AkihiroSuda @fahedouch for formal maintainer review.

@AkihiroSuda AkihiroSuda added this to the v2.0.5 milestone Apr 18, 2025
@@ -253,6 +253,14 @@ func GetBuildkitHost(cmd *cobra.Command, namespace string) (string, error) {
}
return buildkitHost, nil
}

if buildkitHost := os.Getenv("BUILDKIT_HOST"); buildkitHost != "" {
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't it be more logical to introduce the --buildkit-host flag in the prune command?

Unless I'm mistaken, we wanted to have a single entry point for CLI flag and parameters, which is cmd. I think this ensures better management of flags, environment variables, and parameters related to the CLI.

Copy link
Member

@AkihiroSuda AkihiroSuda Apr 18, 2025

Choose a reason for hiding this comment

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

Probably this should be a global flag as in --address ($CONTAINERD_ADDRESS)

helpers.AddPersistentStringFlag(rootCmd, "address", []string{"a", "H"}, nil, []string{"host"}, aliasToBeInherited, cfg.Address, "CONTAINERD_ADDRESS", `containerd address, optionally with "unix://" prefix`)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can add that as well along with the change.
But we should not deprecate the current flags for backward compatibility. Global flag will be overridden by the cmd specific options?

Copy link
Member

Choose a reason for hiding this comment

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

A global flag can be specified for a subcommand too, so the compatibility is not affected AFAICS

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok i can add it to the global flag and refactor the code. Makes sense to me.
Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i started doing this change but i feel this requires a larger refactor and some testing, will want to put it in a separate PR.

@AkihiroSuda
Copy link
Member

Needs rebase

@AkihiroSuda
Copy link
Member

Note: helpers.AddStringFlag(cmd, "buildkit-host", nil, "", "BUILDKIT_HOST", "BuildKit address") this masks the logic for build commands but for prune command it doesn't work correctly

How is buildctl prune different from buildctl build for handling BUILDKIT_HOST? 🤔

@apostasie
Copy link
Contributor

Note: helpers.AddStringFlag(cmd, "buildkit-host", nil, "", "BUILDKIT_HOST", "BuildKit address") this masks the logic for build commands but for prune command it doesn't work correctly

How is buildctl prune different from buildctl build for handling BUILDKIT_HOST? 🤔

I think @Shubhranshu153 was talking about nerdctl system prune (not nerdctl build prune).

@apostasie
Copy link
Contributor

Note: helpers.AddStringFlag(cmd, "buildkit-host", nil, "", "BUILDKIT_HOST", "BuildKit address") this masks the logic for build commands but for prune command it doesn't work correctly

How is buildctl prune different from buildctl build for handling BUILDKIT_HOST? 🤔

I think @Shubhranshu153 was talking about nerdctl system prune (not nerdctl build prune).

eg: my understanding is that buildctl itself never look into BUILDKIT_HOST because we explicitly pass to it a --addr argument.

@Shubhranshu153 Shubhranshu153 force-pushed the fix-buildkit-host branch 3 times, most recently from 619191a to 83abfb9 Compare April 18, 2025 18:56
@apostasie
Copy link
Contributor

@Shubhranshu153 main is bust right now.

Suggesting you wait for #4128 then rebase.

@apostasie
Copy link
Contributor

Tagging unrelated failure: #4068

Copy link
Contributor

@apostasie apostasie left a comment

Choose a reason for hiding this comment

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

One minor question about NoParallel on the first test, but otherwise LGTM.
Thanks @Shubhranshu153 !

Signed-off-by: Shubharanshu Mahapatra <shubhum@amazon.com>
@Shubhranshu153
Copy link
Contributor Author

Can we merge this? Thanks

Copy link
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.

Thanks

@AkihiroSuda AkihiroSuda merged commit 1673252 into containerd:main Apr 24, 2025
30 checks passed
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.

4 participants