Skip to content

Commit

Permalink
feat(build, buildah): support mirror registries (v1)
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksei Igrychev <aleksei.igrychev@palark.com>
  • Loading branch information
alexey-igrychev committed May 30, 2024
1 parent 9ccc030 commit 66f4942
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
34 changes: 33 additions & 1 deletion pkg/buildah/native_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"strconv"
"strings"
"syscall"
"text/template"
"time"

"github.com/containerd/containerd/platforms"
Expand Down Expand Up @@ -43,6 +44,7 @@ import (

"github.com/werf/werf/pkg/buildah/thirdparty"
"github.com/werf/werf/pkg/image"
"github.com/werf/werf/pkg/util"
)

const (
Expand Down Expand Up @@ -121,8 +123,14 @@ func NewNativeBuildah(commonOpts CommonBuildahOpts, opts NativeModeOpts) (*Nativ
return nil, fmt.Errorf("unable to set env var CONTAINERS_CONF: %w", err)
}

mirrorsFromEnv := util.PredefinedValuesByEnvNamePrefix("WERF_CONTAINER_REGISTRY_MIRROR_")
registriesConfig, err := generateRegistriesConfig(mirrorsFromEnv)
if err != nil {
return nil, fmt.Errorf("unable to generate registries config: %w", err)
}

b.RegistriesConfigPath = filepath.Join(b.ConfigTmpDir, "registries.conf")
if err := ioutil.WriteFile(b.RegistriesConfigPath, []byte(DefaultRegistriesConfig), os.ModePerm); err != nil {
if err := ioutil.WriteFile(b.RegistriesConfigPath, []byte(registriesConfig), os.ModePerm); err != nil {
return nil, fmt.Errorf("unable to write file %q: %w", b.RegistriesConfigPath, err)
}

Expand Down Expand Up @@ -210,6 +218,30 @@ func (b *NativeBuildah) getSystemContext(targetPlatform string) (*imgtypes.Syste
return systemContext, nil
}

func generateRegistriesConfig(mirrors []string) (string, error) {
tpl := `
unqualified-search-registries = ["docker.io"]
{{ range . -}}
[[registry.mirror]]
location = "{{ . }}"
{{ end -}}
`
tmpl, err := template.New("tmp").Parse(tpl)
if err != nil {
return "", err
}

var result bytes.Buffer
err = tmpl.Execute(&result, mirrors)
if err != nil {
return "", err
}

return result.String(), nil
}

func (b *NativeBuildah) getRuntime(systemContext *imgtypes.SystemContext) (*libimage.Runtime, error) {
return libimage.RuntimeFromStore(b.Store, &libimage.RuntimeOptions{
SystemContext: systemContext,
Expand Down
4 changes: 4 additions & 0 deletions pkg/docker_registry/generic_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/werf/werf/pkg/docker"
"github.com/werf/werf/pkg/image"
"github.com/werf/werf/pkg/util"
)

type genericApi struct {
Expand Down Expand Up @@ -141,6 +142,9 @@ func (api *genericApi) getOrCreateRegistryMirrors(ctx context.Context) ([]string
}
}

mirrorsFromEnv := util.PredefinedValuesByEnvNamePrefix("WERF_CONTAINER_REGISTRY_MIRROR_")
mirrors = append(mirrors, mirrorsFromEnv...)

api.mirrors = &mirrors
}

Expand Down

0 comments on commit 66f4942

Please sign in to comment.