benjamin ac0b58c9a5
Add timeout to the rollout status (#425)
* Added timeout to the rollout status and tests for it

* Fixed integration test errors

* Fix for blue green integration test

* Probable fix for integration errors

* No jobs run error fixed

* Changed timeout to file level constant

* Added parsing logic for timeout

* Made tests more concise

* implemented timeout validation check in an extracted utils mod

* Changed function name to parseDuration

* Removed timeout parameter from getResource

---------

Co-authored-by: David Gamero <david340804@gmail.com>
Co-authored-by: Suneha Bose <123775811+bosesuneha@users.noreply.github.com>
2025-07-09 10:22:21 -07:00

151 lines
4.2 KiB
TypeScript

import {sleep} from '../../utilities/timeUtils'
import {RouteStrategy} from '../../types/routeStrategy'
import {Kubectl} from '../../types/kubectl'
import {
BlueGreenDeployment,
BlueGreenManifests
} from '../../types/blueGreenTypes'
import {
getManifestObjects,
GREEN_LABEL_VALUE,
deployObjects
} from './blueGreenHelper'
import {
getUpdatedBlueGreenIngress,
isIngressRouted
} from './ingressBlueGreenHelper'
import {getUpdatedBlueGreenService} from './serviceBlueGreenHelper'
import {createTrafficSplitObject} from './smiBlueGreenHelper'
import * as core from '@actions/core'
import {K8sObject, TrafficSplitObject} from '../../types/k8sObject'
import {getBufferTime} from '../../inputUtils'
export async function routeBlueGreenForDeploy(
kubectl: Kubectl,
inputManifestFiles: string[],
routeStrategy: RouteStrategy,
timeout?: string
): Promise<BlueGreenDeployment> {
// sleep for buffer time
const bufferTime: number = getBufferTime()
const startSleepDate = new Date()
core.info(
`Starting buffer time of ${bufferTime} minute(s) at ${startSleepDate.toISOString()}`
)
await sleep(bufferTime * 1000 * 60)
const endSleepDate = new Date()
core.info(
`Stopping buffer time of ${bufferTime} minute(s) at ${endSleepDate.toISOString()}`
)
const manifestObjects: BlueGreenManifests =
getManifestObjects(inputManifestFiles)
// route to new deployments
if (routeStrategy == RouteStrategy.INGRESS) {
return await routeBlueGreenIngress(
kubectl,
manifestObjects.serviceNameMap,
manifestObjects.ingressEntityList,
timeout
)
} else if (routeStrategy == RouteStrategy.SMI) {
return await routeBlueGreenSMI(
kubectl,
GREEN_LABEL_VALUE,
manifestObjects.serviceEntityList,
timeout
)
} else {
return await routeBlueGreenService(
kubectl,
GREEN_LABEL_VALUE,
manifestObjects.serviceEntityList,
timeout
)
}
}
export async function routeBlueGreenIngress(
kubectl: Kubectl,
serviceNameMap: Map<string, string>,
ingressEntityList: any[],
timeout?: string
): Promise<BlueGreenDeployment> {
// const newObjectsList = []
const newObjectsList: K8sObject[] = ingressEntityList.map((obj) => {
if (isIngressRouted(obj, serviceNameMap)) {
const newBlueGreenIngressObject = getUpdatedBlueGreenIngress(
obj,
serviceNameMap,
GREEN_LABEL_VALUE
)
return newBlueGreenIngressObject
} else {
core.debug(`unrouted ingress detected ${obj.metadata.name}`)
return obj
}
})
const deployResult = await deployObjects(kubectl, newObjectsList, timeout)
return {deployResult, objects: newObjectsList}
}
export async function routeBlueGreenIngressUnchanged(
kubectl: Kubectl,
serviceNameMap: Map<string, string>,
ingressEntityList: any[],
timeout?: string
): Promise<BlueGreenDeployment> {
const objects = ingressEntityList.filter((ingress) =>
isIngressRouted(ingress, serviceNameMap)
)
const deployResult = await deployObjects(kubectl, objects, timeout)
return {deployResult, objects}
}
export async function routeBlueGreenService(
kubectl: Kubectl,
nextLabel: string,
serviceEntityList: any[],
timeout?: string
): Promise<BlueGreenDeployment> {
const objects = serviceEntityList.map((serviceObject) =>
getUpdatedBlueGreenService(serviceObject, nextLabel)
)
const deployResult = await deployObjects(kubectl, objects, timeout)
return {deployResult, objects}
}
export async function routeBlueGreenSMI(
kubectl: Kubectl,
nextLabel: string,
serviceEntityList: any[],
timeout?: string
): Promise<BlueGreenDeployment> {
// let tsObjects: TrafficSplitObject[] = []
const tsObjects: TrafficSplitObject[] = await Promise.all(
serviceEntityList.map(async (serviceObject) => {
const tsObject: TrafficSplitObject = await createTrafficSplitObject(
kubectl,
serviceObject.metadata.name,
nextLabel,
timeout
)
return tsObject
})
)
const deployResult = await deployObjects(kubectl, tsObjects, timeout)
return {deployResult, objects: tsObjects}
}