mirror of
https://github.com/Azure/k8s-set-context.git
synced 2026-06-24 15:59:27 +08:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d32f722e54 | |||
| 4ad647536e | |||
| f95370c1a4 | |||
| 0fc754ad67 |
@@ -13,6 +13,10 @@ inputs:
|
|||||||
kubeconfig:
|
kubeconfig:
|
||||||
description: 'Contents of kubeconfig file'
|
description: 'Contents of kubeconfig file'
|
||||||
required: false
|
required: false
|
||||||
|
kubeconfig-encoding:
|
||||||
|
description: 'Encoding of the kubeconfig input. Accepts "plaintext" (default) or "base64".'
|
||||||
|
required: false
|
||||||
|
default: 'plaintext'
|
||||||
context:
|
context:
|
||||||
description: 'If your kubeconfig has multiple contexts, use this field to use a specific context, otherwise the default one would be chosen'
|
description: 'If your kubeconfig has multiple contexts, use this field to use a specific context, otherwise the default one would be chosen'
|
||||||
required: false
|
required: false
|
||||||
|
|||||||
Generated
+15
-14
@@ -21,7 +21,7 @@
|
|||||||
"@babel/preset-typescript": "^7.27.1",
|
"@babel/preset-typescript": "^7.27.1",
|
||||||
"@types/jest": "^30.0.0",
|
"@types/jest": "^30.0.0",
|
||||||
"@types/js-yaml": "^4.0.9",
|
"@types/js-yaml": "^4.0.9",
|
||||||
"@types/node": "^24.0.13",
|
"@types/node": "^24.0.15",
|
||||||
"@vercel/ncc": "^0.38.3",
|
"@vercel/ncc": "^0.38.3",
|
||||||
"babel-jest": "^30.0.4",
|
"babel-jest": "^30.0.4",
|
||||||
"jest": "^30.0.4",
|
"jest": "^30.0.4",
|
||||||
@@ -2541,9 +2541,9 @@
|
|||||||
"integrity": "sha512-k4MGaQl5TGo/iipqb2UDG2UwjXziSWkh0uysQelTlJpX1qGlpUZYm8PnO4DxG1qBomtJUdYJ6qR6xdIah10JLg=="
|
"integrity": "sha512-k4MGaQl5TGo/iipqb2UDG2UwjXziSWkh0uysQelTlJpX1qGlpUZYm8PnO4DxG1qBomtJUdYJ6qR6xdIah10JLg=="
|
||||||
},
|
},
|
||||||
"node_modules/@types/node": {
|
"node_modules/@types/node": {
|
||||||
"version": "24.0.13",
|
"version": "24.0.15",
|
||||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-24.0.13.tgz",
|
"resolved": "https://registry.npmjs.org/@types/node/-/node-24.0.15.tgz",
|
||||||
"integrity": "sha512-Qm9OYVOFHFYg3wJoTSrz80hoec5Lia/dPp84do3X7dZvLikQvM1YpmvTBEdIr/e+U8HTkFjLHLnl78K/qjf+jQ==",
|
"integrity": "sha512-oaeTSbCef7U/z7rDeJA138xpG3NuKc64/rZ2qmUFkFJmnMsAPaluIifqyWd8hSSMxyP9oie3dLAqYPblag9KgA==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"undici-types": "~7.8.0"
|
"undici-types": "~7.8.0"
|
||||||
}
|
}
|
||||||
@@ -3883,14 +3883,14 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/form-data": {
|
"node_modules/form-data": {
|
||||||
"version": "4.0.2",
|
"version": "4.0.4",
|
||||||
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.4.tgz",
|
||||||
"integrity": "sha512-hGfm/slu0ZabnNt4oaRZ6uREyfCj6P4fT/n6A1rGV+Z0VdGXjfOhVUpkn6qVQONHGIFwmveGXyDs75+nr6FM8w==",
|
"integrity": "sha512-KrGhL9Q4zjj0kiUt5OO4Mr/A/jlI2jDYs5eHBpYHPcBEVSiipAvn2Ko2HnPe20rmcuuvMHNdZFp+4IlGTMF0Ow==",
|
||||||
"license": "MIT",
|
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"asynckit": "^0.4.0",
|
"asynckit": "^0.4.0",
|
||||||
"combined-stream": "^1.0.8",
|
"combined-stream": "^1.0.8",
|
||||||
"es-set-tostringtag": "^2.1.0",
|
"es-set-tostringtag": "^2.1.0",
|
||||||
|
"hasown": "^2.0.2",
|
||||||
"mime-types": "^2.1.12"
|
"mime-types": "^2.1.12"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
@@ -8474,9 +8474,9 @@
|
|||||||
"integrity": "sha512-k4MGaQl5TGo/iipqb2UDG2UwjXziSWkh0uysQelTlJpX1qGlpUZYm8PnO4DxG1qBomtJUdYJ6qR6xdIah10JLg=="
|
"integrity": "sha512-k4MGaQl5TGo/iipqb2UDG2UwjXziSWkh0uysQelTlJpX1qGlpUZYm8PnO4DxG1qBomtJUdYJ6qR6xdIah10JLg=="
|
||||||
},
|
},
|
||||||
"@types/node": {
|
"@types/node": {
|
||||||
"version": "24.0.13",
|
"version": "24.0.15",
|
||||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-24.0.13.tgz",
|
"resolved": "https://registry.npmjs.org/@types/node/-/node-24.0.15.tgz",
|
||||||
"integrity": "sha512-Qm9OYVOFHFYg3wJoTSrz80hoec5Lia/dPp84do3X7dZvLikQvM1YpmvTBEdIr/e+U8HTkFjLHLnl78K/qjf+jQ==",
|
"integrity": "sha512-oaeTSbCef7U/z7rDeJA138xpG3NuKc64/rZ2qmUFkFJmnMsAPaluIifqyWd8hSSMxyP9oie3dLAqYPblag9KgA==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"undici-types": "~7.8.0"
|
"undici-types": "~7.8.0"
|
||||||
}
|
}
|
||||||
@@ -9378,13 +9378,14 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"form-data": {
|
"form-data": {
|
||||||
"version": "4.0.2",
|
"version": "4.0.4",
|
||||||
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.4.tgz",
|
||||||
"integrity": "sha512-hGfm/slu0ZabnNt4oaRZ6uREyfCj6P4fT/n6A1rGV+Z0VdGXjfOhVUpkn6qVQONHGIFwmveGXyDs75+nr6FM8w==",
|
"integrity": "sha512-KrGhL9Q4zjj0kiUt5OO4Mr/A/jlI2jDYs5eHBpYHPcBEVSiipAvn2Ko2HnPe20rmcuuvMHNdZFp+4IlGTMF0Ow==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"asynckit": "^0.4.0",
|
"asynckit": "^0.4.0",
|
||||||
"combined-stream": "^1.0.8",
|
"combined-stream": "^1.0.8",
|
||||||
"es-set-tostringtag": "^2.1.0",
|
"es-set-tostringtag": "^2.1.0",
|
||||||
|
"hasown": "^2.0.2",
|
||||||
"mime-types": "^2.1.12"
|
"mime-types": "^2.1.12"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
+1
-1
@@ -33,7 +33,7 @@
|
|||||||
"@babel/preset-typescript": "^7.27.1",
|
"@babel/preset-typescript": "^7.27.1",
|
||||||
"@types/jest": "^30.0.0",
|
"@types/jest": "^30.0.0",
|
||||||
"@types/js-yaml": "^4.0.9",
|
"@types/js-yaml": "^4.0.9",
|
||||||
"@types/node": "^24.0.13",
|
"@types/node": "^24.0.15",
|
||||||
"@vercel/ncc": "^0.38.3",
|
"@vercel/ncc": "^0.38.3",
|
||||||
"babel-jest": "^30.0.4",
|
"babel-jest": "^30.0.4",
|
||||||
"jest": "^30.0.4",
|
"jest": "^30.0.4",
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import * as fs from 'fs'
|
import * as fs from 'fs'
|
||||||
|
import * as core from '@actions/core'
|
||||||
import {getRequiredInputError} from '../../tests/util'
|
import {getRequiredInputError} from '../../tests/util'
|
||||||
import {createKubeconfig, getDefaultKubeconfig} from './default'
|
import {createKubeconfig, getDefaultKubeconfig} from './default'
|
||||||
|
|
||||||
@@ -62,6 +63,47 @@ describe('Default kubeconfig', () => {
|
|||||||
|
|
||||||
expect(getDefaultKubeconfig()).toBe(kc)
|
expect(getDefaultKubeconfig()).toBe(kc)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('returns kubeconfig as plaintext when encoding is plaintext', () => {
|
||||||
|
const kc = 'example kc'
|
||||||
|
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
|
||||||
|
if (name === 'method') return 'default'
|
||||||
|
if (name === 'kubeconfig-encoding') return 'plaintext'
|
||||||
|
if (name === 'kubeconfig') return kc
|
||||||
|
return ''
|
||||||
|
})
|
||||||
|
expect(getDefaultKubeconfig()).toBe(kc)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('it gets default config through base64 kubeconfig input', () => {
|
||||||
|
const kc = 'example kc'
|
||||||
|
const base64Kc = Buffer.from(kc, 'utf-8').toString('base64')
|
||||||
|
|
||||||
|
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
|
||||||
|
if (name === 'method') return 'default'
|
||||||
|
if (name === 'kubeconfig-encoding') return 'base64'
|
||||||
|
if (name === 'kubeconfig') return base64Kc
|
||||||
|
return ''
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(getDefaultKubeconfig()).toBe(kc)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('it throws error for unknown kubeconfig-encoding', () => {
|
||||||
|
const kc = 'example kc'
|
||||||
|
const unknownEncoding = 'foobar'
|
||||||
|
|
||||||
|
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
|
||||||
|
if (name === 'method') return 'default'
|
||||||
|
if (name === 'kubeconfig-encoding') return unknownEncoding
|
||||||
|
if (name === 'kubeconfig') return kc
|
||||||
|
return ''
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(() => getDefaultKubeconfig()).toThrow(
|
||||||
|
"Invalid kubeconfig-encoding: 'foobar'. Must be 'plaintext' or 'base64'."
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
test('it defaults to default method', () => {
|
test('it defaults to default method', () => {
|
||||||
|
|||||||
@@ -44,7 +44,27 @@ export function getDefaultKubeconfig(): string {
|
|||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
core.debug('Setting context using kubeconfig')
|
core.debug('Setting context using kubeconfig')
|
||||||
return core.getInput('kubeconfig', {required: true})
|
enum Encoding {
|
||||||
|
Base64 = 'base64',
|
||||||
|
Plaintext = 'plaintext'
|
||||||
|
}
|
||||||
|
|
||||||
|
const rawKubeconfig = core.getInput('kubeconfig', {required: true})
|
||||||
|
const encoding =
|
||||||
|
core.getInput('kubeconfig-encoding')?.toLowerCase() ||
|
||||||
|
Encoding.Plaintext
|
||||||
|
|
||||||
|
if (encoding !== Encoding.Base64 && encoding !== Encoding.Plaintext) {
|
||||||
|
throw new Error(
|
||||||
|
`Invalid kubeconfig-encoding: '${encoding}'. Must be 'plaintext' or 'base64'.`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
const kubeconfig =
|
||||||
|
encoding === Encoding.Base64
|
||||||
|
? Buffer.from(rawKubeconfig, 'base64').toString('utf-8')
|
||||||
|
: rawKubeconfig
|
||||||
|
|
||||||
|
return kubeconfig
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user