mirror of
https://github.com/Azure/setup-kubectl.git
synced 2026-05-22 17:02:04 +00:00
feat: add downloadBaseURL input to support custom download mirrors
This commit is contained in:
parent
9095542ace
commit
96bb93543b
4 changed files with 72 additions and 8 deletions
|
|
@ -5,6 +5,10 @@ inputs:
|
||||||
description: 'Version of kubectl'
|
description: 'Version of kubectl'
|
||||||
required: true
|
required: true
|
||||||
default: 'latest'
|
default: 'latest'
|
||||||
|
downloadBaseURL:
|
||||||
|
description: 'Set the download base URL'
|
||||||
|
required: false
|
||||||
|
default: 'https://dl.k8s.io'
|
||||||
outputs:
|
outputs:
|
||||||
kubectl-path:
|
kubectl-path:
|
||||||
description: 'Path to the cached kubectl binary'
|
description: 'Path to the cached kubectl binary'
|
||||||
|
|
|
||||||
|
|
@ -11,17 +11,22 @@ export function getKubectlArch(): string {
|
||||||
return arch
|
return arch
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getkubectlDownloadURL(version: string, arch: string): string {
|
export function getkubectlDownloadURL(
|
||||||
|
version: string,
|
||||||
|
arch: string,
|
||||||
|
baseURL: string = 'https://dl.k8s.io'
|
||||||
|
): string {
|
||||||
|
const base = baseURL.replace(/\/$/, '')
|
||||||
switch (os.type()) {
|
switch (os.type()) {
|
||||||
case 'Linux':
|
case 'Linux':
|
||||||
return `https://dl.k8s.io/release/${version}/bin/linux/${arch}/kubectl`
|
return `${base}/release/${version}/bin/linux/${arch}/kubectl`
|
||||||
|
|
||||||
case 'Darwin':
|
case 'Darwin':
|
||||||
return `https://dl.k8s.io/release/${version}/bin/darwin/${arch}/kubectl`
|
return `${base}/release/${version}/bin/darwin/${arch}/kubectl`
|
||||||
|
|
||||||
case 'Windows_NT':
|
case 'Windows_NT':
|
||||||
default:
|
default:
|
||||||
return `https://dl.k8s.io/release/${version}/bin/windows/${arch}/kubectl.exe`
|
return `${base}/release/${version}/bin/windows/${arch}/kubectl.exe`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -89,6 +89,34 @@ describe('Testing all functions in run file.', () => {
|
||||||
expect(os.type).toHaveBeenCalled()
|
expect(os.type).toHaveBeenCalled()
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
test.each([['arm'], ['arm64'], ['amd64']])(
|
||||||
|
'getkubectlDownloadURL() - return the URL to download %s kubectl for Linux with custom base URL',
|
||||||
|
(arch) => {
|
||||||
|
vi.mocked(os.type).mockReturnValue('Linux')
|
||||||
|
const customBase = 'https://my-mirror.example.com'
|
||||||
|
const expected = util.format(
|
||||||
|
`${customBase}/release/v1.15.0/bin/linux/%s/kubectl`,
|
||||||
|
arch
|
||||||
|
)
|
||||||
|
expect(getkubectlDownloadURL('v1.15.0', arch, customBase)).toBe(
|
||||||
|
expected
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
test.each([['arm'], ['arm64'], ['amd64']])(
|
||||||
|
'getkubectlDownloadURL() - strip trailing slash from custom base URL for %s on Darwin',
|
||||||
|
(arch) => {
|
||||||
|
vi.mocked(os.type).mockReturnValue('Darwin')
|
||||||
|
const customBase = 'https://my-mirror.example.com/'
|
||||||
|
const expected = util.format(
|
||||||
|
`https://my-mirror.example.com/release/v1.15.0/bin/darwin/%s/kubectl`,
|
||||||
|
arch
|
||||||
|
)
|
||||||
|
expect(getkubectlDownloadURL('v1.15.0', arch, customBase)).toBe(
|
||||||
|
expected
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
test('getStableKubectlVersion() - download stable version file, read version and return it', async () => {
|
test('getStableKubectlVersion() - download stable version file, read version and return it', async () => {
|
||||||
vi.mocked(toolCache.downloadTool).mockResolvedValue('pathToTool')
|
vi.mocked(toolCache.downloadTool).mockResolvedValue('pathToTool')
|
||||||
vi.mocked(fs.readFileSync).mockReturnValue('v1.20.4')
|
vi.mocked(fs.readFileSync).mockReturnValue('v1.20.4')
|
||||||
|
|
@ -118,7 +146,9 @@ describe('Testing all functions in run file.', () => {
|
||||||
path.join('pathToCachedTool', 'kubectl.exe')
|
path.join('pathToCachedTool', 'kubectl.exe')
|
||||||
)
|
)
|
||||||
expect(toolCache.find).toHaveBeenCalledWith('kubectl', 'v1.15.0')
|
expect(toolCache.find).toHaveBeenCalledWith('kubectl', 'v1.15.0')
|
||||||
expect(toolCache.downloadTool).toHaveBeenCalled()
|
expect(toolCache.downloadTool).toHaveBeenCalledWith(
|
||||||
|
'https://dl.k8s.io/release/v1.15.0/bin/windows/amd64/kubectl.exe'
|
||||||
|
)
|
||||||
expect(toolCache.cacheFile).toHaveBeenCalled()
|
expect(toolCache.cacheFile).toHaveBeenCalled()
|
||||||
expect(os.type).toHaveBeenCalled()
|
expect(os.type).toHaveBeenCalled()
|
||||||
expect(fs.chmodSync).toHaveBeenCalledWith(
|
expect(fs.chmodSync).toHaveBeenCalledWith(
|
||||||
|
|
@ -126,6 +156,21 @@ describe('Testing all functions in run file.', () => {
|
||||||
'775'
|
'775'
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
test('downloadKubectl() - download kubectl using custom downloadBaseURL', async () => {
|
||||||
|
vi.mocked(toolCache.find).mockReturnValue('')
|
||||||
|
vi.mocked(toolCache.downloadTool).mockResolvedValue('pathToTool')
|
||||||
|
vi.mocked(toolCache.cacheFile).mockResolvedValue('pathToCachedTool')
|
||||||
|
vi.mocked(os.type).mockReturnValue('Linux')
|
||||||
|
vi.mocked(os.arch).mockReturnValue('x64')
|
||||||
|
vi.mocked(fs.chmodSync).mockImplementation(() => {})
|
||||||
|
const customBase = 'https://my-mirror.example.com'
|
||||||
|
expect(await run.downloadKubectl('v1.15.0', customBase)).toBe(
|
||||||
|
path.join('pathToCachedTool', 'kubectl')
|
||||||
|
)
|
||||||
|
expect(toolCache.downloadTool).toHaveBeenCalledWith(
|
||||||
|
`${customBase}/release/v1.15.0/bin/linux/amd64/kubectl`
|
||||||
|
)
|
||||||
|
})
|
||||||
test('downloadKubectl() - throw DownloadKubectlFailed error when unable to download kubectl', async () => {
|
test('downloadKubectl() - throw DownloadKubectlFailed error when unable to download kubectl', async () => {
|
||||||
vi.mocked(toolCache.find).mockReturnValue('')
|
vi.mocked(toolCache.find).mockReturnValue('')
|
||||||
vi.mocked(toolCache.downloadTool).mockRejectedValue(
|
vi.mocked(toolCache.downloadTool).mockRejectedValue(
|
||||||
|
|
@ -235,6 +280,9 @@ describe('Testing all functions in run file.', () => {
|
||||||
vi.mocked(core.setOutput).mockImplementation()
|
vi.mocked(core.setOutput).mockImplementation()
|
||||||
expect(await run.run()).toBeUndefined()
|
expect(await run.run()).toBeUndefined()
|
||||||
expect(core.getInput).toHaveBeenCalledWith('version', {required: true})
|
expect(core.getInput).toHaveBeenCalledWith('version', {required: true})
|
||||||
|
expect(core.getInput).toHaveBeenCalledWith('downloadBaseURL', {
|
||||||
|
required: false
|
||||||
|
})
|
||||||
expect(core.addPath).toHaveBeenCalledWith('pathToCachedTool')
|
expect(core.addPath).toHaveBeenCalledWith('pathToCachedTool')
|
||||||
expect(core.setOutput).toHaveBeenCalledWith(
|
expect(core.setOutput).toHaveBeenCalledWith(
|
||||||
'kubectl-path',
|
'kubectl-path',
|
||||||
|
|
@ -256,6 +304,9 @@ describe('Testing all functions in run file.', () => {
|
||||||
'https://dl.k8s.io/release/stable.txt'
|
'https://dl.k8s.io/release/stable.txt'
|
||||||
)
|
)
|
||||||
expect(core.getInput).toHaveBeenCalledWith('version', {required: true})
|
expect(core.getInput).toHaveBeenCalledWith('version', {required: true})
|
||||||
|
expect(core.getInput).toHaveBeenCalledWith('downloadBaseURL', {
|
||||||
|
required: false
|
||||||
|
})
|
||||||
expect(core.addPath).toHaveBeenCalledWith('pathToCachedTool')
|
expect(core.addPath).toHaveBeenCalledWith('pathToCachedTool')
|
||||||
expect(core.setOutput).toHaveBeenCalledWith(
|
expect(core.setOutput).toHaveBeenCalledWith(
|
||||||
'kubectl-path',
|
'kubectl-path',
|
||||||
|
|
|
||||||
10
src/run.ts
10
src/run.ts
|
|
@ -21,7 +21,8 @@ export async function run() {
|
||||||
} else {
|
} else {
|
||||||
version = await resolveKubectlVersion(version)
|
version = await resolveKubectlVersion(version)
|
||||||
}
|
}
|
||||||
const cachedPath = await downloadKubectl(version)
|
const downloadBaseURL = core.getInput('downloadBaseURL', {required: false})
|
||||||
|
const cachedPath = await downloadKubectl(version, downloadBaseURL)
|
||||||
|
|
||||||
core.addPath(path.dirname(cachedPath))
|
core.addPath(path.dirname(cachedPath))
|
||||||
|
|
||||||
|
|
@ -48,14 +49,17 @@ export async function getStableKubectlVersion(): Promise<string> {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function downloadKubectl(version: string): Promise<string> {
|
export async function downloadKubectl(
|
||||||
|
version: string,
|
||||||
|
downloadBaseURL: string = 'https://dl.k8s.io'
|
||||||
|
): Promise<string> {
|
||||||
let cachedToolpath = toolCache.find(kubectlToolName, version)
|
let cachedToolpath = toolCache.find(kubectlToolName, version)
|
||||||
let kubectlDownloadPath = ''
|
let kubectlDownloadPath = ''
|
||||||
const arch = getKubectlArch()
|
const arch = getKubectlArch()
|
||||||
if (!cachedToolpath) {
|
if (!cachedToolpath) {
|
||||||
try {
|
try {
|
||||||
kubectlDownloadPath = await toolCache.downloadTool(
|
kubectlDownloadPath = await toolCache.downloadTool(
|
||||||
getkubectlDownloadURL(version, arch)
|
getkubectlDownloadURL(version, arch, downloadBaseURL)
|
||||||
)
|
)
|
||||||
} catch (exception) {
|
} catch (exception) {
|
||||||
if (
|
if (
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue