cdk8s-core icon indicating copy to clipboard operation
cdk8s-core copied to clipboard

Inspect (and mutate) L1 properties after initialization

Open eladb opened this issue 4 years ago • 7 comments

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.

eladb avatar May 14 '20 12:05 eladb

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)?

ajaegle avatar May 25 '20 13:05 ajaegle

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.

gabe-l-hart avatar Aug 04 '20 17:08 gabe-l-hart

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)
  }
}

foriequal0 avatar Nov 26 '20 08:11 foriequal0

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.

github-actions[bot] avatar Sep 06 '21 01:09 github-actions[bot]

Closing this issue as it hasn't seen activity for a while. Please add a comment @mentioning a maintainer to reopen.

github-actions[bot] avatar Sep 13 '21 01:09 github-actions[bot]

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.

github-actions[bot] avatar Jan 11 '24 12:01 github-actions[bot]

bump

shinebayar-g avatar Jan 11 '24 14:01 shinebayar-g