Skip to content

Commit

Permalink
Fixes other usages of site ns
Browse files Browse the repository at this point in the history
Signed-off-by: Marcos Candeia <[email protected]>
  • Loading branch information
mcandeia committed Jan 12, 2024
1 parent b3fc215 commit b8be6b1
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 24 deletions.
16 changes: 9 additions & 7 deletions platforms/kubernetes/actions/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
};
}
7 changes: 4 additions & 3 deletions platforms/kubernetes/actions/deployments/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions platforms/kubernetes/actions/domains/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -48,7 +49,7 @@ export default async function newDomain(
kind: "DomainMapping",
metadata: {
name: domain,
namespace: Namespace.forSite(site),
namespace: siteNs,
},
spec: {
ref: {
Expand All @@ -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,
),
Expand Down
6 changes: 4 additions & 2 deletions platforms/kubernetes/actions/domains/delete.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
),
Expand Down
8 changes: 5 additions & 3 deletions platforms/kubernetes/actions/siteState/upsert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,15 +21,16 @@ export default async function setSiteState(
): Promise<void> {
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,
));
}
8 changes: 5 additions & 3 deletions platforms/kubernetes/actions/sites/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion platforms/kubernetes/actions/sites/delete.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { k8s } from "../../deps.ts";
import { AppContext } from "../../mod.ts";
import { Namespace } from "./create.ts";

export interface Props {
site: string;
Expand All @@ -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));
}
4 changes: 3 additions & 1 deletion platforms/kubernetes/loaders/siteState/get.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -90,9 +91,10 @@ export default async function getSiteState(
ctx: AppContext,
): Promise<SiteState | undefined> {
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
Expand Down

0 comments on commit b8be6b1

Please sign in to comment.