-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathimageNameUtils.ts
More file actions
73 lines (67 loc) · 3.26 KB
/
imageNameUtils.ts
File metadata and controls
73 lines (67 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type { ContainerRegistryManagementClient, Registry } from "@azure/arm-containerregistry";
import { uiUtils } from "@microsoft/vscode-azext-azureutils";
import { ISubscriptionActionContext } from "@microsoft/vscode-azext-utils";
import { SupportedRegistries, acrDomain, dockerHubDomain } from "../constants";
import { createContainerRegistryManagementClient } from "./azureClients";
interface ParsedImageName {
imageNameReference?: string;
registryDomain?: SupportedRegistries;
registryName?: string;
namespace?: string;
repositoryName?: string;
tag?: string;
}
/**
* @param imageName The full image name, including any registry, namespace, repository, and tag
*
* @example
* Format: '<registryName>/<...namespaces>/<repositoryName>:<tag>'
* ACR: 'acrRegistryName.azurecr.io/repositoryName:tag'
* DH: 'docker.io/namespace/repositoryName:tag'
*
* @returns A 'ParsedImageName' with the following properties:
* (1) 'imageNameReference': The original full image name;
* (2) 'registryDomain': The 'SupportedRegistries' domain, if it can be determined from the 'registryName';
* (3) 'registryName': Everything before the first slash;
* (4) 'namespace': Everything between the 'registryName' and the 'repositoryName', including intermediate slashes;
* (5) 'repositoryName': Everything after the last slash (until the tag, if it is present);
* (6) 'tag': Everything after the ":", if it is present
*/
export function parseImageName(imageName?: string): ParsedImageName {
if (!imageName) {
return {};
}
const match: RegExpMatchArray | null = imageName.match(/^(?:(?<registryName>[^/]+)\/)?(?:(?<namespace>[^/]+(?:\/[^/]+)*)\/)?(?<repositoryName>[^/:]+)(?::(?<tag>[^/]+))?$/);
return {
imageNameReference: imageName,
registryDomain: match?.groups?.registryName ? detectRegistryDomain(match.groups.registryName) : undefined,
registryName: match?.groups?.registryName,
namespace: match?.groups?.namespace,
repositoryName: match?.groups?.repositoryName,
tag: match?.groups?.tag
};
}
/**
* @param registryName When parsed from a full image name, everything before the first slash
*/
export function detectRegistryDomain(registryName: string): SupportedRegistries | undefined {
if (/\.azurecr\.io$/i.test(registryName)) {
return acrDomain;
} else if (/^docker\.io$/i.test(registryName)) {
return dockerHubDomain;
} else {
return undefined;
}
}
/**
* @param acrName When parsed from a full ACR image name, everything before the first slash
*/
export async function getRegistryFromAcrName(context: ISubscriptionActionContext, acrName: string): Promise<Registry> {
const client: ContainerRegistryManagementClient = await createContainerRegistryManagementClient(context);
const registries = await uiUtils.listAllIterator(client.registries.list());
return registries.find(r => r.loginServer === acrName) as Registry;
}