Skip to content

Commit

Permalink
feature(Kubernetes) : Cleaned Shulker_utils.ts & making all post requ…
Browse files Browse the repository at this point in the history
…est endpoint
  • Loading branch information
mrallan140 committed Dec 6, 2023
1 parent 952a61b commit 9774144
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,32 @@ const kc = new k8s.KubeConfig();
kc.loadFromDefault();
const k8sCRDApi = kc.makeApiClient(k8s.CustomObjectsApi);
export default defineEventHandler(async (event) => {
const name = getRouterParam(event, "name") || "Qraft-Proxyfleet";
const body = await readBody(event);
if (!body) {
throw createError({
statusCode: 400,
statusMessage: "Bad request",
message: "Missing body",
});
}
if (!body.name) {
throw createError({
statusCode: 400,
statusMessage: "Bad request",
message: "Missing MinecraftCluster name",
});
}

const name = body.name;
const spec: shulker.MinecraftClusterSpec =
body.spec || new shulker.MinecraftClusterSpec();
const metadata = new shulker.ShulkerMetadata(name);

const Deploy_MinecraftCluster = new shulker.MinecraftCluster(metadata);
//console.log(JSON.stringify(Deploy_MinecraftCluster, null, 4));
const Deploy_MinecraftCluster = new shulker.MinecraftCluster(
metadata,
spec,
);
console.log(JSON.stringify(Deploy_MinecraftCluster, null, 4));
const Deployments_Result = await k8sCRDApi.createNamespacedCustomObject(
"shulkermc.io",
"v1alpha1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,32 @@ const kc = new k8s.KubeConfig();
kc.loadFromDefault();
const k8sCRDApi = kc.makeApiClient(k8s.CustomObjectsApi);
export default defineEventHandler(async (event) => {
const name = getRouterParam(event, "name") || "Qraft-Proxyfleet";
const body = await readBody(event);
if (!body) {
throw createError({
statusCode: 400,
statusMessage: "Bad request",
message: "Missing body",
});
}
if (!body.name || !body.clusterRef) {
throw createError({
statusCode: 400,
statusMessage: "Bad request",
message: "Missing ProxyFleet parameters",
});
}
const name = body.name;
const clusterRef = body.clusterRef;
const metadata = new shulker.ShulkerMetadata(name);
const spec: shulker.MinecraftServerFleetSpec =
new shulker.MinecraftServerFleetSpec(
new shulker.ClusterRef(clusterRef),
);

const deployMinecraftServerFleet = new shulker.MinecraftServerFleet(
metadata,
spec,
);
console.log(JSON.stringify(deployMinecraftServerFleet, null, 4));
const Deployments_Result = await k8sCRDApi.createNamespacedCustomObject(
Expand Down
53 changes: 53 additions & 0 deletions app/server/api/kubernetes/deploy/proxyfleet.post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import k8s from "@kubernetes/client-node";
import * as shulker from "~/server/utils/Shulker_utils";

const kc = new k8s.KubeConfig();
kc.loadFromDefault();
const k8sCRDApi = kc.makeApiClient(k8s.CustomObjectsApi);
export default defineEventHandler(async (event) => {
const body = await readBody(event);
if (!body) {
throw createError({
statusCode: 400,
statusMessage: "Bad request",
message: "Missing body",
});
}
if (!body.name || !body.clusterRef) {
throw createError({
statusCode: 400,
statusMessage: "Bad request",
message: "Missing ProxyFleet parameters",
});
}

const clusterRef = new shulker.ClusterRef(body.clusterRef);
const metadata = new shulker.ShulkerMetadata(body.name);

const service: shulker.ProxyFleetSpecService =
body.services || new shulker.ProxyFleetSpecService();
const templateSpec: shulker.ProxyFleetSpecTemplateSpec =
new shulker.ProxyFleetSpecTemplateSpec(body.version);
const template: shulker.ProxyFleetSpecTemplate =
new shulker.ProxyFleetSpecTemplate(templateSpec);

const spec: shulker.ProxyFleetSpec = new shulker.ProxyFleetSpec(
clusterRef,
service,
template,
);

const Deploy_ProxyFleet = new shulker.ProxyFleet(metadata, spec);
//console.log(JSON.stringify(body, null, 4));
//console.log(JSON.stringify(Deploy_ProxyFleet, null, 4));
const Deployments_Result = await k8sCRDApi.createNamespacedCustomObject(
"shulkermc.io",
"v1alpha1",
Deploy_ProxyFleet.metadata.namespace,
"proxyfleets",
Deploy_ProxyFleet,
);
return {
deploy: Deployments_Result,
};
});
24 changes: 0 additions & 24 deletions app/server/api/kubernetes/deploy_proxyfleet/[name].get.ts

This file was deleted.

5 changes: 5 additions & 0 deletions app/server/api/kubernetes/test.post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default defineEventHandler(async (event) => {
const body = await readBody(event);
console.log(body);
return { body };
});
75 changes: 39 additions & 36 deletions app/server/utils/Shulker_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,26 @@ class ProxyFleetSpecService {
public externalTrafficPolicy: string = "Local",
) {}
}
class ProxyFleetSpecClusterRef {
class ClusterRef {
constructor(public name: string) {}
}
class ProxyFleetSpec {
constructor(
public clusterRef: ProxyFleetSpecClusterRef,
public replicas: number = 1,
public services: ProxyFleetSpecService = new ProxyFleetSpecService(),
public clusterRef: ClusterRef,
public service: ProxyFleetSpecService = new ProxyFleetSpecService(),
public template: ProxyFleetSpecTemplate = new ProxyFleetSpecTemplate(),
public replicas: number = 1,
) {
// Initialize other properties if needed
}
}

class ProxyFleetStatus {
constructor(public replicas?: number | null) {
// Initialize other properties if needed
}
}

class ProxyFleet {
constructor(
public metadata: ShulkerMetadata,
public spec: ProxyFleetSpec,
public apiVersion: string = "shulkermc.io/v1alpha1",
public kind: string = "ProxyFleet",
public status?: ProxyFleetStatus,
) {}
}

Expand All @@ -61,42 +54,53 @@ class MinecraftServerSpec {
}
}

class MinecraftServerStatus {
constructor(public replicas?: number | null) {
// Initialize other properties if needed
}
}

class MinecraftServer {
constructor(
public metadata: ShulkerMetadata,
public apiVersion: string = "shulkermc.io/v1alpha1",
public kind: string = "MinecraftServer",
public spec?: MinecraftServerSpec,
public status?: MinecraftServerStatus,
) {}
}
class MinecraftServerFleetSpecTemplateSpecVersion {
constructor(
public channel: string = "Paper",
public name: string = "1.20.2",
) {}
}

// MinecraftServerFleet classes
class MinecraftServerFleetSpec {
constructor(public replicas?: number | null) {
// Initialize other properties if needed
}
class MinecraftServerFleetSpecTemplateSpec {
constructor(
public clusterRef: ClusterRef,
public tags: [] = [],
public version: MinecraftServerFleetSpecTemplateSpecVersion = new MinecraftServerFleetSpecTemplateSpecVersion(),
public config: object = {},
) {}
}

class MinecraftServerFleetStatus {
constructor(public replicas?: number | null) {
class MinecraftServerFleetSpecTemplate {
constructor(public spec: MinecraftServerFleetSpecTemplateSpec) {}
}

// MinecraftServerFleet classes
class MinecraftServerFleetSpec {
constructor(
public clusterRef: ClusterRef,
public template: MinecraftServerFleetSpecTemplate = new MinecraftServerFleetSpecTemplate(
new MinecraftServerFleetSpecTemplateSpec(clusterRef),
),
public replicas: number = 1,
) {
// Initialize other properties if needed
}
}

class MinecraftServerFleet {
constructor(
public metadata: ShulkerMetadata,
public spec: MinecraftServerFleetSpec,
public apiVersion: string = "shulkermc.io/v1alpha1",
public kind: string = "MinecraftServerFleet",
public spec?: MinecraftServerFleetSpec,
public status?: MinecraftServerFleetStatus,
) {}
}

Expand All @@ -107,31 +111,30 @@ class MinecraftClusterSpec {
}
}

class MinecraftClusterStatus {}

class MinecraftCluster {
constructor(
public metadata: ShulkerMetadata,
public spec: MinecraftClusterSpec = {},
public apiVersion: string = "shulkermc.io/v1alpha1",
public kind: string = "MinecraftCluster",
public spec: MinecraftClusterSpec = {},
public status?: MinecraftClusterStatus,
) {}
}

export {
ShulkerMetadata,
ProxyFleet,
ProxyFleetSpec,
ProxyFleetSpecClusterRef,
ProxyFleetStatus,
ClusterRef,
ProxyFleetSpecService,
ProxyFleetSpecTemplateSpec,
ProxyFleetSpecTemplate,
MinecraftServer,
MinecraftServerSpec,
MinecraftServerStatus,
MinecraftServerFleet,
MinecraftServerFleetSpecTemplate,
MinecraftServerFleetSpecTemplateSpec,
MinecraftServerFleetSpecTemplateSpecVersion,
MinecraftServerFleetSpec,
MinecraftServerFleetStatus,
MinecraftCluster,
MinecraftClusterSpec,
MinecraftClusterStatus,
};

0 comments on commit 9774144

Please sign in to comment.