Skip to content

Commit

Permalink
feat(plus): Container is now inputed as an interface instead of class (
Browse files Browse the repository at this point in the history
…#376)

When a container needs to be passed as an input, it can be passed with a simple interface.

```ts
const pod = new kplus.Pod(this, 'Pod', {
  containers: [{ image: 'nginx' }]
});
```

When a container is retrieved as an output, it is a class, with the familiar api.

```ts
const container = pod.addContainer({ image: 'nginx' });
container.mount(...)
```

BREAKING CHANGE: Containers now need to be inputed as interfaces rather than classes. Instead of passing `new kplus.Container(props)`, simply pass in `props`.

Resolves #365

*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 committed Nov 15, 2020
1 parent a3d3e27 commit 33bf97a
Show file tree
Hide file tree
Showing 20 changed files with 283 additions and 206 deletions.
7 changes: 4 additions & 3 deletions docs/plus/config-map.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ appMap.addDirectory(path.join(__dirname, 'app'));

const appVolume = kplus.Volume.fromConfigMap(appMap);

// for here, just mount the volume to a container, and run your app!
const mountPath = '/var/app';
const container = new kplus.Container({
const pod = new kplus.Pod(chart, 'Pod');
const container = pod.addContainer({
image: 'node',
command: [ 'node', 'app.js' ],
workingDir: mountPath,
})
});

// from here, just mount the volume to a container, and run your app!
container.mount(mountPath, appVolume);
```
18 changes: 12 additions & 6 deletions docs/plus/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ Environment variables can be added to containers using various sources, via sema
```typescript
import * as kplus from 'cdk8s-plus'

const container = new kplus.Container({
const pod = new kplus.Pod(this, 'Pod');
const container = pod.addContainer({
image: 'my-app'
})
});

// explicitly use a value.
container.addEnv('endpoint', kplus.EnvValue.fromValue('value'));
Expand Down Expand Up @@ -58,7 +59,8 @@ import * as kplus from 'cdk8s-plus';
const config = kplus.ConfigMap.fromConfigMapName('config');
const volume = kplus.Volume.fromConfigMap(config);
const container = new kplus.Container({
const pod = new kplus.Pod(this, 'Pod');
const container = pod.addContainer({
image: 'my-app'
})
Expand All @@ -82,9 +84,13 @@ A `Probe` instance can be created through one of the `fromXxx` static methods:
Readiness, liveness, and startup probes can be configured at the container-level through the `readiness`, `liveness`, and `startup` options:

```ts
new kplus.Container({
// ...
readiness: kplus.Probe.fromHttpGet('/ping')
new kplus.Pod(this, 'Pod', {
containers: [
{
// ...
readiness: kplus.Probe.fromHttpGet('/ping'),
}
]
});
```

Expand Down
2 changes: 1 addition & 1 deletion docs/plus/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const app = new k.App();
const chart = new k.Chart(app, 'Chart');

new kplus.Deployment(chart, 'FrontEnds', {
containers: [ new kplus.Container({ image: 'node' }) ],
containers: [ { image: 'node' } ],
});
```

Expand Down
21 changes: 10 additions & 11 deletions docs/plus/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Overview

**cdk8s+** is a library with high level abstractions for authoring Kubernetes
applications.
applications.

Built on top of the auto-generated building blocks provided by CDK8s, this
library includes a hand crafted *construct* for each native kubernetes object,
Expand Down Expand Up @@ -33,25 +33,24 @@ appData.addDirectory(path.join(__dirname, 'app'));

const appVolume = kplus.Volume.fromConfigMap(appData);

// lets create a deployment to run a few instances of a pod
const deployment = new kplus.Deployment(chart, 'Deployment', {
replicas: 3,
});

// now we create a container that runs our app
const appPath = '/var/lib/app';
const port = 80;
const container = new kplus.Container({
const container = deployment.addContainer({
image: 'node:14.4.0-alpine3.12',
command: ['node', 'index.js', `${port}`],
port: port,
workingDir: appPath,
})
});

// make the app accessible to the container
container.mount(appPath, appVolume);

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

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

Expand Down Expand Up @@ -146,9 +145,9 @@ app.synth();

new kplus.Deployment(chart, 'Deployment', {
replicas: 3,
containers: [new kplus.Container({
containers: [{
image: 'ubuntu',
})],
}],
});
```

Expand Down
4 changes: 2 additions & 2 deletions docs/plus/ingress.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ to a service associated with a deployment of the
```ts
const helloDeployment = new kplus.Deployment(this, text, {
containers: [
new kplus.Container({
{
image: 'hashicorp/http-echo',
args: [ '-text', 'hello ingress' ]
})
}
]
});

Expand Down
4 changes: 2 additions & 2 deletions docs/plus/job.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const load = new kplus.Job(chart, 'LoadData', {


// now add a container to all the pods created by this job
job.addContainer(new kplus.Container({
job.addContainer({
image: 'loader'
}));
});
```
8 changes: 4 additions & 4 deletions docs/plus/pod.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ Containers and volumes can be added to pod definition like so:
```typescript
import * as kplus from 'cdk8s-plus';

const container = new kplus.Container({
const pod = new new kplus.Pod(chart, 'Pod');

const container = pod.addContainer({
image: 'node',
})
});

const storage = kplus.Volume.fromEmptyDir('storage');

Expand All @@ -23,8 +25,6 @@ container.mount('/data', storage);
const app = new k.App();
const chart = new k.Chart(app, 'Chart');

const pod = new new kplus.Pod(chart, 'Pod');

// this will automatically add the volume as well.
pod.addContainer(container);

Expand Down
3 changes: 2 additions & 1 deletion docs/plus/volume.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import * as kplus from 'cdk8s-plus';

const data = kplus.Volume.fromEmptyDir(configMap);

const redis = new kplus.Container({
const pod = new kplus.Pod(this, 'Pod');
const redis = pod.addContainer({
image: 'redis'
})

Expand Down

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions examples/typescript/cdk8s-plus-elasticsearch-query/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ export class MyChart extends Chart {

const passwordSecret = kplus.Secret.fromSecretName(`${elastic.name}-es-elastic-user`);

const deployment = new kplus.Deployment(this, 'Deployment', {
replicas: 1,
})

const workingDir = '/root';
const queryPort = 8000;
const container = new kplus.Container({
const container = deployment.addContainer({
image: 'node:12.18.0-stretch',
workingDir: workingDir,
command: ['node', 'query.js', queryPort.toString()],
Expand All @@ -55,19 +59,14 @@ export class MyChart extends Chart {
ELASTIC_ENDPOINT: kplus.EnvValue.fromValue(`http://${elastic.name}-es-http:${esPort}`),
ELASTIC_PASSWORD: kplus.EnvValue.fromSecretValue({ secret: passwordSecret, key: 'elastic' })
}
})
});

const configMap = new kplus.ConfigMap(this, 'Config');
configMap.addFile(`${__dirname}/query.js`);

const volume = kplus.Volume.fromConfigMap(configMap);
container.mount(workingDir, volume);

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

deployment.expose(9000);

}
Expand Down
4 changes: 2 additions & 2 deletions examples/typescript/cdk8s-plus-ingress/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export class MyChart extends Chart {
private echoBackend(text: string) {
const deploy = new kplus.Deployment(this, text, {
containers: [
new kplus.Container({
{
image: 'hashicorp/http-echo',
args: [ '-text', text ]
})
}
]
});

Expand Down
Loading

0 comments on commit 33bf97a

Please sign in to comment.