mirror of
https://github.com/Azure/k8s-set-context
synced 2025-09-23 12:08:06 +00:00
* encoding input variable added * encoding input variable added * encoding input variable added * corrected unit tests * corrected unit tests * prettier edits * working on tests * working on tests * working on tests * minor edits * minor edits * better logic structure * added tests for edge cases * edited to enum
This commit is contained in:
parent
8440376895
commit
0fc754ad67
3 changed files with 67 additions and 1 deletions
|
@ -13,6 +13,10 @@ inputs:
|
|||
kubeconfig:
|
||||
description: 'Contents of kubeconfig file'
|
||||
required: false
|
||||
kubeconfig-encoding:
|
||||
description: 'Encoding of the kubeconfig input. Accepts "plaintext" (default) or "base64".'
|
||||
required: false
|
||||
default: 'plaintext'
|
||||
context:
|
||||
description: 'If your kubeconfig has multiple contexts, use this field to use a specific context, otherwise the default one would be chosen'
|
||||
required: false
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import * as fs from 'fs'
|
||||
import * as core from '@actions/core'
|
||||
import {getRequiredInputError} from '../../tests/util'
|
||||
import {createKubeconfig, getDefaultKubeconfig} from './default'
|
||||
|
||||
|
@ -62,6 +63,47 @@ describe('Default kubeconfig', () => {
|
|||
|
||||
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', () => {
|
||||
|
|
|
@ -44,7 +44,27 @@ export function getDefaultKubeconfig(): string {
|
|||
}
|
||||
default: {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue