0

I would like to create a namespace within a project using the Rancher CLI.

What I already did:

rancher login https://URI --token abcde --context c-abc:p-abc

rancher kubectl create namespace myns --dry-run=client -o yaml | rancher kubectl apply -f -

But the namespace is created in "default" and not in my project.

Maxster17
  • 5
  • 4

2 Answers2

1

There are a couple ways I've found to do this:

  1. Create it from a yaml file using kubectl apply -f namespace.yml
apiVersion: v1
kind: Namespace
metadata:
  name: NAME
  annotations:
    field.cattle.io/projectId: PROJECT_NAME:PROJECT_NAMESPACE
  1. If you have the rights to modify a namespace that's not in a project you can add the annotation after you create it
kubectl annotate namespace work-test field.cattle.io/projectId=PROJECT_NAME:PROJECT_NAMESPACE

You can get the project name and namespace by viewing the project yaml file and looking in the metadata section.

0
  1. View your project's yaml in Rancher and take note of the metadata.name and metadata.namespace fields.

  2. Create your namespace as follows, replacing <name> and <namespace> with the values you noted in step 1:

cat <<EOF | kubectl apply -f -
  apiVersion: v1
  kind: Namespace
  metadata:
    name: myns
    annotations:
      field.cattle.io/projectId: <namespace>:<name>
EOF
Evee
  • 101
  • 1
  • 4