cdk8s-team / cdk8s

Define Kubernetes native apps and abstractions using object-oriented programming
https://cdk8s.io
Apache License 2.0
4.29k stars 290 forks source link

'Getting Started with Typescript' documentation neglects replicas property of WebServiceOptions #264

Closed dillon-odonovan closed 4 years ago

dillon-odonovan commented 4 years ago

Description of the bug:

I was going through the documentation for the 'Getting Started with Typescript' example and noticed that it appears the replicas property of WebServiceOptions is not being used.

We see several other nullable properties being assigned default values in the constructor, such as in the case of

const port = options.containerPort || 80;

However, there is not an equivalent line to this for replicas. Instead, in the deployment spec, replicas is always set to 1, regardless of any user-supplied value for replicas.

new Deployment(this, 'deployment', {
  spec: {
    replicas: 1,
    selector: {
      matchLabels: label
    },
    template: {
      metadata: { labels: label },
      spec: {
        containers: [
          {
             name: 'app',
             image: options.image,
             ports: [ { containerPort } ]
          }
        ]
      }
    }
  }
});

Reproduction Steps:

Define a WebService in the constructor of MyChart as such:

new WebService(this, "hello", {
  image: "paulbouwer/hello-kubernetes:1.7",
  replicas: 10,
});

This produces the following k8s manifest:

apiVersion: v1
kind: Service
metadata:
  name: hello-hello-service-cf7c398a
spec:
  ports:
    - port: 80
      targetPort: 8080
  selector:
    app: hello1B298237
  type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-hello-deployment-83984d2d
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello1B298237
  template:
    metadata:
      labels:
        app: hello1B298237
    spec:
      containers:
        - image: paulbouwer/hello-kubernetes:1.7
          name: app
          ports:
            - containerPort: 8080

Note that replicas is set to 1, despite requesting 10 in our typescript.

Error Log:

Environment:

Other:

I would expect a line such as

const replicas = options.replicas || 1

to appear with the other assignments in the constructor, and for the deployment's spec to use

new Deployment(this, 'deployment', {
  spec: {
    replicas: replicas,
    selector: {
      matchLabels: label
    },
    template: {
      metadata: { labels: label },
      spec: {
        containers: [
          {
             name: 'app',
             image: options.image,
             ports: [ { containerPort } ]
          }
        ]
      }
    }
  }
});

or possibly even the shorthand, like below, depending on your style preference

new Deployment(this, 'deployment', {
  spec: {
    replicas,
    selector: {
      matchLabels: label
    },
    template: {
      metadata: { labels: label },
      spec: {
        containers: [
          {
             name: 'app',
             image: options.image,
             ports: [ { containerPort } ]
          }
        ]
      }
    }
  }
});

This is :bug: Bug Report

cmdallas commented 4 years ago

fixed in https://github.com/awslabs/cdk8s/commit/0f70a4fa0a471b27b3c75e25d0609935060b44ed