id: k8s-memory-limits-not-set

info:
  name: Memory limits not set in Deployments
  author: princechaddha
  severity: medium
  description: Checks for missing memory limits in Kubernetes Deployments, which can lead to resource contention and instability
  impact: |
    Missing memory limits in Kubernetes Deployments can cause resource contention and potential application instability.
  remediation: Set memory limits for all containers in Kubernetes Deployments to ensure resource management and application stability
  reference:
    - https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource/
  tags: cloud,devops,kubernetes,k8s,devsecops,deployments,k8s-cluster-security

flow: |
  code(1);
  for (let deployment of template.items) {
    set("deployment",deployment)
    javascript(1);
  }

self-contained: true
code:
  - engine:
      - sh
      - bash
    source: kubectl get deployments --all-namespaces --output=json
    extractors:
      - type: json
        name: items
        internal: true
        json:
          - '.items[]'

javascript:
  - code: |
        deployment = JSON.parse(template.deployment);
        if (!deployment.spec.template.spec.containers.some(container => container.resources && container.resources.limits && container.resources.limits.memory)) {
          let result = (`Deployment '${deployment.metadata.name}' in namespace '${deployment.metadata.namespace}' lacks memory limits.`);
          Export(result);
        }

    extractors:
      - type: dsl
        dsl:
          - response
# digest: 4a0a004730450220485b39477db2add4c8b95137fdefbc18dfdf63c61ce89bbbaca68d6fad7396c802210099c2bd9d765e71dcc8e1009f9f23fe307c825c8f2d700e55587c3047974732a0:922c64590222798bb761d5b6d8e72950