1 Commits

Author SHA1 Message Date
GitHub Action
0c5e050edf build 2025-02-07 20:01:06 +00:00
18 changed files with 8599 additions and 3345 deletions

2
.github/CODEOWNERS vendored
View File

@@ -1 +1 @@
* @Azure/cloud-native-github-action-owners
* @Azure/aks-atlanta

View File

@@ -55,11 +55,11 @@ jobs:
# your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages
steps:
- name: Checkout repository
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@0499de31b99561a6d14a36a5f662c2a54f91beee # v3.29.5
uses: github/codeql-action/init@17a820bf2e43b47be2c72b39cc905417bc1ab6d0 # v3.28.6
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
@@ -86,6 +86,6 @@ jobs:
echo ' make release'
exit 1
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@0499de31b99561a6d14a36a5f662c2a54f91beee # v3.29.5
uses: github/codeql-action/analyze@17a820bf2e43b47be2c72b39cc905417bc1ab6d0 # v3.28.6
with:
category: '/language:${{matrix.language}}'

View File

@@ -8,7 +8,7 @@ jobs:
label-issues:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@5f858e3efba33a5ca4407a664cc011ad407f2008 # v10.1.0
- uses: actions/stale@5bef64f19d7facfb25b37b414482c7164d639639 # v9.1.0
name: Setting issue as idle
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
@@ -19,7 +19,7 @@ jobs:
operations-per-run: 100
exempt-issue-labels: 'backlog'
- uses: actions/stale@5f858e3efba33a5ca4407a664cc011ad407f2008 # v10.1.0
- uses: actions/stale@5bef64f19d7facfb25b37b414482c7164d639639 # v9.1.0
name: Setting PR as idle
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

View File

@@ -17,7 +17,7 @@ jobs:
KUBECONFIG: /home/runner/.kube/config
PR_BASE_REF: ${{ github.event.pull_request.base.ref }}
steps:
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
name: Checkout from PR branch
- id: action-npm-build
@@ -27,11 +27,9 @@ jobs:
if [[ $PR_BASE_REF != releases/* ]]; then
npm install
npm run build
# remove node_modules to match production environment where only index.js is present
rm -rf node_modules
fi
- uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6.0.0
- uses: actions/setup-python@42375524e23c412d93fb67b49958b491fce71c38 # v5.4.0
name: Install Python
with:
python-version: '3.x'

View File

@@ -10,10 +10,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Setup Node.js
uses: actions/setup-node@2028fbc5c25fe9cf00d9f06a71cc4710d4507903 # v6.0.0
uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
with:
node-version: 'lts/*'
cache: 'npm'

View File

@@ -13,7 +13,7 @@ jobs:
build: # make sure build/ci works properly
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Build and run L0 tests.
run: |

1
.gitignore vendored
View File

@@ -330,4 +330,3 @@ ASALocalRun/
node_modules
# Transpiled JS
lib/

View File

@@ -1,9 +0,0 @@
set +e
npm test
# Run format check
npm run format-check || {
echo ""
echo "❌ Formatting check failed."
echo "💡 Run 'npm run format' or 'prettier --write .' to fix formatting issues."
exit 1
}

View File

@@ -1,11 +1,6 @@
# Changelog
## [4.0.1] - 2025-06-17
- Remove erronious 'v' prefix on previous changelog for v4.0.0 that led to "vv4.0.0" tag issue
- Dependabot fixes
## [4.0.0] - 2024-01-30
## [v4.0.0] - 2024-01-30
### Changed

View File

@@ -1,5 +1,5 @@
name: 'Kubectl tool installer'
description: 'Install a specific version of kubectl binary. Acceptable values are latest or any semantic version string like "v1.15.0"'
description: 'Install a specific version of kubectl binary. Acceptable values are latest or any semantic version string like 1.15.0'
inputs:
version:
description: 'Version of kubectl'

6280
lib/index.js Normal file

File diff suppressed because it is too large Load Diff

5432
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -8,8 +8,7 @@
"test": "jest",
"test-coverage": "jest --coverage",
"format": "prettier --write .",
"format-check": "prettier --check .",
"prepare": "husky"
"format-check": "prettier --check ."
},
"keywords": [
"actions",
@@ -21,16 +20,16 @@
"dependencies": {
"@actions/core": "^1.11.1",
"@actions/exec": "^1.0.0",
"@actions/tool-cache": "^2.0.2"
"@actions/tool-cache": "^2.0.2",
"ncc": "^0.3.6"
},
"devDependencies": {
"@types/jest": "^30.0.0",
"@types/node": "^24.9.1",
"@vercel/ncc": "^0.38.4",
"husky": "^9.1.7",
"jest": "^30.2.0",
"prettier": "3.6.2",
"ts-jest": "^29.4.5",
"typescript": "5.9.3"
"@types/jest": "^29.5.14",
"@types/node": "^22.10.10",
"@vercel/ncc": "^0.38.3",
"jest": "^29.7.0",
"prettier": "3.4.2",
"ts-jest": "^29.2.5",
"typescript": "5.7.3"
}
}

View File

@@ -1,8 +1,6 @@
import * as os from 'os'
import * as util from 'util'
import * as fs from 'fs'
import * as core from '@actions/core'
import * as toolCache from '@actions/tool-cache'
export function getKubectlArch(): string {
const arch = os.arch()
if (arch === 'x64') {
@@ -25,29 +23,6 @@ export function getkubectlDownloadURL(version: string, arch: string): string {
}
}
export async function getLatestPatchVersion(
major: string,
minor: string
): Promise<string> {
const version = `${major}.${minor}`
const sourceURL = `https://cdn.dl.k8s.io/release/stable-${version}.txt`
try {
const downloadPath = await toolCache.downloadTool(sourceURL)
const latestPatch = fs
.readFileSync(downloadPath, 'utf8')
.toString()
.trim()
if (!latestPatch) {
throw new Error(`No patch version found for ${version}`)
}
return latestPatch
} catch (error) {
core.debug(error)
core.warning('GetLatestPatchVersionFailed')
throw new Error(`Failed to get latest patch version for ${version}`)
}
}
export function getExecutableExtension(): string {
if (os.type().match(/^Win/)) {
return '.exe'

View File

@@ -2,8 +2,7 @@ import * as run from './run'
import {
getkubectlDownloadURL,
getKubectlArch,
getExecutableExtension,
getLatestPatchVersion
getExecutableExtension
} from './helpers'
import * as os from 'os'
import * as toolCache from '@actions/tool-cache'
@@ -13,18 +12,15 @@ import * as core from '@actions/core'
import * as util from 'util'
describe('Testing all functions in run file.', () => {
beforeEach(() => {
jest.clearAllMocks()
})
test('getExecutableExtension() - return .exe when os is Windows', () => {
jest.spyOn(os, 'type').mockReturnValue('Windows_NT')
expect(getExecutableExtension()).toBe('.exe')
expect(os.type).toHaveBeenCalled()
expect(os.type).toBeCalled()
})
test('getExecutableExtension() - return empty string for non-windows OS', () => {
jest.spyOn(os, 'type').mockReturnValue('Darwin')
expect(getExecutableExtension()).toBe('')
expect(os.type).toHaveBeenCalled()
expect(os.type).toBeCalled()
})
test.each([
['arm', 'arm'],
@@ -33,9 +29,9 @@ describe('Testing all functions in run file.', () => {
])(
'getKubectlArch() - return on %s os arch %s kubectl arch',
(osArch, kubectlArch) => {
jest.spyOn(os, 'arch').mockReturnValue(osArch as NodeJS.Architecture)
jest.spyOn(os, 'arch').mockReturnValue(osArch)
expect(getKubectlArch()).toBe(kubectlArch)
expect(os.arch).toHaveBeenCalled()
expect(os.arch).toBeCalled()
}
)
test.each([['arm'], ['arm64'], ['amd64']])(
@@ -47,7 +43,7 @@ describe('Testing all functions in run file.', () => {
arch
)
expect(getkubectlDownloadURL('v1.15.0', arch)).toBe(kubectlLinuxUrl)
expect(os.type).toHaveBeenCalled()
expect(os.type).toBeCalled()
}
)
test.each([['arm'], ['arm64'], ['amd64']])(
@@ -59,7 +55,7 @@ describe('Testing all functions in run file.', () => {
arch
)
expect(getkubectlDownloadURL('v1.15.0', arch)).toBe(kubectlDarwinUrl)
expect(os.type).toHaveBeenCalled()
expect(os.type).toBeCalled()
}
)
test.each([['arm'], ['arm64'], ['amd64']])(
@@ -71,7 +67,7 @@ describe('Testing all functions in run file.', () => {
arch
)
expect(getkubectlDownloadURL('v1.15.0', arch)).toBe(kubectlWindowsUrl)
expect(os.type).toHaveBeenCalled()
expect(os.type).toBeCalled()
}
)
test('getStableKubectlVersion() - download stable version file, read version and return it', async () => {
@@ -80,7 +76,7 @@ describe('Testing all functions in run file.', () => {
.mockReturnValue(Promise.resolve('pathToTool'))
jest.spyOn(fs, 'readFileSync').mockReturnValue('v1.20.4')
expect(await run.getStableKubectlVersion()).toBe('v1.20.4')
expect(toolCache.downloadTool).toHaveBeenCalled()
expect(toolCache.downloadTool).toBeCalled()
expect(fs.readFileSync).toHaveBeenCalledWith('pathToTool', 'utf8')
})
test('getStableKubectlVersion() - return default v1.15.0 if version read is empty', async () => {
@@ -89,7 +85,7 @@ describe('Testing all functions in run file.', () => {
.mockReturnValue(Promise.resolve('pathToTool'))
jest.spyOn(fs, 'readFileSync').mockReturnValue('')
expect(await run.getStableKubectlVersion()).toBe('v1.15.0')
expect(toolCache.downloadTool).toHaveBeenCalled()
expect(toolCache.downloadTool).toBeCalled()
expect(fs.readFileSync).toHaveBeenCalledWith('pathToTool', 'utf8')
})
test('getStableKubectlVersion() - return default v1.15.0 if unable to download file', async () => {
@@ -97,7 +93,7 @@ describe('Testing all functions in run file.', () => {
.spyOn(toolCache, 'downloadTool')
.mockRejectedValue('Unable to download.')
expect(await run.getStableKubectlVersion()).toBe('v1.15.0')
expect(toolCache.downloadTool).toHaveBeenCalled()
expect(toolCache.downloadTool).toBeCalled()
})
test('downloadKubectl() - download kubectl, add it to toolCache and return path to it', async () => {
jest.spyOn(toolCache, 'find').mockReturnValue('')
@@ -113,9 +109,9 @@ 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.cacheFile).toHaveBeenCalled()
expect(os.type).toHaveBeenCalled()
expect(toolCache.downloadTool).toBeCalled()
expect(toolCache.cacheFile).toBeCalled()
expect(os.type).toBeCalled()
expect(fs.chmodSync).toHaveBeenCalledWith(
path.join('pathToCachedTool', 'kubectl.exe'),
'775'
@@ -130,12 +126,12 @@ describe('Testing all functions in run file.', () => {
'DownloadKubectlFailed'
)
expect(toolCache.find).toHaveBeenCalledWith('kubectl', 'v1.15.0')
expect(toolCache.downloadTool).toHaveBeenCalled()
expect(toolCache.downloadTool).toBeCalled()
})
test('downloadKubectl() - throw kubectl not found error when receive 404 response', async () => {
const kubectlVersion = 'v1.15.0'
const arch = 'arm128'
jest.spyOn(os, 'arch').mockReturnValue(arch as any)
jest.spyOn(os, 'arch').mockReturnValue(arch)
jest.spyOn(toolCache, 'find').mockReturnValue('')
jest.spyOn(toolCache, 'downloadTool').mockImplementation((_) => {
throw new toolCache.HTTPError(404)
@@ -147,9 +143,9 @@ describe('Testing all functions in run file.', () => {
arch
)
)
expect(os.arch).toHaveBeenCalled()
expect(os.arch).toBeCalled()
expect(toolCache.find).toHaveBeenCalledWith('kubectl', kubectlVersion)
expect(toolCache.downloadTool).toHaveBeenCalled()
expect(toolCache.downloadTool).toBeCalled()
})
test('downloadKubectl() - return path to existing cache of kubectl', async () => {
jest.spyOn(core, 'getInput').mockImplementation(() => 'v1.15.5')
@@ -161,65 +157,12 @@ describe('Testing all functions in run file.', () => {
path.join('pathToCachedTool', 'kubectl.exe')
)
expect(toolCache.find).toHaveBeenCalledWith('kubectl', 'v1.15.0')
expect(os.type).toHaveBeenCalled()
expect(os.type).toBeCalled()
expect(fs.chmodSync).toHaveBeenCalledWith(
path.join('pathToCachedTool', 'kubectl.exe'),
'775'
)
expect(toolCache.downloadTool).not.toHaveBeenCalled()
})
test('getLatestPatchVersion() - download and return latest patch version', async () => {
jest.spyOn(toolCache, 'downloadTool').mockResolvedValue('pathToTool')
jest.spyOn(fs, 'readFileSync').mockReturnValue('v1.27.15')
const result = await getLatestPatchVersion('1', '27')
expect(result).toBe('v1.27.15')
expect(toolCache.downloadTool).toHaveBeenCalledWith(
'https://cdn.dl.k8s.io/release/stable-1.27.txt'
)
})
test('getLatestPatchVersion() - throw error when patch version is empty', async () => {
jest.spyOn(toolCache, 'downloadTool').mockResolvedValue('pathToTool')
jest.spyOn(fs, 'readFileSync').mockReturnValue('')
await expect(getLatestPatchVersion('1', '27')).rejects.toThrow(
'Failed to get latest patch version for 1.27'
)
})
test('getLatestPatchVersion() - throw error when download fails', async () => {
jest
.spyOn(toolCache, 'downloadTool')
.mockRejectedValue(new Error('Network error'))
await expect(getLatestPatchVersion('1', '27')).rejects.toThrow(
'Failed to get latest patch version for 1.27'
)
})
test('resolveKubectlVersion() - expands major.minor to latest patch', async () => {
jest.spyOn(toolCache, 'downloadTool').mockResolvedValue('pathToTool')
jest.spyOn(fs, 'readFileSync').mockReturnValue('v1.27.15')
const result = await run.resolveKubectlVersion('1.27')
expect(result).toBe('v1.27.15')
})
test('resolveKubectlVersion() - returns full version unchanged', async () => {
const result = await run.resolveKubectlVersion('v1.27.15')
expect(result).toBe('v1.27.15')
})
test('resolveKubectlVersion() - adds v prefix to full version', async () => {
const result = await run.resolveKubectlVersion('1.27.15')
expect(result).toBe('v1.27.15')
})
test('resolveKubectlVersion() - expands v-prefixed major.minor to latest patch', async () => {
jest.spyOn(toolCache, 'downloadTool').mockResolvedValue('pathToTool')
jest.spyOn(fs, 'readFileSync').mockReturnValue('v1.27.15')
const result = await run.resolveKubectlVersion('v1.27')
expect(result).toBe('v1.27.15')
expect(toolCache.downloadTool).not.toBeCalled()
})
test('run() - download specified version and set output', async () => {
jest.spyOn(core, 'getInput').mockReturnValue('v1.15.5')
@@ -251,7 +194,7 @@ describe('Testing all functions in run file.', () => {
jest.spyOn(core, 'setOutput').mockImplementation()
expect(await run.run()).toBeUndefined()
expect(toolCache.downloadTool).toHaveBeenCalledWith(
'https://dl.k8s.io/release/stable.txt'
'https://storage.googleapis.com/kubernetes-release/release/stable.txt'
)
expect(core.getInput).toHaveBeenCalledWith('version', {required: true})
expect(core.addPath).toHaveBeenCalledWith('pathToCachedTool')

View File

@@ -1,25 +1,25 @@
import * as path from 'path'
import * as util from 'util'
import * as fs from 'fs'
import * as toolCache from '@actions/tool-cache'
import * as core from '@actions/core'
import {
getkubectlDownloadURL,
getKubectlArch,
getExecutableExtension,
getLatestPatchVersion
getExecutableExtension
} from './helpers'
const kubectlToolName = 'kubectl'
const stableKubectlVersion = 'v1.15.0'
const stableVersionUrl = 'https://dl.k8s.io/release/stable.txt'
const stableVersionUrl =
'https://storage.googleapis.com/kubernetes-release/release/stable.txt'
export async function run() {
let version = core.getInput('version', {required: true})
if (version.toLocaleLowerCase() === 'latest') {
version = await getStableKubectlVersion()
} else {
version = await resolveKubectlVersion(version)
}
const cachedPath = await downloadKubectl(version)
@@ -89,28 +89,3 @@ export async function downloadKubectl(version: string): Promise<string> {
fs.chmodSync(kubectlPath, '775')
return kubectlPath
}
export async function resolveKubectlVersion(version: string): Promise<string> {
const cleanedVersion = version.trim()
const versionMatch = cleanedVersion.match(
/^v?(?<major>\d+)\.(?<minor>\d+)(?:\.(?<patch>\d+))?$/
)
if (!versionMatch?.groups) {
throw new Error(
`Invalid version format: "${version}". Version must be in "major.minor" or "major.minor.patch" format (e.g., "1.27" or "v1.27.15").`
)
}
const {major, minor, patch} = versionMatch.groups
if (patch) {
// Full version was provided, just ensure it has a 'v' prefix
return cleanedVersion.startsWith('v')
? cleanedVersion
: `v${cleanedVersion}`
}
// Patch version is missing, fetch the latest
return await getLatestPatchVersion(major, minor)
}

View File

@@ -10,7 +10,7 @@ def get_latest_version():
time_to_sleep = 2
for _ in range(10):
response = requests.get(
'https://dl.k8s.io/release/stable.txt')
'https://storage.googleapis.com/kubernetes-release/release/stable.txt')
if response.status_code == 200:
break
print('Failed to obtain latest version info, retrying.')

View File

@@ -1,8 +1,7 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"lib": ["ES2020", "DOM"]
"target": "ES6",
"module": "commonjs"
},
"exclude": ["node_modules", "test"]
}