Skip to content

Commit 4720b46

Browse files
feat(build, buildah): support mirror registries (v1)
Signed-off-by: Aleksei Igrychev <aleksei.igrychev@palark.com>
1 parent 661f357 commit 4720b46

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

pkg/buildah/native_linux.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"strconv"
1616
"strings"
1717
"syscall"
18+
"text/template"
1819
"time"
1920

2021
"github.com/containerd/containerd/platforms"
@@ -43,6 +44,7 @@ import (
4344

4445
"github.com/werf/werf/v2/pkg/buildah/thirdparty"
4546
"github.com/werf/werf/v2/pkg/image"
47+
"github.com/werf/werf/v2/pkg/util"
4648
)
4749

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

126+
mirrorsFromEnv := util.PredefinedValuesByEnvNamePrefix("WERF_CONTAINER_REGISTRY_MIRROR_")
127+
registriesConfig, err := generateRegistriesConfig(mirrorsFromEnv)
128+
if err != nil {
129+
return nil, fmt.Errorf("unable to generate registries config: %w", err)
130+
}
131+
124132
b.RegistriesConfigPath = filepath.Join(b.ConfigTmpDir, "registries.conf")
125-
if err := ioutil.WriteFile(b.RegistriesConfigPath, []byte(DefaultRegistriesConfig), os.ModePerm); err != nil {
133+
if err := ioutil.WriteFile(b.RegistriesConfigPath, []byte(registriesConfig), os.ModePerm); err != nil {
126134
return nil, fmt.Errorf("unable to write file %q: %w", b.RegistriesConfigPath, err)
127135
}
128136

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

221+
func generateRegistriesConfig(mirrors []string) (string, error) {
222+
tpl := `
223+
unqualified-search-registries = ["docker.io"]
224+
225+
{{ range . -}}
226+
[[registry.mirror]]
227+
location = "{{ . }}"
228+
229+
{{ end -}}
230+
`
231+
tmpl, err := template.New("tmp").Parse(tpl)
232+
if err != nil {
233+
return "", err
234+
}
235+
236+
var result bytes.Buffer
237+
err = tmpl.Execute(&result, mirrors)
238+
if err != nil {
239+
return "", err
240+
}
241+
242+
return result.String(), nil
243+
}
244+
213245
func (b *NativeBuildah) getRuntime(systemContext *imgtypes.SystemContext) (*libimage.Runtime, error) {
214246
return libimage.RuntimeFromStore(b.Store, &libimage.RuntimeOptions{
215247
SystemContext: systemContext,

pkg/docker_registry/generic_api.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/werf/werf/v2/pkg/docker"
1313
"github.com/werf/werf/v2/pkg/image"
14+
"github.com/werf/werf/v2/pkg/util"
1415
)
1516

1617
type genericApi struct {
@@ -141,6 +142,9 @@ func (api *genericApi) getOrCreateRegistryMirrors(ctx context.Context) ([]string
141142
}
142143
}
143144

145+
mirrorsFromEnv := util.PredefinedValuesByEnvNamePrefix("WERF_CONTAINER_REGISTRY_MIRROR_")
146+
mirrors = append(mirrors, mirrorsFromEnv...)
147+
144148
api.mirrors = &mirrors
145149
}
146150

0 commit comments

Comments
 (0)