mirror of
https://github.com/Azure/k8s-deploy.git
synced 2026-06-29 08:19:26 +08:00
Blue/Green Refactor (#229)
* fresh new branch * Added coverage to gitignore Signed-off-by: Jaiveer Katariya <jaiveerkatariya@Jaiveers-MBP.lan> * reverted package-lock.json Signed-off-by: Jaiveer Katariya <jaiveerkatariya@Jaiveers-MBP.lan> Co-authored-by: Jaiveer Katariya <jaiveerkatariya@Jaiveers-MBP.lan>
This commit is contained in:
@@ -1,209 +1,20 @@
|
||||
import {Kubectl} from '../../types/kubectl'
|
||||
import * as fileHelper from '../../utilities/fileUtils'
|
||||
import * as core from '@actions/core'
|
||||
import {K8sIngress} from '../../types/k8sObject'
|
||||
import {
|
||||
addBlueGreenLabelsAndAnnotations,
|
||||
BLUE_GREEN_VERSION_LABEL,
|
||||
BlueGreenManifests,
|
||||
createWorkloadsWithLabel,
|
||||
deleteWorkloadsAndServicesWithLabel,
|
||||
fetchResource,
|
||||
getManifestObjects,
|
||||
getNewBlueGreenObject,
|
||||
GREEN_LABEL_VALUE,
|
||||
NONE_LABEL_VALUE
|
||||
fetchResource
|
||||
} from './blueGreenHelper'
|
||||
import * as core from '@actions/core'
|
||||
import {Kubectl} from '../../types/kubectl'
|
||||
|
||||
const BACKEND = 'backend'
|
||||
|
||||
export async function deployBlueGreenIngress(
|
||||
kubectl: Kubectl,
|
||||
filePaths: string[]
|
||||
) {
|
||||
// get all kubernetes objects defined in manifest files
|
||||
const manifestObjects: BlueGreenManifests = getManifestObjects(filePaths)
|
||||
|
||||
// create deployments with green label value
|
||||
const workloadDeployment = await createWorkloadsWithLabel(
|
||||
kubectl,
|
||||
manifestObjects.deploymentEntityList,
|
||||
GREEN_LABEL_VALUE
|
||||
)
|
||||
|
||||
let newObjectsList = []
|
||||
manifestObjects.serviceEntityList.forEach((inputObject) => {
|
||||
const newBlueGreenObject = getNewBlueGreenObject(
|
||||
inputObject,
|
||||
GREEN_LABEL_VALUE
|
||||
)
|
||||
newObjectsList.push(newBlueGreenObject)
|
||||
})
|
||||
newObjectsList = newObjectsList
|
||||
.concat(manifestObjects.otherObjects)
|
||||
.concat(manifestObjects.unroutedServiceEntityList)
|
||||
|
||||
const manifestFiles = fileHelper.writeObjectsToFile(newObjectsList)
|
||||
await kubectl.apply(manifestFiles)
|
||||
|
||||
core.debug(
|
||||
'new objects after processing services and other objects: \n' +
|
||||
JSON.stringify(newObjectsList)
|
||||
)
|
||||
|
||||
return {workloadDeployment, newObjectsList}
|
||||
}
|
||||
|
||||
export async function promoteBlueGreenIngress(
|
||||
kubectl: Kubectl,
|
||||
manifestObjects
|
||||
) {
|
||||
//checking if anything to promote
|
||||
const {areValid, invalidIngresses} = validateIngresses(
|
||||
kubectl,
|
||||
manifestObjects.ingressEntityList,
|
||||
manifestObjects.serviceNameMap
|
||||
)
|
||||
if (!areValid) {
|
||||
throw 'Ingresses are not in promote state' + invalidIngresses.toString()
|
||||
}
|
||||
|
||||
// create stable deployments with new configuration
|
||||
const result = createWorkloadsWithLabel(
|
||||
kubectl,
|
||||
manifestObjects.deploymentEntityList,
|
||||
NONE_LABEL_VALUE
|
||||
)
|
||||
|
||||
// create stable services with new configuration
|
||||
const newObjectsList = []
|
||||
manifestObjects.serviceEntityList.forEach((inputObject) => {
|
||||
const newBlueGreenObject = getNewBlueGreenObject(
|
||||
inputObject,
|
||||
NONE_LABEL_VALUE
|
||||
)
|
||||
newObjectsList.push(newBlueGreenObject)
|
||||
})
|
||||
|
||||
const manifestFiles = fileHelper.writeObjectsToFile(newObjectsList)
|
||||
await kubectl.apply(manifestFiles)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
export async function rejectBlueGreenIngress(
|
||||
kubectl: Kubectl,
|
||||
filePaths: string[]
|
||||
) {
|
||||
// get all kubernetes objects defined in manifest files
|
||||
const manifestObjects: BlueGreenManifests = getManifestObjects(filePaths)
|
||||
|
||||
// route ingress to stables services
|
||||
await routeBlueGreenIngress(
|
||||
kubectl,
|
||||
null,
|
||||
manifestObjects.serviceNameMap,
|
||||
manifestObjects.ingressEntityList
|
||||
)
|
||||
|
||||
// delete green services and deployments
|
||||
await deleteWorkloadsAndServicesWithLabel(
|
||||
kubectl,
|
||||
GREEN_LABEL_VALUE,
|
||||
manifestObjects.deploymentEntityList,
|
||||
manifestObjects.serviceEntityList
|
||||
)
|
||||
}
|
||||
|
||||
export async function routeBlueGreenIngress(
|
||||
kubectl: Kubectl,
|
||||
nextLabel: string,
|
||||
serviceNameMap: Map<string, string>,
|
||||
ingressEntityList: any[]
|
||||
) {
|
||||
let newObjectsList = []
|
||||
|
||||
if (!nextLabel) {
|
||||
newObjectsList = ingressEntityList.filter((ingress) =>
|
||||
isIngressRouted(ingress, serviceNameMap)
|
||||
)
|
||||
} else {
|
||||
ingressEntityList.forEach((inputObject) => {
|
||||
if (isIngressRouted(inputObject, serviceNameMap)) {
|
||||
const newBlueGreenIngressObject = getUpdatedBlueGreenIngress(
|
||||
inputObject,
|
||||
serviceNameMap,
|
||||
GREEN_LABEL_VALUE
|
||||
)
|
||||
newObjectsList.push(newBlueGreenIngressObject)
|
||||
} else {
|
||||
newObjectsList.push(inputObject)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const manifestFiles = fileHelper.writeObjectsToFile(newObjectsList)
|
||||
await kubectl.apply(manifestFiles)
|
||||
return newObjectsList
|
||||
}
|
||||
|
||||
export function validateIngresses(
|
||||
kubectl: Kubectl,
|
||||
ingressEntityList: any[],
|
||||
serviceNameMap: Map<string, string>
|
||||
): {areValid: boolean; invalidIngresses: string[]} {
|
||||
let areValid: boolean = true
|
||||
const invalidIngresses = []
|
||||
ingressEntityList.forEach(async (inputObject) => {
|
||||
if (isIngressRouted(inputObject, serviceNameMap)) {
|
||||
//querying existing ingress
|
||||
const existingIngress = await fetchResource(
|
||||
kubectl,
|
||||
inputObject.kind,
|
||||
inputObject.metadata.name
|
||||
)
|
||||
|
||||
let isValid =
|
||||
!!existingIngress &&
|
||||
existingIngress?.metadata?.labels[BLUE_GREEN_VERSION_LABEL] ===
|
||||
GREEN_LABEL_VALUE
|
||||
if (!isValid) {
|
||||
invalidIngresses.push(inputObject.metadata.name)
|
||||
}
|
||||
// to be valid, ingress should exist and should be green
|
||||
areValid = areValid && isValid
|
||||
}
|
||||
})
|
||||
|
||||
return {areValid, invalidIngresses}
|
||||
}
|
||||
|
||||
export function isIngressRouted(
|
||||
ingressObject: any,
|
||||
serviceNameMap: Map<string, string>
|
||||
): boolean {
|
||||
let isIngressRouted: boolean = false
|
||||
// check if ingress targets a service in the given manifests
|
||||
JSON.parse(JSON.stringify(ingressObject), (key, value) => {
|
||||
isIngressRouted =
|
||||
isIngressRouted || (key === 'service' && value.hasOwnProperty('name'))
|
||||
isIngressRouted =
|
||||
isIngressRouted || (key === 'serviceName' && serviceNameMap.has(value))
|
||||
|
||||
return value
|
||||
})
|
||||
|
||||
return isIngressRouted
|
||||
}
|
||||
|
||||
export function getUpdatedBlueGreenIngress(
|
||||
inputObject: any,
|
||||
serviceNameMap: Map<string, string>,
|
||||
type: string
|
||||
): object {
|
||||
if (!type) {
|
||||
return inputObject
|
||||
}
|
||||
|
||||
): K8sIngress {
|
||||
const newObject = JSON.parse(JSON.stringify(inputObject))
|
||||
// add green labels and values
|
||||
addBlueGreenLabelsAndAnnotations(newObject, type)
|
||||
@@ -241,7 +52,7 @@ export function updateIngressBackend(
|
||||
inputObject = JSON.parse(JSON.stringify(inputObject), (key, value) => {
|
||||
if (
|
||||
key.toLowerCase() === BACKEND &&
|
||||
serviceNameMap.has(value?.service?.name)
|
||||
serviceNameMap.has(value.service.name)
|
||||
) {
|
||||
value.service.name = serviceNameMap.get(value.service.name)
|
||||
}
|
||||
@@ -250,3 +61,60 @@ export function updateIngressBackend(
|
||||
|
||||
return inputObject
|
||||
}
|
||||
|
||||
export function isIngressRouted(
|
||||
ingressObject: any,
|
||||
serviceNameMap: Map<string, string>
|
||||
): boolean {
|
||||
let isIngressRouted: boolean = false
|
||||
// check if ingress targets a service in the given manifests
|
||||
JSON.parse(JSON.stringify(ingressObject), (key, value) => {
|
||||
isIngressRouted =
|
||||
isIngressRouted ||
|
||||
(key === 'service' &&
|
||||
value.hasOwnProperty('name') &&
|
||||
serviceNameMap.has(value.name))
|
||||
isIngressRouted =
|
||||
isIngressRouted || (key === 'serviceName' && serviceNameMap.has(value))
|
||||
|
||||
return value
|
||||
})
|
||||
|
||||
return isIngressRouted
|
||||
}
|
||||
|
||||
export async function validateIngresses(
|
||||
kubectl: Kubectl,
|
||||
ingressEntityList: any[],
|
||||
serviceNameMap: Map<string, string>
|
||||
): Promise<{areValid: boolean; invalidIngresses: string[]}> {
|
||||
let areValid: boolean = true
|
||||
const invalidIngresses = []
|
||||
|
||||
for (const inputObject of ingressEntityList) {
|
||||
if (isIngressRouted(inputObject, serviceNameMap)) {
|
||||
//querying existing ingress
|
||||
const existingIngress = await fetchResource(
|
||||
kubectl,
|
||||
inputObject.kind,
|
||||
inputObject.metadata.name
|
||||
)
|
||||
|
||||
const isValid =
|
||||
!!existingIngress &&
|
||||
existingIngress?.metadata?.labels[BLUE_GREEN_VERSION_LABEL] ===
|
||||
GREEN_LABEL_VALUE
|
||||
if (!isValid) {
|
||||
core.debug(
|
||||
`Invalid ingress detected (must be in green state): ${JSON.stringify(
|
||||
inputObject
|
||||
)}`
|
||||
)
|
||||
invalidIngresses.push(inputObject.metadata.name)
|
||||
}
|
||||
// to be valid, ingress should exist and should be green
|
||||
areValid = areValid && isValid
|
||||
}
|
||||
}
|
||||
return {areValid, invalidIngresses}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user