cdk8s-team / cdk8s-core

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

Unable to directly use `ApiObject` in python #2570

Open iliapolo opened 3 months ago

iliapolo commented 3 months ago

Description of the bug:

The ApiObject class in python doesn't allow arbitrary key values, which means it doesn't allow configuring spec, data, or whatever else belongs in the body of a resource.

Reproduction Steps:

#!/usr/bin/env python
from constructs import Construct
from cdk8s import App, Chart, ApiObject

class MyChart(Chart):
    def __init__(self, scope: Construct, id: str):
        super().__init__(scope, id)

        ApiObject(self, 'ApiObject', spec={ 'foo': 'bar' })

app = App()
MyChart(app, "pod")

app.synth()

Error Log:

TypeError: ApiObject.__init__() got an unexpected keyword argument 'spec'
Error: command "pipenv run python main.py " at /private/tmp/cdk8s-python returned a non-zero exit code 1
    at ChildProcess.<anonymous> (/opt/homebrew/lib/node_modules/cdk8s-cli/lib/util.js:54:27)
    at Object.onceWrapper (node:events:634:26)
    at ChildProcess.emit (node:events:519:28)
    at ChildProcess._handle.onexit (node:internal/child_process:294:12)

This is :bug: Bug Report

iliapolo commented 3 months ago

This is because of https://github.com/cdk8s-team/cdk8s-core/issues/1297.

https://github.com/cdk8s-team/cdk8s-core/blob/f0f5413a41915c5343a73efa49b63e6604e0be8f/src/api-object.ts#L30-L35

As a workaround, we can apply json patches:

obj = ApiObject(self, ...)
obj.add_json_patch(JsonPatch.add('/spec', {}))
obj.add_json_patch(JsonPatch.add('/spec/foo', 'bar'))