mirror of
https://github.com/Azure/k8s-deploy.git
synced 2026-04-03 10:52:16 +08:00
* fix ns bug * add tests * rename some variables * rename ns to namespace * fix delete + correctly type * add typing to input obj parser
96 lines
2.5 KiB
TypeScript
96 lines
2.5 KiB
TypeScript
import * as core from '@actions/core'
|
|
import {ExecOutput} from '@actions/exec'
|
|
import {Kubectl} from '../types/kubectl'
|
|
|
|
const NAMESPACE = 'namespace'
|
|
|
|
export function checkForErrors(
|
|
execResults: ExecOutput[],
|
|
warnIfError?: boolean
|
|
) {
|
|
let stderr = ''
|
|
execResults.forEach((result) => {
|
|
if (result?.exitCode !== 0) {
|
|
stderr += result?.stderr + ' \n'
|
|
} else if (result?.stderr) {
|
|
core.warning(result.stderr)
|
|
}
|
|
})
|
|
|
|
if (stderr.length > 0) {
|
|
if (warnIfError) {
|
|
core.warning(stderr.trim())
|
|
} else {
|
|
throw new Error(stderr.trim())
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function getLastSuccessfulRunSha(
|
|
kubectl: Kubectl,
|
|
namespaceName: string,
|
|
annotationKey: string
|
|
): Promise<string> {
|
|
try {
|
|
const result = await kubectl.getResource(
|
|
NAMESPACE,
|
|
namespaceName,
|
|
false,
|
|
namespaceName
|
|
)
|
|
if (result?.stderr) {
|
|
core.warning(result.stderr)
|
|
return process.env.GITHUB_SHA
|
|
} else if (result?.stdout) {
|
|
const annotationsSet = JSON.parse(result.stdout).metadata.annotations
|
|
if (annotationsSet && annotationsSet[annotationKey]) {
|
|
return JSON.parse(annotationsSet[annotationKey].replace(/'/g, '"'))
|
|
.commit
|
|
} else {
|
|
return 'NA'
|
|
}
|
|
}
|
|
} catch (ex) {
|
|
core.warning(`Failed to get commits from cluster. ${JSON.stringify(ex)}`)
|
|
return ''
|
|
}
|
|
}
|
|
|
|
export async function annotateChildPods(
|
|
kubectl: Kubectl,
|
|
resourceType: string,
|
|
resourceName: string,
|
|
namespace: string | undefined,
|
|
annotationKeyValStr: string,
|
|
allPods
|
|
): Promise<ExecOutput[]> {
|
|
let owner = resourceName
|
|
if (resourceType.toLowerCase().indexOf('deployment') > -1) {
|
|
owner = await kubectl.getNewReplicaSet(resourceName, namespace)
|
|
}
|
|
|
|
const commandExecutionResults = []
|
|
if (allPods?.items && allPods.items?.length > 0) {
|
|
allPods.items.forEach((pod) => {
|
|
const owners = pod?.metadata?.ownerReferences
|
|
if (owners) {
|
|
for (const ownerRef of owners) {
|
|
if (ownerRef.name === owner) {
|
|
commandExecutionResults.push(
|
|
kubectl.annotate(
|
|
'pod',
|
|
pod.metadata.name,
|
|
annotationKeyValStr,
|
|
namespace
|
|
)
|
|
)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
return await Promise.all(commandExecutionResults)
|
|
}
|