Skip to content

Commit

Permalink
change kube apiserver request header
Browse files Browse the repository at this point in the history
  • Loading branch information
momom-i committed Mar 12, 2022
1 parent 0ea96b1 commit 02b9633
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 120 deletions.
21 changes: 3 additions & 18 deletions web/api/v1/node.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import { V1Node, V1NodeList } from "@kubernetes/client-node";
import { k8sInstance } from "@/api/v1/index";
import axios from "axios";

export const applyNode = async (req: V1Node, onError: (_: string) => void) => {
try {
if (!req.metadata?.name) {
onError("metadata.name is not provided");
return;
}
req.kind = "Node";
req.apiVersion = "v1";
const res = await k8sInstance.patch<V1Node>(
`/nodes/${req.metadata.name}?fieldManager=simulator`,
req,
{ headers: { "Content-Type": "application/strategic-merge-patch+json" } }
{ headers: { "Content-Type": "application/apply-patch+yaml" } }
);
return res.data;
} catch (e: any) {
if (axios.isAxiosError(e) && e.response && e.response.status === 404) {
const res = await createNode(req, onError);
return res;
}
onError("failed to applyNode: " + e);
}
};
Expand Down Expand Up @@ -52,15 +49,3 @@ export const deleteNode = async (
onError("failed to deleteNode: " + e);
}
};

const createNode = async (req: V1Node, onError: (_: string) => void) => {
try {
const res = await k8sInstance.post<V1Node>(
`/nodes?fieldManager=simulator`,
req
);
return res.data;
} catch (e: any) {
onError("failed to createNode: " + e);
}
};
21 changes: 3 additions & 18 deletions web/api/v1/pod.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import { V1Pod, V1PodList } from "@kubernetes/client-node";
import { k8sInstance, namespaceURL } from "@/api/v1/index";
import axios from "axios";

export const applyPod = async (req: V1Pod, onError: (_: string) => void) => {
try {
if (!req.metadata?.name) {
onError("metadata.name is not provided");
return;
}
req.kind = "Pod";
req.apiVersion = "v1";
const res = await k8sInstance.patch<V1Pod>(
namespaceURL + `/pods/${req.metadata.name}?fieldManager=simulator`,
req,
{ headers: { "Content-Type": "application/strategic-merge-patch+json" } }
{ headers: { "Content-Type": "application/apply-patch+yaml" } }
);
return res.data;
} catch (e: any) {
if (axios.isAxiosError(e) && e.response && e.response.status === 404) {
const res = await createPod(req, onError);
return res;
}
onError("failed to applyPod: " + e);
}
};
Expand Down Expand Up @@ -55,15 +52,3 @@ export const deletePod = async (name: string, onError: (_: string) => void) => {
onError("failed to deletePod: " + e);
}
};

const createPod = async (req: V1Pod, onError: (_: string) => void) => {
try {
const res = await k8sInstance.post<V1Pod>(
namespaceURL + `/pods?fieldManager=simulator`,
req
);
return res.data;
} catch (e: any) {
onError("failed to createPod: " + e);
}
};
24 changes: 3 additions & 21 deletions web/api/v1/priorityclass.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { V1PriorityClass, V1PriorityClassList } from "@kubernetes/client-node";
import { k8sSchedulingInstance } from "@/api/v1/index";
import axios from "axios";

export const applyPriorityClass = async (
req: V1PriorityClass,
Expand All @@ -11,17 +10,15 @@ export const applyPriorityClass = async (
onError("metadata.name is not provided");
return;
}
req.kind = "PriorityClass";
req.apiVersion = "scheduling.k8s.io/v1";
const res = await k8sSchedulingInstance.patch<V1PriorityClass>(
`/priorityclasses/${req.metadata.name}?fieldManager=simulator`,
req,
{ headers: { "Content-Type": "application/strategic-merge-patch+json" } }
{ headers: { "Content-Type": "application/apply-patch+yaml" } }
);
return res.data;
} catch (e: any) {
if (axios.isAxiosError(e) && e.response && e.response.status === 404) {
const res = await createPriorityClass(req, onError);
return res;
}
onError("failed to applyPriorityClass: " + e);
}
};
Expand Down Expand Up @@ -67,18 +64,3 @@ export const deletePriorityClass = async (
onError("failed to deletePriorityClass: " + e);
}
};

const createPriorityClass = async (
req: V1PriorityClass,
onError: (_: string) => void
) => {
try {
const res = await k8sSchedulingInstance.post<V1PriorityClass>(
`/priorityclasses?fieldManager=simulator`,
req
);
return res.data;
} catch (e: any) {
onError("failed to createPriorityClass: " + e);
}
};
24 changes: 3 additions & 21 deletions web/api/v1/pv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
V1PersistentVolumeList,
} from "@kubernetes/client-node";
import { k8sInstance } from "@/api/v1/index";
import axios from "axios";

export const applyPersistentVolume = async (
req: V1PersistentVolume,
Expand All @@ -14,17 +13,15 @@ export const applyPersistentVolume = async (
onError("metadata.name is not provided");
return;
}
req.kind = "PersistentVolume";
req.apiVersion = "v1";
const res = await k8sInstance.patch<V1PersistentVolume>(
`/persistentvolumes/${req.metadata.name}?fieldManager=simulator`,
req,
{ headers: { "Content-Type": "application/strategic-merge-patch+json" } }
{ headers: { "Content-Type": "application/apply-patch+yaml" } }
);
return res.data;
} catch (e: any) {
if (axios.isAxiosError(e) && e.response && e.response.status === 404) {
const res = await createPersistentvolumes(req, onError);
return res;
}
onError("failed to applyPersistentVolume: " + e);
}
};
Expand Down Expand Up @@ -67,18 +64,3 @@ export const deletePersistentVolume = async (
onError("failed to deletePersistentVolume: " + e);
}
};

const createPersistentvolumes = async (
req: V1PersistentVolume,
onError: (_: string) => void
) => {
try {
const res = await k8sInstance.post<V1PersistentVolume>(
`/persistentvolumes?fieldManager=simulator`,
req
);
return res.data;
} catch (e: any) {
onError("failed to createPersistentvolumes: " + e);
}
};
24 changes: 3 additions & 21 deletions web/api/v1/pvc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
V1PersistentVolumeClaimList,
} from "@kubernetes/client-node";
import { k8sInstance, namespaceURL } from "@/api/v1/index";
import axios from "axios";

export const applyPersistentVolumeClaim = async (
req: V1PersistentVolumeClaim,
Expand All @@ -14,18 +13,16 @@ export const applyPersistentVolumeClaim = async (
onError("metadata.name is not provided");
return;
}
req.kind = "PersistentVolumeClaim";
req.apiVersion = "v1";
const res = await k8sInstance.patch<V1PersistentVolumeClaim>(
namespaceURL +
`/persistentvolumeclaims/${req.metadata.name}?fieldManager=simulator`,
req,
{ headers: { "Content-Type": "application/strategic-merge-patch+json" } }
{ headers: { "Content-Type": "application/apply-patch+yaml" } }
);
return res.data;
} catch (e: any) {
if (axios.isAxiosError(e) && e.response && e.response.status === 404) {
const res = await createPersistentVolumeClaim(req, onError);
return res;
}
onError("failed to applyPersistentVolumeClaim: " + e);
}
};
Expand Down Expand Up @@ -73,18 +70,3 @@ export const deletePersistentVolumeClaim = async (
onError("failed to deletePersistentVolumeClaim: " + e);
}
};

const createPersistentVolumeClaim = async (
req: V1PersistentVolumeClaim,
onError: (_: string) => void
) => {
try {
const res = await k8sInstance.post<V1PersistentVolumeClaim>(
namespaceURL + `/persistentvolumeclaims?fieldManager=simulator`,
req
);
return res.data;
} catch (e: any) {
onError("failed to createPersistentVolumeClaim: " + e);
}
};
24 changes: 3 additions & 21 deletions web/api/v1/storageclass.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { V1StorageClass, V1StorageClassList } from "@kubernetes/client-node";
import { k8sStorageInstance } from "@/api/v1/index";
import axios from "axios";

export const applyStorageClass = async (
req: V1StorageClass,
Expand All @@ -11,17 +10,15 @@ export const applyStorageClass = async (
onError("metadata.name is not provided");
return;
}
req.kind = "StorageClass";
req.apiVersion = "storage.k8s.io/v1";
const res = await k8sStorageInstance.patch<V1StorageClass>(
`/storageclasses/${req.metadata.name}?fieldManager=simulator`,
req,
{ headers: { "Content-Type": "application/strategic-merge-patch+json" } }
{ headers: { "Content-Type": "application/apply-patch+yaml" } }
);
return res.data;
} catch (e: any) {
if (axios.isAxiosError(e) && e.response && e.response.status === 404) {
const res = await createStorageClass(req, onError);
return res;
}
onError("failed to applyStorageClass: " + e);
}
};
Expand Down Expand Up @@ -64,18 +61,3 @@ export const deleteStorageClass = async (
onError("failed to deleteStorageClass: " + e);
}
};

const createStorageClass = async (
req: V1StorageClass,
onError: (_: string) => void
) => {
try {
const res = await k8sStorageInstance.post<V1StorageClass>(
`/storageclasses?fieldManager=simulator`,
req
);
return res.data;
} catch (e: any) {
onError("failed to createStorageClass: " + e);
}
};

0 comments on commit 02b9633

Please sign in to comment.