diff --git a/docker/Dockerfile.frontend b/docker/Dockerfile.frontend index 34a4b7d..7bd778c 100644 --- a/docker/Dockerfile.frontend +++ b/docker/Dockerfile.frontend @@ -52,7 +52,7 @@ RUN wget -qO- "https://github.com/tx3-lang/trix/releases/download/${TRIX_VERSION chmod +x /root/.tx3/default/bin/trix ENV PREVIEW_API_URL=https://8000-skillful-employee-kb9ou6.us1.demeter.run -ENV MAINNET_API_URL=https://8000-ethereal-audience-bb83g6.us1.demeter.run +ENV MAINNET_API_URL=https://asteria.txpipe.io/api ENV PREVIEW_TRP_URL=https://cardano-preview.trp-m1.demeter.run ENV PREVIEW_TRP_API_KEY=trpjodqbmjblunzpbikpcrl diff --git a/frontend/src/components/how-to-play/create-ship/Description.mdx b/frontend/src/components/how-to-play/create-ship/Description.mdx index 372c59d..65f1fc7 100644 --- a/frontend/src/components/how-to-play/create-ship/Description.mdx +++ b/frontend/src/components/how-to-play/create-ship/Description.mdx @@ -14,7 +14,7 @@ This transaction creates a `ShipState` UTxO locking min ada, a `ShipToken` (mint #### You can query the next available ship and pilot token names using the following curl: ``` -curl --location 'https://8000-ethereal-audience-bb83g6.us1.demeter.run/graphql' \ +curl --location 'https://asteria.txpipe.io/api/graphql' \ --header 'Content-Type: application/json' \ --data '{"query":"query { nextShipTokenName(spacetimePolicyId: \"0291ae7aebaf064b785542093c2b13169effb34462301e68d4b44f43\", spacetimeAddress: \"addr1wypfrtn6awhsvjmc24pqj0ptzvtfalang33rq8ng6j6y7scnlkytx\") { shipName pilotName } }","variables":{}}' ``` @@ -22,7 +22,7 @@ curl --location 'https://8000-ethereal-audience-bb83g6.us1.demeter.run/graphql' #### You can query the tip of the chain using the following curl: ``` -curl --location 'https://8000-ethereal-audience-bb83g6.us1.demeter.run/graphql' \ +curl --location 'https://asteria.txpipe.io/api/graphql' \ --header 'Content-Type: application/json' \ --data '{"query":"query { lastSlot { slot } }","variables":{}}' ``` diff --git a/frontend/src/components/how-to-play/create-ship/Section.tsx b/frontend/src/components/how-to-play/create-ship/Section.tsx index 10f0028..ca4f58a 100644 --- a/frontend/src/components/how-to-play/create-ship/Section.tsx +++ b/frontend/src/components/how-to-play/create-ship/Section.tsx @@ -181,7 +181,7 @@ export default function CreateShip(props: CreateShipProps) { } const handleFetchShipNumber = async () => { - const shipRes = await (await fetch("https://8000-ethereal-audience-bb83g6.us1.demeter.run/graphql", { + const shipRes = await (await fetch("https://asteria.txpipe.io/api/graphql", { "headers": { "content-type": "application/json" }, "body": "{\"query\":\"query { nextShipTokenName(spacetimePolicyId: \\\"0291ae7aebaf064b785542093c2b13169effb34462301e68d4b44f43\\\", spacetimeAddress: \\\"addr1wypfrtn6awhsvjmc24pqj0ptzvtfalang33rq8ng6j6y7scnlkytx\\\") { shipName pilotName } }\",\"variables\":{}}", "method": "POST" @@ -197,7 +197,7 @@ export default function CreateShip(props: CreateShipProps) { } const handleFetchLastSlot = async () => { - const slotRes = await (await fetch("https://8000-ethereal-audience-bb83g6.us1.demeter.run/graphql", { + const slotRes = await (await fetch("https://asteria.txpipe.io/api/graphql", { "headers": { "content-type": "application/json" }, "body": "{\"query\":\"query { lastSlot { slot } }\"}", "method": "POST" diff --git a/frontend/src/components/how-to-play/gather-fuel/Section.tsx b/frontend/src/components/how-to-play/gather-fuel/Section.tsx index 060b505..2a54c6f 100644 --- a/frontend/src/components/how-to-play/gather-fuel/Section.tsx +++ b/frontend/src/components/how-to-play/gather-fuel/Section.tsx @@ -142,7 +142,7 @@ export default function GatherFuel(props: GatherFuelProps) { } const handleFetchLastSlot = async () => { - const slotRes = await (await fetch("https://8000-ethereal-audience-bb83g6.us1.demeter.run/graphql", { + const slotRes = await (await fetch("https://asteria.txpipe.io/api/graphql", { "headers": { "content-type": "application/json" }, "body": "{\"query\":\"query { lastSlot { slot } }\"}", "method": "POST" diff --git a/frontend/src/components/how-to-play/gather-token/Section.tsx b/frontend/src/components/how-to-play/gather-token/Section.tsx index 1721172..3e72cc8 100644 --- a/frontend/src/components/how-to-play/gather-token/Section.tsx +++ b/frontend/src/components/how-to-play/gather-token/Section.tsx @@ -152,7 +152,7 @@ export default function GatherToken(props: GatherTokenProps) { } const handleFetchLastSlot = async () => { - const slotRes = await (await fetch("https://8000-ethereal-audience-bb83g6.us1.demeter.run/graphql", { + const slotRes = await (await fetch("https://asteria.txpipe.io/api/graphql", { "headers": { "content-type": "application/json" }, "body": "{\"query\":\"query { lastSlot { slot } }\"}", "method": "POST" diff --git a/frontend/src/components/how-to-play/mine-asteria/Section.tsx b/frontend/src/components/how-to-play/mine-asteria/Section.tsx index a4b0d81..a670f6d 100644 --- a/frontend/src/components/how-to-play/mine-asteria/Section.tsx +++ b/frontend/src/components/how-to-play/mine-asteria/Section.tsx @@ -155,7 +155,7 @@ export default function MineAsteria(props: MineAsteriaProps) { } const handleFetchLastSlot = async () => { - const slotRes = await (await fetch("https://8000-ethereal-audience-bb83g6.us1.demeter.run/graphql", { + const slotRes = await (await fetch("https://asteria.txpipe.io/api/graphql", { "headers": { "content-type": "application/json" }, "body": "{\"query\":\"query { lastSlot { slot } }\"}", "method": "POST" diff --git a/frontend/src/components/how-to-play/move-ship/Section.tsx b/frontend/src/components/how-to-play/move-ship/Section.tsx index 50232d2..ed80632 100644 --- a/frontend/src/components/how-to-play/move-ship/Section.tsx +++ b/frontend/src/components/how-to-play/move-ship/Section.tsx @@ -185,7 +185,7 @@ export default function MoveShip(props: MoveShipProps) { } const handleFetchLastSlot = async () => { - const slotRes = await (await fetch("https://8000-ethereal-audience-bb83g6.us1.demeter.run/graphql", { + const slotRes = await (await fetch("https://asteria.txpipe.io/api/graphql", { "headers": { "content-type": "application/json" }, "body": "{\"query\":\"query { lastSlot { slot } }\"}", "method": "POST" diff --git a/frontend/src/components/how-to-play/quit-game/Section.tsx b/frontend/src/components/how-to-play/quit-game/Section.tsx index af681ce..ac718f1 100644 --- a/frontend/src/components/how-to-play/quit-game/Section.tsx +++ b/frontend/src/components/how-to-play/quit-game/Section.tsx @@ -134,7 +134,7 @@ export default function QuitGame(props: QuitGameProps) { } const handleFetchLastSlot = async () => { - const slotRes = await (await fetch("https://8000-ethereal-audience-bb83g6.us1.demeter.run/graphql", { + const slotRes = await (await fetch("https://asteria.txpipe.io/api/graphql", { "headers": { "content-type": "application/json" }, "body": "{\"query\":\"query { lastSlot { slot } }\"}", "method": "POST" diff --git a/k8s/README.md b/k8s/README.md new file mode 100644 index 0000000..b330444 --- /dev/null +++ b/k8s/README.md @@ -0,0 +1,23 @@ +# Kubernetes + +`chart/` deploys the Asteria challenge services from the images the workflows +publish to ghcr.io: + +- **backend**: the GraphQL API (`backend/`, `ghcr.io/txpipe/asteria-backend`). + It reads chain state from a Blockfrost-compatible endpoint, set with + `backend.blockfrostBaseUrl`. +- **frontend**: the game site (`frontend/`, `ghcr.io/txpipe/asteria-frontend`), + disabled by default. Its backend URLs are inlined when the image is built, so + pointing it at another backend means building a new image. + +The chart assumes no cluster, hostname, chain endpoint or image tag. Each +instance supplies them in its own values file, and the chart refuses to render +without them: + +```sh +helm lint chart --values values.my-instance.yaml +helm template asteria chart --namespace asteria --values values.my-instance.yaml +``` + +Both images are built for `linux/amd64` only, so placement must land on x86 +nodes. The backend runs as a non-root user on a read-only root filesystem. diff --git a/k8s/chart/Chart.yaml b/k8s/chart/Chart.yaml new file mode 100644 index 0000000..91cda10 --- /dev/null +++ b/k8s/chart/Chart.yaml @@ -0,0 +1,11 @@ +apiVersion: v2 +name: asteria +description: The Asteria challenge services, the GraphQL backend and the game frontend +type: application +# Chart version: this chart. appVersion: not tracked; each component's image +# is pinned in the instance values. +version: 0.1.0 +home: https://github.com/txpipe/asteria +keywords: + - asteria + - cardano diff --git a/k8s/chart/templates/NOTES.txt b/k8s/chart/templates/NOTES.txt new file mode 100644 index 0000000..b60de1f --- /dev/null +++ b/k8s/chart/templates/NOTES.txt @@ -0,0 +1,20 @@ +{{ .Chart.Name }} {{ .Chart.Version }} +{{ if .Values.backend.enabled }} +Backend ({{ .Values.backend.image.tag }}): + in-cluster: http://{{ include "asteria.componentName" (dict "root" . "component" "backend") }}.{{ .Release.Namespace }}.svc.cluster.local:{{ .Values.backend.service.port }}/graphql +{{- if .Values.backend.ingress.enabled }} + public: https://{{ .Values.backend.ingress.host }}{{ .Values.backend.ingress.path }} +{{- end }} +{{- end }} +{{ if .Values.frontend.enabled }} +Frontend ({{ .Values.frontend.image.tag }}): + in-cluster: http://{{ include "asteria.componentName" (dict "root" . "component" "frontend") }}.{{ .Release.Namespace }}.svc.cluster.local:{{ .Values.frontend.service.port }}/ +{{- if .Values.frontend.ingress.enabled }} + public: https://{{ .Values.frontend.ingress.host }}{{ .Values.frontend.ingress.path }} +{{- end }} +{{- end }} + +Smoke test: + kubectl -n {{ .Release.Namespace }} port-forward svc/{{ include "asteria.componentName" (dict "root" . "component" "backend") }} 18000:{{ .Values.backend.service.port }} + curl -s http://localhost:18000/graphql -H 'content-type: application/json' \ + -d '{"query":"{ __typename }"}' # {"data":{"__typename":"QueryRoot"}} diff --git a/k8s/chart/templates/_helpers.tpl b/k8s/chart/templates/_helpers.tpl new file mode 100644 index 0000000..ee5a4c8 --- /dev/null +++ b/k8s/chart/templates/_helpers.tpl @@ -0,0 +1,61 @@ +{{- define "asteria.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}} +{{- end -}} + +{{- define "asteria.fullname" -}} +{{- if .Values.fullnameOverride -}} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- $name := default .Chart.Name .Values.nameOverride -}} +{{- if contains $name .Release.Name -}} +{{- .Release.Name | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}} +{{- end -}} +{{- end -}} +{{- end -}} + +{{- define "asteria.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}} +{{- end -}} + +{{/* Name of one component's objects: "-". */}} +{{- define "asteria.componentName" -}} +{{- printf "%s-%s" (include "asteria.fullname" .root) .component | trunc 63 | trimSuffix "-" -}} +{{- end -}} + +{{- define "asteria.selectorLabels" -}} +app.kubernetes.io/name: {{ include "asteria.name" .root }} +app.kubernetes.io/instance: {{ .root.Release.Name }} +app.kubernetes.io/component: {{ .component }} +{{- end -}} + +{{- define "asteria.labels" -}} +helm.sh/chart: {{ include "asteria.chart" .root }} +{{ include "asteria.selectorLabels" . }} +app.kubernetes.io/version: {{ .values.image.tag | trunc 63 | quote }} +app.kubernetes.io/managed-by: {{ .root.Release.Service }} +{{- end -}} + +{{- define "asteria.image" -}} +{{- $img := .values.image -}} +{{- $repository := required (printf "%s.image.repository is required" .component) $img.repository -}} +{{- $tag := required (printf "%s.image.tag is required" .component) $img.tag -}} +{{- if $img.digest -}} +{{- printf "%s:%s@%s" $repository $tag $img.digest -}} +{{- else -}} +{{- printf "%s:%s" $repository $tag -}} +{{- end -}} +{{- end -}} + +{{/* A component's nodeSelector, falling back to the shared one. */}} +{{- define "asteria.placement" -}} +{{- with (default .root.Values.nodeSelector .values.nodeSelector) }} +nodeSelector: + {{- toYaml . | nindent 2 }} +{{- end }} +{{- with (default .root.Values.tolerations .values.tolerations) }} +tolerations: + {{- toYaml . | nindent 2 }} +{{- end }} +{{- end -}} diff --git a/k8s/chart/templates/_workload.tpl b/k8s/chart/templates/_workload.tpl new file mode 100644 index 0000000..c1e537f --- /dev/null +++ b/k8s/chart/templates/_workload.tpl @@ -0,0 +1,120 @@ +{{/* +One component's Deployment, Service and optional Ingress. The caller passes +root (the chart context), component (its name), values (its values block), +env (the environment the component needs, which wins over values.env) and +probe (a probe handler). +*/}} +{{- define "asteria.workload" -}} +{{- $name := include "asteria.componentName" . -}} +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ $name }} + namespace: {{ .root.Release.Namespace }} + labels: + {{- include "asteria.labels" . | nindent 4 }} +spec: + replicas: {{ .values.replicaCount }} + selector: + matchLabels: + {{- include "asteria.selectorLabels" . | nindent 6 }} + template: + metadata: + labels: + {{- include "asteria.selectorLabels" . | nindent 8 }} + spec: + {{- with include "asteria.placement" . | trim }} + {{- . | nindent 6 }} + {{- end }} + {{- with .values.podSecurityContext }} + securityContext: + {{- toYaml . | nindent 8 }} + {{- end }} + containers: + - name: {{ .component }} + image: {{ include "asteria.image" . }} + imagePullPolicy: {{ .values.image.pullPolicy }} + ports: + - name: http + containerPort: {{ .values.port | int }} + env: + {{- range $key, $value := merge (dict) .env .values.env }} + - name: {{ $key }} + value: {{ $value | toString | quote }} + {{- end }} + {{- with .values.existingSecret }} + envFrom: + - secretRef: + name: {{ . }} + {{- end }} + readinessProbe: + {{- toYaml .probe | nindent 12 }} + periodSeconds: 10 + livenessProbe: + {{- toYaml .probe | nindent 12 }} + periodSeconds: 20 + failureThreshold: 3 + {{- with .values.resources }} + resources: + {{- toYaml . | nindent 12 }} + {{- end }} + {{- with .values.securityContext }} + securityContext: + {{- toYaml . | nindent 12 }} + {{- end }} +--- +apiVersion: v1 +kind: Service +metadata: + name: {{ $name }} + namespace: {{ .root.Release.Namespace }} + labels: + {{- include "asteria.labels" . | nindent 4 }} + {{- with .values.service.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + type: {{ .values.service.type }} + selector: + {{- include "asteria.selectorLabels" . | nindent 4 }} + ports: + - name: http + port: {{ .values.service.port }} + targetPort: http +{{- with .values.ingress }} +{{- if .enabled }} +{{- $host := required (printf "%s.ingress.host is required" $.component) .host }} +--- +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: {{ $name }} + namespace: {{ $.root.Release.Namespace }} + labels: + {{- include "asteria.labels" $ | nindent 4 }} + annotations: + cert-manager.io/cluster-issuer: {{ required (printf "%s.ingress.clusterIssuer is required" $.component) .clusterIssuer }} + {{- with .annotations }} + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + ingressClassName: {{ required (printf "%s.ingress.className is required" $.component) .className }} + rules: + - host: {{ $host }} + http: + paths: + - path: {{ .path }} + pathType: {{ .pathType }} + backend: + service: + name: {{ $name }} + port: + name: http + tls: + - hosts: + - {{ $host }} + secretName: {{ required (printf "%s.ingress.tlsSecretName is required" $.component) .tlsSecretName }} +{{- end }} +{{- end }} +{{- end -}} diff --git a/k8s/chart/templates/backend.yaml b/k8s/chart/templates/backend.yaml new file mode 100644 index 0000000..41b5d5f --- /dev/null +++ b/k8s/chart/templates/backend.yaml @@ -0,0 +1,11 @@ +{{- if .Values.backend.enabled }} +{{- $values := .Values.backend }} +{{- $env := dict + "BLOCKFROST_BASE_URL" (required "backend.blockfrostBaseUrl is required" $values.blockfrostBaseUrl) + "ROCKET_ADDRESS" "0.0.0.0" + "ROCKET_PORT" $values.port +}} +{{- $probe := dict "httpGet" (dict "path" "/" "port" "http") }} +--- +{{ include "asteria.workload" (dict "root" . "component" "backend" "values" $values "env" $env "probe" $probe) }} +{{- end }} diff --git a/k8s/chart/templates/frontend.yaml b/k8s/chart/templates/frontend.yaml new file mode 100644 index 0000000..e152d57 --- /dev/null +++ b/k8s/chart/templates/frontend.yaml @@ -0,0 +1,10 @@ +{{- if .Values.frontend.enabled }} +{{- $values := .Values.frontend }} +{{- $env := dict + "HOSTNAME" "0.0.0.0" + "PORT" $values.port +}} +{{- $probe := dict "tcpSocket" (dict "port" "http") }} +--- +{{ include "asteria.workload" (dict "root" . "component" "frontend" "values" $values "env" $env "probe" $probe) }} +{{- end }} diff --git a/k8s/chart/values.yaml b/k8s/chart/values.yaml new file mode 100644 index 0000000..7968dad --- /dev/null +++ b/k8s/chart/values.yaml @@ -0,0 +1,114 @@ +# The Asteria challenge: the GraphQL backend (backend/) and the Next.js game +# frontend (frontend/), each built by its own workflow into ghcr.io. +# +# These defaults are instance-agnostic: no cluster, hostname, chain endpoint +# or image is assumed. What an instance must supply is empty here and +# `required` in the templates, so a missing value fails at render instead of +# deploying a half-configured service. + +nameOverride: "" +fullnameOverride: "" + +# Placement shared by both components; a component's own nodeSelector or +# tolerations replace these. Both images are built for linux/amd64 only. +nodeSelector: {} +tolerations: [] + +backend: + enabled: true + image: + repository: ghcr.io/txpipe/asteria-backend + # The workflows tag every image with the full commit SHA. Pin a tag and + # its digest; never a moving tag. + tag: "" + digest: "" + pullPolicy: IfNotPresent + replicaCount: 1 + port: 8000 + # A Blockfrost-compatible API root (Blockfrost itself, or Dolos minibf). + blockfrostBaseUrl: "" + # Optional Secret whose keys become environment variables, e.g. + # DMTR_API_KEY or BLOCKFROST_PROJECT_ID for an authenticated endpoint. + existingSecret: "" + # Extra plain environment variables. + env: {} + nodeSelector: {} + tolerations: [] + resources: + requests: + cpu: 100m + memory: 128Mi + limits: + memory: 512Mi + podSecurityContext: + runAsNonRoot: true + runAsUser: 65532 + runAsGroup: 65532 + seccompProfile: + type: RuntimeDefault + securityContext: + allowPrivilegeEscalation: false + readOnlyRootFilesystem: true + capabilities: + drop: ["ALL"] + service: + type: ClusterIP + port: 8000 + annotations: {} + ingress: + enabled: false + className: "" + clusterIssuer: "" + host: "" + # The backend answers on /graphql. A path other than / needs the ingress + # to map it back, e.g. through annotations for the controller in use. + path: / + pathType: Prefix + tlsSecretName: "" + annotations: {} + +frontend: + enabled: false + image: + repository: ghcr.io/txpipe/asteria-frontend + tag: "" + digest: "" + pullPolicy: IfNotPresent + replicaCount: 1 + port: 3000 + # The backend URLs (MAINNET_API_URL, PREVIEW_API_URL) are inlined at image + # build time by next.config.mjs, so changing them means a new image. Server + # side settings, such as the TRP endpoints and keys, are read at runtime + # and can be set here or through existingSecret. + existingSecret: "" + env: {} + nodeSelector: {} + tolerations: [] + resources: + requests: + cpu: 100m + memory: 256Mi + limits: + memory: 1Gi + podSecurityContext: + seccompProfile: + type: RuntimeDefault + # The image runs as root and Next.js writes its cache under /app/.next, so + # the root filesystem stays writable. + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: ["ALL"] + service: + type: ClusterIP + port: 3000 + annotations: {} + ingress: + enabled: false + className: "" + clusterIssuer: "" + host: "" + path: / + pathType: Prefix + tlsSecretName: "" + annotations: {}