Create ResourceQuota objects to be deployed for each Namspace in repository

7/22/2021

Need to create new ResourceQuota object, one for each namespace.

The default ResourceQuota should be 2 CPU and 10 GB per namespace.

Any solutions for the above scenario.

Example I tried:

apiVersion: v1
kind: ResourceQuota
metadata:
  name: mem-cpu-demo
spec:
  hard:
    requests.cpu: "2"
    requests.memory: 10Gi
    limits.cpu: "2"
    limits.memory: 10Gi
-- Hari Babu
azure-devops
kubernetes
opa

1 Answer

7/26/2021

There's no built-in solution for it, however as an option, you can use a bash script to achieve it.

This will create ResourceQuota objects in all namespaces except for kube-system:

#!/bin/bash

for ns in $(kubectl get ns -o jsonpath="{.items[*].metadata.name}"); # get all namespaces in cluster
do
  if [ $ns != kube-system ] # check if it's not kube-system namespace
  then 
  cat <<EOF | kubectl apply -f - # body of kubectl apply -f
  apiVersion: v1
  kind: ResourceQuota
  metadata:
    name: resource-quota-$ns
    namespace: $ns
  spec:
    hard:
      requests.cpu: "2"
      requests.memory: 10Gi
      limits.cpu: "2"
      limits.memory: 10Gi
EOF
  fi
done

Useful link:

-- moonkotte
Source: StackOverflow