Skip to content

Instantly share code, notes, and snippets.

@stevefan1999-personal
Last active June 2, 2026 15:40
Show Gist options
  • Select an option

  • Save stevefan1999-personal/f7b642501f25997323147e1b67fbf66b to your computer and use it in GitHub Desktop.

Select an option

Save stevefan1999-personal/f7b642501f25997323147e1b67fbf66b to your computer and use it in GitHub Desktop.
function-deno demo: nginx compose script pulled via source.remote
// Fetched at runtime from gist.githubusercontent.com via source.remote.
// The Go-side resolver enforces HTTPS and verifies the host appears in
// spec.permissions.importNet before invoking Deno; the URL is passed
// directly to `deno run`, so this file is a fully self-contained shim
// (reads request JSON from stdin, writes structured response to stdout).
//
// Crossplane v2 native composition: the Deployment + Service are emitted
// DIRECTLY as composed resources (owned by the namespaced XR) — no
// provider-kubernetes Object wrapper. The SDK-style helpers (to/fromModel/
// setDesiredComposedResources/condition) build the structured response;
// kubernetes-models gives the typed manifests. Both packages are baked into
// the function image's Deno cache (Dockerfile prewarm), so they resolve
// offline — only the gist host needs to be in spec.permissions.importNet,
// not the jsr/npm registries.
import type { Request } from "jsr:@crossplane-contrib/function-deno-types@^0.1";
import {
condition,
setDesiredComposedResources,
to,
} from "jsr:@crossplane-contrib/function-deno-types@^0.1/compose";
import { Service } from "npm:kubernetes-models@4/v1";
import { Deployment } from "npm:kubernetes-models@4/apps/v1";
const payload = JSON.parse(await new Response(Deno.stdin.readable).text());
const req = payload.request as Request;
const xr = req.observed.composite!.resource;
const spec = (xr.spec ?? {}) as {
image?: string;
replicas?: number;
host?: string;
namespace?: string;
};
const host = spec.host ?? "web";
const image = spec.image ?? "nginx:1.27";
const replicas = spec.replicas ?? 1;
const ns = spec.namespace ?? "default";
const labels = { app: host };
const rsp = to(req);
setDesiredComposedResources(rsp, {
deployment: {
resource: new Deployment({
metadata: { name: host, namespace: ns, labels },
spec: {
replicas,
selector: { matchLabels: labels },
template: {
metadata: { labels },
spec: {
containers: [{
name: "web",
image,
ports: [{ containerPort: 80 }],
readinessProbe: {
httpGet: { path: "/", port: 80 },
initialDelaySeconds: 2,
},
}],
},
},
},
}).toJSON(),
},
service: {
resource: new Service({
metadata: { name: host, namespace: ns },
spec: {
selector: labels,
ports: [{ name: "http", port: 80, targetPort: 80 }],
},
}).toJSON(),
},
});
condition(rsp, {
type: "WebsiteRenderedRemote",
status: "True",
reason: "ComposedRemote",
message:
`Rendered ${image} from remote URL (replicas=${replicas}) for host=${host}`,
});
await Deno.stdout.write(new TextEncoder().encode(JSON.stringify(rsp)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment