Skip to content

Commit

Permalink
Reverting private logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jaiveerk committed Jul 31, 2024
1 parent 48afb64 commit d61b384
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
25 changes: 9 additions & 16 deletions src/types/privatekubectl.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import * as fileUtils from '../utilities/fileUtils'
import {
PrivateKubectl,
extractFileNames,
replaceFileNamesWithNamesRelativeToTemp
replaceFileNamesWithBaseNames
} from './privatekubectl'
import * as exec from '@actions/exec'

describe('Private kubectl', () => {
const testString = `kubectl annotate -f /tmp/testdir/test.yml,/tmp/test2.yml,/tmp/testdir/subdir/test3.yml -f /tmp/test4.yml --filename /tmp/test5.yml actions.github.com/k8s-deploy={"run":"3498366832","repository":"jaiveerk/k8s-deploy","workflow":"Minikube Integration Tests - private cluster","workflowFileName":"run-integration-tests-private.yml","jobName":"run-integration-test","createdBy":"jaiveerk","runUri":"https://github.com/jaiveerk/k8s-deploy/actions/runs/3498366832","commit":"c63b323186ea1320a31290de6dcc094c06385e75","lastSuccessRunCommit":"NA","branch":"refs/heads/main","deployTimestamp":1668787848577,"dockerfilePaths":{"nginx:1.14.2":""},"manifestsPaths":["https://github.com/jaiveerk/k8s-deploy/blob/c63b323186ea1320a31290de6dcc094c06385e75/test/integration/manifests/test.yml"],"helmChartPaths":[],"provider":"GitHub"} --overwrite --namespace test-3498366832`
const testString = `kubectl annotate -f testdir/test.yml,test2.yml,testdir/subdir/test3.yml -f test4.yml --filename test5.yml actions.github.com/k8s-deploy={"run":"3498366832","repository":"jaiveerk/k8s-deploy","workflow":"Minikube Integration Tests - private cluster","workflowFileName":"run-integration-tests-private.yml","jobName":"run-integration-test","createdBy":"jaiveerk","runUri":"https://github.com/jaiveerk/k8s-deploy/actions/runs/3498366832","commit":"c63b323186ea1320a31290de6dcc094c06385e75","lastSuccessRunCommit":"NA","branch":"refs/heads/main","deployTimestamp":1668787848577,"dockerfilePaths":{"nginx:1.14.2":""},"manifestsPaths":["https://github.com/jaiveerk/k8s-deploy/blob/c63b323186ea1320a31290de6dcc094c06385e75/test/integration/manifests/test.yml"],"helmChartPaths":[],"provider":"GitHub"} --overwrite --namespace test-3498366832`
const mockKube = new PrivateKubectl(
'kubectlPath',
'namespace',
Expand All @@ -16,25 +15,19 @@ describe('Private kubectl', () => {
'resourceName'
)

const spy = jest
.spyOn(fileUtils, 'getTempDirectory')
.mockImplementation(() => {
return '/tmp'
})

it('should extract filenames correctly', () => {
expect(extractFileNames(testString)).toEqual([
'/tmp/testdir/test.yml',
'/tmp/test2.yml',
'/tmp/testdir/subdir/test3.yml',
'/tmp/test4.yml',
'/tmp/test5.yml'
'testdir/test.yml',
'test2.yml',
'testdir/subdir/test3.yml',
'test4.yml',
'test5.yml'
])
})

it('should replace filenames with basenames correctly', () => {
expect(replaceFileNamesWithNamesRelativeToTemp(testString)).toEqual(
`kubectl annotate -f testdir/test.yml,test2.yml,testdir/subdir/test3.yml -f test4.yml --filename test5.yml actions.github.com/k8s-deploy={"run":"3498366832","repository":"jaiveerk/k8s-deploy","workflow":"Minikube Integration Tests - private cluster","workflowFileName":"run-integration-tests-private.yml","jobName":"run-integration-test","createdBy":"jaiveerk","runUri":"https://github.com/jaiveerk/k8s-deploy/actions/runs/3498366832","commit":"c63b323186ea1320a31290de6dcc094c06385e75","lastSuccessRunCommit":"NA","branch":"refs/heads/main","deployTimestamp":1668787848577,"dockerfilePaths":{"nginx:1.14.2":""},"manifestsPaths":["https://github.com/jaiveerk/k8s-deploy/blob/c63b323186ea1320a31290de6dcc094c06385e75/test/integration/manifests/test.yml"],"helmChartPaths":[],"provider":"GitHub"} --overwrite --namespace test-3498366832`
expect(replaceFileNamesWithBaseNames(testString)).toEqual(
`kubectl annotate -f test.yml,test2.yml,test3.yml -f test4.yml --filename test5.yml actions.github.com/k8s-deploy={"run":"3498366832","repository":"jaiveerk/k8s-deploy","workflow":"Minikube Integration Tests - private cluster","workflowFileName":"run-integration-tests-private.yml","jobName":"run-integration-test","createdBy":"jaiveerk","runUri":"https://github.com/jaiveerk/k8s-deploy/actions/runs/3498366832","commit":"c63b323186ea1320a31290de6dcc094c06385e75","lastSuccessRunCommit":"NA","branch":"refs/heads/main","deployTimestamp":1668787848577,"dockerfilePaths":{"nginx:1.14.2":""},"manifestsPaths":["https://github.com/jaiveerk/k8s-deploy/blob/c63b323186ea1320a31290de6dcc094c06385e75/test/integration/manifests/test.yml"],"helmChartPaths":[],"provider":"GitHub"} --overwrite --namespace test-3498366832`
)
})

Expand Down
32 changes: 20 additions & 12 deletions src/types/privatekubectl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ export class PrivateKubectl extends Kubectl {
let kubectlCmd = args.join(' ')
let addFileFlag = false
let eo = <ExecOptions>{
silent: false,
silent: true,
failOnStdErr: false,
ignoreReturnCode: false
ignoreReturnCode: true
}

if (this.containsFilenames(kubectlCmd)) {
kubectlCmd = replaceFileNamesWithNamesRelativeToTemp(kubectlCmd)
// For private clusters, files will referenced solely by their basename
kubectlCmd = replaceFileNamesWithBaseNames(kubectlCmd)
addFileFlag = true
}

Expand Down Expand Up @@ -48,6 +49,16 @@ export class PrivateKubectl extends Kubectl {
const tempDirectory = getTempDirectory()
eo.cwd = tempDirectory
privateClusterArgs.push(...['--file', '.'])

for (const filename of filenames) {
try {
this.moveFileToTempManifestDir(filename)
} catch (e) {
core.debug(
`Error moving file ${filename} to temp directory: ${e}`
)
}
}
}

core.debug(
Expand Down Expand Up @@ -89,7 +100,6 @@ export class PrivateKubectl extends Kubectl {
}

private createTempManifestsDirectory() {
const manifestsDir = '/tmp/manifests'
if (!fs.existsSync('/tmp/manifests')) {
fs.mkdirSync('/tmp/manifests', {recursive: true})
}
Expand Down Expand Up @@ -128,24 +138,22 @@ export class PrivateKubectl extends Kubectl {
}
}

export function replaceFileNamesWithNamesRelativeToTemp(kubectlCmd: string) {
export function replaceFileNamesWithBaseNames(kubectlCmd: string) {
let filenames = extractFileNames(kubectlCmd)
let relativeNames = filenames.map((filename) =>
path.relative(getTempDirectory(), filename)
)
let basenames = filenames.map((filename) => path.basename(filename))

let result = kubectlCmd
if (filenames.length != relativeNames.length) {
if (filenames.length != basenames.length) {
throw Error(
'replacing filenames with relative path from temp dir, ' +
'replacing filenames with basenames, ' +
filenames.length +
' filenames != ' +
relativeNames.length +
basenames.length +
'basenames'
)
}
for (let index = 0; index < filenames.length; index++) {
result = result.replace(filenames[index], relativeNames[index])
result = result.replace(filenames[index], basenames[index])
}
return result
}
Expand Down

0 comments on commit d61b384

Please sign in to comment.