Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ inputs:
description: 'Version of kubectl'
required: true
default: 'latest'
downloadBaseURL:
description: 'Set the download base URL'
required: false
default: 'https://dl.k8s.io'
outputs:
kubectl-path:
description: 'Path to the cached kubectl binary'
Expand Down
13 changes: 9 additions & 4 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,22 @@ export function getKubectlArch(): string {
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()) {
case 'Linux':
return `https://dl.k8s.io/release/${version}/bin/linux/${arch}/kubectl`
return `${base}/release/${version}/bin/linux/${arch}/kubectl`

case 'Darwin':
return `https://dl.k8s.io/release/${version}/bin/darwin/${arch}/kubectl`
return `${base}/release/${version}/bin/darwin/${arch}/kubectl`

case 'Windows_NT':
default:
return `https://dl.k8s.io/release/${version}/bin/windows/${arch}/kubectl.exe`
return `${base}/release/${version}/bin/windows/${arch}/kubectl.exe`
}
}

Expand Down
53 changes: 52 additions & 1 deletion src/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,34 @@ describe('Testing all functions in run file.', () => {
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 () => {
vi.mocked(toolCache.downloadTool).mockResolvedValue('pathToTool')
vi.mocked(fs.readFileSync).mockReturnValue('v1.20.4')
Expand Down Expand Up @@ -118,14 +146,31 @@ describe('Testing all functions in run file.', () => {
path.join('pathToCachedTool', 'kubectl.exe')
)
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(os.type).toHaveBeenCalled()
expect(fs.chmodSync).toHaveBeenCalledWith(
path.join('pathToCachedTool', 'kubectl.exe'),
'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 () => {
vi.mocked(toolCache.find).mockReturnValue('')
vi.mocked(toolCache.downloadTool).mockRejectedValue(
Expand Down Expand Up @@ -235,6 +280,9 @@ describe('Testing all functions in run file.', () => {
vi.mocked(core.setOutput).mockImplementation()
expect(await run.run()).toBeUndefined()
expect(core.getInput).toHaveBeenCalledWith('version', {required: true})
expect(core.getInput).toHaveBeenCalledWith('downloadBaseURL', {
required: false
})
expect(core.addPath).toHaveBeenCalledWith('pathToCachedTool')
expect(core.setOutput).toHaveBeenCalledWith(
'kubectl-path',
Expand All @@ -256,6 +304,9 @@ describe('Testing all functions in run file.', () => {
'https://dl.k8s.io/release/stable.txt'
)
expect(core.getInput).toHaveBeenCalledWith('version', {required: true})
expect(core.getInput).toHaveBeenCalledWith('downloadBaseURL', {
required: false
})
expect(core.addPath).toHaveBeenCalledWith('pathToCachedTool')
expect(core.setOutput).toHaveBeenCalledWith(
'kubectl-path',
Expand Down
10 changes: 7 additions & 3 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export async function run() {
} else {
version = await resolveKubectlVersion(version)
}
const cachedPath = await downloadKubectl(version)
const downloadBaseURL = core.getInput('downloadBaseURL', {required: false})
const cachedPath = await downloadKubectl(version, downloadBaseURL)
Comment on lines +24 to +25

core.addPath(path.dirname(cachedPath))

Expand All @@ -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> {
Comment on lines +52 to +55
let cachedToolpath = toolCache.find(kubectlToolName, version)
let kubectlDownloadPath = ''
const arch = getKubectlArch()
if (!cachedToolpath) {
try {
kubectlDownloadPath = await toolCache.downloadTool(
getkubectlDownloadURL(version, arch)
getkubectlDownloadURL(version, arch, downloadBaseURL)
)
} catch (exception) {
if (
Expand Down