cdk8s-core icon indicating copy to clipboard operation
cdk8s-core copied to clipboard

Add support to Deno

Open Alvise88 opened this issue 2 years ago • 3 comments

Even with the latest version of deno it doesn't seem possible to use cdk8s.

Deno: v1.21.3

import { Chart } from "https://esm.sh/[email protected]";
import {Construct} from "https://esm.sh/[email protected]";

class MyChart extends Chart {
  constructor(scope: Construct, ns: string) {
    super(scope, ns);

    // add contents here
  }
}

/**
 * hello-world.js
 */
function capitalize(word: string) {
  return word.charAt(0).toUpperCase() + word.slice(1);
}

function hello(name: string) {
  return "Hello " + capitalize(name);
}

console.log(hello("john"));
console.log(hello("Sarah"));
console.log(hello("kai"));

/**
 * Output:
 *
 * Hello John
 * Hello Sarah
 * Hello Kai
 */
deno --unstable run stack.ts
Uncaught SyntaxError: The requested module 'https://esm.sh/[email protected]' does not provide an export named 'Chart'
import { Chart } from "https://esm.sh/[email protected]";

Alvise88 avatar May 13 '22 06:05 Alvise88

It seems like recent version already support cdk8s and cdk8s+.

docker run -i --entrypoint sh docker.io/denoland/deno:1.30.3 <<\EOF
deno run -A - <<\EOOF
import * as kplus from 'npm:cdk8s-plus-25'
import * as cdk8s from 'npm:cdk8s'
const app = new cdk8s.App()
const chart = new cdk8s.Chart(app, 'ch101')
const deploy203 = new kplus.Deployment(chart, 'd203', {
    replicas: 2,
    containers: [
        { 
            image: 'nginx', 
            securityContext: { ensureNonRoot: false, readOnlyRootFilesystem: false}
        }
    ],
})
new kplus.Service(chart, 'srv987', {
        selector: deploy203, 
        ports: [ { port: 80, targetPort: 8080}]
    });
console.log(app.synthYaml())
EOOF
EOF

output

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ch101-d203-c886a9fd
spec:
  minReadySeconds: 0
  progressDeadlineSeconds: 600
  replicas: 2
  selector:
    matchLabels:
      cdk8s.io/metadata.addr: ch101-d203-c8cfa8e5
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        cdk8s.io/metadata.addr: ch101-d203-c8cfa8e5
    spec:
      automountServiceAccountToken: false
      containers:
        - image: nginx
          imagePullPolicy: Always
          name: main
          resources:
            limits:
              cpu: 1500m
              memory: 2048Mi
            requests:
              cpu: 1000m
              memory: 512Mi
          securityContext:
            allowPrivilegeEscalation: false
            privileged: false
            readOnlyRootFilesystem: false
            runAsNonRoot: false
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      securityContext:
        fsGroupChangePolicy: Always
        runAsNonRoot: true
      setHostnameAsFQDN: false
---
apiVersion: v1
kind: Service
metadata:
  name: ch101-srv987-c8aad50a
spec:
  externalIPs: []
  ports:
    - port: 80
      targetPort: 8080
  selector:
    cdk8s.io/metadata.addr: ch101-d203-c8cfa8e5
  type: ClusterIP

y12studio avatar Feb 10 '23 10:02 y12studio

I've been using cdk8s with Deno for a while now since they introduced npm support. Code itself runs fine, but there are some gaps. For example CLI installation npm install -g cdk8s-cli and cdk8s init typescript-app both assumes you're using NodeJS. But I don't see any technical difficulties supporting Deno at this point.

Edit:

Posting from slack for visibility.

cdk8s package itself was working fine for me. From https://github.com/cdk8s-team/cdk8s-plus/issues/503#issuecomment-1340005946

# cdk8s.yaml:
language: typescript
app: deno run --allow-env --allow-write --allow-read main.ts
imports:
  - k8s

I believe deno equivalent of npx cdk8s synth is deno run --allow-env --allow-read npm:cdk8s-cli synth But I get this error.

Error: ENOENT: no such file or directory, readdir
    at __node_internal_captureLargerStackTrace (ext:deno_node/internal/errors.ts:91:11)
    at __node_internal_uvException (ext:deno_node/internal/errors.ts:184:12)
    at denoErrorToNodeError (ext:deno_node/internal/errors.ts:1845:16)
    at Object.readdirSync (ext:deno_node/_fs/_fs_readdir.ts:73:15)
....

So I still needed to use node/npm for cdk8s-cli

shinebayar-g avatar Apr 05 '23 22:04 shinebayar-g

I've no idea whether I'm doing something wrong, but even though a simple cdk8s examples work without a problem with Deno, I've stumbled upon the following with Helm:

error: Uncaught ReferenceError: require is not defined
const { http, https } = require('follow-redirects');

with error location being node_modules/.deno/[email protected]/node_modules/cdk8s/lib/_loadurl.js:8:25.

rassie avatar Apr 25 '23 12:04 rassie