Skip to content

Commit

Permalink
refactor createService
Browse files Browse the repository at this point in the history
  • Loading branch information
adb-sh committed Nov 10, 2024
1 parent 7604223 commit 3d5cbb3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
36 changes: 19 additions & 17 deletions src/components/cloud/CreateServiceForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ const createGithubWebhook = async (namespace: string, service) => {
auth: account.access_token,
});
return await ok.repos.createWebhook({
owner: service.ghPackageOwner,
repo: service.ghPackageRepo,
owner: service.annotations["apps.deploycat.io/gh-package-owner"],
repo: service.annotations["apps.deploycat.io/gh-package-repo"],
config: {
url: `${config?.publicurl}/api/webhooks/github/apps/${namespace}/${service.name}/package`,
content_type: "json",
secret: service.webhookSecret,
secret: service.annotations["apps.deploycat.io/gh-webhook-secret"],
},
events: ["package"],
active: true,
Expand All @@ -75,15 +75,10 @@ const createGithubWebhook = async (namespace: string, service) => {

const createServiceFromForm = async (form: FormData) => {
"use server";
const source = form.get("source") as string;
const service = {
name: form.get("name") as string,
source: form.get("source") as string,
image: form.get("image") as string,
ghPackage: form.get("ghPackage") as string,
ghPackageTag: form.get("ghPackageTag") as string,
ghPackageName: form.get("ghPackageName") as string,
ghPackageOwner: form.get("ghPackageOwner") as string,
ghPackageRepo: form.get("ghPackageRepo") as string,
port: Number(form.get("port")) as number,
resources: {
cpuLimit: toNumber(form.get("cpuLimit")),
Expand All @@ -94,26 +89,33 @@ const createServiceFromForm = async (form: FormData) => {
maxRequests: toNumber(form.get("maxRequests")),
},
envVars: JSON.parse(form.get("env") as string) as { [key: string]: string },
annotations: {
"apps.deploycat.io/gh-package": form.get("ghPackage"),
"apps.deploycat.io/gh-package-repo": form.get("ghPackageRepo"),
"apps.deploycat.io/gh-package-name": form.get("ghPackageName"),
"apps.deploycat.io/gh-package-owner": form.get("ghPackageOwner"),
"apps.deploycat.io/gh-package-tag": form.get("ghPackageTag"),
},
} as Service;

const user = await getUser();
if (service?.source === "ghcr") {
if (source === "ghcr") {
try {
service.webhookSecret = crypto.randomBytes(16).toString("hex");
await ensureGithubPullSecret(user.name);
service.annotations["apps.deploycat.io/gh-webhook-secret"] = crypto
.randomBytes(16)
.toString("hex");
const { data: hook } = await createGithubWebhook(user.name, service);
service.annotations = {
"apps.deploycat.io/gh-webhook-id": hook.id.toString(),
"apps.deploycat.io/gh-repo": service.ghPackageRepo,
};
service.annotations["apps.deploycat.io/gh-webhook-id"] =
hook.id.toString();
} catch (e) {
console.error(e);
}
service.image = `ghcr.io/${service.ghPackageOwner}/${service.ghPackageName}:${service.ghPackageTag}`;
service.image = `ghcr.io/${service.annotations["apps.deploycat.io/gh-package-owner"]}/${service.annotations["apps.deploycat.io/gh-package-name"]}:${service.annotations["apps.deploycat.io/gh-package-tag"]}`;
service.pullSecret = "pull-secret-ghcr";
}
try {
await knative.createService(service, user.name, service.source);
await knative.createService(service, user.name, source);
} catch (e) {
console.error(e);
}
Expand Down
6 changes: 4 additions & 2 deletions src/lib/knative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import k8s from "@kubernetes/client-node";
export type Service = {
name: string;
image: string;
pullSecret?: string;
port: number;
resources: {
cpuLimit: number;
Expand All @@ -14,6 +15,7 @@ export type Service = {
};
envVars: { [key: string]: string };
raw?: any;
annotations: { [key: string]: string };
};

export const toNumber = (value: any) => value && Number(value);
Expand Down Expand Up @@ -55,6 +57,7 @@ const toKnService = (service: any) =>
value,
]) ?? []
),
annotations: service.metadata.annotations,
raw: service,
} as Service);

Expand Down Expand Up @@ -126,9 +129,8 @@ export class Knative {
"app.kubernetes.io/managed-by": "deploycat",
},
annotations: {
"apps.deploycat.io/webhook-secret": service.webhookSecret,
"apps.deploycat.io/source": source,
...(service.annotations ?? {}),
...service.annotations,
},
},
spec: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function POST({ params, request }: APIEvent) {

const currentService = await knative.getService(app, namespace);
const secret =
currentService.raw.metadata.annotations["apps.deploycat.io/webhook-secret"];
currentService.raw.metadata.annotations["apps.deploycat.io/gh-webhook-secret"];

const wh = new Webhooks({ secret });
const signature = request.headers.get("x-hub-signature-256");
Expand Down
2 changes: 1 addition & 1 deletion src/routes/cloud/apps/[app].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const deleteServiceFromForm = async (form: FormData) => {
await ok.repos.deleteWebhook({
owner: user.name,
repo: currentService.raw.metadata.annotations[
"apps.deploycat.io/gh-repo"
"apps.deploycat.io/gh-package-repo"
],
hook_id: Number(
currentService.raw.metadata.annotations[
Expand Down

0 comments on commit 3d5cbb3

Please sign in to comment.