Skip to content

Commit

Permalink
Merge pull request #22 from hyperspike/resources
Browse files Browse the repository at this point in the history
feat: add suport for resource requests and limits
  • Loading branch information
dmolik authored Aug 8, 2024
2 parents ed8aab3 + 0b8c7b7 commit b6294e6
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
3 changes: 3 additions & 0 deletions api/v1/valkey_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ type ValkeySpec struct {

// Persistent volume claim
Storage *corev1.PersistentVolumeClaim `json:"storage,omitempty"`

// Resources requirements and limits for the Valkey Server container
Resources *corev1.ResourceRequirements `json:"resources,omitempty"`
}

// ValkeyStatus defines the observed state of Valkey
Expand Down
5 changes: 5 additions & 0 deletions api/v1/zz_generated.deepcopy.go

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

56 changes: 56 additions & 0 deletions config/crd/bases/hyperspike.io_valkeys.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,62 @@ spec:
description: Number of replicas
format: int32
type: integer
resources:
description: Resources requirements and limits for the Valkey Server
container
properties:
claims:
description: |-
Claims lists the names of resources, defined in spec.resourceClaims,
that are used by this container.
This is an alpha field and requires enabling the
DynamicResourceAllocation feature gate.
This field is immutable. It can only be set for containers.
items:
description: ResourceClaim references one entry in PodSpec.ResourceClaims.
properties:
name:
description: |-
Name must match the name of one entry in pod.spec.resourceClaims of
the Pod where this field is used. It makes that resource available
inside a container.
type: string
required:
- name
type: object
type: array
x-kubernetes-list-map-keys:
- name
x-kubernetes-list-type: map
limits:
additionalProperties:
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
description: |-
Limits describes the maximum amount of compute resources allowed.
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
type: object
requests:
additionalProperties:
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
description: |-
Requests describes the minimum amount of compute resources required.
If Requests is omitted for a container, it defaults to Limits if that is explicitly specified,
otherwise to an implementation-defined value. Requests cannot exceed Limits.
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
type: object
type: object
storage:
description: Persistent volume claim
properties:
Expand Down
49 changes: 49 additions & 0 deletions internal/controller/valkey_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,7 @@ func exporter(valkey *hyperv1.Valkey) corev1.Container {
"-c",
"redis_exporter", // this seems liable to change
},
Resources: getExporterResourceRequirements(),
SecurityContext: &corev1.SecurityContext{
AllowPrivilegeEscalation: func(b bool) *bool { return &b }(false),
Capabilities: &corev1.Capabilities{
Expand Down Expand Up @@ -1130,7 +1131,53 @@ func generatePVC(valkey *hyperv1.Valkey) corev1.PersistentVolumeClaim {
}
return pv
}
func getResourceRequirements(valkey *hyperv1.Valkey) corev1.ResourceRequirements {
if valkey.Spec.Resources != nil {
return *valkey.Spec.Resources
}

// Default resource requirements if not specified in the CR
return corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("100m"),
corev1.ResourceEphemeralStorage: resource.MustParse("50Mi"),
corev1.ResourceMemory: resource.MustParse("128Mi"),
},
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("150m"),
corev1.ResourceEphemeralStorage: resource.MustParse("2Gi"),
corev1.ResourceMemory: resource.MustParse("192Mi"),
},
}
}

func getExporterResourceRequirements() corev1.ResourceRequirements {
return corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("50m"),
corev1.ResourceEphemeralStorage: resource.MustParse("50Mi"),
corev1.ResourceMemory: resource.MustParse("64Mi"),
},
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("100m"),
corev1.ResourceEphemeralStorage: resource.MustParse("2Gi"),
corev1.ResourceMemory: resource.MustParse("128Mi"),
},
}
}

func getInitContainerResourceRequirements() corev1.ResourceRequirements {
return corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("50m"),
corev1.ResourceMemory: resource.MustParse("64Mi"),
},
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("100m"),
corev1.ResourceMemory: resource.MustParse("128Mi"),
},
}
}
func (r *ValkeyReconciler) upsertStatefulSet(ctx context.Context, valkey *hyperv1.Valkey) error {
logger := log.FromContext(ctx)

Expand Down Expand Up @@ -1315,6 +1362,7 @@ fi
},
},
},
Resources: getResourceRequirements(valkey),
VolumeMounts: []corev1.VolumeMount{
{
Name: "scripts",
Expand Down Expand Up @@ -1397,6 +1445,7 @@ fi
"1001:1001",
"/bitnami/valkey/data",
},
Resources: getInitContainerResourceRequirements(),
SecurityContext: &corev1.SecurityContext{
RunAsUser: func(i int64) *int64 { return &i }(0),
},
Expand Down

0 comments on commit b6294e6

Please sign in to comment.