k8s-deploy/src/actions/deploy.ts
Vidya Reddy 71e93a71d4
Added Traffic split annotations (#215)
* Added Traffic split annotations

* traffic split - blueGreen deployment

* traffic split - canary deployment

* Traffic split annotations - canary deployment

* updated Readme and action.yml

* Traffic split - canary deployment

* clean code

* Clean code

* Clean code

* Create annotation object

* Updated Readme and action.yml

* Spelling correction

Co-authored-by: Vidya Reddy <vidyareddy@microsoft.com>
2022-07-25 13:43:13 -04:00

93 lines
2.9 KiB
TypeScript

import * as core from '@actions/core'
import * as models from '../types/kubernetesTypes'
import * as KubernetesConstants from '../types/kubernetesTypes'
import {Kubectl, Resource} from '../types/kubectl'
import {
getResources,
updateManifestFiles
} from '../utilities/manifestUpdateUtils'
import {routeBlueGreen} from '../strategyHelpers/blueGreen/blueGreenHelper'
import {
annotateAndLabelResources,
checkManifestStability,
deployManifests
} from '../strategyHelpers/deploymentHelper'
import {DeploymentStrategy} from '../types/deploymentStrategy'
import {parseTrafficSplitMethod} from '../types/trafficSplitMethod'
import {parseRouteStrategy} from '../types/routeStrategy'
export async function deploy(
kubectl: Kubectl,
manifestFilePaths: string[],
deploymentStrategy: DeploymentStrategy,
annotations: {[key: string]: string} = {}
) {
// update manifests
const inputManifestFiles: string[] = updateManifestFiles(manifestFilePaths)
core.debug('Input manifest files: ' + inputManifestFiles)
// deploy manifests
core.startGroup('Deploying manifests')
const trafficSplitMethod = parseTrafficSplitMethod(
core.getInput('traffic-split-method', {required: true})
)
const deployedManifestFiles = await deployManifests(
inputManifestFiles,
deploymentStrategy,
kubectl,
trafficSplitMethod,
annotations
)
core.endGroup()
core.debug('Deployed manifest files: ' + deployedManifestFiles)
// check manifest stability
core.startGroup('Checking manifest stability')
const resourceTypes: Resource[] = getResources(
deployedManifestFiles,
models.DEPLOYMENT_TYPES.concat([
KubernetesConstants.DiscoveryAndLoadBalancerResource.SERVICE
])
)
await checkManifestStability(kubectl, resourceTypes)
core.endGroup()
if (deploymentStrategy == DeploymentStrategy.BLUE_GREEN) {
core.startGroup('Routing blue green')
const routeStrategy = parseRouteStrategy(
core.getInput('route-method', {required: true})
)
await routeBlueGreen(kubectl, inputManifestFiles, routeStrategy)
core.endGroup()
}
// print ingresses
core.startGroup('Printing ingresses')
const ingressResources: Resource[] = getResources(deployedManifestFiles, [
KubernetesConstants.DiscoveryAndLoadBalancerResource.INGRESS
])
for (const ingressResource of ingressResources) {
await kubectl.getResource(
KubernetesConstants.DiscoveryAndLoadBalancerResource.INGRESS,
ingressResource.name
)
}
core.endGroup()
// annotate resources
core.startGroup('Annotating resources')
let allPods
try {
allPods = JSON.parse((await kubectl.getAllPods()).stdout)
} catch (e) {
core.debug('Unable to parse pods: ' + e)
}
await annotateAndLabelResources(
deployedManifestFiles,
kubectl,
resourceTypes,
allPods
)
core.endGroup()
}