Skip to content

Commit

Permalink
feat: added support for deleting objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroen Nijhuis committed Mar 7, 2024
1 parent ee0088e commit 9105689
Show file tree
Hide file tree
Showing 16 changed files with 194 additions and 27 deletions.
22 changes: 9 additions & 13 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
<script setup lang="ts">
import AuthCheck from "./components/AuthCheck.vue";
import AppLayout from "@/components/AppLayout.vue";
import Navigation from "@/components/Navigation.vue";
import RouterViewport from "@/components/RouterViewport.vue";
import Toaster from "@/components/ui/toast/Toaster.vue";
import Loading from "@/components/Loading.vue";
import CommandPalette from "./components/CommandPalette.vue";
import SettingsContextProvider from "./providers/SettingsContextProvider";
import KubeContextProvider from "./providers/KubeContextProvider";
import CommandPaletteProvider from "./providers/CommandPaletteProvider";
import TabProvider from "./providers/TabProvider";
import DialogProvider from "./providers/DialogProvider";
import DialogHandler from "./components/DialogHandler.vue";
</script>

<template>
<AppLayout class="dark bg-zinc-900 text-sm rounded-lg">
<!-- <AuthCheck> -->
<!-- <template #default> -->
<Suspense>
<SettingsContextProvider>
<KubeContextProvider>
<TabProvider>
<CommandPaletteProvider>
<Navigation />
<RouterViewport />
<Toaster />
<CommandPalette />
<DialogProvider>
<Navigation />
<RouterViewport />
<Toaster />
<CommandPalette />
<DialogHandler />
</DialogProvider>
</CommandPaletteProvider>
</TabProvider>
</KubeContextProvider>
</SettingsContextProvider>
</Suspense>
<!-- </template> -->
<!-- <template #loading> -->
<!-- <Loading label="K8s crew, prepare for take-off..." /> -->
<!-- </template> -->
<!-- </AuthCheck> -->
</AppLayout>
</template>
38 changes: 38 additions & 0 deletions src/components/DialogHandler.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<script setup lang="ts">
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { DialogProviderStateKey } from "@/providers/DialogProvider";
import { injectStrict } from "@/lib/utils";
const { dialog } = injectStrict(DialogProviderStateKey);
</script>

<template>
<AlertDialog v-if="dialog" :open="true">
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{{ dialog.title }}</AlertDialogTitle>
<AlertDialogDescription>
{{ dialog.message }}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogAction
v-for="(button, index) in dialog.buttons"
:key="index"
@click="button.handler(dialog)"
>{{ button.label }}</AlertDialogAction
>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</template>
8 changes: 7 additions & 1 deletion src/components/tables/pods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ export const columns: ColumnDef<V1Pod>[] = [
},
{
header: "Status",
accessorFn: (row) => row.status?.phase,
accessorFn: (row) => {
if (row.metadata?.deletionTimestamp) {
return "Terminating";
}

return row.status?.phase;
},
},
{
header: "CPU",
Expand Down
46 changes: 45 additions & 1 deletion src/components/tables/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BaseDialogInterface } from "@/providers/DialogProvider";
import { Command } from "@tauri-apps/api/shell";
import { VirtualService } from "@kubernetes-models/istio/networking.istio.io/v1beta1";
import { KubernetesObject } from "@kubernetes/client-node";

Expand All @@ -19,8 +21,9 @@ export type RowAction<T> = WithOptions<T> | WithHandler<T>;

export function getDefaultActions<T extends KubernetesObject | VirtualService>(
addTab: any,
spawnDialog: any,
context: string,
isGenericResource: boolean = false
isGenericResource = false
): RowAction<T>[] {
return [
{
Expand Down Expand Up @@ -58,5 +61,46 @@ export function getDefaultActions<T extends KubernetesObject | VirtualService>(
);
},
},
{
label: "Delete",
handler: (row) => {
const dialog: BaseDialogInterface = {
title: "Delete",
message: `Are you sure you want to delete ${row.metadata?.name}?`,
buttons: [
{
label: "Cancel",
handler: (dialog) => {
dialog.close();
},
},
{
label: "Delete",
handler: (dialog) => {
const command = new Command("kubectl", [
"delete",
`${row.kind}/${row.metadata?.name}`,
"--context",
context,
"--namespace",
row.metadata?.namespace || "",
]);

command.stderr.on("data", (error: string) => {
console.log(error);
});

command.on("close", () => {
dialog.close();
});

command.spawn();
},
},
],
};
spawnDialog(dialog);
},
},
];
}
46 changes: 46 additions & 0 deletions src/providers/DialogProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { ToRefs } from "vue";

export const DialogProviderStateKey: InjectionKey<ToRefs<DialogProviderState>> =
Symbol("DialogProviderState");

export const DialogProviderSpawnDialogKey: InjectionKey<
(dialog: DialogInterface) => void
> = Symbol("DialogProviderSpawnDialog");

export interface DialogButtonInterface {
label: string;
handler: (dialog: DialogInterface) => void;
}

export interface BaseDialogInterface {
title: string;
message: string;
buttons: DialogButtonInterface[];
}

export interface DialogInterface extends BaseDialogInterface {
close: () => void;
}

export interface DialogProviderState {
dialog: DialogInterface | null;
}

export default {
setup() {
const state: DialogProviderState = reactive({
dialog: null,
});

provide(DialogProviderStateKey, toRefs(state));

const spawnDialog = (dialog: BaseDialogInterface) => {
state.dialog = { ...dialog, close: () => (state.dialog = null) };
};

provide(DialogProviderSpawnDialogKey, spawnDialog);
},
render(): any {
return this.$slots.default();
},
};
5 changes: 4 additions & 1 deletion src/views/ConfigMaps.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import { RowAction, getDefaultActions } from "@/components/tables/types";
import { TabProviderAddTabKey } from "@/providers/TabProvider";
const addTab = injectStrict(TabProviderAddTabKey);
import { DialogProviderSpawnDialogKey } from "@/providers/DialogProvider";
const spawnDialog = injectStrict(DialogProviderSpawnDialogKey);
const rowActions: RowAction<V1ConfigMap>[] = [
...getDefaultActions<V1ConfigMap>(addTab, context.value),
...getDefaultActions<V1ConfigMap>(addTab, spawnDialog, context.value),
];
async function getConfigMaps(refresh: boolean = false) {
Expand Down
5 changes: 4 additions & 1 deletion src/views/CronJobs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import { RowAction, getDefaultActions } from "@/components/tables/types";
import { TabProviderAddTabKey } from "@/providers/TabProvider";
const addTab = injectStrict(TabProviderAddTabKey);
import { DialogProviderSpawnDialogKey } from "@/providers/DialogProvider";
const spawnDialog = injectStrict(DialogProviderSpawnDialogKey);
const rowActions: RowAction<V1CronJob>[] = [
...getDefaultActions<V1CronJob>(addTab, context.value),
...getDefaultActions<V1CronJob>(addTab, spawnDialog, context.value),
];
async function getCronJobs(refresh: boolean = false) {
Expand Down
7 changes: 5 additions & 2 deletions src/views/Deployments.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const { context, namespace } = injectStrict(KubeContextStateKey);
import { TabProviderAddTabKey } from "@/providers/TabProvider";
const addTab = injectStrict(TabProviderAddTabKey);
import { DialogProviderSpawnDialogKey } from "@/providers/DialogProvider";
const spawnDialog = injectStrict(DialogProviderSpawnDialogKey);
import DataTable from "@/components/ui/DataTable.vue";
import { RowAction, getDefaultActions } from "@/components/tables/types";
import { columns } from "@/components/tables/deployments";
Expand All @@ -20,8 +23,8 @@ const { toast } = useToast();
const deployments = ref<V1Deployment[]>([]);
const rowActions: RowAction<V1Deployment>[] = [
...getDefaultActions<V1Deployment>(addTab, context.value),
{
...getDefaultActions<V1Deployment>(addTab, spawnDialog, context.value),
{
label: "Logs",
handler: (row) => {
addTab(
Expand Down
5 changes: 4 additions & 1 deletion src/views/GenericResource.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ import { RowAction, getDefaultActions } from "@/components/tables/types";
import { TabProviderAddTabKey } from "@/providers/TabProvider";
const addTab = injectStrict(TabProviderAddTabKey);
import { DialogProviderSpawnDialogKey } from "@/providers/DialogProvider";
const spawnDialog = injectStrict(DialogProviderSpawnDialogKey);
const rowActions: RowAction<any>[] = [
...getDefaultActions<any>(addTab, context.value, true),
...getDefaultActions<any>(addTab, spawnDialog, context.value, true),
];
onBeforeRouteUpdate((to, from, next) => {
Expand Down
5 changes: 4 additions & 1 deletion src/views/Ingresses.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import { RowAction, getDefaultActions } from "@/components/tables/types";
import { TabProviderAddTabKey } from "@/providers/TabProvider";
const addTab = injectStrict(TabProviderAddTabKey);
import { DialogProviderSpawnDialogKey } from "@/providers/DialogProvider";
const spawnDialog = injectStrict(DialogProviderSpawnDialogKey);
const rowActions: RowAction<V1Ingress>[] = [
...getDefaultActions<V1Ingress>(addTab, context.value),
...getDefaultActions<V1Ingress>(addTab, spawnDialog, context.value),
];
async function getIngresses(refresh: boolean = false) {
Expand Down
5 changes: 4 additions & 1 deletion src/views/Jobs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import { RowAction, getDefaultActions } from "@/components/tables/types";
import { TabProviderAddTabKey } from "@/providers/TabProvider";
const addTab = injectStrict(TabProviderAddTabKey);
import { DialogProviderSpawnDialogKey } from "@/providers/DialogProvider";
const spawnDialog = injectStrict(DialogProviderSpawnDialogKey);
const rowActions: RowAction<V1Job>[] = [
...getDefaultActions<V1Job>(addTab, context.value),
...getDefaultActions<V1Job>(addTab, spawnDialog, context.value),
];
async function getJobs(refresh: boolean = false) {
Expand Down
9 changes: 8 additions & 1 deletion src/views/PersistentVolumeClaims.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ import { RowAction, getDefaultActions } from "@/components/tables/types";
import { TabProviderAddTabKey } from "@/providers/TabProvider";
const addTab = injectStrict(TabProviderAddTabKey);
import { DialogProviderSpawnDialogKey } from "@/providers/DialogProvider";
const spawnDialog = injectStrict(DialogProviderSpawnDialogKey);
const rowActions: RowAction<V1PersistentVolumeClaim>[] = [
...getDefaultActions<V1PersistentVolumeClaim>(addTab, context.value),
...getDefaultActions<V1PersistentVolumeClaim>(
addTab,
spawnDialog,
context.value
),
];
async function getPersistentVolumeClaims(refresh: boolean = false) {
Expand Down
5 changes: 4 additions & 1 deletion src/views/Pods.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ import { TabProviderAddTabKey } from "@/providers/TabProvider";
const { context, namespace } = injectStrict(KubeContextStateKey);
const addTab = injectStrict(TabProviderAddTabKey);
import { DialogProviderSpawnDialogKey } from "@/providers/DialogProvider";
const spawnDialog = injectStrict(DialogProviderSpawnDialogKey);
const { toast } = useToast();
const pods = ref<V1Pod[]>([]);
const rowActions: RowAction<V1Pod>[] = [
...getDefaultActions<V1Pod>(addTab, context.value),
...getDefaultActions<V1Pod>(addTab, spawnDialog, context.value),
{
label: "Shell",
options: (row) => {
Expand Down
5 changes: 4 additions & 1 deletion src/views/Secrets.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import { RowAction, getDefaultActions } from "@/components/tables/types";
import { TabProviderAddTabKey } from "@/providers/TabProvider";
const addTab = injectStrict(TabProviderAddTabKey);
import { DialogProviderSpawnDialogKey } from "@/providers/DialogProvider";
const spawnDialog = injectStrict(DialogProviderSpawnDialogKey);
const rowActions: RowAction<V1Secret>[] = [
...getDefaultActions<V1Secret>(addTab, context.value),
...getDefaultActions<V1Secret>(addTab, spawnDialog, context.value),
];
async function getConfigMaps(refresh: boolean = false) {
Expand Down
5 changes: 4 additions & 1 deletion src/views/Services.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import { RowAction, getDefaultActions } from "@/components/tables/types";
import { TabProviderAddTabKey } from "@/providers/TabProvider";
const addTab = injectStrict(TabProviderAddTabKey);
import { DialogProviderSpawnDialogKey } from "@/providers/DialogProvider";
const spawnDialog = injectStrict(DialogProviderSpawnDialogKey);
const rowActions: RowAction<V1Service>[] = [
...getDefaultActions<V1Service>(addTab, context.value),
...getDefaultActions<V1Service>(addTab, spawnDialog, context.value),
];
async function getServices(refresh: boolean = false) {
Expand Down
5 changes: 4 additions & 1 deletion src/views/VirtualServices.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import { RowAction, getDefaultActions } from "@/components/tables/types";
import { TabProviderAddTabKey } from "@/providers/TabProvider";
const addTab = injectStrict(TabProviderAddTabKey);
import { DialogProviderSpawnDialogKey } from "@/providers/DialogProvider";
const spawnDialog = injectStrict(DialogProviderSpawnDialogKey);
const rowActions: RowAction<VirtualService>[] = [
...getDefaultActions<VirtualService>(addTab, context.value),
...getDefaultActions<VirtualService>(addTab, spawnDialog, context.value),
];
async function getVirtualServices(refresh: boolean = false) {
Expand Down

0 comments on commit 9105689

Please sign in to comment.