From 5b4a87dcd652920c8137ea1114b17d376d5f8726 Mon Sep 17 00:00:00 2001 From: cdk8s-automation <81352262+cdk8s-automation@users.noreply.github.com> Date: Thu, 13 May 2021 11:33:15 +0300 Subject: [PATCH] chore: upgrade projen (#9) * chore: upgrade projen * chore: self mutation Co-authored-by: cdk8s-automation Co-authored-by: Automation --- .projen/deps.json | 2 +- .projen/tasks.json | 3 + .projenrc.js | 6 +- API.md | 43 +++++ lib/__snapshots__/redis.test.js.snap | 234 --------------------------- lib/index.ts | 1 - lib/redis.test.ts | 57 ------- lib/redis.ts | 69 -------- lib/service-deployment.ts | 125 -------------- package.json | 2 +- yarn.lock | 8 +- 11 files changed, 55 insertions(+), 495 deletions(-) create mode 100644 API.md delete mode 100644 lib/__snapshots__/redis.test.js.snap delete mode 100644 lib/index.ts delete mode 100644 lib/redis.test.ts delete mode 100644 lib/redis.ts delete mode 100644 lib/service-deployment.ts diff --git a/.projen/deps.json b/.projen/deps.json index d7b8199d..a9320904 100644 --- a/.projen/deps.json +++ b/.projen/deps.json @@ -72,7 +72,7 @@ }, { "name": "projen", - "version": "^0.17.84", + "version": "^0.17.85", "type": "build" }, { diff --git a/.projen/tasks.json b/.projen/tasks.json index 594e7396..9c2cf48b 100644 --- a/.projen/tasks.json +++ b/.projen/tasks.json @@ -41,6 +41,9 @@ { "exec": "git -c \"versionsort.suffix=-\" tag --sort=\"-version:refname\" --list \"v*\" | head -n1 > .version.tmp.json" }, + { + "exec": "if [ \"$(cat .version.tmp.json)\" == \"\" ]; then echo \"v0.1.0\" > .version.tmp.json; fi" + }, { "exec": "standard-version" } diff --git a/.projenrc.js b/.projenrc.js index ab73af29..0172dbf3 100644 --- a/.projenrc.js +++ b/.projenrc.js @@ -11,12 +11,12 @@ const project = new JsiiProject({ projenUpgradeSecret: 'PROJEN_GITHUB_TOKEN', peerDeps: [ 'cdk8s', - 'constructs' + 'constructs', ], publishToPypi: { distName: 'cdk8s-redis', - module: 'cdk8s_redis' - } + module: 'cdk8s_redis', + }, }); project.synth(); diff --git a/API.md b/API.md new file mode 100644 index 00000000..43cd34cf --- /dev/null +++ b/API.md @@ -0,0 +1,43 @@ +# API Reference + +**Classes** + +Name|Description +----|----------- +[Hello](#cdk8s-redis-hello)|*No description* + + + +## class Hello 🔹 + + + + +### Initializer + + + + +```ts +new Hello() +``` + + + +### Methods + + +#### sayHello()🔹 + + + +```ts +sayHello(): string +``` + + +__Returns__: +* string + + + diff --git a/lib/__snapshots__/redis.test.js.snap b/lib/__snapshots__/redis.test.js.snap deleted file mode 100644 index 65dfc2fc..00000000 --- a/lib/__snapshots__/redis.test.js.snap +++ /dev/null @@ -1,234 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`defaults 1`] = ` -Array [ - Object { - "apiVersion": "v1", - "kind": "Service", - "metadata": Object { - "labels": Object { - "app": "testredismaster73F27A6F", - "role": "master", - }, - "name": "test-redis-master-service-54ad70b8", - }, - "spec": Object { - "ports": Array [ - Object { - "port": 6379, - "targetPort": 6379, - }, - ], - "selector": Object { - "app": "testredismaster73F27A6F", - "role": "master", - }, - "type": "ClusterIP", - }, - }, - Object { - "apiVersion": "apps/v1", - "kind": "Deployment", - "metadata": Object { - "name": "test-redis-master-deployment-f9f616d4", - }, - "spec": Object { - "replicas": 1, - "selector": Object { - "matchLabels": Object { - "app": "testredismaster73F27A6F", - "role": "master", - }, - }, - "template": Object { - "metadata": Object { - "labels": Object { - "app": "testredismaster73F27A6F", - "role": "master", - }, - }, - "spec": Object { - "containers": Array [ - Object { - "env": Array [ - Object { - "name": "GET_HOSTS_FROM", - "value": "dns", - }, - ], - "image": "k8s.gcr.io/redis:e2e", - "name": "master", - "ports": Array [ - Object { - "containerPort": 6379, - }, - ], - "resources": Object { - "requests": Object { - "cpu": "100m", - "memory": "100Mi", - }, - }, - }, - ], - }, - }, - }, - }, - Object { - "apiVersion": "v1", - "kind": "Service", - "metadata": Object { - "labels": Object { - "app": "testredisslave47C5B2C0", - "role": "slave", - }, - "name": "test-redis-slave-service-aad78a45", - }, - "spec": Object { - "ports": Array [ - Object { - "port": 6379, - "targetPort": 6379, - }, - ], - "selector": Object { - "app": "testredisslave47C5B2C0", - "role": "slave", - }, - "type": "ClusterIP", - }, - }, - Object { - "apiVersion": "apps/v1", - "kind": "Deployment", - "metadata": Object { - "name": "test-redis-slave-deployment-49d103d7", - }, - "spec": Object { - "replicas": 2, - "selector": Object { - "matchLabels": Object { - "app": "testredisslave47C5B2C0", - "role": "slave", - }, - }, - "template": Object { - "metadata": Object { - "labels": Object { - "app": "testredisslave47C5B2C0", - "role": "slave", - }, - }, - "spec": Object { - "containers": Array [ - Object { - "env": Array [ - Object { - "name": "GET_HOSTS_FROM", - "value": "env", - }, - Object { - "name": "REDIS_MASTER_SERVICE_HOST", - "value": "test-redis-master-service-54ad70b8", - }, - ], - "image": "gcr.io/google_samples/gb-redisslave:v1", - "name": "slave", - "ports": Array [ - Object { - "containerPort": 6379, - }, - ], - "resources": Object { - "requests": Object { - "cpu": "100m", - "memory": "100Mi", - }, - }, - }, - ], - }, - }, - }, - }, -] -`; - -exports[`no slave replicas will deploy only the master 1`] = ` -Array [ - Object { - "apiVersion": "v1", - "kind": "Service", - "metadata": Object { - "labels": Object { - "app": "testredismaster73F27A6F", - "role": "master", - }, - "name": "test-redis-master-service-54ad70b8", - }, - "spec": Object { - "ports": Array [ - Object { - "port": 6379, - "targetPort": 6379, - }, - ], - "selector": Object { - "app": "testredismaster73F27A6F", - "role": "master", - }, - "type": "ClusterIP", - }, - }, - Object { - "apiVersion": "apps/v1", - "kind": "Deployment", - "metadata": Object { - "name": "test-redis-master-deployment-f9f616d4", - }, - "spec": Object { - "replicas": 1, - "selector": Object { - "matchLabels": Object { - "app": "testredismaster73F27A6F", - "role": "master", - }, - }, - "template": Object { - "metadata": Object { - "labels": Object { - "app": "testredismaster73F27A6F", - "role": "master", - }, - }, - "spec": Object { - "containers": Array [ - Object { - "env": Array [ - Object { - "name": "GET_HOSTS_FROM", - "value": "dns", - }, - ], - "image": "k8s.gcr.io/redis:e2e", - "name": "master", - "ports": Array [ - Object { - "containerPort": 6379, - }, - ], - "resources": Object { - "requests": Object { - "cpu": "100m", - "memory": "100Mi", - }, - }, - }, - ], - }, - }, - }, - }, -] -`; diff --git a/lib/index.ts b/lib/index.ts deleted file mode 100644 index f4309687..00000000 --- a/lib/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './redis'; diff --git a/lib/redis.test.ts b/lib/redis.test.ts deleted file mode 100644 index 7a09e1f8..00000000 --- a/lib/redis.test.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { Chart, Testing } from 'cdk8s'; -import { Redis } from './redis'; - -test('defaults', () => { - // GIVEN - const app = Testing.app(); - const chart = new Chart(app, 'test'); - - // WHEN - const redis = new Redis(chart, 'redis'); - - // THEN - expect(Testing.synth(chart)).toMatchSnapshot(); - expect(redis.masterHost).toEqual('test-redis-master-service-54ad70b8'); - expect(redis.slaveHost).toEqual('test-redis-slave-service-aad78a45'); -}); - -test('no slave replicas will deploy only the master', () => { - // GIVEN - const app = Testing.app(); - const chart = new Chart(app, 'test'); - - // WHEN - const redis = new Redis(chart, 'redis', { - slaveReplicas: 0, - }); - - // THEN - expect(Testing.synth(chart)).toMatchSnapshot(); - expect(redis.masterHost).toEqual('test-redis-master-service-54ad70b8'); - expect(redis.masterHost).toEqual(redis.slaveHost); // slave = master -}); - -test('labels can be added', () => { - // GIVEN - const app = Testing.app(); - const chart = new Chart(app, 'test'); - - // WHEN - new Redis(chart, 'redis', { - labels: { - mylabel: '1234', - }, - }); - - // THEN - const resources = Testing.synth(chart); - - for (const svc of resources.filter(r => r.kind === 'Service')) { - expect(svc.metadata.labels.mylabel).toEqual('1234'); - } - - for (const dep of resources.filter(r => r.kind === 'Deployment')) { - expect(dep.spec.selector.matchLabels.mylabel).toEqual('1234'); - expect(dep.spec.template.metadata.labels.mylabel).toEqual('1234'); - } -}); diff --git a/lib/redis.ts b/lib/redis.ts deleted file mode 100644 index 6bcaba61..00000000 --- a/lib/redis.ts +++ /dev/null @@ -1,69 +0,0 @@ -import { Construct } from 'constructs'; -import { ServiceDeployment } from './service-deployment'; - -export interface RedisOptions { - /** - * Number of slave replicas. - * @default 2 - */ - readonly slaveReplicas?: number; - - /** - * Extra labels to associate with resources. - * @default - none - */ - readonly labels?: { [name: string]: string }; -} - -export class Redis extends Construct { - /** - * The DNS host for the master server. - */ - public readonly masterHost: string; - - /** - * The DNS host for the slave service. - */ - public readonly slaveHost: string; - - constructor(scope: Construct, id: string, options: RedisOptions = { }) { - super(scope, id); - - const master = new ServiceDeployment(this, 'master', { - image: 'k8s.gcr.io/redis:e2e', - containerPort: 6379, - externalPort: 6379, - containerName: 'master', - env: { GET_HOSTS_FROM: 'dns' }, - labels: { - app: 'redis', - role: 'master', - ...options.labels, - }, - }); - - this.masterHost = master.host; - - const slaveReplicas = options.slaveReplicas ?? 2; - if (slaveReplicas > 0) { - const slave = new ServiceDeployment(this, 'slave', { - image: 'gcr.io/google_samples/gb-redisslave:v1', - containerName: 'slave', - containerPort: 6379, - externalPort: 6379, - env: { GET_HOSTS_FROM: 'env', REDIS_MASTER_SERVICE_HOST: this.masterHost }, - replicas: slaveReplicas, - labels: { - app: 'redis', - role: 'slave', - ...options.labels, - }, - }); - - this.slaveHost = slave.host; - } else { - // if we have no slave, then use the same host as the master - this.slaveHost = master.host; - } - } -} diff --git a/lib/service-deployment.ts b/lib/service-deployment.ts deleted file mode 100644 index 917c1e6f..00000000 --- a/lib/service-deployment.ts +++ /dev/null @@ -1,125 +0,0 @@ -import * as k8s from '../imports/k8s'; -import { Construct, Node } from 'constructs'; - -export interface ServiceDeploymentOptions { - /** - * @default 1 - */ - readonly replicas?: number; - - /** - * Container image. - */ - readonly image: string; - - /** - * Additional labels to apply to resources. - * @default - none - */ - readonly labels?: { [name: string]: string }; - - /** - * Pod resource requirements - * @default - cpu: '100m', memory: '100Mi' - */ - readonly resources?: k8s.ResourceRequirements; - - /** - * @default 80 - */ - readonly externalPort?: number; - - /** - * @default 8080 - */ - readonly containerPort?: number; - - /** - * Environment variables to pass to the pod - */ - readonly env?: { [name: string]: string }; - - /** - * The name of the container - * @default 'app' - */ - readonly containerName?: string; - - /** - * The type of service resource. - * @default ServiceType.CLUSTER_IP - */ - readonly serviceType?: ServiceType; -} - -export enum ServiceType { - EXTERNAL_NAME = 'ExternalName', - CLUSTER_IP = 'ClusterIP', - NODE_PORT = 'NodePort', - LOAD_BALANCER = 'LoadBalancer' -} - -export class ServiceDeployment extends Construct { - - public readonly host: string; - - constructor(scope: Construct, id: string, options: ServiceDeploymentOptions) { - super(scope, id); - - const replicas = options.replicas ?? 1; - const resources = options.resources ?? { requests: { cpu: '100m', memory: '100Mi' } }; - const externalPort = options.externalPort ?? 80; - const containerPort = options.containerPort ?? 8080; - const containerName = options.containerName ?? 'app'; - const serviceType = options.serviceType ?? ServiceType.CLUSTER_IP; - - const label = { - ...options.labels, - app: Node.of(this).uniqueId, - }; - - const service = new k8s.Service(this, 'service', { - metadata: { labels: label }, - spec: { - type: serviceType, - ports: [ { port: externalPort, targetPort: containerPort } ], - selector: label, - }, - }); - - - new k8s.Deployment(this, 'deployment', { - spec: { - selector: { matchLabels: label }, - replicas: replicas, - template: { - metadata: { labels: label }, - spec: { - containers: [ - { - name: containerName, - image: options.image, - ports: [ { containerPort } ], - resources: resources, - env: renderEnv(options.env), - }, - ], - }, - }, - }, - }); - - this.host = service.name; - } -} - -function renderEnv(env: { [key: string]: string } = { }): k8s.EnvVar[] { - const result = new Array(); - for (const [ key, value ] of Object.entries(env)) { - result.push({ - name: key, - value: value, - }) - } - return result; -} \ No newline at end of file diff --git a/package.json b/package.json index 7e589248..cff9bc6c 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ "jsii-docgen": "^1.8.81", "jsii-pacmak": "^1.29.0", "json-schema": "^0.3.0", - "projen": "^0.17.84", + "projen": "^0.17.85", "standard-version": "^9", "ts-jest": "^26.5.6", "typescript": "^4.2.4" diff --git a/yarn.lock b/yarn.lock index 3dc6e9cd..fcf9a579 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4735,10 +4735,10 @@ progress@^2.0.0: resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.3.tgz#7e8cf8d8f5b8f239c1bc68beb4eb78567d572ef8" integrity sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA== -projen@^0.17.84: - version "0.17.84" - resolved "https://registry.yarnpkg.com/projen/-/projen-0.17.84.tgz#ce804ecded53749a4f7ce2f0b882cce467dc6470" - integrity sha512-j39QxJiqbGBAzXkSjNXpo0CGkCKjqA1q7ZJVarNNAb/ZA1W+ogtmRM7NP0LFJSusQLGL6LRrfGdoUWHE1Tshgw== +projen@^0.17.85: + version "0.17.85" + resolved "https://registry.yarnpkg.com/projen/-/projen-0.17.85.tgz#554766ae46664d797dcf06da8bc77a86bdf1aede" + integrity sha512-/+e5DHGb4zYX+KrAlAHe9EH4UQvBDuxXu6zFUwmt2OzNwPrQed0G76FP2qsMG1bstNyBWVtZt08If+yeTzqIGg== dependencies: "@iarna/toml" "^2.2.5" chalk "^4.1.0"