Skip to content

Commit

Permalink
Just skip pods that are marked as terminating
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaster11 committed Nov 28, 2023
1 parent 0d97ccd commit ced5473
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 122 deletions.
236 changes: 118 additions & 118 deletions __tests__/locks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ describe('test isLocked', () => {
`
const gitSha = 'abc123'
const image = `prod.artifactor.ee/serviceName:${gitSha}`
const podsStdout = `${image}
${image}
${image}
${image}
${image}
${image}
${image}
${image}
${image}
const podsStdout = `${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
`
const runKubectlMock = mocked(runKubectl)
runKubectlMock.mockImplementation(
Expand Down Expand Up @@ -73,15 +73,15 @@ ${image}
`
const gitSha = 'abc123'
const image = `cache.artifactor.ee/serviceName:${gitSha}`
const podsStdout = `${image}
${image}
${image}
${image}
${image}
${image}
${image}
${image}
${image}
const podsStdout = `${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
`
const runKubectlMock = mocked(runKubectl)
runKubectlMock.mockImplementation(
Expand All @@ -107,20 +107,20 @@ ${image}

test('no locked deployments, one image version multiple containers', async () => {
const deploymentsStdout = `<none>
<none>
<none>
<none> <none>
<none> <none>
`
const gitSha = 'abc123'
const image = `prod.artifactor.ee/serviceName:${gitSha}`
const podsStdout = `${image}
${image}
${image}
${image}
${image}
${image}
${image}
${image}
${image},otherimage:latest
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image},otherimage:latest <none>
`
const runKubectlMock = mocked(runKubectl)
runKubectlMock.mockImplementation(
Expand All @@ -145,22 +145,22 @@ ${image},otherimage:latest
})

test('no locked deployments, two image versions', async () => {
const deploymentsStdout = `<none>
const deploymentsStdout = `<none> <none>
<none>
<none>
`
const gitSha = 'abc123'
const image = `prod.artifactor.ee/serviceName:${gitSha}`
const podsStdout = `${image}
${image}
${image}
${image}
${image}
${image}
${image}
${image}
${image}
prod.artifactor.ee/serviceName:567def
const podsStdout = `${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
prod.artifactor.ee/serviceName:567def <none>
`
const runKubectlMock = mocked(runKubectl)
runKubectlMock.mockImplementation(
Expand Down Expand Up @@ -194,17 +194,17 @@ prod.artifactor.ee/serviceName:567def
`
const gitSha = 'abc123'
const image = `prod.artifactor.ee/serviceName:${gitSha}`
const podsStdout = `${image}
${image}
${image}
${image}
${image}
${image}
${image}
${image}
${image},other:1.0.0
prod.artifactor.ee/serviceName:567def
prod.artifactor.ee/serviceName:567def,other:latest
const podsStdout = `${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image},other:1.0.0 <none>
prod.artifactor.ee/serviceName:567def <none>
prod.artifactor.ee/serviceName:567def,other:latest <none>
`
const runKubectlMock = mocked(runKubectl)
runKubectlMock.mockImplementation(
Expand Down Expand Up @@ -240,15 +240,15 @@ prod.artifactor.ee/serviceName:567def,other:latest
`
const gitSha = 'abc123'
const image = `prod.artifactor.ee/otherthing:${gitSha}`
const podsStdout = `${image}
${image}
${image}
${image}
${image}
${image}
${image}
${image}
${image}
const podsStdout = `${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
`
const runKubectlMock = mocked(runKubectl)
runKubectlMock.mockImplementation(
Expand Down Expand Up @@ -311,15 +311,15 @@ true
`
const gitSha = 'abc123'
const image = `prod.artifactor.ee/serviceName:${gitSha}`
const podsStdout = `${image}
${image}
${image}
${image}
${image}
${image}
${image}
${image}
${image}
const podsStdout = `${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
`
const runKubectlMock = mocked(runKubectl)
runKubectlMock.mockImplementation(
Expand Down Expand Up @@ -357,16 +357,16 @@ true
`
const gitSha = 'abc123'
const image = `prod.artifactor.ee/serviceName:${gitSha}`
const podsStdout = `${image}
${image}
${image}
${image}
${image}
${image}
${image}
${image}
${image}
prod.artifactor.ee/serviceName:def678
const podsStdout = `${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
prod.artifactor.ee/serviceName:def678 <none>
`
const runKubectlMock = mocked(runKubectl)
runKubectlMock.mockImplementation(
Expand Down Expand Up @@ -403,16 +403,16 @@ true
`
const gitSha = 'abc123'
const image = `prod.artifactor.ee/serviceName:${gitSha}`
const podsStdout = `${image}
${image}
${image}
${image}
${image}
${image}
${image}
${image}
${image}
otherthing:latest,prod.artifactor.ee/serviceName:def678
const podsStdout = `${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
otherthing:latest,prod.artifactor.ee/serviceName:def678 <none>
`
const runKubectlMock = mocked(runKubectl)
runKubectlMock.mockImplementation(
Expand Down Expand Up @@ -449,16 +449,16 @@ true
`
const gitSha = 'abc123'
const image = `prod.artifactor.ee/serviceName:${gitSha}`
const podsStdout = `${image}
${image}
${image}
${image}
${image}
${image}
${image}
${image}
${image}
prod.artifactor.ee/serviceName:def678,otherthing:latest
const podsStdout = `${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
prod.artifactor.ee/serviceName:def678,otherthing:latest <none>
`
const runKubectlMock = mocked(runKubectl)
runKubectlMock.mockImplementation(
Expand Down Expand Up @@ -522,17 +522,17 @@ prod.artifactor.ee/serviceName:def678,otherthing:latest
`
const gitSha = 'abc123'
const image = `prod.artifactor.ee/serviceName:${gitSha}`
const podsStdout = `${image}
${image}
${image}
${image}
${image}
${image}
${image}
${image}
${image},other:1.0.0
prod.artifactor.ee/serviceName:567def
prod.artifactor.ee/serviceName:567def,other:latest
const podsStdout = `${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image},other:1.0.0 <none>
prod.artifactor.ee/serviceName:567def <none>
prod.artifactor.ee/serviceName:567def,other:latest <none>
`
const runKubectlMock = mocked(runKubectl)
runKubectlMock.mockImplementation(
Expand Down Expand Up @@ -566,15 +566,15 @@ prod.artifactor.ee/serviceName:567def,other:latest
`
const gitSha = 'abc123'
const image = `prod.artifactor.ee/otherthing:${gitSha}`
const podsStdout = `${image}
${image}
${image}
${image}
${image}
${image}
${image}
${image}
${image}
const podsStdout = `${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
${image} <none>
`
const runKubectlMock = mocked(runKubectl)
runKubectlMock.mockImplementation(
Expand Down
5 changes: 4 additions & 1 deletion src/kubectl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export function stringToArray(value: string, sep?: string): string[] {
if (!trimmed) {
return []
}
return value.trim().split(separator)
return value
.trim()
.split(separator)
.filter(e => e !== '')
}

export function uniq(items: string[]): string[] {
Expand Down
10 changes: 7 additions & 3 deletions src/locks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ export async function isLocked(
'--selector=canary!=true',
'--no-headers',
'-o',
'custom-columns=NAME:.spec.containers[*].image,PHASE:.status.phase'
'custom-columns=NAME:.spec.containers[*].image,TIMESTAMP:.metadata.deletionTimestamp'
])
const images = uniq(
stringToArray(imagesRaw)
.map(image => stringToArray(image, ','))
.filter(([, phase]) => phase !== 'Succeeded' && phase !== 'Failed')
// Split into image/timestamp
.map(image => stringToArray(image, ' '))
// Only pods that have no deletion timestamp set
.filter(([, deletionTimestamp]) => deletionTimestamp === '<none>')
.map(([image]) => image)
// Extract all images from the list
.map(image => stringToArray(image, ','))
.flat()
.filter(value => {
return imageRegex.test(value)
Expand Down

0 comments on commit ced5473

Please sign in to comment.