Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: image usage by containers #5663

Merged
merged 1 commit into from Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions packages/renderer/src/lib/image/image-utils.spec.ts
Expand Up @@ -21,6 +21,7 @@ import { ImageUtils } from './image-utils';
import type { ImageInfo } from '../../../../main/src/plugin/api/image-info';
import { ContextUI } from '../context/context';
import type { ViewInfoUI } from '../../../../main/src/plugin/api/view-info';
import type { ContainerInfo } from '../../../../main/src/plugin/api/container-info';

let imageUtils: ImageUtils;

Expand Down Expand Up @@ -137,3 +138,48 @@ test('should expect badge to be valid value with context/view set', async () =>
expect(badges[0].label).toBe('my-custom-badge');
expect(badges[0].color).toBe('#ff0000');
});

describe('inUse', () => {
const imageInfoHello = {
Id: 'sha256:1b10fa0fd8d184d9de22a553688af8f9f8adbabb11f5dfc15f1a0fdd21873db2',
RepoTags: ['quay.io/podman/hello3:latest', 'quay.io/podman/hello2:latest', 'quay.io/podman/hello:latest'],
} as unknown as ImageInfo;

const untaggedImageInfo = {
Id: 'sha256:1b10fa0fd8d184d9de22a553688af8f9f8adbabb11f5dfc15f1a0fdd21873db2',
} as unknown as ImageInfo;

const containerInfo = {
Id: 'container1',
Image: 'quay.io/podman/hello:latest',
ImageID: 'sha256:1b10fa0fd8d184d9de22a553688af8f9f8adbabb11f5dfc15f1a0fdd21873db2',
} as unknown as ContainerInfo;

test('should expect inUsed with an untagged image', async () => {
const containerInfo = {
Id: 'container1',
Image: 'sha256:1b10fa0fd8d184d9de22a553688af8f9f8adbabb11f5dfc15f1a0fdd21873db2',
ImageID: 'sha256:1b10fa0fd8d184d9de22a553688af8f9f8adbabb11f5dfc15f1a0fdd21873db2',
} as unknown as ContainerInfo;

const isUsed = imageUtils.getInUse(untaggedImageInfo, undefined, [containerInfo]);
// image should be used
expect(isUsed).toBeTruthy();
});

test('should not expect inUsed without a containerInfo', async () => {
const isUsed = imageUtils.getInUse(imageInfoHello);
// image should not be used
expect(isUsed).toBeFalsy();
});

test.each([
['quay.io/podman/hello:latest', true],
['quay.io/podman/hello2:latest', false],
['quay.io/podman/hello3:latest', false],
])('should expect different inUsed based on repoTag %s', async (repoTag: string, expected: boolean) => {
const isUsed = imageUtils.getInUse(imageInfoHello, repoTag, [containerInfo]);
// image should be used
expect(isUsed).toBe(expected);
});
});
16 changes: 12 additions & 4 deletions packages/renderer/src/lib/image/image-utils.ts
Expand Up @@ -90,11 +90,19 @@ export class ImageUtils {

// is that this image is used by a container or not
// search if there is a container matching this image
getInUse(imageInfo: ImageInfo, containersInfo?: ContainerInfo[]): boolean {
getInUse(imageInfo: ImageInfo, repositoryTag?: string, containersInfo?: ContainerInfo[]): boolean {
if (!containersInfo) {
return false;
}
return containersInfo.some(container => container.ImageID === imageInfo.Id);

// if there is a container with the same image id and the same repository tag, it's in use
// else check if we have an untagged ilmage and in that case we check that container is matching the image id
return containersInfo.some(container => {
return (
(container.ImageID === imageInfo.Id && container.Image === repositoryTag) ||
(!!repositoryTag === false && imageInfo.Id.includes(container.Image) && (imageInfo.RepoTags ?? []).length === 0)
);
});
}

computeBagdes(
Expand Down Expand Up @@ -176,7 +184,7 @@ export class ImageUtils {
tag: '',
base64RepoTag: this.getBase64EncodedName('<none>'),
selected: false,
inUse: this.getInUse(imageInfo, containersInfo),
inUse: this.getInUse(imageInfo, undefined, containersInfo),
badges,
icon,
labels: imageInfo.Labels,
Expand All @@ -197,7 +205,7 @@ export class ImageUtils {
tag: this.getTag(repoTag),
base64RepoTag: this.getBase64EncodedName(repoTag),
selected: false,
inUse: this.getInUse(imageInfo, containersInfo),
inUse: this.getInUse(imageInfo, repoTag, containersInfo),
badges,
icon,
labels: imageInfo.Labels,
Expand Down