From 804b0d9851dd7a8ee4afef2ae1778b30fd0a1403 Mon Sep 17 00:00:00 2001 From: Tomas Pierantoni Date: Wed, 25 Dec 2024 15:34:33 +0100 Subject: [PATCH] Move resources management to defaults --- api/v1alpha1/redis_types.go | 1 - api/v1alpha1/zz_generated.deepcopy.go | 1 - config/crd/bases/cache.yazio.com_redis.yaml | 6 ------ config/samples/cache_v1alpha1_redis.yaml | 1 - internal/controller/redis_controller.go | 18 ++++++++++++++++++ 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/api/v1alpha1/redis_types.go b/api/v1alpha1/redis_types.go index c407093..8b26d57 100644 --- a/api/v1alpha1/redis_types.go +++ b/api/v1alpha1/redis_types.go @@ -26,7 +26,6 @@ type RedisSpec struct { // +kubebuilder:validation:Minimum=1 Replicas int32 `json:"replicas,omitempty"` - Memory resource.Quantity `json:"memory,omitempty"` Version string `json:"version,omitempty"` VolumeStorage resource.Quantity `json:"volumeStorage,omitempty"` } diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 679eba1..cebb20e 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -87,7 +87,6 @@ func (in *RedisList) DeepCopyObject() runtime.Object { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *RedisSpec) DeepCopyInto(out *RedisSpec) { *out = *in - out.Memory = in.Memory.DeepCopy() out.VolumeStorage = in.VolumeStorage.DeepCopy() } diff --git a/config/crd/bases/cache.yazio.com_redis.yaml b/config/crd/bases/cache.yazio.com_redis.yaml index 79af2bf..da6f032 100644 --- a/config/crd/bases/cache.yazio.com_redis.yaml +++ b/config/crd/bases/cache.yazio.com_redis.yaml @@ -38,12 +38,6 @@ spec: spec: description: RedisSpec defines the desired state of Redis properties: - memory: - anyOf: - - type: integer - - type: string - pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ - x-kubernetes-int-or-string: true replicas: format: int32 minimum: 1 diff --git a/config/samples/cache_v1alpha1_redis.yaml b/config/samples/cache_v1alpha1_redis.yaml index 93b18fa..deb5c52 100644 --- a/config/samples/cache_v1alpha1_redis.yaml +++ b/config/samples/cache_v1alpha1_redis.yaml @@ -7,6 +7,5 @@ metadata: name: redis-sample spec: replicas: 3 - memory: 2G version: "7.4.1" volumeStorage: 2Gi diff --git a/internal/controller/redis_controller.go b/internal/controller/redis_controller.go index f6f693e..c39dadc 100644 --- a/internal/controller/redis_controller.go +++ b/internal/controller/redis_controller.go @@ -53,6 +53,9 @@ const ( passwordLength = 12 redisImage = "bitnami/redis" redisPasswordSecretKey = "redis-password" + defaultMemoryRequest = "512Mi" + defaultCpuRequest = "100m" + defaultMemoryLimit = "512Mi" ) var BaseLabels = map[string]string{ @@ -407,6 +410,7 @@ func (r *RedisReconciler) createOrUpdateMasterSS(ctx context.Context, redis *cac MountPath: "/data", }, }, + Resources: getResources(), }, }, Volumes: []corev1.Volume{ @@ -552,6 +556,7 @@ func (r *RedisReconciler) createOrUpdateReplicasSS(ctx context.Context, redis *c MountPath: "/data", }, }, + Resources: getResources(), }, }, Volumes: []corev1.Volume{ @@ -655,6 +660,19 @@ func (r *RedisReconciler) createOrUpdateConfigMaps(ctx context.Context, redis *c return nil } +// Making this a method in case this needs to be calculated in some other way in the future +func getResources() corev1.ResourceRequirements { + return corev1.ResourceRequirements{ + Requests: corev1.ResourceList{ + corev1.ResourceMemory: resource.MustParse(defaultMemoryRequest), + corev1.ResourceCPU: resource.MustParse(defaultCpuRequest), + }, + Limits: corev1.ResourceList{ + corev1.ResourceMemory: resource.MustParse(defaultMemoryLimit), + }, + } +} + func generateSecureRedisPassword() string { var password []byte //TODO improve this, it works fine but the code is ugly and will only have 1 uppercase character, though it is good enough for now