GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — env: move builder getenv out of cobra cmds by christoph-zededa · Pull Request #4206 · linuxkit/linuxkit

env: move builder getenv out of cobra cmds #4206

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.

Sign up for GitHub

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

Draft
christoph-zededa wants to merge 1 commit into linuxkit:master
base: master
Loading
Loading
from christoph-zededa:fixup_builder_env_variables_liblinuxkit

Uh oh!

There was an error while loading. Please reload this page.

Draft

env: move builder getenv out of cobra cmds #4206

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
073c05f
env: move builder getenv out of cobra cmds
christoph-zededa Mar 12, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
env: move builder getenv out of cobra cmds
as it has been introduced in 666bbfd

this makes it possible to profit from this change also when
using linuxkit as a library (e.g. lf-edge/eve/bpftrace-compiler)

Signed-off-by: Christoph Ostarek <[email protected]>
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@christoph-zededa
christoph-zededa committed Apr 16, 2026
commit 073c05f2a7297773b5e3c904f0364490959717a7

There are no files selected for viewing

84 changes: 50 additions & 34 deletions 84 src/cmd/linuxkit/cmd.go
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ func readConfig() {
}
}

func init() {
mirrorsEnv := make([]string, 0)
// prepend mirrors from env var so CLI flags take precedence (last SetProxy call wins)
if envMirrors := os.Getenv(envVarMirror); envMirrors != "" {
envList := strings.FieldsFunc(envMirrors, func(r rune) bool { return r == ',' || r == ' ' })
mirrorsEnv = append(envList, mirrorsEnv...)
}

err := convertProvidedMirrorsToMap(mirrorsEnv)

if err != nil {
panic(err)
}
}

func newCmd() *cobra.Command {
var (
flagQuiet bool
Expand All @@ -58,40 +73,9 @@ func newCmd() *cobra.Command {
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
readConfig()

// prepend mirrors from env var so CLI flags take precedence (last SetProxy call wins)
if envMirrors := os.Getenv(envVarMirror); envMirrors != "" {
envList := strings.FieldsFunc(envMirrors, func(r rune) bool { return r == ',' || r == ' ' })
mirrorsRaw = append(envList, mirrorsRaw...)
}

// convert the provided mirrors to a map
for _, m := range mirrorsRaw {
if m == "" {
continue
}
parts := strings.SplitN(m, "=", 2)
// if no equals sign, use the whole string as the mirror for all registries
// not otherwise specified
var key, value string
if len(parts) == 1 {
key = "*"
value = parts[0]
} else {
key = parts[0]
value = parts[1]
}
// value must start with http:// or https://
if !strings.HasPrefix(value, "http://") && !strings.HasPrefix(value, "https://") {
return fmt.Errorf("mirror %q must start with http:// or https://", value)
}
// special logic for docker.io because of its odd references
if key == "docker.io" || key == "docker.io/" {
for _, prefix := range []string{"docker.io", "index.docker.io", "registry-1.docker.io"} {
registry.SetProxy(prefix, value)
}
} else {
registry.SetProxy(key, value)
}
err := convertProvidedMirrorsToMap(mirrorsRaw)
if err != nil {
return err
}

for _, f := range certFiles {
Expand Down Expand Up @@ -128,3 +112,35 @@ func newCmd() *cobra.Command {

return cmd
}

func convertProvidedMirrorsToMap(mirrorsRaw []string) error {
for _, m := range mirrorsRaw {
if m == "" {
continue
}
parts := strings.SplitN(m, "=", 2)
// if no equals sign, use the whole string as the mirror for all registries
// not otherwise specified
var key, value string
if len(parts) == 1 {
key = "*"
value = parts[0]
} else {
key = parts[0]
value = parts[1]
}
// value must start with http:// or https://
if !strings.HasPrefix(value, "http://") && !strings.HasPrefix(value, "https://") {
return fmt.Errorf("mirror %q must start with http:// or https://", value)
}
// special logic for docker.io because of its odd references
if key == "docker.io" || key == "docker.io/" {
for _, prefix := range []string{"docker.io", "index.docker.io", "registry-1.docker.io"} {
registry.SetProxy(prefix, value)
}
} else {
registry.SetProxy(key, value)
}
}
return nil
}
3 changes: 0 additions & 3 deletions 3 src/cmd/linuxkit/pkg.go
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"errors"
"fmt"
"os"

"github.com/linuxkit/linuxkit/src/cmd/linuxkit/pkglib"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -76,8 +75,6 @@ func pkgCmd() *cobra.Command {
}
if cmd.Flags().Changed("org") {
pkglibConfig.Org = &argOrg
} else if org := os.Getenv(envVarPkgOrg); org != "" {
pkglibConfig.Org = &org
}
if cmd.Flags().Changed("tag") {
pkglibConfig.Tag = tag
Expand Down
15 changes: 6 additions & 9 deletions 15 src/cmd/linuxkit/pkg_build.go
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ import (
const (
buildersEnvVar = "LINUXKIT_BUILDERS"
envVarCacheDir = "LINUXKIT_CACHE"
envVarBuilderName = "LINUXKIT_BUILDER_NAME"
envVarBuilderImage = "LINUXKIT_BUILDER_IMAGE"
envVarBuilderConfig = "LINUXKIT_BUILDER_CONFIG"
envVarMirror = "LINUXKIT_MIRROR"
envVarPkgOrg = "LINUXKIT_PKG_ORG"
defaultBuilderImage = "moby/buildkit:v0.26.3"
Expand All @@ -45,9 +42,9 @@ func pkgBuildCmd() *cobra.Command {
platforms string
skipPlatforms string
builders string
builderName = flagOverEnvVarOverDefaultString{def: pkglib.DefaultBuilderName(), envVar: envVarBuilderName}
builderImage = flagOverEnvVarOverDefaultString{def: defaultBuilderImage, envVar: envVarBuilderImage}
builderConfig = flagOverEnvVarOverDefaultString{def: "", envVar: envVarBuilderConfig}
builderName = flagOverEnvVarOverDefaultString{def: pkglib.DefaultBuilderName()}
builderImage = flagOverEnvVarOverDefaultString{def: defaultBuilderImage}
builderConfig = flagOverEnvVarOverDefaultString{def: ""}
builderRestart bool
preCacheImages bool
release string
Expand Down Expand Up @@ -312,9 +309,9 @@ func pkgBuildCmd() *cobra.Command {
cmd.Flags().StringVar(&platforms, "platforms", "", "Which platforms to build for, defaults to all of those for which the package can be built")
cmd.Flags().StringVar(&skipPlatforms, "skip-platforms", "", "Platforms that should be skipped, even if present in build.yml")
cmd.Flags().StringVar(&builders, "builders", "", "Which builders to use for which platforms, e.g. linux/arm64=docker-context-arm64, overrides defaults and environment variables, see https://github.com/linuxkit/linuxkit/blob/master/docs/packages.md#Providing-native-builder-nodes")
cmd.Flags().Var(&builderName, "builder-name", fmt.Sprintf("Name of the buildkit builder container, default: %s, overrides env var %s", pkglib.DefaultBuilderName(), envVarBuilderName))
cmd.Flags().Var(&builderImage, "builder-image", fmt.Sprintf("buildkit builder container image to use, overrides env var %s", envVarBuilderImage))
cmd.Flags().Var(&builderConfig, "builder-config", fmt.Sprintf("path to buildkit builder config.toml file to use, overrides the default config.toml in the builder image. When provided, copied over into builder, along with all certs. Use paths for certificates relative to your local host, they will be adjusted on copying into the container. USE WITH CAUTION. Overrides env var %s", envVarBuilderConfig))
cmd.Flags().Var(&builderName, "builder-name", fmt.Sprintf("Name of the buildkit builder container, default: %s, overrides env var %s", pkglib.DefaultBuilderName(), pkglib.EnvVarBuilderName))
cmd.Flags().Var(&builderImage, "builder-image", fmt.Sprintf("buildkit builder container image to use, overrides env var %s", pkglib.EnvVarBuilderImage))
cmd.Flags().Var(&builderConfig, "builder-config", fmt.Sprintf("path to buildkit builder config.toml file to use, overrides the default config.toml in the builder image. When provided, copied over into builder, along with all certs. Use paths for certificates relative to your local host, they will be adjusted on copying into the container. USE WITH CAUTION. Overrides env var %s", pkglib.EnvVarBuilderConfig))
cmd.Flags().BoolVar(&builderRestart, "builder-restart", false, "force restarting builder, even if container with correct name and image exists")
cmd.Flags().BoolVar(&preCacheImages, "precache-images", false, "download all referenced images in the Dockerfile to the linuxkit cache before building, thus referencing the local cache instead of pulling from the registry; this is useful for handling mirrors and special connections")
cmd.Flags().Var(&cacheDir, "cache", fmt.Sprintf("Directory for caching and finding cached image, overrides env var %s", envVarCacheDir))
Expand Down
4 changes: 2 additions & 2 deletions 4 src/cmd/linuxkit/pkg_builder.go
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func pkgBuilderCmd() *cobra.Command {
var (
builders string
builderName = flagOverEnvVarOverDefaultString{def: pkglib.DefaultBuilderName(), envVar: envVarBuilderName}
builderName = flagOverEnvVarOverDefaultString{def: pkglib.DefaultBuilderName(), envVar: pkglib.EnvVarBuilderName}
platforms string
builderImage string
builderConfigPath string
Expand Down Expand Up @@ -62,7 +62,7 @@ func pkgBuilderCmd() *cobra.Command {
}

cmd.PersistentFlags().StringVar(&builders, "builders", "", "Which builders to use for which platforms, e.g. linux/arm64=docker-context-arm64, overrides defaults and environment variables, see https://github.com/linuxkit/linuxkit/blob/master/docs/packages.md#Providing-native-builder-nodes")
cmd.PersistentFlags().Var(&builderName, "builder-name", fmt.Sprintf("Name of the buildkit builder container, default: %s, overrides env var %s", pkglib.DefaultBuilderName(), envVarBuilderName))
cmd.PersistentFlags().Var(&builderName, "builder-name", fmt.Sprintf("Name of the buildkit builder container, default: %s, overrides env var %s", pkglib.DefaultBuilderName(), pkglib.EnvVarBuilderName))
cmd.PersistentFlags().StringVar(&platforms, "platforms", fmt.Sprintf("linux/%s", runtime.GOARCH), "Which platforms we built images for")
cmd.PersistentFlags().StringVar(&builderImage, "builder-image", defaultBuilderImage, "buildkit builder container image to use")
cmd.Flags().StringVar(&builderConfigPath, "builder-config", "", "path to buildkit builder config.toml file to use, overrides the default config.toml in the builder image; USE WITH CAUTION")
Expand Down
32 changes: 32 additions & 0 deletions 32 src/cmd/linuxkit/pkglib/build.go
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ import (
"golang.org/x/sync/errgroup"
)

const (
EnvVarBuilderName = "LINUXKIT_BUILDER_NAME"
EnvVarBuilderImage = "LINUXKIT_BUILDER_IMAGE"
EnvVarBuilderConfig = "LINUXKIT_BUILDER_CONFIG"
)

type buildOpts struct {
skipBuild bool
force bool
Expand Down Expand Up @@ -251,6 +257,32 @@ func (p Pkg) Build(bos ...BuildOpt) error {
}
}

// override builder from env variables
for _, k := range []struct {
p *string
env string
}{
{
p: &bo.builderConfig.Name,
env: EnvVarBuilderName,
},
{
p: &bo.builderConfig.Image,
env: EnvVarBuilderImage,
},
{
p: &bo.builderConfig.ConfigPath,
env: EnvVarBuilderConfig,
},
} {
env := os.Getenv(k.env)
if env == "" {
continue
}

*k.p = env
}

writer := bo.writer
if writer == nil {
writer = os.Stdout
Expand Down
10 changes: 10 additions & 0 deletions 10 src/cmd/linuxkit/pkglib/pkglib.go
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (
"github.com/linuxkit/linuxkit/src/cmd/linuxkit/util"
)

const (
envVarPkgOrg = "LINUXKIT_PKG_ORG"
)

// Contains fields settable in the build.yml
type pkgInfo struct {
Image string `yaml:"image"`
Expand Down Expand Up @@ -192,6 +196,12 @@ func NewFromConfig(cfg PkglibConfig, args ...string) ([]Pkg, error) {
if cfg.Network != nil {
pi.Network = *cfg.Network
}
if cfg.Org == nil {
env := os.Getenv(envVarPkgOrg)
if env != "" {
cfg.Org = &env
}
}
if cfg.Org != nil {
pi.Org = *cfg.Org
}
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402605 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.