#146 Adding optional kubeconfig encoding variable (#164)
Some checks failed
CodeQL Advanced / Analyze (javascript-typescript) (push) Failing after 11s
Run Integration Tests / kubeconfig-method-integration-test (push) Failing after 6s
Run Prettify / Prettier Check (push) Failing after 2s
Run Unit Tests / unit-test (push) Failing after 2s

* 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:
Betsy George 2025-07-17 14:27:04 -04:00 committed by GitHub
parent 8440376895
commit 0fc754ad67
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 67 additions and 1 deletions

View file

@ -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

View file

@ -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', () => {

View file

@ -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
}
}
}