From b8be6b16ad8001ee4f0ef4815677954953a5389d Mon Sep 17 00:00:00 2001 From: Marcos Candeia Date: Fri, 12 Jan 2024 20:35:34 -0300 Subject: [PATCH] Fixes other usages of site ns Signed-off-by: Marcos Candeia --- platforms/kubernetes/actions/build.ts | 16 +++++++++------- .../kubernetes/actions/deployments/create.ts | 7 ++++--- platforms/kubernetes/actions/domains/create.ts | 9 +++++---- platforms/kubernetes/actions/domains/delete.ts | 6 ++++-- platforms/kubernetes/actions/siteState/upsert.ts | 8 +++++--- platforms/kubernetes/actions/sites/create.ts | 8 +++++--- platforms/kubernetes/actions/sites/delete.ts | 3 ++- platforms/kubernetes/loaders/siteState/get.ts | 4 +++- 8 files changed, 37 insertions(+), 24 deletions(-) diff --git a/platforms/kubernetes/actions/build.ts b/platforms/kubernetes/actions/build.ts index 5adff7528..0c0ce5ed4 100644 --- a/platforms/kubernetes/actions/build.ts +++ b/platforms/kubernetes/actions/build.ts @@ -231,6 +231,7 @@ export default async function build( if (!builderImg) { badRequest({ message: "builder image is required" }); } + const siteNs = Namespace.forSite(site); const batchAPI = ctx.kc.makeApiClient(k8s.BatchV1Api); const binder = SrcBinder.fromRepo(owner, repo, commitSha); // Define the Job specification @@ -249,20 +250,21 @@ export default async function build( }); await batchAPI.createNamespacedJob( - site, + siteNs, job, ).catch(ignoreIfExists); const getBuildStatusFn = () => - getBuildStatus(batchAPI, site, job.metadata!.name!); + getBuildStatus(batchAPI, siteNs, job.metadata!.name!); return { sourceBinder: binder, wait: (timeout?: number) => - watchJobStatus(ctx.kc, site, jobName, timeout).then(buildStatusOf).catch( - () => { - return "running" as const; - }, - ), + watchJobStatus(ctx.kc, siteNs, jobName, timeout).then(buildStatusOf) + .catch( + () => { + return "running" as const; + }, + ), getBuildStatus: getBuildStatusFn, }; } diff --git a/platforms/kubernetes/actions/deployments/create.ts b/platforms/kubernetes/actions/deployments/create.ts index 809f2244a..17f5d3857 100644 --- a/platforms/kubernetes/actions/deployments/create.ts +++ b/platforms/kubernetes/actions/deployments/create.ts @@ -220,6 +220,7 @@ export default async function newDeployment( if (!source) { badRequest({ message: "source is required" }); } + const siteNs = Namespace.forSite(site); const { owner, repo, commitSha } = source!; if (build) { @@ -256,7 +257,7 @@ export default async function newDeployment( envVars: siteState.envVars, sourceBinder, site, - namespace: Namespace.forSite(site), + namespace: siteNs, deploymentId, labels, scaling: scaling ?? { initialScale: 0, maxScale: 3, minScale: 0 }, @@ -296,12 +297,12 @@ export default async function newDeployment( await k8sApi.createNamespacedCustomObject( "serving.knative.dev", "v1", - site, + siteNs, "routes", routeOf({ routeName: deploymentRoute, revisionName, - namespace: Namespace.forSite(site), + namespace: siteNs, }), ).catch(ignoreIfExists).catch((err) => { console.error("creating site route error", err); diff --git a/platforms/kubernetes/actions/domains/create.ts b/platforms/kubernetes/actions/domains/create.ts index 96078de9c..fe57bec34 100644 --- a/platforms/kubernetes/actions/domains/create.ts +++ b/platforms/kubernetes/actions/domains/create.ts @@ -18,12 +18,13 @@ export default async function newDomain( _req: Request, ctx: AppContext, ) { + const siteNs = Namespace.forSite(site); const certificateManifest = { apiVersion: "cert-manager.io/v1", kind: "Certificate", metadata: { name: domain, - namespace: Namespace.forSite(site), + namespace: siteNs, }, spec: { commonName: "selfsigned-ca", @@ -48,7 +49,7 @@ export default async function newDomain( kind: "DomainMapping", metadata: { name: domain, - namespace: Namespace.forSite(site), + namespace: siteNs, }, spec: { ref: { @@ -68,14 +69,14 @@ export default async function newDomain( k8sApi.createNamespacedCustomObject( "cert-manager.io", "v1", - site, + siteNs, "certificates", certificateManifest, ), k8sApi.createNamespacedCustomObject( "serving.knative.dev", "v1beta1", - site, + siteNs, "domainmappings", domainMappingManifest, ), diff --git a/platforms/kubernetes/actions/domains/delete.ts b/platforms/kubernetes/actions/domains/delete.ts index 9c9cb441d..8d7f6966b 100644 --- a/platforms/kubernetes/actions/domains/delete.ts +++ b/platforms/kubernetes/actions/domains/delete.ts @@ -1,6 +1,7 @@ import { badRequest } from "deco/mod.ts"; import { k8s } from "../../deps.ts"; import { AppContext } from "../../mod.ts"; +import { Namespace } from "../sites/create.ts"; export interface Props { site: string; @@ -17,19 +18,20 @@ export default async function deleteDomain( ctx: AppContext, ) { const k8sApi = ctx.kc.makeApiClient(k8s.CustomObjectsApi); + const siteNs = Namespace.forSite(site); const [_certificate, _domainMapping, currentSiteState] = await Promise.all([ k8sApi.deleteNamespacedCustomObject( "cert-manager.io", "v1", - site, + siteNs, "certificates", domain, ), k8sApi.deleteNamespacedCustomObject( "serving.knative.dev", "v1beta1", - site, + siteNs, "domainmappings", domain, ), diff --git a/platforms/kubernetes/actions/siteState/upsert.ts b/platforms/kubernetes/actions/siteState/upsert.ts index aa93eea89..00138abe9 100644 --- a/platforms/kubernetes/actions/siteState/upsert.ts +++ b/platforms/kubernetes/actions/siteState/upsert.ts @@ -2,6 +2,7 @@ import { ignoreIfExists } from "../../common/objects.ts"; import { k8s } from "../../deps.ts"; import { SiteState, State } from "../../loaders/siteState/get.ts"; import { AppContext } from "../../mod.ts"; +import { Namespace } from "../sites/create.ts"; export interface Props { site: string; @@ -20,15 +21,16 @@ export default async function setSiteState( ): Promise { const k8sApi = ctx.kc.makeApiClient(k8s.CoreV1Api); const releaseName = State.secretName; - const siteSecret = State.toSecret(site, state); + const siteNs = Namespace.forSite(site); + const siteSecret = State.toSecret(siteNs, state); await (create ? k8sApi.createNamespacedSecret( - site, + siteNs, siteSecret, ).catch(ignoreIfExists) : k8sApi.replaceNamespacedSecret( releaseName, - site, + siteNs, siteSecret, )); } diff --git a/platforms/kubernetes/actions/sites/create.ts b/platforms/kubernetes/actions/sites/create.ts index db6f1497a..2c00c2dc0 100644 --- a/platforms/kubernetes/actions/sites/create.ts +++ b/platforms/kubernetes/actions/sites/create.ts @@ -81,13 +81,15 @@ export default async function newSite( }; }); + const siteNs = Namespace.forSite(site); + await corev1Api.createNamespace({ - metadata: { name: Namespace.forSite(site) }, + metadata: { name: siteNs }, }).catch(ignoreIfExists); const [secretEnvVar] = await Promise.all([ secretEnvVarPromise, - corev1Api.createNamespacedPersistentVolumeClaim(site, { - metadata: { name: DECO_SITES_PVC, namespace: Namespace.forSite(site) }, + corev1Api.createNamespacedPersistentVolumeClaim(siteNs, { + metadata: { name: DECO_SITES_PVC, namespace: siteNs }, spec: { accessModes: ["ReadWriteMany"], storageClassName: EFS_SC, diff --git a/platforms/kubernetes/actions/sites/delete.ts b/platforms/kubernetes/actions/sites/delete.ts index 25a4c2c38..12c09f580 100644 --- a/platforms/kubernetes/actions/sites/delete.ts +++ b/platforms/kubernetes/actions/sites/delete.ts @@ -1,5 +1,6 @@ import { k8s } from "../../deps.ts"; import { AppContext } from "../../mod.ts"; +import { Namespace } from "./create.ts"; export interface Props { site: string; @@ -16,5 +17,5 @@ export default async function deleteSite( ) { const corev1Api = ctx.kc.makeApiClient(k8s.CoreV1Api); - await corev1Api.deleteNamespace(site); + await corev1Api.deleteNamespace(Namespace.forSite(site)); } diff --git a/platforms/kubernetes/loaders/siteState/get.ts b/platforms/kubernetes/loaders/siteState/get.ts index 61529039a..b07637ad8 100644 --- a/platforms/kubernetes/loaders/siteState/get.ts +++ b/platforms/kubernetes/loaders/siteState/get.ts @@ -1,5 +1,6 @@ import { Domain } from "../../../../admin/platform.ts"; import { EnvVar } from "../../actions/deployments/create.ts"; +import { Namespace } from "../../actions/sites/create.ts"; import { k8s } from "../../deps.ts"; import { AppContext } from "../../mod.ts"; @@ -90,9 +91,10 @@ export default async function getSiteState( ctx: AppContext, ): Promise { const k8sApi = ctx.kc.makeApiClient(k8s.CoreV1Api); + const siteNs = Namespace.forSite(site); const secret = await k8sApi.readNamespacedSecret( State.secretName, - site, + siteNs, ).catch(async (err) => { if ((err as k8s.HttpError)?.statusCode === 404) { await ctx.invoke.kubernetes.actions.sites.create({ site }); // create site on 404