How can I create a k8s namespace manifest in an idempotent way or ignore the error if the namespace already exist with AWS CDK.
If I create a K8s namespace with a manifest, I regularly get errors that the namespace already exists:
from custom resource. Message returned: Error: b'Error from server (AlreadyExists): error when creating "/tmp/manifest.yaml": namespaces "dev-advanced" already exists\n
Create Manifest:
def create_namespace(self) -> KubernetesManifest:
m = self.cluster.add_manifest(
f"ns_{self.tenant.name}",
{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": {
"name": f"{self.tenant.name}",
"labels": {
"name": f"{self.tenant.name}"
}
}
}
)
return m
I am facing the same issue and got a solution.
Just create a KubernetesManifest, that's the type returned by addManifest
, it justs need the option overwrite: true
.
I did the following with a manually created namespace and it worked:
const namespace = `test-ns`;
new eks.KubernetesManifest(
this,
`${namespace}-namespace`,
{
cluster,
overwrite: true,
manifest: [
{
apiVersion: 'v1',
kind: 'Namespace',
metadata: {
name: namespace,
labels: {
name: 'test'
}
},
},
]
}
);
I had pods running under that namespace and I were worried the namespace would be recreated but all ok.