From 16f291f1d90d6cf01d6da2b1c1f8ef1dac3725d6 Mon Sep 17 00:00:00 2001 From: Elad Ben-Israel Date: Tue, 23 Jul 2024 14:46:14 +0300 Subject: [PATCH] feat(k8s): `bring k8s` (#298) Rename `cdk8s` to `k8s` to decouple it from CDK8s (at least semantically). --- .github/workflows/canary.yaml | 66 +++++++-------- .../{cdk8s-pull.yaml => k8s-pull.yaml} | 14 ++-- .../{cdk8s-release.yaml => k8s-release.yaml} | 26 +++--- .github/workflows/pull-request-lint.yaml | 2 +- .mergify.yml | 12 +-- README.md | 2 +- {cdk8s => k8s}/.gitignore | 0 {cdk8s => k8s}/LICENSE | 0 {cdk8s => k8s}/README.md | 82 +++++++++++-------- {cdk8s => k8s}/api-object.w | 2 +- k8s/bin/kwing | 1 + {cdk8s => k8s}/examples/api-object.main.w | 0 .../examples/api-object.main.w.snap | 0 {cdk8s => k8s}/examples/nodejs-app/index.js | 0 {cdk8s => k8s}/examples/nodejs.main.w | 0 {cdk8s => k8s}/examples/nodejs.main.w.snap | 0 {cdk8s => k8s}/examples/ubuntu.main.w | 0 {cdk8s => k8s}/examples/ubuntu.main.w.snap | 0 {cdk8s => k8s}/lib/index.js | 2 +- {cdk8s => k8s}/package-lock.json | 8 +- {cdk8s => k8s}/package.json | 8 +- {cdk8s => k8s}/root.test.w | 0 {cdk8s => k8s}/test.sh | 0 23 files changed, 121 insertions(+), 104 deletions(-) rename .github/workflows/{cdk8s-pull.yaml => k8s-pull.yaml} (74%) rename .github/workflows/{cdk8s-release.yaml => k8s-release.yaml} (72%) rename {cdk8s => k8s}/.gitignore (100%) rename {cdk8s => k8s}/LICENSE (100%) rename {cdk8s => k8s}/README.md (65%) rename {cdk8s => k8s}/api-object.w (71%) create mode 100644 k8s/bin/kwing rename {cdk8s => k8s}/examples/api-object.main.w (100%) rename {cdk8s => k8s}/examples/api-object.main.w.snap (100%) rename {cdk8s => k8s}/examples/nodejs-app/index.js (100%) rename {cdk8s => k8s}/examples/nodejs.main.w (100%) rename {cdk8s => k8s}/examples/nodejs.main.w.snap (100%) rename {cdk8s => k8s}/examples/ubuntu.main.w (100%) rename {cdk8s => k8s}/examples/ubuntu.main.w.snap (100%) rename {cdk8s => k8s}/lib/index.js (98%) rename {cdk8s => k8s}/package-lock.json (99%) rename {cdk8s => k8s}/package.json (76%) rename {cdk8s => k8s}/root.test.w (100%) rename {cdk8s => k8s}/test.sh (100%) diff --git a/.github/workflows/canary.yaml b/.github/workflows/canary.yaml index 1d37cf90..3d5a1a63 100644 --- a/.github/workflows/canary.yaml +++ b/.github/workflows/canary.yaml @@ -70,39 +70,6 @@ jobs: cd budget wing test timeout_minutes: 5 - canary-cdk8s: - name: Test cdk8s - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v3 - with: - sparse-checkout: cdk8s - - name: Setup Node.js - uses: actions/setup-node@v3 - with: - node-version: 20.x - registry-url: https://registry.npmjs.org - - name: Install winglang and dependencies - uses: nick-fields/retry@v3 - with: - max_attempts: 3 - command: npm i -g winglang --loglevel verbose - timeout_minutes: 3 - - name: Install dependencies - uses: nick-fields/retry@v3 - with: - max_attempts: 3 - command: cd cdk8s && npm i --include=dev --loglevel verbose - timeout_minutes: 3 - - name: Run tests - uses: nick-fields/retry@v3 - with: - max_attempts: 3 - command: |- - cd cdk8s - ./test.sh - timeout_minutes: 5 canary-checks: name: Test checks runs-on: ubuntu-latest @@ -301,6 +268,39 @@ jobs: cd jwt wing test timeout_minutes: 5 + canary-k8s: + name: Test k8s + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + sparse-checkout: k8s + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: 20.x + registry-url: https://registry.npmjs.org + - name: Install winglang and dependencies + uses: nick-fields/retry@v3 + with: + max_attempts: 3 + command: npm i -g winglang --loglevel verbose + timeout_minutes: 3 + - name: Install dependencies + uses: nick-fields/retry@v3 + with: + max_attempts: 3 + command: cd k8s && npm i --include=dev --loglevel verbose + timeout_minutes: 3 + - name: Run tests + uses: nick-fields/retry@v3 + with: + max_attempts: 3 + command: |- + cd k8s + ./test.sh + timeout_minutes: 5 canary-lock: name: Test lock runs-on: ubuntu-latest diff --git a/.github/workflows/cdk8s-pull.yaml b/.github/workflows/k8s-pull.yaml similarity index 74% rename from .github/workflows/cdk8s-pull.yaml rename to .github/workflows/k8s-pull.yaml index 2b65f0be..cacc8df8 100644 --- a/.github/workflows/cdk8s-pull.yaml +++ b/.github/workflows/k8s-pull.yaml @@ -1,16 +1,16 @@ -name: cdk8s-pull +name: k8s-pull on: pull_request: paths: - - cdk8s/** + - k8s/** jobs: - build-cdk8s: + build-k8s: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v3 with: - sparse-checkout: cdk8s + sparse-checkout: k8s - name: Setup Node.js uses: actions/setup-node@v3 with: @@ -20,10 +20,10 @@ jobs: run: npm i -g winglang - name: Install dependencies run: npm install --include=dev - working-directory: cdk8s + working-directory: k8s - name: Test run: ./test.sh - working-directory: cdk8s + working-directory: k8s - name: Pack run: wing pack - working-directory: cdk8s + working-directory: k8s diff --git a/.github/workflows/cdk8s-release.yaml b/.github/workflows/k8s-release.yaml similarity index 72% rename from .github/workflows/cdk8s-release.yaml rename to .github/workflows/k8s-release.yaml index 02b3d684..3736ae7d 100644 --- a/.github/workflows/cdk8s-release.yaml +++ b/.github/workflows/k8s-release.yaml @@ -1,19 +1,19 @@ -name: cdk8s-release +name: k8s-release on: push: branches: - main paths: - - cdk8s/** - - "!cdk8s/package-lock.json" + - k8s/** + - "!k8s/package-lock.json" jobs: - build-cdk8s: + build-k8s: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v3 with: - sparse-checkout: cdk8s + sparse-checkout: k8s - name: Setup Node.js uses: actions/setup-node@v3 with: @@ -23,32 +23,32 @@ jobs: run: npm i -g winglang - name: Install dependencies run: npm install --include=dev - working-directory: cdk8s + working-directory: k8s - name: Test run: ./test.sh - working-directory: cdk8s + working-directory: k8s - name: Pack run: wing pack - working-directory: cdk8s + working-directory: k8s - name: Get package version run: echo WINGLIB_VERSION=$(node -p "require('./package.json').version") >> "$GITHUB_ENV" - working-directory: cdk8s + working-directory: k8s - name: Publish run: npm publish --access=public --registry https://registry.npmjs.org --tag latest *.tgz - working-directory: cdk8s + working-directory: k8s env: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} - name: Tag commit uses: tvdias/github-tagger@v0.0.1 with: repo-token: ${{ secrets.PROJEN_GITHUB_TOKEN }} - tag: cdk8s-v${{ env.WINGLIB_VERSION }} + tag: k8s-v${{ env.WINGLIB_VERSION }} - name: GitHub release uses: softprops/action-gh-release@v1 with: - name: cdk8s v${{ env.WINGLIB_VERSION }} - tag_name: cdk8s-v${{ env.WINGLIB_VERSION }} + name: k8s v${{ env.WINGLIB_VERSION }} + tag_name: k8s-v${{ env.WINGLIB_VERSION }} files: "*.tgz" token: ${{ secrets.PROJEN_GITHUB_TOKEN }} diff --git a/.github/workflows/pull-request-lint.yaml b/.github/workflows/pull-request-lint.yaml index 8dd551a2..253ff54c 100644 --- a/.github/workflows/pull-request-lint.yaml +++ b/.github/workflows/pull-request-lint.yaml @@ -26,7 +26,6 @@ jobs: revert bedrock budget - cdk8s checks cognito containers @@ -35,6 +34,7 @@ jobs: fifoqueue github jwt + k8s lock messagefanout ngrok diff --git a/.mergify.yml b/.mergify.yml index 7f30e66f..0681d37f 100644 --- a/.mergify.yml +++ b/.mergify.yml @@ -38,9 +38,6 @@ pull_request_rules: - -check-failure=build-budget - -check-pending=build-budget - -check-stale=build-budget - - -check-failure=build-cdk8s - - -check-pending=build-cdk8s - - -check-stale=build-cdk8s - -check-failure=build-checks - -check-pending=build-checks - -check-stale=build-checks @@ -65,6 +62,9 @@ pull_request_rules: - -check-failure=build-jwt - -check-pending=build-jwt - -check-stale=build-jwt + - -check-failure=build-k8s + - -check-pending=build-k8s + - -check-stale=build-k8s - -check-failure=build-lock - -check-pending=build-lock - -check-stale=build-lock @@ -138,9 +138,6 @@ pull_request_rules: - -check-failure=build-budget - -check-pending=build-budget - -check-stale=build-budget - - -check-failure=build-cdk8s - - -check-pending=build-cdk8s - - -check-stale=build-cdk8s - -check-failure=build-checks - -check-pending=build-checks - -check-stale=build-checks @@ -165,6 +162,9 @@ pull_request_rules: - -check-failure=build-jwt - -check-pending=build-jwt - -check-stale=build-jwt + - -check-failure=build-k8s + - -check-pending=build-k8s + - -check-stale=build-k8s - -check-failure=build-lock - -check-pending=build-lock - -check-stale=build-lock diff --git a/README.md b/README.md index 6f7e113a..960d946f 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,6 @@ publishing them for you. | --- | --- | --- | | [bedrock](./bedrock) | [@winglibs/bedrock](https://www.npmjs.com/package/@winglibs/bedrock) | sim, tf-aws | | [budget](./budget) | [@winglibs/budget](https://www.npmjs.com/package/@winglibs/budget) | sim, tf-aws | -| [cdk8s](./cdk8s) | [@winglibs/cdk8s](https://www.npmjs.com/package/@winglibs/cdk8s) | cdk8s | | [checks](./checks) | [@winglibs/checks](https://www.npmjs.com/package/@winglibs/checks) | * | | [cognito](./cognito) | [@winglibs/cognito](https://www.npmjs.com/package/@winglibs/cognito) | sim, tf-aws | | [containers](./containers) | [@winglibs/containers](https://www.npmjs.com/package/@winglibs/containers) | sim, tf-aws | @@ -23,6 +22,7 @@ publishing them for you. | [fifoqueue](./fifoqueue) | [@winglibs/fifoqueue](https://www.npmjs.com/package/@winglibs/fifoqueue) | sim, tf-aws | | [github](./github) | [@winglibs/github](https://www.npmjs.com/package/@winglibs/github) | * | | [jwt](./jwt) | [@winglibs/jwt](https://www.npmjs.com/package/@winglibs/jwt) | * | +| [k8s](./k8s) | [@winglibs/k8s](https://www.npmjs.com/package/@winglibs/k8s) | k8s | | [lock](./lock) | [@winglibs/lock](https://www.npmjs.com/package/@winglibs/lock) | * | | [messagefanout](./messagefanout) | [@winglibs/messagefanout](https://www.npmjs.com/package/@winglibs/messagefanout) | sim, tf-aws | | [ngrok](./ngrok) | [@winglibs/ngrok](https://www.npmjs.com/package/@winglibs/ngrok) | * | diff --git a/cdk8s/.gitignore b/k8s/.gitignore similarity index 100% rename from cdk8s/.gitignore rename to k8s/.gitignore diff --git a/cdk8s/LICENSE b/k8s/LICENSE similarity index 100% rename from cdk8s/LICENSE rename to k8s/LICENSE diff --git a/cdk8s/README.md b/k8s/README.md similarity index 65% rename from cdk8s/README.md rename to k8s/README.md index b1f0b988..47daf198 100644 --- a/cdk8s/README.md +++ b/k8s/README.md @@ -1,7 +1,6 @@ -# cdk8s support for Wing +# Wing for Kubernetes -This library is a custom Wing platform that can be used to synthesize Kubernetes YAML manifests -through [cdk8s](https://cdk8s.io) constructs. +A framework for synthesizing Kubernetes manifests using Winglang. ## Prerequisites @@ -18,45 +17,71 @@ npm i -g winglang Create a new project and install this library: ```sh -mkdir wing-loves-cdk8s -cd wing-loves-cdk8s -npm i @winglibs/cdk8s -``` - -If you wish to use [cdk8s-plus](https://cdk8s.io/docs/latest/plus/), you'll also -need to install it (choose the relevant K8S version): - -```sh -npm i cdk8s-plus-27 +mkdir wing-loves-k8s +cd wing-loves-k8s +npm i @winglibs/k8s ``` ## Usage -Let's define a Deployment resource with 3 replicas of the `ubuntu` public Docker image: +Let's define an app with a simple Kubernetes object: ```js -// ubuntu.main.w -bring "cdk8s-plus-27" as k8s; - -let deployment = new k8s.Deployment(replicas: 3); -deployment.addContainer(image: "ubuntu"); +// main.w +bring k8s; + +new k8s.ApiObject( + apiVersion: "v1", + kind: "ConfigMap", + spec: { + data: { + key: "value", + }, + } +); ``` Now, compile it to YAML: ```sh -$ wing compile -t @winglibs/cdk8s ubuntu.main.w -target/ubuntu.main.cdk8s +$ wing compile -t @winglibs/k8s ubuntu.main.w +target/ubuntu.main.k8s ``` -The output is a valid K8S YAML is in `target/ubuntu.main.cdk8s`: +You + +The output is a valid K8S YAML is in `target/ubuntu.main.k8s`: ```sh -$ ls target/ubuntu.main.cdk8s +$ ls target/ubuntu.main.k8s chart-c86185a7.k8s.yaml ``` -Here's a more interesting example: +### Creating Helm charts + +You can set `WING_K8S_OUTPUT` to `helm` in order to produce a helm chart instead of simple manifest. +This requires a `Chart.yaml` file next in the current directory. + +### Applying labels to all resources + +You can use the `WING_K8S_LABELS` environment variable to apply labels to all resources in an app. +The value is a JSON-encoded map. + +```sh +export WING_K8S_LABELS='{ "my-label": "123", "your-label": "444" }' +wing compile -t @winglibs/k8s main.w +``` + +### CDK8s Support + +This library supports [cdk8s](https://cdk8s.io) and +[cdk8s-plus](https://cdk8s.io/docs/latest/plus/), so you can do stuff like this: + +```sh +npm i cdk8s-plus-27 +``` + +And then: ```js bring "cdk8s-plus-27" as k8s; @@ -89,15 +114,6 @@ container.mount(appPath, appVolume); deployment.exposeViaService(serviceType: k8s.ServiceType.LOAD_BALANCER); ``` -### Applying labels to all resources - -You can use the `WING_K8S_LABELS` environment variable to apply labels to all resources in an app. -The value is a JSON-encoded map. - -```sh -export WING_K8S_LABELS='{ "my-label": "123", "your-label": "444" }' -wing compile -t @winglibs/cdk8s main.w -``` ## Roadmap diff --git a/cdk8s/api-object.w b/k8s/api-object.w similarity index 71% rename from cdk8s/api-object.w rename to k8s/api-object.w index e3c3c564..595e1ea2 100644 --- a/cdk8s/api-object.w +++ b/k8s/api-object.w @@ -1,6 +1,6 @@ bring "cdk8s" as cdk8s; -pub struct ApiObjectProps extends cdk8s.ApiObjectProps{ +pub struct ApiObjectProps extends cdk8s.ApiObjectProps { spec: Json?; } diff --git a/k8s/bin/kwing b/k8s/bin/kwing new file mode 100644 index 00000000..1a248525 --- /dev/null +++ b/k8s/bin/kwing @@ -0,0 +1 @@ +#!/bin/sh diff --git a/cdk8s/examples/api-object.main.w b/k8s/examples/api-object.main.w similarity index 100% rename from cdk8s/examples/api-object.main.w rename to k8s/examples/api-object.main.w diff --git a/cdk8s/examples/api-object.main.w.snap b/k8s/examples/api-object.main.w.snap similarity index 100% rename from cdk8s/examples/api-object.main.w.snap rename to k8s/examples/api-object.main.w.snap diff --git a/cdk8s/examples/nodejs-app/index.js b/k8s/examples/nodejs-app/index.js similarity index 100% rename from cdk8s/examples/nodejs-app/index.js rename to k8s/examples/nodejs-app/index.js diff --git a/cdk8s/examples/nodejs.main.w b/k8s/examples/nodejs.main.w similarity index 100% rename from cdk8s/examples/nodejs.main.w rename to k8s/examples/nodejs.main.w diff --git a/cdk8s/examples/nodejs.main.w.snap b/k8s/examples/nodejs.main.w.snap similarity index 100% rename from cdk8s/examples/nodejs.main.w.snap rename to k8s/examples/nodejs.main.w.snap diff --git a/cdk8s/examples/ubuntu.main.w b/k8s/examples/ubuntu.main.w similarity index 100% rename from cdk8s/examples/ubuntu.main.w rename to k8s/examples/ubuntu.main.w diff --git a/cdk8s/examples/ubuntu.main.w.snap b/k8s/examples/ubuntu.main.w.snap similarity index 100% rename from cdk8s/examples/ubuntu.main.w.snap rename to k8s/examples/ubuntu.main.w.snap diff --git a/cdk8s/lib/index.js b/k8s/lib/index.js similarity index 98% rename from cdk8s/lib/index.js rename to k8s/lib/index.js index fbc1c9e2..fc97a81c 100644 --- a/cdk8s/lib/index.js +++ b/k8s/lib/index.js @@ -2,7 +2,7 @@ const cdk8s = require('cdk8s'); const { core, std } = require('@winglang/sdk');; exports.Platform = class { - target = "cdk8s"; + target = "k8s"; newApp(props) { const app = new cdk8s.App({ outdir: props.outdir }); diff --git a/cdk8s/package-lock.json b/k8s/package-lock.json similarity index 99% rename from cdk8s/package-lock.json rename to k8s/package-lock.json index 7fd82242..db476d86 100644 --- a/cdk8s/package-lock.json +++ b/k8s/package-lock.json @@ -1,12 +1,12 @@ { - "name": "@winglibs/cdk8s", - "version": "0.0.3", + "name": "@winglibs/k8s", + "version": "0.0.4", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "@winglibs/cdk8s", - "version": "0.0.3", + "name": "@winglibs/k8s", + "version": "0.0.4", "license": "MIT", "devDependencies": { "cdk8s-plus-27": "^2.9.0" diff --git a/cdk8s/package.json b/k8s/package.json similarity index 76% rename from cdk8s/package.json rename to k8s/package.json index 2b94e02e..9cd5fa86 100644 --- a/cdk8s/package.json +++ b/k8s/package.json @@ -1,6 +1,6 @@ { - "name": "@winglibs/cdk8s", - "description": "cdk8s library for Wing", + "name": "@winglibs/k8s", + "description": "Wing for Kubernetes", "version": "0.0.4", "author": { "name": "Elad Ben-Israel", @@ -9,11 +9,11 @@ "repository": { "type": "git", "url": "https://github.com/winglang/winglibs.git", - "directory": "cdk8s" + "directory": "k8s" }, "wing": { "platforms": [ - "cdk8s" + "k8s" ] }, "license": "MIT", diff --git a/cdk8s/root.test.w b/k8s/root.test.w similarity index 100% rename from cdk8s/root.test.w rename to k8s/root.test.w diff --git a/cdk8s/test.sh b/k8s/test.sh similarity index 100% rename from cdk8s/test.sh rename to k8s/test.sh