Skip to content

Commit

Permalink
this might work
Browse files Browse the repository at this point in the history
  • Loading branch information
jaiveerk committed Aug 1, 2024
1 parent d61b384 commit bda065e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 32 deletions.
27 changes: 17 additions & 10 deletions src/types/privatekubectl.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import * as fileUtils from '../utilities/fileUtils'
import {
PrivateKubectl,
extractFileNames,
replaceFileNamesWithBaseNames
replaceFileNamesWithNamesRelativeToTemp
} from './privatekubectl'
import * as exec from '@actions/exec'

describe('Private kubectl', () => {
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 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 mockKube = new PrivateKubectl(
'kubectlPath',
'namespace',
Expand All @@ -15,19 +16,25 @@ describe('Private kubectl', () => {
'resourceName'
)

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

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

it('should replace filenames with basenames correctly', () => {
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`
it('should replace filenames with relative locations in tmp 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`
)
})

Expand Down
30 changes: 10 additions & 20 deletions src/types/privatekubectl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ export class PrivateKubectl extends Kubectl {
}

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

Expand All @@ -44,21 +43,9 @@ export class PrivateKubectl extends Kubectl {
]

if (addFileFlag) {
const filenames = extractFileNames(kubectlCmd)

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 @@ -138,22 +125,25 @@ export class PrivateKubectl extends Kubectl {
}
}

export function replaceFileNamesWithBaseNames(kubectlCmd: string) {
export function replaceFileNamesWithNamesRelativeToTemp(kubectlCmd: string) {
let filenames = extractFileNames(kubectlCmd)
let basenames = filenames.map((filename) => path.basename(filename))
core.debug(`filenames originally provided in kubectl command: ${filenames}`)
let relativeNames = filenames.map((filename) =>
path.relative(getTempDirectory(), filename)
)

let result = kubectlCmd
if (filenames.length != basenames.length) {
if (filenames.length != relativeNames.length) {
throw Error(
'replacing filenames with basenames, ' +
'replacing filenames with relative path from temp dir, ' +
filenames.length +
' filenames != ' +
basenames.length +
relativeNames.length +
'basenames'
)
}
for (let index = 0; index < filenames.length; index++) {
result = result.replace(filenames[index], basenames[index])
result = result.replace(filenames[index], relativeNames[index])
}
return result
}
Expand Down
5 changes: 3 additions & 2 deletions src/utilities/manifestUpdateUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as fileUtils from './fileUtils'
import * as manifestUpdateUtils from './manifestUpdateUtils'
import * as path from 'path'
import * as fs from 'fs'

describe('manifestUpdateUtils', () => {
jest.spyOn(fileUtils, 'getTempDirectory').mockImplementation(() => {
return '/tmp'
jest.spyOn(fileUtils, 'moveFileToTmpDir').mockImplementation((filename) => {
return path.join('/tmp', filename)
})
jest.spyOn(fs, 'writeFileSync').mockImplementation(() => {})
jest.spyOn(fs, 'readFileSync').mockImplementation((filename) => {
Expand Down

0 comments on commit bda065e

Please sign in to comment.