Skip to content

Commit

Permalink
feat(build): new option --container-registry-mirror
Browse files Browse the repository at this point in the history
Signed-off-by: Ilya Lesikov <ilya@lesikov.com>
  • Loading branch information
ilya-lesikov committed May 31, 2024
1 parent a3db3aa commit bf185f2
Show file tree
Hide file tree
Showing 30 changed files with 251 additions and 101 deletions.
10 changes: 8 additions & 2 deletions cmd/werf/build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupInsecureRegistry(&commonCmdData, cmd)
common.SetupInsecureHelmDependencies(&commonCmdData, cmd, true)
common.SetupSkipTlsVerifyRegistry(&commonCmdData, cmd)
common.SetupContainerRegistryMirror(&commonCmdData, cmd)

common.SetupIntrospectAfterError(&commonCmdData, cmd)
common.SetupIntrospectBeforeError(&commonCmdData, cmd)
Expand Down Expand Up @@ -128,7 +129,12 @@ func runMain(ctx context.Context, imagesToProcess build.ImagesToProcess) error {
return fmt.Errorf("initialization error: %w", err)
}

containerBackend, processCtx, err := common.InitProcessContainerBackend(ctx, &commonCmdData)
registryMirrors, err := common.GetContainerRegistryMirror(ctx, &commonCmdData)
if err != nil {
return fmt.Errorf("get container registry mirrors: %w", err)
}

containerBackend, processCtx, err := common.InitProcessContainerBackend(ctx, &commonCmdData, registryMirrors)
if err != nil {
return err
}
Expand All @@ -155,7 +161,7 @@ func runMain(ctx context.Context, imagesToProcess build.ImagesToProcess) error {
return err
}

if err := common.DockerRegistryInit(ctx, &commonCmdData); err != nil {
if err := common.DockerRegistryInit(ctx, &commonCmdData, registryMirrors); err != nil {
return err
}

Expand Down
8 changes: 7 additions & 1 deletion cmd/werf/bundle/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupInsecureRegistry(&commonCmdData, cmd)
common.SetupInsecureHelmDependencies(&commonCmdData, cmd, false)
common.SetupSkipTlsVerifyRegistry(&commonCmdData, cmd)
common.SetupContainerRegistryMirror(&commonCmdData, cmd)

common.SetupLogOptions(&commonCmdData, cmd)
common.SetupLogProjectDir(&commonCmdData, cmd)
Expand Down Expand Up @@ -157,7 +158,12 @@ func runApply(ctx context.Context) error {
return fmt.Errorf("initialization error: %w", err)
}

if err := common.DockerRegistryInit(ctx, &commonCmdData); err != nil {
registryMirrors, err := common.GetContainerRegistryMirror(ctx, &commonCmdData)
if err != nil {
return fmt.Errorf("get container registry mirrors: %w", err)
}

if err := common.DockerRegistryInit(ctx, &commonCmdData, registryMirrors); err != nil {
return err
}

Expand Down
8 changes: 7 additions & 1 deletion cmd/werf/bundle/copy/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupInsecureRegistry(&commonCmdData, cmd)
common.SetupInsecureHelmDependencies(&commonCmdData, cmd, false)
common.SetupSkipTlsVerifyRegistry(&commonCmdData, cmd)
common.SetupContainerRegistryMirror(&commonCmdData, cmd)

common.SetupLogOptions(&commonCmdData, cmd)
common.SetupLogProjectDir(&commonCmdData, cmd)
Expand All @@ -75,7 +76,12 @@ func runCopy(ctx context.Context) error {
return fmt.Errorf("initialization error: %w", err)
}

if err := common.DockerRegistryInit(ctx, &commonCmdData); err != nil {
registryMirrors, err := common.GetContainerRegistryMirror(ctx, &commonCmdData)
if err != nil {
return fmt.Errorf("get container registry mirrors: %w", err)
}

if err := common.DockerRegistryInit(ctx, &commonCmdData, registryMirrors); err != nil {
return err
}

Expand Down
10 changes: 8 additions & 2 deletions cmd/werf/bundle/publish/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupInsecureRegistry(&commonCmdData, cmd)
common.SetupInsecureHelmDependencies(&commonCmdData, cmd, true)
common.SetupSkipTlsVerifyRegistry(&commonCmdData, cmd)
common.SetupContainerRegistryMirror(&commonCmdData, cmd)

common.SetupLogOptions(&commonCmdData, cmd)
common.SetupLogProjectDir(&commonCmdData, cmd)
Expand Down Expand Up @@ -157,7 +158,12 @@ func runPublish(ctx context.Context, imagesToProcess build.ImagesToProcess) erro
return fmt.Errorf("initialization error: %w", err)
}

containerBackend, processCtx, err := common.InitProcessContainerBackend(ctx, &commonCmdData)
registryMirrors, err := common.GetContainerRegistryMirror(ctx, &commonCmdData)
if err != nil {
return fmt.Errorf("get container registry mirrors: %w", err)
}

containerBackend, processCtx, err := common.InitProcessContainerBackend(ctx, &commonCmdData, registryMirrors)
if err != nil {
return err
}
Expand All @@ -184,7 +190,7 @@ func runPublish(ctx context.Context, imagesToProcess build.ImagesToProcess) erro
return err
}

if err := common.DockerRegistryInit(ctx, &commonCmdData); err != nil {
if err := common.DockerRegistryInit(ctx, &commonCmdData, registryMirrors); err != nil {
return err
}

Expand Down
8 changes: 7 additions & 1 deletion cmd/werf/bundle/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupInsecureRegistry(&commonCmdData, cmd)
common.SetupInsecureHelmDependencies(&commonCmdData, cmd, false)
common.SetupSkipTlsVerifyRegistry(&commonCmdData, cmd)
common.SetupContainerRegistryMirror(&commonCmdData, cmd)

common.SetupLogOptionsDefaultQuiet(&commonCmdData, cmd)
common.SetupLogProjectDir(&commonCmdData, cmd)
Expand Down Expand Up @@ -182,7 +183,12 @@ func runRender(ctx context.Context) error {
if isLocal {
bundleDir = cmdData.BundleDir
} else {
if err := common.DockerRegistryInit(ctx, &commonCmdData); err != nil {
registryMirrors, err := common.GetContainerRegistryMirror(ctx, &commonCmdData)
if err != nil {
return fmt.Errorf("get container registry mirrors: %w", err)
}

if err := common.DockerRegistryInit(ctx, &commonCmdData, registryMirrors); err != nil {
return err
}

Expand Down
10 changes: 8 additions & 2 deletions cmd/werf/cleanup/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupInsecureRegistry(&commonCmdData, cmd)
common.SetupInsecureHelmDependencies(&commonCmdData, cmd, true)
common.SetupSkipTlsVerifyRegistry(&commonCmdData, cmd)
common.SetupContainerRegistryMirror(&commonCmdData, cmd)

common.SetupScanContextNamespaceOnly(&commonCmdData, cmd)
common.SetupDryRun(&commonCmdData, cmd)
Expand Down Expand Up @@ -111,7 +112,12 @@ func runCleanup(ctx context.Context) error {
return fmt.Errorf("initialization error: %w", err)
}

containerBackend, processCtx, err := common.InitProcessContainerBackend(ctx, &commonCmdData)
registryMirrors, err := common.GetContainerRegistryMirror(ctx, &commonCmdData)
if err != nil {
return fmt.Errorf("get container registry mirrors: %w", err)
}

containerBackend, processCtx, err := common.InitProcessContainerBackend(ctx, &commonCmdData, registryMirrors)
if err != nil {
return err
}
Expand All @@ -138,7 +144,7 @@ func runCleanup(ctx context.Context) error {
return err
}

if err := common.DockerRegistryInit(ctx, &commonCmdData); err != nil {
if err := common.DockerRegistryInit(ctx, &commonCmdData, registryMirrors); err != nil {
return err
}

Expand Down
1 change: 1 addition & 0 deletions cmd/werf/common/cmd_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type CmdData struct {
KeepStagesBuiltWithinLastNHours *uint64
WithoutKube *bool
KubeVersion *string
ContainerRegistryMirror *[]string

LooseGiterminism *bool
Dev *bool
Expand Down
56 changes: 54 additions & 2 deletions cmd/werf/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import (
"github.com/werf/logboek/pkg/types"
"github.com/werf/werf/v2/pkg/build"
"github.com/werf/werf/v2/pkg/build/stage"
"github.com/werf/werf/v2/pkg/buildah"
"github.com/werf/werf/v2/pkg/config"
"github.com/werf/werf/v2/pkg/container_backend"
"github.com/werf/werf/v2/pkg/docker"
"github.com/werf/werf/v2/pkg/docker_registry"
"github.com/werf/werf/v2/pkg/git_repo"
"github.com/werf/werf/v2/pkg/giterminism_manager"
Expand Down Expand Up @@ -578,6 +580,11 @@ func SetupInsecureRegistry(cmdData *CmdData, cmd *cobra.Command) {
cmd.Flags().BoolVarP(cmdData.InsecureRegistry, "insecure-registry", "", util.GetBoolEnvironmentDefaultFalse("WERF_INSECURE_REGISTRY"), "Use plain HTTP requests when accessing a registry (default $WERF_INSECURE_REGISTRY)")
}

func SetupContainerRegistryMirror(cmdData *CmdData, cmd *cobra.Command) {
cmdData.ContainerRegistryMirror = new([]string)
cmd.Flags().StringArrayVarP(cmdData.ContainerRegistryMirror, "container-registry-mirror", "", []string{}, "(Buildah-only) Use specified mirrors for docker.io")
}

func SetupSkipTlsVerifyRegistry(cmdData *CmdData, cmd *cobra.Command) {
if cmdData.SkipTlsVerifyRegistry != nil {
return
Expand Down Expand Up @@ -1219,6 +1226,51 @@ func GetSecondaryStagesStorage(cmdData *CmdData) []string {
return append(util.PredefinedValuesByEnvNamePrefix("WERF_SECONDARY_REPO_"), *cmdData.SecondaryStagesStorage...)
}

func GetContainerRegistryMirror(ctx context.Context, cmdData *CmdData) ([]string, error) {
mirrors := append(util.PredefinedValuesByEnvNamePrefix("WERF_CONTAINER_REGISTRY_MIRROR_"), *cmdData.ContainerRegistryMirror...)

if len(mirrors) > 0 {
buildahMode, _, err := GetBuildahMode()
if err != nil {
return nil, fmt.Errorf("get buildah mode: %w", err)
}

if *buildahMode == buildah.ModeDisabled {
global_warnings.GlobalWarningLn(ctx, "in Docker mode container registry mirrors should be configured in daemon.json file, not via --container-registry-mirrors: https://werf.io/docs/usage/build/process.html#container-registry-mirrors")
return nil, nil
}
}

// init registry mirrors if docker cli initialized in context
if docker.IsEnabled() && docker.IsContext(ctx) {
info, err := docker.Info(ctx)
if err != nil {
return nil, fmt.Errorf("unable to get docker system info: %w", err)
}

if info.RegistryConfig == nil {
return nil, nil
}

return info.RegistryConfig.Mirrors, nil
}

var result []string
for _, mirror := range mirrors {
if strings.HasPrefix(mirror, "http://") {
return nil, fmt.Errorf("invalid container registry mirror %q: only https schema allowed", mirror)
}

if !strings.HasPrefix(mirror, "http://") && !strings.HasPrefix(mirror, "https://") {
mirror = "https://" + mirror
}

result = append(result, mirror)
}

return result, nil
}

func getAddCustomTag(cmdData *CmdData) []string {
return append(util.PredefinedValuesByEnvNamePrefix("WERF_ADD_CUSTOM_TAG_"), *cmdData.AddCustomTag...)
}
Expand Down Expand Up @@ -1400,8 +1452,8 @@ func ProcessLogTerminalWidth(cmdData *CmdData) error {
return nil
}

func DockerRegistryInit(ctx context.Context, cmdData *CmdData) error {
return docker_registry.Init(ctx, *cmdData.InsecureRegistry, *cmdData.SkipTlsVerifyRegistry)
func DockerRegistryInit(ctx context.Context, cmdData *CmdData, registryMirrors []string) error {
return docker_registry.Init(ctx, *cmdData.InsecureRegistry, *cmdData.SkipTlsVerifyRegistry, registryMirrors)
}

func ValidateMinimumNArgs(minArgs int, args []string, cmd *cobra.Command) error {
Expand Down
11 changes: 6 additions & 5 deletions cmd/werf/common/container_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func wrapContainerBackend(containerBackend container_backend.ContainerBackend) c
return containerBackend
}

func InitProcessContainerBackend(ctx context.Context, cmdData *CmdData) (container_backend.ContainerBackend, context.Context, error) {
func InitProcessContainerBackend(ctx context.Context, cmdData *CmdData, registryMirrors []string) (container_backend.ContainerBackend, context.Context, error) {
buildahMode, buildahIsolation, err := GetBuildahMode()
if err != nil {
return nil, ctx, fmt.Errorf("unable to determine buildah mode: %w", err)
Expand All @@ -109,10 +109,11 @@ func InitProcessContainerBackend(ctx context.Context, cmdData *CmdData) (contain

b, err := buildah.NewBuildah(*buildahMode, buildah.BuildahOpts{
CommonBuildahOpts: buildah.CommonBuildahOpts{
TmpDir: filepath.Join(werf.GetServiceDir(), "tmp", "buildah"),
Insecure: insecure,
Isolation: buildahIsolation,
StorageDriver: storageDriver,
TmpDir: filepath.Join(werf.GetServiceDir(), "tmp", "buildah"),
Insecure: insecure,
Isolation: buildahIsolation,
StorageDriver: storageDriver,
RegistryMirrors: registryMirrors,
},
NativeModeOpts: buildah.NativeModeOpts{},
})
Expand Down
10 changes: 8 additions & 2 deletions cmd/werf/compose/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ func newCmd(ctx context.Context, composeCmdName string, options *newCmdOptions)
common.SetupInsecureRegistry(&commonCmdData, cmd)
common.SetupInsecureHelmDependencies(&commonCmdData, cmd, true)
common.SetupSkipTlsVerifyRegistry(&commonCmdData, cmd)
common.SetupContainerRegistryMirror(&commonCmdData, cmd)

common.SetupLogOptions(&commonCmdData, cmd)
common.SetupLogProjectDir(&commonCmdData, cmd)
Expand Down Expand Up @@ -271,7 +272,12 @@ func runMain(ctx context.Context, dockerComposeCmdName string, cmdData composeCm
return fmt.Errorf("initialization error: %w", err)
}

containerBackend, processCtx, err := common.InitProcessContainerBackend(ctx, &commonCmdData)
registryMirrors, err := common.GetContainerRegistryMirror(ctx, &commonCmdData)
if err != nil {
return fmt.Errorf("get container registry mirrors: %w", err)
}

containerBackend, processCtx, err := common.InitProcessContainerBackend(ctx, &commonCmdData, registryMirrors)
if err != nil {
return err
}
Expand All @@ -298,7 +304,7 @@ func runMain(ctx context.Context, dockerComposeCmdName string, cmdData composeCm
return err
}

if err := common.DockerRegistryInit(ctx, &commonCmdData); err != nil {
if err := common.DockerRegistryInit(ctx, &commonCmdData, registryMirrors); err != nil {
return err
}

Expand Down
10 changes: 8 additions & 2 deletions cmd/werf/converge/converge.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ werf converge --repo registry.mydomain.com/web --env production`,
common.SetupInsecureRegistry(&commonCmdData, cmd)
common.SetupInsecureHelmDependencies(&commonCmdData, cmd, true)
common.SetupSkipTlsVerifyRegistry(&commonCmdData, cmd)
common.SetupContainerRegistryMirror(&commonCmdData, cmd)

common.SetupLogOptions(&commonCmdData, cmd)
common.SetupLogProjectDir(&commonCmdData, cmd)
Expand Down Expand Up @@ -223,7 +224,12 @@ func runMain(ctx context.Context, imagesToProcess build.ImagesToProcess) error {
return fmt.Errorf("initialization error: %w", err)
}

containerBackend, processCtx, err := common.InitProcessContainerBackend(ctx, &commonCmdData)
registryMirrors, err := common.GetContainerRegistryMirror(ctx, &commonCmdData)
if err != nil {
return fmt.Errorf("get container registry mirrors: %w", err)
}

containerBackend, processCtx, err := common.InitProcessContainerBackend(ctx, &commonCmdData, registryMirrors)
if err != nil {
return err
}
Expand All @@ -250,7 +256,7 @@ func runMain(ctx context.Context, imagesToProcess build.ImagesToProcess) error {
return err
}

if err := common.DockerRegistryInit(ctx, &commonCmdData); err != nil {
if err := common.DockerRegistryInit(ctx, &commonCmdData, registryMirrors); err != nil {
return err
}

Expand Down
8 changes: 7 additions & 1 deletion cmd/werf/dismiss/dismiss.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupInsecureRegistry(&commonCmdData, cmd)
common.SetupInsecureHelmDependencies(&commonCmdData, cmd, true)
common.SetupSkipTlsVerifyRegistry(&commonCmdData, cmd)
common.SetupContainerRegistryMirror(&commonCmdData, cmd)

commonCmdData.SetupPlatform(cmd)

Expand All @@ -142,7 +143,12 @@ func runDismiss(ctx context.Context) error {
return fmt.Errorf("initialization error: %w", err)
}

containerBackend, processCtx, err := common.InitProcessContainerBackend(ctx, &commonCmdData)
registryMirrors, err := common.GetContainerRegistryMirror(ctx, &commonCmdData)
if err != nil {
return fmt.Errorf("get container registry mirrors: %w", err)
}

containerBackend, processCtx, err := common.InitProcessContainerBackend(ctx, &commonCmdData, registryMirrors)
if err != nil {
return err
}
Expand Down
10 changes: 8 additions & 2 deletions cmd/werf/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func NewExportCmd(ctx context.Context) *cobra.Command {
common.SetupInsecureRegistry(&commonCmdData, cmd)
common.SetupInsecureHelmDependencies(&commonCmdData, cmd, true)
common.SetupSkipTlsVerifyRegistry(&commonCmdData, cmd)
common.SetupContainerRegistryMirror(&commonCmdData, cmd)

common.SetupLogOptions(&commonCmdData, cmd)
common.SetupLogProjectDir(&commonCmdData, cmd)
Expand Down Expand Up @@ -144,7 +145,12 @@ func run(ctx context.Context, imagesToProcess build.ImagesToProcess, tagTemplate
return fmt.Errorf("initialization error: %w", err)
}

containerBackend, processCtx, err := common.InitProcessContainerBackend(ctx, &commonCmdData)
registryMirrors, err := common.GetContainerRegistryMirror(ctx, &commonCmdData)
if err != nil {
return fmt.Errorf("get container registry mirrors: %w", err)
}

containerBackend, processCtx, err := common.InitProcessContainerBackend(ctx, &commonCmdData, registryMirrors)
if err != nil {
return err
}
Expand All @@ -171,7 +177,7 @@ func run(ctx context.Context, imagesToProcess build.ImagesToProcess, tagTemplate
return err
}

if err := common.DockerRegistryInit(ctx, &commonCmdData); err != nil {
if err := common.DockerRegistryInit(ctx, &commonCmdData, registryMirrors); err != nil {
return err
}

Expand Down

0 comments on commit bf185f2

Please sign in to comment.