Skip to content

Commit

Permalink
feat: propose indexed name for new pod
Browse files Browse the repository at this point in the history
Signed-off-by: Vladyslav Zhukovskyi <[email protected]>
  • Loading branch information
vzhukovs committed Jun 28, 2023
1 parent 5a9f772 commit 6f6ba3c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
16 changes: 15 additions & 1 deletion packages/renderer/src/lib/ContainerList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ import { faChevronDown, faChevronRight } from '@fortawesome/free-solid-svg-icons
import Fa from 'svelte-fa/src/fa.svelte';
import ErrorMessage from './ui/ErrorMessage.svelte';
import { podCreationHolder } from '../stores/creation-from-containers-store';
import { podsInfos } from '../stores/pods';
import KubePlayButton from './kube/KubePlayButton.svelte';
import Prune from './engine/Prune.svelte';
import type { EngineInfoUI } from './engine/EngineInfoUI';
import { containerGroupsInfo } from '../stores/containerGroups';
import Checkbox from './ui/Checkbox.svelte';
import type { PodInfo } from '../../../main/src/plugin/api/pod-info';
import { PodUtils } from '../lib/pod/pod-utils';
const containerUtils = new ContainerUtils();
let openChoiceModal = false;
Expand Down Expand Up @@ -183,8 +186,10 @@ function createPodFromContainers() {
.flat()
.filter(container => container.selected);
const podUtils = new PodUtils();
const podCreation = {
name: 'my-pod',
name: podUtils.calculateNewPodName(pods),
containers: selectedContainers.map(container => {
return { id: container.id, name: container.name, engineId: container.engineId, ports: container.ports };
}),
Expand All @@ -198,6 +203,8 @@ function createPodFromContainers() {
}
let containersUnsubscribe: Unsubscriber;
let podUnsubscribe: Unsubscriber;
let pods: PodInfo[];
onMount(async () => {
// grab previous groups
containerGroups = get(containerGroupsInfo);
Expand Down Expand Up @@ -256,6 +263,10 @@ onMount(async () => {
// compute refresh interval
const interval = computeInterval();
refreshTimeouts.push(setTimeout(refreshUptime, interval));
podUnsubscribe = podsInfos.subscribe(podInfos => {
pods = podInfos;
});
});
});
Expand All @@ -271,6 +282,9 @@ onDestroy(() => {
if (containersUnsubscribe) {
containersUnsubscribe();
}
if (podUnsubscribe) {
podUnsubscribe();
}
});
function openDetailsContainer(container: ContainerInfoUI) {
Expand Down
31 changes: 30 additions & 1 deletion packages/renderer/src/lib/pod/pod-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import { test, expect } from 'vitest';
import { ensureRestrictedSecurityContext } from '/@/lib/pod/pod-utils';
import { ensureRestrictedSecurityContext, PodUtils } from '/@/lib/pod/pod-utils';
import type { PodInfo } from '../../../../main/src/plugin/api/pod-info';

function verifyPodSecurityContext(containers: any[], type = 'RuntimeDefault') {
containers.forEach(container => {
Expand Down Expand Up @@ -79,3 +80,31 @@ test('Expect security context to be added to dual containers pod', async () => {
ensureRestrictedSecurityContext(pod);
verifyPodSecurityContext(pod.spec.containers);
});

test('Expect return a valid name for a new pod', () => {
const podUtils = new PodUtils();
const newPodName = podUtils.calculateNewPodName();

expect(newPodName).toBe('my-pod');
});

test('Expect return a valid name for a new pod if there is a pod with the same name', () => {
const podUtils = new PodUtils();
const newPodName = podUtils.calculateNewPodName([{ Name: 'my-pod' } as PodInfo]);

expect(newPodName).toBe('my-pod-1');
});

test('Expect return a valid name for a new pod if there is a pods with different names', () => {
const podUtils = new PodUtils();
const newPodName = podUtils.calculateNewPodName([{ Name: 'my-super-pod' } as PodInfo]);

expect(newPodName).toBe('my-pod');
});

test('Expect return a valid name for a new pod if there are pods with the same name', () => {
const podUtils = new PodUtils();
const newPodName = podUtils.calculateNewPodName([{ Name: 'my-pod' } as PodInfo, { Name: 'my-pod-1' } as PodInfo]);

expect(newPodName).toBe('my-pod-2');
});
22 changes: 22 additions & 0 deletions packages/renderer/src/lib/pod/pod-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ export class PodUtils {
kind: podinfo.kind,
};
}

calculateNewPodName(existedPods?: PodInfo[]) {
const proposedPodName = 'my-pod';

if (!existedPods) {
return proposedPodName;
}

const existedNames = existedPods.map(pod => pod.Name);

if (!existedNames.includes(proposedPodName)) {
return proposedPodName;
} else {
let count = 1;
let uniqueName = `${proposedPodName}-${count}`;
while (existedNames.includes(uniqueName)) {
count++;
uniqueName = `${proposedPodName}-${count}`;
}
return uniqueName;
}
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down

0 comments on commit 6f6ba3c

Please sign in to comment.