aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.59k stars 3.89k forks source link

(aws-eks): aws-eks.Cluster.addCdk8sChart() guidance #14696

Closed adriantaut closed 3 years ago

adriantaut commented 3 years ago

:question: General Issue

hey guys, I’m a newbie with cdk8s and looking for some guidance, please. I am trying to do the following in a single CDK App as instructed in link: create a simple Cdk8s Chart apply the Cdk8s Chart to an existing EKS Cluster via the @aws-cdk/aws-eks.Cluster.addCdk8sChart() method

My cdk8s.ts file:

import * as cdk8s from 'cdk8s';
import * as kplus from 'cdk8s-plus';
import * as constructs from 'constructs';

export class MyCdk8sChart extends cdk8s.Chart {
  constructor(scope: constructs.Construct, id: string) {
    super(scope, id);

    new kplus.Deployment(this, 'refkube-kplus-deploy', {
        replicas: 1,
        metadata: {
            name: 'cdk8s-deployment',
            labels: {
                'app': 'cdk8s'
            },
        },
        containers: [
            new kplus.Container({
                name: 'cdk8s',
                image: 'nginx:1.14.2',
                port: 80
            })
        ]
    });
  }
}

My application-stack.ts

import * as cdk from '@aws-cdk/core';
import {KubernetesApplication} from '@flutter-global/cdk-lib-ppb';
import deployment from '../resources/deployment.json';
import service from '../resources/service.json';
import { MyCdk8sChart } from './ck8s'
import * as cdk8s from 'cdk8s';

export interface ApplicationStackProps extends cdk.StackProps {
    /**
     * Logical name of the deployment environment (stage): qa/nxt/drk/prd/...
     */
    readonly deployEnvironment: string;
    readonly clusterName: string;
    readonly vpcName?: string;
}

/**
 * Application stack
 */
export class ApplicationStack extends cdk.Stack {
    constructor(scope: cdk.Construct, id: string, props: ApplicationStackProps) {
        super(scope, id, props);

        const k8sApp = new KubernetesApplication(this, 'refkubeService', {
            tla: 'refkube',
            deployEnvironment: props.deployEnvironment,
            vpcName: props.vpcName,
            clusterName: props.clusterName,
        });
        // create a cdk8s chart and use `cdk8s.App` as the scope.
        const myChart = new MyCdk8sChart(new cdk8s.App(), 'MyChart');

        // add the cdk8s chart to the cluster
        k8sApp.cluster.addCdk8sChart('my-chart', myChart);
    }
}

This application is working with a raw k8s manifest and the `@aws-cdk/aws-eks.Cluster.addManifest() method, however, when trying to cdk synth using the addCdk8sChart() method I get

# cdk synth
construct does not have an associated node. All constructs must extend the "Construct" base class
Subprocess exited with error 1

I guess it might be something related with the Cdk8sChart being created with the constructs.Construc scope, while the CDK App with the @aws-cdk/core Construct. Could someone help with some guidance around bundling these two together?

⚠️ Important Information For support questions, please first reference our documentation, then use Stackoverflow. This repository's issues are intended for feature requests and bug reports. -->

Environment

bryanmig commented 3 years ago

I am experiencing the same issue. I cant figure out if this is a documentation issue or a regression. Any guidance would be much appreciated.

iliapolo commented 3 years ago

@adriantaut @bryanmig Im unable to reproduce this using the following code:

export class Issue14696Stack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // The code that defines your stack goes here

    const cluster = new eks.Cluster(this, 'Cluster', {
      version: eks.KubernetesVersion.V1_19,
    })

    const cdk8sChart = new Chart(new cdk8s.App(), 'Chart');

    cluster.addCdk8sChart('chart', cdk8sChart);
  }
}

export class Chart extends cdk8s.Chart {
  constructor(scope: constructs.Construct, id: string) {
    super(scope, id);

    new kplus.Deployment(this, 'refkube-kplus-deploy', {
      replicas: 1,
      metadata: {
          name: 'cdk8s-deployment',
          labels: {
              'app': 'cdk8s'
          },
      },
      containers: [
          {
              name: 'cdk8s',
              image: 'nginx:1.14.2',
              port: 80
          }
      ]
  });
  }
}
  "dependencies": {
    "cdk8s": "1.0.0-beta.11",
    "cdk8s-plus-17": "1.0.0-beta.11",
    "@aws-cdk/core": "1.103.0",
    "@aws-cdk/aws-eks": "1.103.0",
}

Note that I changed new kplus.Container(props) to just specify the props. This is actually a documentation problem which I will address.

In any case this doesn't explain the error you are seeing.

Can you please share your dependency declaration from package.json? Also - what is this @flutter-global/cdk-lib-ppb package you are using? I'm unable to find it on npm, and I suspect it might be the cause of some version mismatches. Would be great if you can confirm by trying to run the code I provided above.

Thanks

adriantaut commented 3 years ago

@iliapolo thank you, indeed, your code works, but this time also works our code. Will investigate if it was a version mismatch or simply the cdk8s-plus vs cdk8s-plus-17 dependencies you've changed.

Cheers!

peterwoodworth commented 3 years ago

Hey @adriantaut, do you have an update on your investigation?

If you don't need any more help from us, feel free to close the issue 😄

bryanmig commented 3 years ago

For me it turned out to be a dependency conflict - I ha copied some example code which used an old/incompatible version. Once I updated, the issue was gone.

adriantaut commented 3 years ago

I agree, I will close the issue. Many thanks!

github-actions[bot] commented 3 years ago

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.