cdk8s-team / cdk8s-core

Define Kubernetes native apps and abstractions using object-oriented programming
Apache License 2.0
67 stars 26 forks source link

Chart namespace is applied to non-namespaced objects #731

Open maybedino opened 2 years ago

maybedino commented 2 years ago

Currently, if you set a namespace for a chart, it will set the namespace on every object in the chart that does not have one:

https://github.com/cdk8s-team/cdk8s-core/blob/ab3fbf376841dd37cbfb53201fd1c8169bac16ae/src/api-object.ts#L133

While every Kubernetes object can have a metadata.namespace, some are cluster objects that are not namespaced. Applying a namespace to a cluster object breaks tools that check for this, like Terraform (I was trying to deploy with cdktf-cdk8s)

iliapolo commented 2 years ago

@maybedino Oooh nice! Thanks for this, we'll look into it.

github-actions[bot] commented 1 year ago

This issue has not received any attention in 1 year and will be closed soon. If you want to keep it open, please leave a comment below @mentioning a maintainer.

maybedino commented 12 months ago

This does not really cause any issues unless you have to validate your YAML files. The Kubernetes API does not complain.

@iliapolo Maybe it would still be good to keep this open, since technically it creates invalid YAML files which can cause problems with some deployment tools.

Our current workaround is to do this:


const ns = new k8s.KubeNamespace(this, "FooBar", {
    metadata: { name: "FooBar" },
});
ns.addJsonPatch(JsonPatch.remove("/metadata/namespace"));
shinebayar-g commented 3 days ago

Workaround that I used in https://github.com/cdk8s-team/cdk8s-plus/issues/4239#issuecomment-2143753520 requires me to update the if condition whenever we introduce a new cdk8s imported class which can be error prone.

I guess something like class ClusterApiObject extends ApiObject can be introduced. Then ClusterApiObject would be used during cdk8s import process. Since CRD yamls already contain wether it's namespaced or not, it should be possible for cdk8s to check that. I don't know how to achieve this in kubernetes-schemas, but we can even maintain hardcoded list for built-in k8s resources.