diff --git a/packages/apps/tenant/templates/quota.yaml b/packages/apps/tenant/templates/quota.yaml index 6962d7c3a..b32917321 100644 --- a/packages/apps/tenant/templates/quota.yaml +++ b/packages/apps/tenant/templates/quota.yaml @@ -7,6 +7,17 @@ metadata: spec: hard: {{- include "cozy-lib.resources.flatten" (list .Values.resourceQuotas $) | nindent 6 }} +{{- /* The LimitRange only exists so pods without requests or limits still pass + a quota that tracks them, so it defaults only the resources the quota + bounds. A default limit on any other resource does nothing for the quota + and refuses every container that requests more without a limit of its + own: a 128Mi memory default under a quota on load balancers, cpu or + ephemeral-storage alone kept virt-launcher from starting. */}} +{{- $hard := include "cozy-lib.resources.flatten" (list .Values.resourceQuotas $) | fromYaml }} +{{- $cpu := or (hasKey $hard "requests.cpu") (hasKey $hard "limits.cpu") }} +{{- $memory := or (hasKey $hard "requests.memory") (hasKey $hard "limits.memory") }} +{{- $ephemeral := or (hasKey $hard "requests.ephemeral-storage") (hasKey $hard "limits.ephemeral-storage") }} +{{- if or $cpu $memory $ephemeral }} --- apiVersion: v1 kind: LimitRange @@ -16,12 +27,25 @@ metadata: spec: limits: - default: + {{- if $cpu }} cpu: "250m" + {{- end }} + {{- if $memory }} memory: "128Mi" + {{- end }} + {{- if $ephemeral }} ephemeral-storage: "2Gi" + {{- end }} defaultRequest: + {{- if $cpu }} cpu: "25m" + {{- end }} + {{- if $memory }} memory: "128Mi" + {{- end }} + {{- if $ephemeral }} ephemeral-storage: "50Mi" + {{- end }} type: Container {{- end }} +{{- end }} diff --git a/packages/apps/tenant/tests/quota_limitrange_test.yaml b/packages/apps/tenant/tests/quota_limitrange_test.yaml new file mode 100644 index 000000000..1de01e576 --- /dev/null +++ b/packages/apps/tenant/tests/quota_limitrange_test.yaml @@ -0,0 +1,186 @@ +suite: tenant LimitRange defaults only what the quota bounds + +# The LimitRange defaults requests and limits so pods without them still pass a +# quota that tracks cpu, memory or ephemeral-storage. A default on a resource +# the quota does not bound only gets in the way: its 128Mi memory limit refuses +# any container that requests more without a limit of its own, virt-launcher +# included. Each test pins the document count, so a quota that stops rendering +# at all reddens here too. +templates: + - templates/quota.yaml + +release: + name: tenant-test + namespace: tenant-test + +tests: + - it: renders nothing without resourceQuotas + asserts: + - hasDocuments: + count: 0 + + - it: renders no LimitRange under a quota on load balancers alone + set: + resourceQuotas: + services.loadbalancers: "1" + asserts: + - hasDocuments: + count: 1 + - isKind: + of: ResourceQuota + documentSelector: + path: kind + value: ResourceQuota + - equal: + path: spec.hard["services.loadbalancers"] + value: "1" + documentSelector: + path: kind + value: ResourceQuota + + - it: renders no LimitRange under a quota on storage alone + set: + resourceQuotas: + storage: 100Gi + asserts: + - hasDocuments: + count: 1 + - isKind: + of: ResourceQuota + documentSelector: + path: kind + value: ResourceQuota + + - it: renders no LimitRange under a quota on GPUs alone + set: + resourceQuotas: + nvidia.com/gpu: "2" + asserts: + - hasDocuments: + count: 1 + - isKind: + of: ResourceQuota + documentSelector: + path: kind + value: ResourceQuota + + - it: keeps every default under a quota on cpu, memory and ephemeral-storage + set: + resourceQuotas: + cpu: "4" + memory: 8Gi + ephemeral-storage: 10Gi + asserts: + - hasDocuments: + count: 2 + - isKind: + of: LimitRange + documentSelector: + path: kind + value: LimitRange + - equal: + path: metadata.name + value: tenant-range-limits + documentSelector: + path: kind + value: LimitRange + - equal: + path: spec.limits[0].default + value: + cpu: 250m + memory: 128Mi + ephemeral-storage: 2Gi + documentSelector: + path: kind + value: LimitRange + - equal: + path: spec.limits[0].defaultRequest + value: + cpu: 25m + memory: 128Mi + ephemeral-storage: 50Mi + documentSelector: + path: kind + value: LimitRange + + - it: defaults only cpu under a quota on cpu alone + set: + resourceQuotas: + cpu: "4" + asserts: + - hasDocuments: + count: 2 + - equal: + path: spec.limits[0].default + value: + cpu: 250m + documentSelector: + path: kind + value: LimitRange + - equal: + path: spec.limits[0].defaultRequest + value: + cpu: 25m + documentSelector: + path: kind + value: LimitRange + + - it: defaults only memory under a quota on memory alone + set: + resourceQuotas: + memory: 8Gi + asserts: + - hasDocuments: + count: 2 + - equal: + path: spec.limits[0].default + value: + memory: 128Mi + documentSelector: + path: kind + value: LimitRange + - equal: + path: spec.limits[0].defaultRequest + value: + memory: 128Mi + documentSelector: + path: kind + value: LimitRange + + - it: defaults only ephemeral-storage under a quota on ephemeral-storage alone + set: + resourceQuotas: + ephemeral-storage: 10Gi + asserts: + - hasDocuments: + count: 2 + - equal: + path: spec.limits[0].default + value: + ephemeral-storage: 2Gi + documentSelector: + path: kind + value: LimitRange + - equal: + path: spec.limits[0].defaultRequest + value: + ephemeral-storage: 50Mi + documentSelector: + path: kind + value: LimitRange + + - it: defaults only memory when memory sits beside non-compute quota keys + set: + resourceQuotas: + services.loadbalancers: "1" + memory: 8Gi + asserts: + - hasDocuments: + count: 2 + - equal: + path: spec.limits[0].default + value: + memory: 128Mi + documentSelector: + path: kind + value: LimitRange