Create Kubernetes Namespace

Action ID: kubernetes:create-namespace
NPM Package:

@backstage-community/plugin-scaffolder-backend-module-kubernetes

Description

Creates a kubernetes namespace

Input Schema

PropertyTypeDescriptionRequired
urlstring-
tokenstring-
caDatastring-
labelsstring-
namespacestring-
clusterRefstring-
skipTLSVerifyboolean-

Output Schema

No output schema defined for this action.

Usage Examples

Create a namespace with minimal inputs

Creates a namespace using a provided service account token. Use this when the scaffolder has access to the target cluster by default and you only need to set the namespace name.

Copy
steps:
  - id: fetch-skeleton
    action: fetch:template
    input:
      url: ./skeleton
      targetPath: ./
      values:
        name: ${{ parameters.name }}

  - id: create-dev-namespace
    action: kubernetes:create-namespace
    input:
      namespace: ${{ parameters.namespace }}
      token: ${{ parameters.kubeToken }}

Create a namespace in a specific cluster by reference

Creates a namespace in a named cluster using a cluster reference from Backstage configuration. Use this when your Backstage instance has multiple clusters and you select one by reference.

Copy
steps:
  - id: fetch-skeleton
    action: fetch:template
    input:
      url: ./skeleton
      targetPath: ./
      values:
        componentId: ${{ parameters.componentId }}

  - id: create-staging-namespace
    action: kubernetes:create-namespace
    input:
      namespace: ${{ parameters.componentId }}-staging
      token: ${{ parameters.kubeToken }}
      clusterRef: gke-us-central1-staging
      labels: "owner=payments,environment=staging"

Create a namespace against a direct API server URL with insecure TLS

Creates a namespace by connecting directly to the Kubernetes API server URL and skipping TLS verification. Use this for ephemeral or lab clusters where you cannot validate certificates.

Copy
steps:
  - id: fetch-skeleton
    action: fetch:template
    input:
      url: ./skeleton
      targetPath: ./
      values:
        service: ${{ parameters.service }}

  - id: create-lab-namespace
    action: kubernetes:create-namespace
    input:
      namespace: ${{ parameters.service }}-lab
      token: ${{ parameters.kubeToken }}
      url: https://10.0.20.15:6443
      skipTLSVerify: true
      labels: "team=experiments,env=lab"

Create a namespace using API server URL with CA bundle

Creates a namespace using a direct API server URL with a provided base64 encoded CA certificate. Use this when connecting to a self hosted cluster with custom CA.

Copy
steps:
  - id: fetch-template
    action: fetch:template
    input:
      url: ./skeleton
      targetPath: ./
      values:
        app: ${{ parameters.app }}

  - id: create-prod-namespace
    action: kubernetes:create-namespace
    input:
      namespace: ${{ parameters.app }}-prod
      token: ${{ parameters.kubeToken }}
      url: https://k8s.prod.company.internal
      skipTLSVerify: false
      caData: ${{ parameters.kubeCaData }}
      labels: "owner=platform,environment=prod,cost-center=cc-042"

Create a namespace with dynamic name and labels

Creates a namespace with a name derived from user input and adds descriptive labels. Use this when enforcing naming conventions and tagging resources for governance.

Copy
steps:
  - id: fetch-template
    action: fetch:template
    input:
      url: ./skeleton
      targetPath: ./
      values:
        name: ${{ parameters.componentId }}
        env: ${{ parameters.env }}

  - id: create-namespaced-env
    action: kubernetes:create-namespace
    input:
      namespace: ${{ parameters.componentId }}-${{ parameters.env }}
      token: ${{ parameters.kubeToken }}
      clusterRef: eks-eu-west-1-shared
      labels: "app=${{ parameters.componentId }},environment=${{ parameters.env }},owner=${{ parameters.owner }}"

In these examples the template fetch step uses fetch:template to prepare files before creating the namespace.