From d61b3841cf35cd1593696f7140378e91fde5ec5d Mon Sep 17 00:00:00 2001 From: Jaiveer Katariya Date: Wed, 31 Jul 2024 16:27:23 -0400 Subject: [PATCH] Reverting private logic --- src/types/privatekubectl.test.ts | 25 +++++++++---------------- src/types/privatekubectl.ts | 32 ++++++++++++++++++++------------ 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/types/privatekubectl.test.ts b/src/types/privatekubectl.test.ts index 2da0c29a2..3b735f25b 100644 --- a/src/types/privatekubectl.test.ts +++ b/src/types/privatekubectl.test.ts @@ -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', @@ -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` ) }) diff --git a/src/types/privatekubectl.ts b/src/types/privatekubectl.ts index efb1ad3f8..1d3d96a23 100644 --- a/src/types/privatekubectl.ts +++ b/src/types/privatekubectl.ts @@ -13,13 +13,14 @@ export class PrivateKubectl extends Kubectl { let kubectlCmd = args.join(' ') let addFileFlag = false let eo = { - 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 } @@ -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( @@ -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}) } @@ -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 }