cdk8s-team / cdk8s-plus

A software development framework that provides high level abstractions for authoring Kubernetes applications.
https://cdk8s.io/docs/latest/plus/
Apache License 2.0
133 stars 33 forks source link

Chart.namespace shouldn't generate namespace field for cluster scope objects #4239

Closed shinebayar-g closed 2 days ago

shinebayar-g commented 11 months ago

Description of the bug:

Chart.namespace shouldn't generate .metadata.namespace field for cluster level objects such as

apiVersion: storage.k8s.io/v1
kind: StorageClass

Reproduction Steps:

Given a chart

export class MyChart extends Chart {
    constructor(scope: App) {
        super(scope, 'my-chart', {
            namespace: 'something',
        });

        new KubeStorageClass(this, 'storage-class', {
            provisioner: 'kubernetes.io/gce-pd',
        });
    }
}

It generates

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  ...
  namespace: something
provisioner: kubernetes.io/gce-pd

while namespace is ignored by kubernetes, it would be nice if cdk8s didn't generate namespace.

Error Log:

Environment:

Other:


This is :bug: Bug Report

shinebayar-g commented 11 months ago

Our use case is we generate a cdk8s chart per folder by default and set chart props based on folder name.

Actually this works perfectly in our case, but namespace property is read-only.

image image

shinebayar-g commented 11 months ago

Got it working. 😄

    chart.apiObjects.forEach((apiObject) => {
        if (apiObject.kind === 'StorageClass') {
            apiObject.addJsonPatch(JsonPatch.replace('/metadata/namespace', undefined));
        }
    });
drmorr0 commented 7 months ago

I don't think this should be marked closed, I just ran into the same issue. While it's nice to know that there's a workaround, this should really be considered a bug in cdk8s output generation itself imo.

iliapolo commented 3 months ago

Just reopened but on second thought, there's no good way for us to know that KubeNamespace should not be assigned a namespace in this case, but KubePod should. It requires knowledge about which L1 resources are cluster scoped, which I don't think the schema provides.

This though would be a great fit for cdk8s-plus, where we can manually encode this. Moving to a feature request in cdk8s-plus.

shinebayar-g commented 3 days ago

I just found out it's duplicate of https://github.com/cdk8s-team/cdk8s-core/issues/731