added oliver's feedback + unit test demonstrating regex glitch and fix

This commit is contained in:
Jaiveer Katariya
2022-12-12 14:17:40 -05:00
parent f507264575
commit 86aca609b9
3 changed files with 35 additions and 26 deletions
+13 -18
View File
@@ -162,25 +162,20 @@ export async function annotateAndLabelResources(
const deploymentConfig = await getDeploymentConfig() const deploymentConfig = await getDeploymentConfig()
const annotationKeyLabel = getWorkflowAnnotationKeyLabel() const annotationKeyLabel = getWorkflowAnnotationKeyLabel()
try {
await annotateResources(
files,
kubectl,
resourceTypes,
allPods,
annotationKeyLabel,
workflowFilePath,
deploymentConfig
)
} catch (ex) {
core.warning(`Failed to annotate resources: ${ex} `)
}
try { await annotateResources(
await labelResources(files, kubectl, annotationKeyLabel) files,
} catch (ex) { kubectl,
core.warning(`Failed to label resources: ${ex}`) resourceTypes,
} allPods,
annotationKeyLabel,
workflowFilePath,
deploymentConfig
).catch((err) => core.warning(`Failed to annotate resources: ${err} `))
await labelResources(files, kubectl, annotationKeyLabel).catch((err) =>
core.warning(`Failed to label resources: ${err}`)
)
} }
async function annotateResources( async function annotateResources(
+9 -8
View File
@@ -73,17 +73,18 @@ export class Kubectl {
core.debug('stdout from getNewReplicaSet is ' + JSON.stringify(stdout)) core.debug('stdout from getNewReplicaSet is ' + JSON.stringify(stdout))
stdout.forEach((line: string) => { stdout.forEach((line: string) => {
const newreplicaset = 'newreplicaset' const newreplicaset = 'newreplicaset'
if (line && line.toLowerCase().indexOf(newreplicaset) > -1) if (line && line.toLowerCase().indexOf(newreplicaset) > -1) {
core.debug( core.debug(
`found string of interest for replicaset, line is ${line}` `found string of interest for replicaset, line is ${line}`
) )
core.debug( core.debug(
`substring is ${line.substring(newreplicaset.length).trim()}` `substring is ${line.substring(newreplicaset.length).trim()}`
) )
newReplicaSet = line newReplicaSet = line
.substring(newreplicaset.length) .substring(newreplicaset.length)
.trim() .trim()
.split(' ')[0] .split(' ')[0]
}
}) })
} }
@@ -16,5 +16,18 @@ describe('WorkflowAnnotationUtils', () => {
cleanLabel('Workflow Name / With Slashes / And Spaces') cleanLabel('Workflow Name / With Slashes / And Spaces')
).toEqual('Workflow_Name_-_With_Slashes_-_And_Spaces') ).toEqual('Workflow_Name_-_With_Slashes_-_And_Spaces')
}) })
it('should return a blank string when regex fails (https://github.com/Azure/k8s-deploy/issues/266)', () => {
const label = '持续部署'
expect(cleanLabel(label)).toEqual('')
let removedInvalidChars = label
.replace(/\s/gi, '_')
.replace(/[\/\\\|]/gi, '-')
.replace(/[^-A-Za-z0-9_.]/gi, '')
const regex = /([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]/
const regexResult = regex.exec(removedInvalidChars)
expect(regexResult).toBe(null)
})
}) })
}) })