Skip to content
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.

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

Rename append mirrors to prepend existing #750

Merged
merged 1 commit into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- [#747](https://github.com/spegel-org/spegel/pull/747) Update Go to 1.23.6.
- [#750](https://github.com/spegel-org/spegel/pull/750) Rename append mirrors to prepend existing.

### Deprecated

Expand Down
2 changes: 1 addition & 1 deletion charts/spegel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ Read the [getting started](https://spegel.dev/docs/getting-started/) guide to de
| serviceMonitor.relabelings | list | `[]` | List of relabeling rules to apply the target’s metadata labels. |
| serviceMonitor.scrapeTimeout | string | `"30s"` | Prometheus scrape interval timeout. |
| spegel.additionalMirrorRegistries | list | `[]` | Additional target mirror registries other than Spegel. |
| spegel.appendMirrors | bool | `false` | When true existing mirror configuration will be appended to instead of replaced. |
| spegel.containerdContentPath | string | `"/var/lib/containerd/io.containerd.content.v1.content"` | Path to Containerd content store.. |
| spegel.containerdMirrorAdd | bool | `true` | If true Spegel will add mirror configuration to the node. |
| spegel.containerdNamespace | string | `"k8s.io"` | Containerd namespace where images are stored. |
Expand All @@ -55,6 +54,7 @@ Read the [getting started](https://spegel.dev/docs/getting-started/) guide to de
| spegel.logLevel | string | `"INFO"` | Minimum log level to output. Value should be DEBUG, INFO, WARN, or ERROR. |
| spegel.mirrorResolveRetries | int | `3` | Max ammount of mirrors to attempt. |
| spegel.mirrorResolveTimeout | string | `"20ms"` | Max duration spent finding a mirror. |
| spegel.prependExisting | bool | `false` | When true existing mirror configuration will be kept and Spegel will prepend it's configuration. |
| spegel.registries | list | `["https://cgr.dev","https://docker.io","https://ghcr.io","https://quay.io","https://mcr.microsoft.com","https://public.ecr.aws","https://gcr.io","https://registry.k8s.io","https://k8s.gcr.io","https://lscr.io"]` | Registries for which mirror configuration will be created. |
| spegel.resolveLatestTag | bool | `true` | When true latest tags will be resolved to digests. |
| spegel.resolveTags | bool | `true` | When true Spegel will resolve tags to digests. |
Expand Down
2 changes: 1 addition & 1 deletion charts/spegel/templates/daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ spec:
{{- end }}
{{- end }}
- --resolve-tags={{ .Values.spegel.resolveTags }}
- --append-mirrors={{ .Values.spegel.appendMirrors }}
- --prepend-existing={{ .Values.spegel.prependExisting }}
env:
- name: NODE_IP
{{- include "networking.nodeIp" . | nindent 10 }}
Expand Down
4 changes: 2 additions & 2 deletions charts/spegel/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ spegel:
resolveTags: true
# -- When true latest tags will be resolved to digests.
resolveLatestTag: true
# -- When true existing mirror configuration will be appended to instead of replaced.
appendMirrors: false
# -- When true existing mirror configuration will be kept and Spegel will prepend it's configuration.
prependExisting: false

verticalPodAutoscaler:
# -- If true creates a Vertical Pod Autoscaler.
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
Registries []url.URL `arg:"--registries,required,env:REGISTRIES" help:"registries that are configured to be mirrored."`
MirrorRegistries []url.URL `arg:"--mirror-registries,env:MIRROR_REGISTRIES,required" help:"registries that are configured to act as mirrors."`
ResolveTags bool `arg:"--resolve-tags,env:RESOLVE_TAGS" default:"true" help:"When true Spegel will resolve tags to digests."`
AppendMirrors bool `arg:"--append-mirrors,env:APPEND_MIRRORS" default:"false" help:"When true existing mirror configuration will be appended to instead of replaced."`
PrependExisting bool `arg:"--prepend-existing,env:PREPEND_EXISTING" default:"false" help:"When true existing mirror configuration will be kept and Spegel will prepend it's configuration."`
}

type BootstrapConfig struct {
Expand Down Expand Up @@ -105,7 +105,7 @@

func configurationCommand(ctx context.Context, args *ConfigurationCmd) error {
fs := afero.NewOsFs()
err := oci.AddMirrorConfiguration(ctx, fs, args.ContainerdRegistryConfigPath, args.Registries, args.MirrorRegistries, args.ResolveTags, args.AppendMirrors)
err := oci.AddMirrorConfiguration(ctx, fs, args.ContainerdRegistryConfigPath, args.Registries, args.MirrorRegistries, args.ResolveTags, args.PrependExisting)

Check warning on line 108 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L108

Added line #L108 was not covered by tests
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/oci/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func createFilters(registries []url.URL) (string, string) {
// Refer to containerd registry configuration documentation for more information about required configuration.
// https://github.com/containerd/containerd/blob/main/docs/cri/config.md#registry-configuration
// https://github.com/containerd/containerd/blob/main/docs/hosts.md#registry-configuration---examples
func AddMirrorConfiguration(ctx context.Context, fs afero.Fs, configPath string, registryURLs, mirrorURLs []url.URL, resolveTags, appendToBackup bool) error {
func AddMirrorConfiguration(ctx context.Context, fs afero.Fs, configPath string, registryURLs, mirrorURLs []url.URL, resolveTags, prependExisting bool) error {
log := logr.FromContextOrDiscard(ctx)
err := validateRegistries(registryURLs)
if err != nil {
Expand Down Expand Up @@ -374,15 +374,15 @@ func AddMirrorConfiguration(ctx context.Context, fs afero.Fs, configPath string,
if err != nil {
return err
}
if appendToBackup {
if prependExisting {
existingHosts, err := existingHosts(fs, configPath, registryURL)
if err != nil {
return err
}
if existingHosts != "" {
templatedHosts = templatedHosts + "\n\n" + existingHosts
}
log.Info("appending to existing Containerd mirror configuration", "registry", registryURL.String())
log.Info("prepending to existing Containerd mirror configuration", "registry", registryURL.String())
}
fp := path.Join(configPath, registryURL.Host, "hosts.toml")
err = fs.MkdirAll(path.Dir(fp), 0o755)
Expand Down
40 changes: 20 additions & 20 deletions pkg/oci/containerd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,14 @@ func TestMirrorConfiguration(t *testing.T) {
mirrors []url.URL
resolveTags bool
createConfigPathDir bool
appendToBackup bool
prependExisting bool
}{
{
name: "multiple mirros",
resolveTags: true,
registries: stringListToUrlList(t, []string{"http://foo.bar:5000"}),
mirrors: stringListToUrlList(t, []string{"http://127.0.0.1:5000", "http://127.0.0.2:5000", "http://127.0.0.1:5001"}),
appendToBackup: false,
name: "multiple mirros",
resolveTags: true,
registries: stringListToUrlList(t, []string{"http://foo.bar:5000"}),
mirrors: stringListToUrlList(t, []string{"http://127.0.0.1:5000", "http://127.0.0.2:5000", "http://127.0.0.1:5001"}),
prependExisting: false,
expectedFiles: map[string]string{
"/etc/containerd/certs.d/foo.bar:5000/hosts.toml": `server = 'http://foo.bar:5000'

Expand All @@ -305,11 +305,11 @@ capabilities = ['pull', 'resolve']`,
},
},
{
name: "resolve tags disabled",
resolveTags: false,
registries: stringListToUrlList(t, []string{"https://docker.io", "http://foo.bar:5000"}),
mirrors: stringListToUrlList(t, []string{"http://127.0.0.1:5000"}),
appendToBackup: false,
name: "resolve tags disabled",
resolveTags: false,
registries: stringListToUrlList(t, []string{"https://docker.io", "http://foo.bar:5000"}),
mirrors: stringListToUrlList(t, []string{"http://127.0.0.1:5000"}),
prependExisting: false,
expectedFiles: map[string]string{
"/etc/containerd/certs.d/docker.io/hosts.toml": `server = 'https://registry-1.docker.io'

Expand All @@ -327,7 +327,7 @@ capabilities = ['pull']`,
registries: stringListToUrlList(t, []string{"https://docker.io", "http://foo.bar:5000"}),
mirrors: stringListToUrlList(t, []string{"http://127.0.0.1:5000"}),
createConfigPathDir: false,
appendToBackup: false,
prependExisting: false,
expectedFiles: map[string]string{
"/etc/containerd/certs.d/docker.io/hosts.toml": `server = 'https://registry-1.docker.io'

Expand All @@ -345,7 +345,7 @@ capabilities = ['pull', 'resolve']`,
registries: stringListToUrlList(t, []string{"https://docker.io", "http://foo.bar:5000"}),
mirrors: stringListToUrlList(t, []string{"http://127.0.0.1:5000"}),
createConfigPathDir: true,
appendToBackup: false,
prependExisting: false,
expectedFiles: map[string]string{
"/etc/containerd/certs.d/docker.io/hosts.toml": `server = 'https://registry-1.docker.io'

Expand All @@ -363,7 +363,7 @@ capabilities = ['pull', 'resolve']`,
registries: stringListToUrlList(t, []string{"https://docker.io", "http://foo.bar:5000"}),
mirrors: stringListToUrlList(t, []string{"http://127.0.0.1:5000"}),
createConfigPathDir: true,
appendToBackup: false,
prependExisting: false,
existingFiles: map[string]string{
"/etc/containerd/certs.d/docker.io/hosts.toml": "hello = 'world'",
"/etc/containerd/certs.d/ghcr.io/hosts.toml": "foo = 'bar'",
Expand All @@ -387,7 +387,7 @@ capabilities = ['pull', 'resolve']`,
registries: stringListToUrlList(t, []string{"https://docker.io", "http://foo.bar:5000"}),
mirrors: stringListToUrlList(t, []string{"http://127.0.0.1:5000"}),
createConfigPathDir: true,
appendToBackup: false,
prependExisting: false,
existingFiles: map[string]string{
"/etc/containerd/certs.d/_backup/docker.io/hosts.toml": "hello = 'world'",
"/etc/containerd/certs.d/_backup/ghcr.io/hosts.toml": "foo = 'bar'",
Expand All @@ -408,12 +408,12 @@ capabilities = ['pull', 'resolve']`,
},
},
{
name: "append to existing configuration",
name: "prepend to existing configuration",
resolveTags: true,
registries: stringListToUrlList(t, []string{"https://docker.io", "http://foo.bar:5000"}),
mirrors: stringListToUrlList(t, []string{"http://127.0.0.1:5000"}),
createConfigPathDir: true,
appendToBackup: true,
prependExisting: true,
existingFiles: map[string]string{
"/etc/containerd/certs.d/docker.io/hosts.toml": `server = 'https://registry-1.docker.io'

Expand Down Expand Up @@ -466,12 +466,12 @@ capabilities = ['pull', 'resolve']`,
},
},
{
name: "append to backup disabled",
name: "prepend existing disabled",
resolveTags: true,
registries: stringListToUrlList(t, []string{"https://docker.io", "http://foo.bar:5000"}),
mirrors: stringListToUrlList(t, []string{"http://127.0.0.1:5000"}),
createConfigPathDir: true,
appendToBackup: false,
prependExisting: false,
existingFiles: map[string]string{
"/etc/containerd/certs.d/docker.io/hosts.toml": `server = 'https://registry-1.docker.io'

Expand Down Expand Up @@ -525,7 +525,7 @@ capabilities = ['pull', 'resolve']`,
err := afero.WriteFile(fs, k, []byte(v), 0o644)
require.NoError(t, err)
}
err := AddMirrorConfiguration(context.TODO(), fs, registryConfigPath, tt.registries, tt.mirrors, tt.resolveTags, tt.appendToBackup)
err := AddMirrorConfiguration(context.TODO(), fs, registryConfigPath, tt.registries, tt.mirrors, tt.resolveTags, tt.prependExisting)
require.NoError(t, err)
ok, err := afero.DirExists(fs, "/etc/containerd/certs.d/_backup")
require.NoError(t, err)
Expand Down
Loading