cdk8s-core
cdk8s-core copied to clipboard
Inspect (and mutate) L1 properties after initialization
At the moment, these types are immutable. All their properties have to be defined upon initialization. Similarly to the AWS CDK's "cfn resources", we should allow users to mutate these objects to improve authoring experience.
Let's say I want to conditionally determine the value of a couple of fields in an imported k8s object. Today, I would need to do it like this:
new MyKind(this, 'foo', {
spec: {
field1: myCondition ? 'valueA' : 'valueB',
sub: {
tree: myCondition ? 'valueA' : 'valueB'
}
}
});
new YourKind(this, 'your', {
spec: {
opt: myCondition ? 10 : 20
}
});
This feature will allow me to write this code like this:
const my = new MyKind(this, 'my');
const your = new YourKind(this, 'your');
if (myCondition) {
my.spec.field1 = my.sub.tree = 'valueA';
your.opt = 10;
} else {
my.spec.field1 = my.sub.tree = 'valueB';
your.opt = 20;
}
An issue here is who is responsible to initialize sub-objects such as spec
and sub.tree
. I'll leave it for the implementer to ponder options.
This feature would be great to have so we can import pre-existing yaml resources and transform them according to e.g. the workspace the current chart will be deployed to, adding missing labels or use a common naming scheme.
Is there an alternative way to weakening the immutability of the constructs? Or is cloning for the transformation a bad idea in terms of parent-child relation (scope)?
In this proposal, would it be possible to add net-new attributes to the object that were not automatically discovered during import
by the validation
schema? We're running into issues where we depend on third-party CRDs which we don't directly control which may be fully missing validation
schemas, or have crucial missing portions (such as metadata
).
My guess is that this would not be possible (at least from the python
interface) because those new fields would somehow need to be plumbed through the jsii
interface to the node
constructs.
I've tried to implement similar to this https://github.com/awslabs/cdk8s/issues/178
Originally, I wanted to set app.kubernetes.io/instance
as Names.toLabelValue(this)
, but there were some hassles.
JS prevents accessing this
before calling super
, so I can't simply initialize it with
super(scope, this, {
labels: {
[INSTANCE]: Names.toLabelValue(this)
}
})
But I couldn't neither this.labels[INSTANCE] = Names.toLabelValue(this)
since this.labels
is an immutable clone.
I've tried Lazy.any
trick, but Error: can't render non-simple object of type 'Lazy'
is thrown while App.synth
.
I've found overriding labels property works:
get labels() {
return {
...super.labels,
[INSTANCE]: Names.toLabelValue(this)
}
}
This issue is now marked as stale because it hasn't seen activity for a while. Add a comment or it will be closed soon.
Closing this issue as it hasn't seen activity for a while. Please add a comment @mentioning a maintainer to reopen.
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.
bump