cdk8s-team / cdk8s-core

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

Regression between v2.64.0 and v2.65.0 on resolver? #2414

Open asgerjensen opened 2 months ago

asgerjensen commented 2 months ago

Description of the bug:

I have a combined CDK8s + CDK stack using an imported cluster to add an extra Deployment to the EKS cluster.

With CDK8s v2.64.0 synth works, but with v2.65.0+ i get an exception like

2.65.0 error:

/Users/.../node_modules/cdk8s/src/app.ts:105
    const scope = c.node.scope;
                    ^
TypeError: Cannot read properties of undefined (reading 'node')
    at Function.of (/Users/.../node_modules/cdk8s/src/app.ts:105:21)

2.68.58:

/Users/...node_modules/cdk8s/src/api-object.ts:224
      throw new Error(`Failed serializing construct at path '${this.node.path}' with name '${this.name}': ${e}`);
            ^
Error: Failed serializing construct at path 'worker-process-inventory/deployment/Resource' with name 'worker-process-inventory-deployment-c80f5b2b': TypeError: Cannot read properties of undefined (reading 'node')
    at KubeDeployment.toJson (/Users/..../node_modules/cdk8s/src/api-object.ts:224:13)
    at KubeDeployment.toJson (/Users/..../node_modules/cdk8s-plus-23/src/imports/k8s.ts:591:28)
    at /Users/.../node_modules/cdk8s/src/app.ts:116:46
    at KubeDeployment.toJson (/Users/..../node_modules/cdk8s/src/api-object.ts:224:13)
    at KubeDeployment.toJson (/Users/..../node_modules/cdk8s-plus-23/src/imports/k8s.ts:591:28)
    at /Users/.../node_modules/cdk8s/src/app.ts:116:46
    at Array.map (<anonymous>)
    at Function._synthChart (/Users/.../node_modules/cdk8s/src/app.ts:116:31)
    at QueueWorkerProcessChart.toJson (/Users/.../node_modules/cdk8s/src/chart.ts:148:16)
    at ImportedCluster.addCdk8sChart (/Users/...node_modules/aws-cdk-lib/aws-eks/lib/cluster.js:1:5732)
    at new FileImportStateMachine (/Users/.../lib/constructs/file-import-statemachine.ts:78:21)
    at new InventoryFileImportStateMachine (/Users/.../lib/constructs/inventory-statemachine.ts:70:37)
    at new FileImportStack (/Users/.../lib/main-stack.ts:48:35)

While the error messages are different, i think it relates to the same thing

Reproduction Steps:

in cdk stack

import { App } from 'cdk8s';
...
export class MyConstruct extends Construct {
 constructor(
    scope: Construct,
    id: string) {

const sqsQueue = new Queue(this, 'tasks');
const kubernetesApp = new App({})
const cluster: ICluster  =  Cluster.fromClusterAttributes(this, ...)
cluster.addCdk8sChart('newChart' new DeploymentChart(kubernetesApp, 'worker-process', {...});
}
}

In the main CDK stack file, i then have

import * as cdk from 'aws-cdk-lib';
import { App, Tags } from 'aws-cdk-lib';
const app = new App();

export class MyStack extends cdk.Stack
 constructor(scope: Construct, id: string, props?: cdk.StackProps) {
   new MyConstruct(this, 'newChartDeployment');
}
}

new MyStack(app, {});

From looking at the stack trace, i would imagine its because it assumes the cdk8s App, as a construct, would have a node, which maybe it doesnt, as it is created /next/ to the normal hierachy that has a aws-cdk-lib App at its top.

Anyways, long story short...

It used to work perfectly, to allow combining CDK and CDK8s constructs. And now it doesn't.

Error Log:

Error: Failed serializing construct at path 'worker-process-inventory/deployment/Resource' with name 'worker-process-inventory-deployment-c80f5b2b': TypeError: Cannot read properties of undefined (reading 'node')
    at KubeDeployment.toJson (/Users/..../node_modules/cdk8s/src/api-object.ts:224:13)
    at KubeDeployment.toJson (/Users/..../node_modules/cdk8s-plus-23/src/imports/k8s.ts:591:28)
    at /Users/.../node_modules/cdk8s/src/app.ts:116:46
    at KubeDeployment.toJson (/Users/..../node_modules/cdk8s/src/api-object.ts:224:13)
    at KubeDeployment.toJson (/Users/..../node_modules/cdk8s-plus-23/src/imports/k8s.ts:591:28)
    at /Users/.../node_modules/cdk8s/src/app.ts:116:46
    at Array.map (<anonymous>)
    at Function._synthChart (/Users/.../node_modules/cdk8s/src/app.ts:116:31)
    at QueueWorkerProcessChart.toJson (/Users/.../node_modules/cdk8s/src/chart.ts:148:16)
    at ImportedCluster.addCdk8sChart (/Users/...node_modules/aws-cdk-lib/aws-eks/lib/cluster.js:1:5732)
    at new FileImportStateMachine (/Users/.../lib/constructs/file-import-statemachine.ts:78:21)
    at new InventoryFileImportStateMachine (/Users/.../lib/constructs/inventory-statemachine.ts:70:37)
    at new FileImportStack (/Users/.../lib/main-stack.ts:48:35)

Environment:

Other:

I've both tried updating cdk8s-23-plus and leaving it at its original version (pre-update-attempt), with no change.


This is :bug: Bug Report

iliapolo commented 1 month ago

@asgerjensen I'm not able to reproduce this...

Here is the code i'm using:

#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import * as cdk8s from 'cdk8s';
import * as eks from 'aws-cdk-lib/aws-eks';

const app = new cdk.App();
const stack = new cdk.Stack(app, 'CdkCdk8STypescriptStack', {});

const cluster = eks.Cluster.fromClusterAttributes(stack, 'Cluster', {
  clusterName: 'my-cluster',
  kubectlRoleArn: 'arn:aws:iam::123456789012:role/example-role'
});

const chart = new cdk8s.Chart(new cdk8s.App(), 'Chart');
new cdk8s.ApiObject(chart, 'ApiObject', {
  kind: 'ConfigMap',
  apiVersion: 'v1',
  data: {
    foo: 'bar'
  }
});

cluster.addCdk8sChart('Chart', chart);

app.synth();

With the following dependencies:

"dependencies": {
  "aws-cdk-lib": "2.94.0",
  "cdk8s":"2.68.73",
  "constructs": "^10.0.0",
}

Are you able to spot a significant difference from what you are doing? Can you give a small (yet complete) code repro of the issue? also please share your dependencies.

Thanks.

github-actions[bot] commented 6 days ago

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