Skip to content

Commit

Permalink
Pull upstream changes to support images with digest (#3721)
Browse files Browse the repository at this point in the history
  • Loading branch information
bwateratmsft authored Nov 28, 2022
1 parent 5df232c commit add1dd7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { ImageNameInfo } from '../../contracts/ContainerClient';
*
* Tag: Everything after the ":", if it is present.
*/
const imageNameRegex = /^((?<registry>(localhost|([\w-]+(\.[\w-]+)+))(:\d+)?)\/)?(?<image>[\w-./<>]+)(:(?<tag>[\w-.<>]+))?$/;
const imageNameRegex = /^((?<registry>(localhost|([\w-]+(\.[\w-]+)+))(:\d+)?)\/)?(?<image>[\w-./<>]+)(:(?<tag>[\w-.<>]+))?(@(?<digest>.+))?$/;

// In certain cases, Docker makes image/tag names "<none>", which is not really valid. We will reinterpret those as `undefined`.
const noneImageName = /[<>]/i;
Expand All @@ -43,12 +43,13 @@ export function parseDockerLikeImageName(originalName: string | undefined): Imag
throw new Error('Invalid image name');
}

const { registry, image, tag } = match.groups;
const { registry, image, tag, digest } = match.groups;

return {
originalName,
image: noneImageName.test(image) ? undefined : image,
tag: noneImageName.test(tag) ? undefined : tag,
digest,
registry,
};
}
4 changes: 4 additions & 0 deletions src/runtimes/docker/contracts/ContainerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export interface ImageNameInfo {
* The tag/anchor name. If absent, this will be undefined.
*/
readonly tag?: string;
/**
* The digest. If absent, this will be undefined.
*/
readonly digest?: string;
}

export type Labels = {
Expand Down
48 changes: 48 additions & 0 deletions src/runtimes/docker/test/parseDockerLikeImageName.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,31 @@ describe('parseDockerLikeImageName', () => {
originalName: 'alpine',
image: 'alpine',
tag: undefined,
digest: undefined,
registry: undefined,
});

expect(parseDockerLikeImageName('hello-world')).to.deep.equal({
originalName: 'hello-world',
image: 'hello-world',
tag: undefined,
digest: undefined,
registry: undefined,
});

expect(parseDockerLikeImageName('hello_world')).to.deep.equal({
originalName: 'hello_world',
image: 'hello_world',
tag: undefined,
digest: undefined,
registry: undefined,
});

expect(parseDockerLikeImageName('library/alpine')).to.deep.equal({
originalName: 'library/alpine',
image: 'library/alpine',
tag: undefined,
digest: undefined,
registry: undefined,
});
});
Expand All @@ -43,13 +47,25 @@ describe('parseDockerLikeImageName', () => {
originalName: 'alpine:latest',
image: 'alpine',
tag: 'latest',
digest: undefined,
registry: undefined,
});

expect(parseDockerLikeImageName('library/alpine:5')).to.deep.equal({
originalName: 'library/alpine:5',
image: 'library/alpine',
tag: '5',
digest: undefined,
registry: undefined,
});
});

it('Should parse those with a digest', () => {
expect(parseDockerLikeImageName('kindest/node:v1.24.0@sha256:0866296e693efe1fed79d5e6c7af8df71fc73ae45e3679af05342239cdc5bc8e')).to.deep.equal({
originalName: 'kindest/node:v1.24.0@sha256:0866296e693efe1fed79d5e6c7af8df71fc73ae45e3679af05342239cdc5bc8e',
image: 'kindest/node',
tag: 'v1.24.0',
digest: 'sha256:0866296e693efe1fed79d5e6c7af8df71fc73ae45e3679af05342239cdc5bc8e',
registry: undefined,
});
});
Expand All @@ -59,34 +75,63 @@ describe('parseDockerLikeImageName', () => {
originalName: 'docker.io/library/alpine:latest',
image: 'library/alpine',
tag: 'latest',
digest: undefined,
registry: 'docker.io',
});

expect(parseDockerLikeImageName('localhost/alpine:latest')).to.deep.equal({
originalName: 'localhost/alpine:latest',
image: 'alpine',
tag: 'latest',
digest: undefined,
registry: 'localhost',
});

expect(parseDockerLikeImageName('with-a.port:5000/library/alpine:latest')).to.deep.equal({
originalName: 'with-a.port:5000/library/alpine:latest',
image: 'library/alpine',
tag: 'latest',
digest: undefined,
registry: 'with-a.port:5000',
});

expect(parseDockerLikeImageName('1.2.3.4:5000/library/alpine:latest')).to.deep.equal({
originalName: '1.2.3.4:5000/library/alpine:latest',
image: 'library/alpine',
tag: 'latest',
digest: undefined,
registry: '1.2.3.4:5000',
});

expect(parseDockerLikeImageName('localhost:5000/alpine:latest')).to.deep.equal({
originalName: 'localhost:5000/alpine:latest',
image: 'alpine',
tag: 'latest',
digest: undefined,
registry: 'localhost:5000',
});

expect(parseDockerLikeImageName('with-a.port:5000/library/alpine:latest@sha256:0866296e693efe1fed79d5e6c7af8df71fc73ae45e3679af05342239cdc5bc8e')).to.deep.equal({
originalName: 'with-a.port:5000/library/alpine:latest@sha256:0866296e693efe1fed79d5e6c7af8df71fc73ae45e3679af05342239cdc5bc8e',
image: 'library/alpine',
tag: 'latest',
digest: 'sha256:0866296e693efe1fed79d5e6c7af8df71fc73ae45e3679af05342239cdc5bc8e',
registry: 'with-a.port:5000',
});

expect(parseDockerLikeImageName('1.2.3.4:5000/library/alpine:latest@sha256:0866296e693efe1fed79d5e6c7af8df71fc73ae45e3679af05342239cdc5bc8e')).to.deep.equal({
originalName: '1.2.3.4:5000/library/alpine:latest@sha256:0866296e693efe1fed79d5e6c7af8df71fc73ae45e3679af05342239cdc5bc8e',
image: 'library/alpine',
tag: 'latest',
digest: 'sha256:0866296e693efe1fed79d5e6c7af8df71fc73ae45e3679af05342239cdc5bc8e',
registry: '1.2.3.4:5000',
});

expect(parseDockerLikeImageName('localhost:5000/alpine:latest@sha256:0866296e693efe1fed79d5e6c7af8df71fc73ae45e3679af05342239cdc5bc8e')).to.deep.equal({
originalName: 'localhost:5000/alpine:latest@sha256:0866296e693efe1fed79d5e6c7af8df71fc73ae45e3679af05342239cdc5bc8e',
image: 'alpine',
tag: 'latest',
digest: 'sha256:0866296e693efe1fed79d5e6c7af8df71fc73ae45e3679af05342239cdc5bc8e',
registry: 'localhost:5000',
});
});
Expand All @@ -97,20 +142,23 @@ describe('parseDockerLikeImageName', () => {
originalName: '<none>:<none>',
image: undefined,
tag: undefined,
digest: undefined,
registry: undefined,
});

expect(parseDockerLikeImageName('<none>')).to.deep.equal({
originalName: '<none>',
image: undefined,
tag: undefined,
digest: undefined,
registry: undefined,
});

expect(parseDockerLikeImageName('alpine:<none>')).to.deep.equal({
originalName: 'alpine:<none>',
image: 'alpine',
tag: undefined,
digest: undefined,
registry: undefined,
});
});
Expand Down

0 comments on commit add1dd7

Please sign in to comment.