From 6db89ac587bb3527a492a819e21c72a9b1212d42 Mon Sep 17 00:00:00 2001 From: Thorsten Klein Date: Fri, 28 Jul 2023 19:06:16 +0200 Subject: [PATCH] change: if default storage class exists use 10Gi PVC for registry (#1985) Signed-off-by: Thorsten Klein --- pkg/imagesystem/registry.go | 50 ++++++++++++++++++++++++++--- pkg/imagesystem/registrytemplate.go | 8 ++--- pkg/system/constants.go | 1 + pkg/volume/volume.go | 15 +++++++++ 4 files changed, 65 insertions(+), 9 deletions(-) diff --git a/pkg/imagesystem/registry.go b/pkg/imagesystem/registry.go index 18cae2b1d..3bd0c4009 100644 --- a/pkg/imagesystem/registry.go +++ b/pkg/imagesystem/registry.go @@ -11,9 +11,12 @@ import ( v1 "github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1" "github.com/acorn-io/runtime/pkg/config" "github.com/acorn-io/runtime/pkg/system" + "github.com/acorn-io/runtime/pkg/volume" "github.com/google/go-containerregistry/pkg/name" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/api/resource" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -121,10 +124,49 @@ func GetRegistryObjects(ctx context.Context, c client.Reader) (result []client.O return nil, err } - result = append(result, registryDeployment( - system.ImagesNamespace, - system.DefaultImage(), - system.ResourceRequirementsFor(*cfg.RegistryMemory, *cfg.RegistryCPU))...) + volumeSource := corev1.VolumeSource{ + EmptyDir: &corev1.EmptyDirVolumeSource{}, + } + + if sc, err := volume.FindDefaultStorageClass(ctx, c); err != nil { + return nil, err + } else if sc != "" { + volumeSource = corev1.VolumeSource{ + PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ + ClaimName: system.RegistryName, + }, + } + + result = append(result, + &corev1.PersistentVolumeClaim{ + ObjectMeta: metav1.ObjectMeta{ + Name: system.RegistryName, + Namespace: system.ImagesNamespace, + Labels: map[string]string{ + "app": system.RegistryName, + }, + }, + Spec: corev1.PersistentVolumeClaimSpec{ + StorageClassName: &sc, + AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce}, + Resources: corev1.ResourceRequirements{ + Requests: corev1.ResourceList{ + corev1.ResourceStorage: resource.MustParse(system.RegistryPVCSize), + }, + }, + }, + }, + ) + } + + result = append(result, + registryDeployment( + system.ImagesNamespace, + system.DefaultImage(), + system.ResourceRequirementsFor(*cfg.RegistryMemory, *cfg.RegistryCPU), + volumeSource, + )..., + ) return result, nil } diff --git a/pkg/imagesystem/registrytemplate.go b/pkg/imagesystem/registrytemplate.go index 2660e5121..cf2f4ac54 100644 --- a/pkg/imagesystem/registrytemplate.go +++ b/pkg/imagesystem/registrytemplate.go @@ -39,7 +39,7 @@ func registryService(namespace string) []client.Object { } } -func registryDeployment(namespace, registryImage string, requirements corev1.ResourceRequirements) []client.Object { +func registryDeployment(namespace, registryImage string, requirements corev1.ResourceRequirements, volumeSource corev1.VolumeSource) []client.Object { deployment := &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: system.RegistryName, @@ -117,10 +117,8 @@ func registryDeployment(namespace, registryImage string, requirements corev1.Res }, Volumes: []corev1.Volume{ { - VolumeSource: corev1.VolumeSource{ - EmptyDir: &corev1.EmptyDirVolumeSource{}, - }, - Name: "registry", + VolumeSource: volumeSource, + Name: "registry", }, }, Tolerations: []corev1.Toleration{ diff --git a/pkg/system/constants.go b/pkg/system/constants.go index 34aff4303..0a9e52f88 100644 --- a/pkg/system/constants.go +++ b/pkg/system/constants.go @@ -22,6 +22,7 @@ const ( var ( RegistryName = "registry" + RegistryPVCSize = "10Gi" RegistryPort = 5000 BuildKitName = "buildkitd" ControllerName = "acorn-controller" diff --git a/pkg/volume/volume.go b/pkg/volume/volume.go index b2f624b1e..96a7b2b31 100644 --- a/pkg/volume/volume.go +++ b/pkg/volume/volume.go @@ -173,3 +173,18 @@ func CopyVolumeDefaults(volumeRequest v1.VolumeRequest, volumeBinding v1.VolumeB return volumeRequest } + +func FindDefaultStorageClass(ctx context.Context, c client.Reader) (string, error) { + storageClasses := &storagev1.StorageClassList{} + if err := c.List(ctx, storageClasses); err != nil { + return "", err + } + + for _, sc := range storageClasses.Items { + if sc.Annotations[storage.IsDefaultStorageClassAnnotation] == "true" { + return sc.Name, nil + } + } + + return "", nil +}