Skip to content

Commit

Permalink
Add optional highAvailability feature for registry cache
Browse files Browse the repository at this point in the history
  • Loading branch information
dergeberl committed Dec 27, 2024
1 parent 1ff1d17 commit 2c2264c
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 15 deletions.
6 changes: 5 additions & 1 deletion docs/usage/registry-cache/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ The `providerConfig.caches[].proxy.httpProxy` field represents the proxy server

The `providerConfig.caches[].proxy.httpsProxy` field represents the proxy server for HTTPS connections which is used by the registry cache. It must include an `https://` or `http://` scheme.

The `providerConfig.caches[].highAvailability` defines if the StatefulSet is scaled to 2 replicas. See [Increase the cache disk size](#high-availability) for more information.

## Garbage Collection

When the registry cache receives a request for an image that is not present in its local store, it fetches the image from the upstream, returns it to the client and stores the image in the local store. The registry cache runs a scheduler that deletes images when their time to live (ttl) expires. When adding an image to the local store, the registry cache also adds a time to live for the image. The ttl defaults to `168h` (7 days) and is configurable. The garbage collection can be disabled by setting the ttl to `0s`. Requesting an image from the registry cache does not extend the time to live of the image. Hence, an image is always garbage collected from the registry cache store when its ttl expires.
Expand Down Expand Up @@ -145,7 +147,9 @@ There is always the option to remove the cache from the Shoot spec and to readd

## High Аvailability

The registry cache runs with a single replica. This fact may lead to concerns for the high availability such as "What happens when the registry cache is down? Does containerd fail to pull the image?". As outlined in the [How does it work? section](#how-does-it-work), containerd is configured to fall back to the upstream registry if it fails to pull the image from the registry cache. Hence, when the registry cache is unavailable, the containerd's image pull operations are not affected because containerd falls back to image pull from the upstream registry.
Per default the registry cache runs with a single replica. This fact may lead to concerns for the high availability such as "What happens when the registry cache is down? Does containerd fail to pull the image?". As outlined in the [How does it work? section](#how-does-it-work), containerd is configured to fall back to the upstream registry if it fails to pull the image from the registry cache. Hence, when the registry cache is unavailable, the containerd's image pull operations are not affected because containerd falls back to image pull from the upstream registry.

In special cases where this is not enough (for example when using the registry cache with a proxy) it is possible to set `providerConfig.caches[].highAvailability` to `true`, this will add the label `high-availability-config.resources.gardener.cloud/type=server` and scale the statefulset to 2 replicas. The `topologySpreadConstraints` is added according to the cluster configuration. See also [High Availability of Deployed Components](https://gardener.cloud/docs/gardener/high-availability/#system-components).

## Possible Pitfalls

Expand Down
12 changes: 12 additions & 0 deletions hack/api-reference/registry.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ Proxy
<p>Proxy contains settings for a proxy used in the registry cache.</p>
</td>
</tr>
<tr>
<td>
<code>highAvailability</code></br>
<em>
bool
</em>
</td>
<td>
<em>(Optional)</em>
<p>HighAvailability defines if the StatefulSet is scaled with the <a href="https://gardener.cloud/docs/gardener/high-availability/#system-components">High Availability</a> feature.</p>
</td>
</tr>
</tbody>
</table>
<h3 id="registry.extensions.gardener.cloud/v1alpha3.RegistryCacheStatus">RegistryCacheStatus
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/registry/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type RegistryCache struct {
SecretReferenceName *string
// Proxy contains settings for a proxy used in the registry cache.
Proxy *Proxy
// HighAvailability defines if the StatefulSet is scaled with the [High Availability](https://gardener.cloud/docs/gardener/high-availability/#system-components) feature.
HighAvailability *bool
}

// Volume contains settings for the registry cache volume.
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/registry/v1alpha3/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ type RegistryCache struct {
// Proxy contains settings for a proxy used in the registry cache.
// +optional
Proxy *Proxy `json:"proxy,omitempty"`
// HighAvailability defines if the StatefulSet is scaled with the [High Availability](https://gardener.cloud/docs/gardener/high-availability/#system-components) feature.
// +optional
HighAvailability *bool `json:"highAvailability,omitempty"`
}

// Volume contains settings for the registry cache volume.
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/registry/v1alpha3/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkg/apis/registry/v1alpha3/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkg/apis/registry/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion pkg/component/registrycaches/registry_caches.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
v1beta1helper "github.com/gardener/gardener/pkg/apis/core/v1beta1/helper"
"github.com/gardener/gardener/pkg/apis/resources/v1alpha1"
"github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/gardener/gardener/pkg/component"
"github.com/gardener/gardener/pkg/utils"
Expand Down Expand Up @@ -296,11 +297,16 @@ func (r *registryCaches) computeResourcesDataForRegistryCache(ctx context.Contex
}
utilruntime.Must(kubernetesutils.MakeUnique(tlsSecret))

additionalStatefulSetLabels := map[string]string{}
if cache.HighAvailability != nil && *cache.HighAvailability {
additionalStatefulSetLabels[v1alpha1.HighAvailabilityConfigType] = v1alpha1.HighAvailabilityConfigTypeServer
}

statefulSet := &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: metav1.NamespaceSystem,
Labels: registryutils.GetLabels(name, upstreamLabel),
Labels: utils.MergeStringMaps(registryutils.GetLabels(name, upstreamLabel), additionalStatefulSetLabels),
},
Spec: appsv1.StatefulSetSpec{
ServiceName: name,
Expand Down
65 changes: 52 additions & 13 deletions pkg/component/registrycaches/registry_caches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ proxy:
}
}

statefulSetFor = func(name, upstream, size, configSecretName, tlsSecretName, tlsSecretChecksum string, storageClassName *string, additionalEnvs []corev1.EnvVar) *appsv1.StatefulSet {
statefulSetFor = func(name, upstream, size, configSecretName, tlsSecretName, tlsSecretChecksum string, storageClassName *string, additionalEnvs []corev1.EnvVar, ha bool) *appsv1.StatefulSet {
env := []corev1.EnvVar{
{
Name: "OTEL_TRACES_EXPORTER",
Expand All @@ -277,14 +277,19 @@ proxy:
}
env = append(env, additionalEnvs...)

stsLabels := map[string]string{
"app": name,
"upstream-host": upstream,
}
if ha {
stsLabels[resourcesv1alpha1.HighAvailabilityConfigType] = resourcesv1alpha1.HighAvailabilityConfigTypeServer
}

return &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: "kube-system",
Labels: map[string]string{
"app": name,
"upstream-host": upstream,
},
Labels: stsLabels,
},
Spec: appsv1.StatefulSetSpec{
ServiceName: name,
Expand Down Expand Up @@ -544,11 +549,11 @@ source /entrypoint.sh /etc/distribution/config.yml
Expect(managedResource).To(consistOf(
dockerConfigSecret,
dockerTLSSecret,
statefulSetFor("registry-docker-io", "docker.io", "10Gi", dockerConfigSecret.Name, dockerTLSSecret.Name, utils.ComputeChecksum(dockerTLSSecret.Data), nil, nil),
statefulSetFor("registry-docker-io", "docker.io", "10Gi", dockerConfigSecret.Name, dockerTLSSecret.Name, utils.ComputeChecksum(dockerTLSSecret.Data), nil, nil, false),
vpaFor("registry-docker-io"),
arConfigSecret,
arTLSSecret,
statefulSetFor("registry-europe-docker-pkg-dev", "europe-docker.pkg.dev", "20Gi", arConfigSecret.Name, arTLSSecret.Name, utils.ComputeChecksum(arTLSSecret.Data), ptr.To("premium"), nil),
statefulSetFor("registry-europe-docker-pkg-dev", "europe-docker.pkg.dev", "20Gi", arConfigSecret.Name, arTLSSecret.Name, utils.ComputeChecksum(arTLSSecret.Data), ptr.To("premium"), nil, false),
vpaFor("registry-europe-docker-pkg-dev"),
))
})
Expand Down Expand Up @@ -577,10 +582,10 @@ source /entrypoint.sh /etc/distribution/config.yml
Expect(managedResource).To(consistOf(
dockerConfigSecret,
dockerTLSSecret,
statefulSetFor("registry-docker-io", "docker.io", "10Gi", dockerConfigSecret.Name, dockerTLSSecret.Name, utils.ComputeChecksum(dockerTLSSecret.Data), nil, nil),
statefulSetFor("registry-docker-io", "docker.io", "10Gi", dockerConfigSecret.Name, dockerTLSSecret.Name, utils.ComputeChecksum(dockerTLSSecret.Data), nil, nil, false),
arConfigSecret,
arTLSSecret,
statefulSetFor("registry-europe-docker-pkg-dev", "europe-docker.pkg.dev", "20Gi", arConfigSecret.Name, arTLSSecret.Name, utils.ComputeChecksum(arTLSSecret.Data), ptr.To("premium"), nil),
statefulSetFor("registry-europe-docker-pkg-dev", "europe-docker.pkg.dev", "20Gi", arConfigSecret.Name, arTLSSecret.Name, utils.ComputeChecksum(arTLSSecret.Data), ptr.To("premium"), nil, false),
))
})
})
Expand Down Expand Up @@ -626,11 +631,45 @@ source /entrypoint.sh /etc/distribution/config.yml
Expect(managedResource).To(consistOf(
dockerConfigSecret,
dockerTLSSecret,
statefulSetFor("registry-docker-io", "docker.io", "10Gi", dockerConfigSecret.Name, dockerTLSSecret.Name, utils.ComputeChecksum(dockerTLSSecret.Data), nil, additionalEnvs),
statefulSetFor("registry-docker-io", "docker.io", "10Gi", dockerConfigSecret.Name, dockerTLSSecret.Name, utils.ComputeChecksum(dockerTLSSecret.Data), nil, additionalEnvs, false),
vpaFor("registry-docker-io"),
arConfigSecret,
arTLSSecret,
statefulSetFor("registry-europe-docker-pkg-dev", "europe-docker.pkg.dev", "20Gi", arConfigSecret.Name, arTLSSecret.Name, utils.ComputeChecksum(arTLSSecret.Data), ptr.To("premium"), additionalEnvs, false),
vpaFor("registry-europe-docker-pkg-dev"),
))
})
})

Context("when HA is enabled", func() {
BeforeEach(func() {
values.Caches[0].HighAvailability = ptr.To(true)
values.Caches[1].HighAvailability = ptr.To(true)
})

It("should successfully deploy the resources", func() {
Expect(registryCaches.Deploy(ctx)).To(Succeed())

Expect(c.Get(ctx, client.ObjectKeyFromObject(managedResource), managedResource)).To(Succeed())

dockerConfigSecret := configSecretFor("registry-docker-io", "docker.io", configYAMLFor("https://registry-1.docker.io", "336h0m0s", "", ""))
arConfigSecret := configSecretFor("registry-europe-docker-pkg-dev", "europe-docker.pkg.dev", configYAMLFor("https://europe-docker.pkg.dev", "0s", "", ""))

dockerSecretsManagerSecret, ok := secretsManager.Get("docker.io-tls")
Expect(ok).To(BeTrue())
dockerTLSSecret := tlsSecretFor("registry-docker-io", "docker.io", dockerSecretsManagerSecret.Data["ca.crt"], dockerSecretsManagerSecret.Data["ca.key"])
arSecretsManagerSecret, ok := secretsManager.Get("europe-docker.pkg.dev-tls")
Expect(ok).To(BeTrue())
arTLSSecret := tlsSecretFor("registry-europe-docker-pkg-dev", "europe-docker.pkg.dev", arSecretsManagerSecret.Data["ca.crt"], arSecretsManagerSecret.Data["ca.key"])

Expect(managedResource).To(consistOf(
dockerConfigSecret,
dockerTLSSecret,
statefulSetFor("registry-docker-io", "docker.io", "10Gi", dockerConfigSecret.Name, dockerTLSSecret.Name, utils.ComputeChecksum(dockerTLSSecret.Data), nil, nil, true),
vpaFor("registry-docker-io"),
arConfigSecret,
arTLSSecret,
statefulSetFor("registry-europe-docker-pkg-dev", "europe-docker.pkg.dev", "20Gi", arConfigSecret.Name, arTLSSecret.Name, utils.ComputeChecksum(arTLSSecret.Data), ptr.To("premium"), additionalEnvs),
statefulSetFor("registry-europe-docker-pkg-dev", "europe-docker.pkg.dev", "20Gi", arConfigSecret.Name, arTLSSecret.Name, utils.ComputeChecksum(arTLSSecret.Data), ptr.To("premium"), nil, true),
vpaFor("registry-europe-docker-pkg-dev"),
))
})
Expand Down Expand Up @@ -698,11 +737,11 @@ source /entrypoint.sh /etc/distribution/config.yml
Expect(managedResource).To(consistOf(
dockerConfigSecret,
dockerTLSSecret,
statefulSetFor("registry-docker-io", "docker.io", "10Gi", dockerConfigSecret.Name, dockerTLSSecret.Name, utils.ComputeChecksum(dockerTLSSecret.Data), nil, nil),
statefulSetFor("registry-docker-io", "docker.io", "10Gi", dockerConfigSecret.Name, dockerTLSSecret.Name, utils.ComputeChecksum(dockerTLSSecret.Data), nil, nil, false),
vpaFor("registry-docker-io"),
arConfigSecret,
arTLSSecret,
statefulSetFor("registry-europe-docker-pkg-dev", "europe-docker.pkg.dev", "20Gi", arConfigSecret.Name, arTLSSecret.Name, utils.ComputeChecksum(arTLSSecret.Data), ptr.To("premium"), nil),
statefulSetFor("registry-europe-docker-pkg-dev", "europe-docker.pkg.dev", "20Gi", arConfigSecret.Name, arTLSSecret.Name, utils.ComputeChecksum(arTLSSecret.Data), ptr.To("premium"), nil, false),
vpaFor("registry-europe-docker-pkg-dev"),
))
})
Expand Down

0 comments on commit 2c2264c

Please sign in to comment.