Skip to content

Commit

Permalink
refactor(plus): Remove the spec nesting level on both input and out…
Browse files Browse the repository at this point in the history
…put (#347)

Removed the need to specify various `spec` properties for both pre and post instantiation. Applies to all constructs that used to accept a pod spec.

### Before

```ts
const deployment = new kplus.Deployment(this, text, {
  spec: {
    podSpecTemplate: {
      containers: [
        new kplus.Container({
          image: 'hashicorp/http-echo',
          args: [ '-text', text ]
        })
      ]
    }
  }
});
deployment.spec.podSpecTemplate.addContainer(...)
```

### After

```ts
const deployment = new kplus.Deployment(this, text, {
  containers: [
    new kplus.Container({
      image: 'hashicorp/http-echo',
      args: [ '-text', text ]
    })
  ]
});
deployment.addContainer(...)
```

BREAKING CHANGE: `spec` was removed from all cdk8s+ constructs and that now have a flat structure. See [Example](https://github.com/awslabs/cdk8s/tree/master/packages/cdk8s-plus#at-a-glance) for new usage.

* **plus**: Construct id's for deployment will change due to a latent bug that appended the word `pod` to them.

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
iliapolo authored Oct 19, 2020
1 parent 6ee449f commit 5e34850
Show file tree
Hide file tree
Showing 19 changed files with 1,200 additions and 1,305 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function doSearch(uri, callback) {
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": Object {
"name": "test-chart-deployment-pod-1ef542cf",
"name": "test-chart-deployment-c5d38cbe",
},
"spec": Object {
"replicas": 1,
Expand Down
8 changes: 2 additions & 6 deletions examples/typescript/cdk8s-plus-elasticsearch-query/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,8 @@ export class MyChart extends Chart {
container.mount(workingDir, volume);

const deployment = new kplus.Deployment(this, 'Deployment', {
spec: {
replicas: 1,
podSpecTemplate: {
containers: [container]
}
}
replicas: 1,
containers: [container]
})

deployment.expose(9000);
Expand Down
16 changes: 6 additions & 10 deletions examples/typescript/cdk8s-plus-ingress/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@ export class MyChart extends Chart {

private echoBackend(text: string) {
const deploy = new kplus.Deployment(this, text, {
spec: {
podSpecTemplate: {
containers: [
new kplus.Container({
image: 'hashicorp/http-echo',
args: [ '-text', text ]
})
]
}
}
containers: [
new kplus.Container({
image: 'hashicorp/http-echo',
args: [ '-text', text ]
})
]
});

return kplus.IngressBackend.fromService(deploy.expose(5678));
Expand Down
525 changes: 329 additions & 196 deletions packages/cdk8s-plus/API.md

Large diffs are not rendered by default.

90 changes: 36 additions & 54 deletions packages/cdk8s-plus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,12 @@ container.mount(appPath, appVolume);

// now lets create a deployment to run a few instances of this container
const deployment = new kplus.Deployment(chart, 'Deployment', {
spec: {
replicas: 3,
podSpecTemplate: {
containers: [ container ]
}
},
replicas: 3,
containers: [ container ]
});

// finally, we expose the deployment as a load balancer service and make it run
deployment.expose({port: 8080, serviceType: kplus.ServiceType.LOAD_BALANCER})
deployment.expose(8080, {serviceType: kplus.ServiceType.LOAD_BALANCER})

// we are done, synth
app.synth();
Expand Down Expand Up @@ -161,14 +157,10 @@ const app = new cdk8s.App();
const chart = new cdk8s.Chart(app, 'Chart');
new kplus.Deployment(chart, 'Deployment', {
spec: {
replicas: 3,
podSpecTemplate: {
containers: [new kplus.Container({
image: 'ubuntu',
})],
},
},
replicas: 3,
containers: [new kplus.Container({
image: 'ubuntu',
})],
});
```

Expand All @@ -184,10 +176,8 @@ app = cdk8s.App()
chart = cdk8s.Chart(app, 'Chart')
kplus.Deployment(chart, 'Deployment',
spec=kplus.DeploymentSpec(
replicas=1,
pod_spec_template=kplus.PodSpec(containers=[kplus.Container(image='ubuntu')])
)
replicas=1,
containers=[kplus.Container(image='ubuntu')]
)
```

Expand Down Expand Up @@ -324,21 +314,19 @@ You can configure a TTL for the job after it finished its execution successfully
import * as k from 'cdk8s';
import * as kplus from 'cdk8s-plus';
const app = new k.App();
const chart = new k.Chart(app, 'Chart');
// let's define a job spec, and set a 1 second TTL.
const jobSpec = {
ttlAfterFinished: kplus.Duration.seconds(1),
};
const load = new kplus.Job(chart, 'LoadData', {
ttlAfterFinished: kplus.Duration.seconds(1)
});
// now add a container to all the pods created by this job
jobSpec.podSpecTemplate.addContainer(new kplus.Container({
job.addContainer(new kplus.Container({
image: 'loader'
}));
const app = new k.App();
const chart = new k.Chart(app, 'Chart');
// now we create the job
const load = new kplus.Job(chart, 'LoadData', { spec: jobSpec });
```

### `Service`
Expand All @@ -362,7 +350,7 @@ const chart = new k.Chart(app, 'Chart');
const frontends = new kplus.Service(chart, 'FrontEnds');
// this will cause the service to select all pods with the 'run: frontend' label.
frontends.spec.selectByLabel('run', 'frontend')
frontends.selectByLabel('run', 'frontend')
```

#### Ports
Expand All @@ -378,7 +366,7 @@ const chart = new k.Chart(app, 'Chart');
const frontends = new kplus.Service(chart, 'FrontEnds');
// make the service bind to port 9000 and redirect to port 80 on the associated containers.
frontends.spec.serve({port: 9000, targetPort: 80)
frontends.serve({port: 9000, targetPort: 80)
```

### `Deployment`
Expand All @@ -400,11 +388,7 @@ const app = new k.App();
const chart = new k.Chart(app, 'Chart');
new kplus.Deployment(chart, 'FrontEnds', {
spec: {
podSpecTemplate: {
containers: [ new kplus.Container({ image: 'node' }) ],
}
},
containers: [ new kplus.Container({ image: 'node' }) ],
});
```

Expand Down Expand Up @@ -440,7 +424,7 @@ Following up on pod selection, you can also easily create a service that will se
const frontends = new kplus.Deployment(chart, 'FrontEnds');
// create a ClusterIP service that listens on port 9000 and redirects to port 9000 on the containers.
frontends.expose({port: 9000})
frontends.expose(9000)
```

Notice the resulting manifest, will have the same `cdk8s.deployment` magic label as the selector.
Expand Down Expand Up @@ -558,10 +542,10 @@ const chart = new k.Chart(app, 'Chart');
const pod = new new kplus.Pod(chart, 'Pod');
// this will automatically add the volume as well.
pod.spec.addContainer(container);
pod.addContainer(container);
// but if you want to explicitly add it, simply use:
pod.spec.addVolume(storage);
pod.addVolume(storage);
```

Expand All @@ -573,8 +557,9 @@ import * as kplus from 'cdk8s-plus';
const app = new k.App();
const chart = new k.Chart(app, 'Chart');
const pod = new new kplus.Pod(chart, 'Pod');
pod.spec.restartPolicy = kplus.RestartPolicy.NEVER;
const pod = new new kplus.Pod(chart, 'Pod', {
restartPolicy: kplus.RestartPolicy.NEVER,
});
```

#### Assigning a ServiceAccount
Expand All @@ -585,8 +570,9 @@ import * as kplus from 'cdk8s-plus';
const app = new k.App();
const chart = new k.Chart(app, 'Chart');
const pod = new new kplus.Pod(chart, 'Pod');
pod.spec.serviceAccount = kplus.ServiceAccount.fromServiceAccountName('aws');
const pod = new new kplus.Pod(chart, 'Pod', {
serviceAccount: kplus.ServiceAccount.fromServiceAccountName('aws'),
});
```

### `Secret`
Expand Down Expand Up @@ -676,19 +662,15 @@ to a service associated with a deployment of the

```ts
const helloDeployment = new kplus.Deployment(this, text, {
spec: {
podSpecTemplate: {
containers: [
new kplus.Container({
image: 'hashicorp/http-echo',
args: [ '-text', 'hello ingress' ]
})
]
}
}
containers: [
new kplus.Container({
image: 'hashicorp/http-echo',
args: [ '-text', 'hello ingress' ]
})
]
});
const helloService = helloDeployment.expose({ port: 5678 });
const helloService = helloDeployment.expose(5678);
const ingress = new Ingress(this, 'ingress');
ingress.addRule('/hello', kplus.IngressBackend.fromService(helloService));
Expand Down
5 changes: 4 additions & 1 deletion packages/cdk8s-plus/src/config-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ export class ConfigMap extends Resource implements IConfigMap {
return { name };
}

/**
* @see base.Resource.apiObject
*/
protected readonly apiObject: cdk8s.ApiObject;

private readonly _binaryData: { [key: string]: string } = { };
private readonly _data: { [key: string]: string } = { };

public constructor(scope: Construct, id: string, props: ConfigMapProps = { }) {
super(scope, id, props);
super(scope, id, { metadata: props.metadata });

this.apiObject = new k8s.ConfigMap(this, 'ConfigMap', {
metadata: props.metadata,
Expand Down
Loading

0 comments on commit 5e34850

Please sign in to comment.