Compare commits

..

1 Commits

Author SHA1 Message Date
Oliver King ebc294c887 add clean function 2022-07-06 16:07:01 -04:00
24 changed files with 2112 additions and 3019 deletions
-27
View File
@@ -74,9 +74,6 @@ Following are the key capabilities of this action:
<td>traffic-split-method </br></br>(Optional)</td>
<td>Acceptable values: pod/smi.<br> Default value: pod <br>SMI: Percentage traffic split is done at request level using service mesh. Service mesh has to be setup by cluster admin. Orchestration of <a href="https://github.com/servicemeshinterface/smi-spec/blob/master/apis/traffic-split/v1alpha3/traffic-split.md" data-raw-source="TrafficSplit](https://github.com/deislabs/smi-spec/blob/master/traffic-split.md)">TrafficSplit</a> objects of SMI is handled by this action. <br>Pod: Percentage split not possible at request level in the absence of service mesh. Percentage input is used to calculate the replicas for baseline and canary as a percentage of replicas specified in the input manifests for the stable variant.</td>
</tr>
<tr>
<td>traffic-split-annotations </br></br>(Optional)</td>
<td>Annotations in the form of key/value pair to be added to TrafficSplit.</td>
<tr>
<td>percentage </br></br>(Optional but required if strategy is canary)</td>
<td>Used to compute the number of replicas of &#39;-baseline&#39; and &#39;-canary&#39; variants of the workloads found in manifest files. For the specified percentage input, if (percentage * numberOfDesirerdReplicas)/100 is not a round number, the floor of this number is used while creating &#39;-baseline&#39; and &#39;-canary&#39;.<br/><br/>For example, if Deployment hello-world was found in the input manifest file with &#39;replicas: 4&#39; and if &#39;strategy: canary&#39; and &#39;percentage: 25&#39; are given as inputs to the action, then the Deployments hello-world-baseline and hello-world-canary are created with 1 replica each. The &#39;-baseline&#39; variant is created with the same image and tag as the stable version (4 replica variant prior to deployment) while the &#39;-canary&#39; variant is created with the image and tag corresponding to the new changes being deployed</td>
@@ -96,10 +93,6 @@ Following are the key capabilities of this action:
<td>version-switch-buffer </br></br>(Optional and relevant only if strategy is blue-green)</td>
<td>Acceptable values: 1-300.</br>Default value: 0.</br>Waits for the given input in minutes before routing traffic to '-green' workloads.</td>
</tr>
<tr>
<td>private-cluster </br></br>(Optional and relevant only using K8's deploy for a cluster with private cluster enabled)</td>
<td>Acceptable values: true, false</br>Default value: false.</td>
</tr>
<tr>
<td>force </br></br>(Optional)</td>
<td>Deploy when a previous deployment already exists. If true then '--force' argument is added to the apply command. Using '--force' argument is not recommended in production.</td>
@@ -126,26 +119,6 @@ Following are the key capabilities of this action:
image-pull-secret2
```
### Private cluster deployment
```yaml
- uses: Azure/k8s-deploy@v4
with:
resource-group: yourResourceGroup
name: yourClusterName
action: deploy
strategy: basic
private-cluster: true
manifests: |
manifests/azure-vote-backend-deployment.yaml
manifests/azure-vote-backend-service.yaml
manifests/azure-vote-frontend-deployment.yaml
manifests/azure-vote-frontend-service.yaml
images: |
registry.azurecr.io/containername
```
### Canary deployment without service mesh
```yaml
-13
View File
@@ -35,9 +35,6 @@ inputs:
description: 'Traffic split method to be used. Allowed values are pod and smi'
required: false
default: 'pod'
traffic-split-annotations:
description: 'Annotations in the form of key/value pair to be added to TrafficSplit. Relevant only if deployement strategy is blue-green or canary'
required: false
baseline-and-canary-replicas:
description: 'Baseline and canary replicas count. Valid value between 0 to 100 (inclusive)'
required: false
@@ -62,16 +59,6 @@ inputs:
description: 'Annotate the target namespace'
required: false
default: true
private-cluster:
description: 'True if cluster is AKS private cluster'
required: false
default: false
resource-group:
description: 'Name of resource group - Only required if using private cluster'
required: false
name:
description: 'Resource group name - Only required if using private cluster'
required: false
branding:
color: 'green'
+1984 -2400
View File
File diff suppressed because it is too large Load Diff
+2 -1
View File
@@ -23,8 +23,9 @@
"@types/jest": "^26.0.0",
"@types/js-yaml": "^3.12.7",
"@types/node": "^12.20.41",
"@vercel/ncc": "^0.34.0",
"jest": "^26.0.0",
"prettier": "^2.7.1",
"prettier": "2.7.1",
"ts-jest": "^26.0.0",
"typescript": "3.9.5"
}
+2 -4
View File
@@ -19,8 +19,7 @@ import {parseRouteStrategy} from '../types/routeStrategy'
export async function deploy(
kubectl: Kubectl,
manifestFilePaths: string[],
deploymentStrategy: DeploymentStrategy,
annotations: {[key: string]: string} = {}
deploymentStrategy: DeploymentStrategy
) {
// update manifests
const inputManifestFiles: string[] = updateManifestFiles(manifestFilePaths)
@@ -35,8 +34,7 @@ export async function deploy(
inputManifestFiles,
deploymentStrategy,
kubectl,
trafficSplitMethod,
annotations
trafficSplitMethod
)
core.endGroup()
core.debug('Deployed manifest files: ' + deployedManifestFiles)
+4 -10
View File
@@ -40,15 +40,14 @@ import {parseRouteStrategy, RouteStrategy} from '../types/routeStrategy'
export async function promote(
kubectl: Kubectl,
manifests: string[],
deploymentStrategy: DeploymentStrategy,
annotations: {[key: string]: string} = {}
deploymentStrategy: DeploymentStrategy
) {
switch (deploymentStrategy) {
case DeploymentStrategy.CANARY:
await promoteCanary(kubectl, manifests)
break
case DeploymentStrategy.BLUE_GREEN:
await promoteBlueGreen(kubectl, manifests, annotations)
await promoteBlueGreen(kubectl, manifests)
break
default:
throw Error('Invalid promote deployment strategy')
@@ -105,11 +104,7 @@ async function promoteCanary(kubectl: Kubectl, manifests: string[]) {
core.endGroup()
}
async function promoteBlueGreen(
kubectl: Kubectl,
manifests: string[],
annotations: {[key: string]: string} = {}
) {
async function promoteBlueGreen(kubectl: Kubectl, manifests: string[]) {
// update container images and pull secrets
const inputManifestFiles: string[] = updateManifestFiles(manifests)
const manifestObjects: BlueGreenManifests =
@@ -162,8 +157,7 @@ async function promoteBlueGreen(
await routeBlueGreenSMI(
kubectl,
NONE_LABEL_VALUE,
manifestObjects.serviceEntityList,
annotations
manifestObjects.serviceEntityList
)
await deleteWorkloadsWithLabel(
kubectl,
+4 -9
View File
@@ -15,15 +15,14 @@ import {parseRouteStrategy, RouteStrategy} from '../types/routeStrategy'
export async function reject(
kubectl: Kubectl,
manifests: string[],
deploymentStrategy: DeploymentStrategy,
annotations: {[key: string]: string} = {}
deploymentStrategy: DeploymentStrategy
) {
switch (deploymentStrategy) {
case DeploymentStrategy.CANARY:
await rejectCanary(kubectl, manifests)
break
case DeploymentStrategy.BLUE_GREEN:
await rejectBlueGreen(kubectl, manifests, annotations)
await rejectBlueGreen(kubectl, manifests)
break
default:
throw 'Invalid delete deployment strategy'
@@ -55,11 +54,7 @@ async function rejectCanary(kubectl: Kubectl, manifests: string[]) {
core.endGroup()
}
async function rejectBlueGreen(
kubectl: Kubectl,
manifests: string[],
annotations: {[key: string]: string} = {}
) {
async function rejectBlueGreen(kubectl: Kubectl, manifests: string[]) {
core.startGroup('Rejecting deployment with blue green strategy')
const routeStrategy = parseRouteStrategy(
@@ -68,7 +63,7 @@ async function rejectBlueGreen(
if (routeStrategy == RouteStrategy.INGRESS) {
await rejectBlueGreenIngress(kubectl, manifests)
} else if (routeStrategy == RouteStrategy.SMI) {
await rejectBlueGreenSMI(kubectl, manifests, annotations)
await rejectBlueGreenSMI(kubectl, manifests)
} else {
await rejectBlueGreenService(kubectl, manifests)
}
+5 -22
View File
@@ -6,8 +6,6 @@ import {reject} from './actions/reject'
import {Action, parseAction} from './types/action'
import {parseDeploymentStrategy} from './types/deploymentStrategy'
import {getFilesFromDirectories} from './utilities/fileUtils'
import {PrivateKubectl} from './types/privatekubectl'
import {parseAnnotations} from './types/annotations'
export async function run() {
// verify kubeconfig is set
@@ -20,9 +18,6 @@ export async function run() {
const action: Action | undefined = parseAction(
core.getInput('action', {required: true})
)
const annotations = parseAnnotations(
core.getInput('annotations', {required: false})
)
const strategy = parseDeploymentStrategy(core.getInput('strategy'))
const manifestsInput = core.getInput('manifests', {required: true})
const manifestFilePaths = manifestsInput
@@ -31,35 +26,23 @@ export async function run() {
.filter((manifest) => manifest.length > 0) // remove any blanks
const fullManifestFilePaths = getFilesFromDirectories(manifestFilePaths)
// create kubectl
const kubectlPath = await getKubectlPath()
const namespace = core.getInput('namespace') || 'default'
const isPrivateCluster =
core.getInput('private-cluster').toLowerCase() === 'true'
const resourceGroup = core.getInput('resource-group') || ''
const resourceName = core.getInput('name') || ''
const kubectl = isPrivateCluster
? new PrivateKubectl(
kubectlPath,
namespace,
true,
resourceGroup,
resourceName
)
: new Kubectl(kubectlPath, namespace, true)
const kubectl = new Kubectl(kubectlPath, namespace, true)
// run action
switch (action) {
case Action.DEPLOY: {
await deploy(kubectl, fullManifestFilePaths, strategy, annotations)
await deploy(kubectl, fullManifestFilePaths, strategy)
break
}
case Action.PROMOTE: {
await promote(kubectl, fullManifestFilePaths, strategy, annotations)
await promote(kubectl, fullManifestFilePaths, strategy)
break
}
case Action.REJECT: {
await reject(kubectl, fullManifestFilePaths, strategy, annotations)
await reject(kubectl, fullManifestFilePaths, strategy)
break
}
default: {
@@ -1,128 +0,0 @@
import {
createWorkloadsWithLabel,
deleteWorkloadsAndServicesWithLabel,
getManifestObjects,
getNewBlueGreenObject,
GREEN_LABEL_VALUE,
isServiceRouted,
NONE_LABEL_VALUE
} from './blueGreenHelper'
import * as bgHelper from './blueGreenHelper'
import {Kubectl} from '../../types/kubectl'
import * as fileHelper from '../../utilities/fileUtils'
jest.mock('../../types/kubectl')
describe('bluegreenhelper functions', () => {
let testObjects
beforeEach(() => {
//@ts-ignore
Kubectl.mockClear()
testObjects = getManifestObjects(['test/unit/manifests/test-ingress.yml'])
jest
.spyOn(fileHelper, 'writeObjectsToFile')
.mockImplementationOnce(() => [''])
})
test('it should parse objects correctly from one file', () => {
expect(testObjects.deploymentEntityList[0].kind).toBe('Deployment')
expect(testObjects.serviceEntityList[0].kind).toBe('Service')
expect(testObjects.ingressEntityList[0].kind).toBe('Ingress')
expect(
testObjects.deploymentEntityList[0].spec.selector.matchLabels.app
).toBe('nginx')
})
test('correctly makes new blue green object', () => {
const modifiedDeployment = getNewBlueGreenObject(
testObjects.deploymentEntityList[0],
GREEN_LABEL_VALUE
)
//@ts-ignore
expect(modifiedDeployment.metadata.name).toBe('nginx-deployment-green')
//@ts-ignore
expect(modifiedDeployment.metadata.labels['k8s.deploy.color']).toBe(
'green'
)
const modifiedSvc = getNewBlueGreenObject(
testObjects.serviceEntityList[0],
GREEN_LABEL_VALUE
)
//@ts-ignore
expect(modifiedSvc.metadata.name).toBe('nginx-service-green')
//@ts-ignore
expect(modifiedSvc.metadata.labels['k8s.deploy.color']).toBe('green')
})
test('correctly makes labeled workloads', () => {
const kubectl = new Kubectl('')
expect(Kubectl).toBeCalledTimes(1)
const cwlResult = createWorkloadsWithLabel(
kubectl,
testObjects.deploymentEntityList,
GREEN_LABEL_VALUE
)
cwlResult.then((value) => {
//@ts-ignore
expect(value.newFilePaths[0]).toBe('')
})
})
test('correctly classifies routed services', () => {
expect(
isServiceRouted(
testObjects.serviceEntityList[0],
testObjects.deploymentEntityList
)
).toBe(true)
testObjects.serviceEntityList[0].spec.selector.app = 'fakeapp'
expect(
isServiceRouted(
testObjects.serviceEntityList[0],
testObjects.deploymentEntityList
)
).toBe(false)
})
test('correctly deletes services and workloads according to label', () => {
const kubectl = new Kubectl('')
jest.spyOn(bgHelper, 'deleteObjects').mockReturnValue({} as Promise<void>)
let objectsToDelete = deleteWorkloadsAndServicesWithLabel(
kubectl,
NONE_LABEL_VALUE,
testObjects.deploymentEntityList,
testObjects.serviceEntityList
)
objectsToDelete.then((value) => {
expect(value).toHaveLength(2)
expect(value).toContainEqual
;({name: 'nginx-service', kind: 'Service'})
expect(value).toContainEqual({
name: 'nginx-deployment',
kind: 'Deployment'
})
})
objectsToDelete = deleteWorkloadsAndServicesWithLabel(
kubectl,
GREEN_LABEL_VALUE,
testObjects.deploymentEntityList,
testObjects.serviceEntityList
)
objectsToDelete.then((value) => {
expect(value).toHaveLength(2)
expect(value).toContainEqual({
name: 'nginx-service-green',
kind: 'Service'
})
expect(value).toContainEqual({
name: 'nginx-deployment-green',
kind: 'Deployment'
})
})
})
})
@@ -40,8 +40,7 @@ export interface BlueGreenManifests {
export async function routeBlueGreen(
kubectl: Kubectl,
inputManifestFiles: string[],
routeStrategy: RouteStrategy,
annotations: {[key: string]: string} = {}
routeStrategy: RouteStrategy
) {
// sleep for buffer time
const bufferTime: number = parseInt(
@@ -75,8 +74,7 @@ export async function routeBlueGreen(
await routeBlueGreenSMI(
kubectl,
GREEN_LABEL_VALUE,
manifestObjects.serviceEntityList,
annotations
manifestObjects.serviceEntityList
)
} else {
await routeBlueGreenService(
@@ -112,7 +110,6 @@ export async function deleteWorkloadsWithLabel(
})
await deleteObjects(kubectl, resourcesToDelete)
return resourcesToDelete
}
export async function deleteWorkloadsAndServicesWithLabel(
@@ -144,7 +141,6 @@ export async function deleteWorkloadsAndServicesWithLabel(
})
await deleteObjects(kubectl, resourcesToDelete)
return resourcesToDelete
}
export async function deleteObjects(kubectl: Kubectl, deleteList: any[]) {
@@ -239,6 +235,9 @@ export async function createWorkloadsWithLabel(
deploymentObjectList.forEach((inputObject) => {
// creating deployment with label
const newBlueGreenObject = getNewBlueGreenObject(inputObject, nextLabel)
core.debug(
'New blue-green object is: ' + JSON.stringify(newBlueGreenObject)
)
newObjectsList.push(newBlueGreenObject)
})
@@ -279,7 +278,7 @@ export function addBlueGreenLabelsAndAnnotations(
updateObjectLabels(inputObject, newLabels, false)
updateSelectorLabels(inputObject, newLabels, false)
// updating spec labels if it is not a service
// updating spec labels if it is a service
if (!isServiceEntity(inputObject.kind)) {
updateSpecLabels(inputObject, newLabels, false)
}
@@ -1,88 +0,0 @@
import {getManifestObjects, GREEN_LABEL_VALUE} from './blueGreenHelper'
import {
deployBlueGreenIngress,
getUpdatedBlueGreenIngress,
isIngressRouted,
routeBlueGreenIngress
} from './ingressBlueGreenHelper'
import {Kubectl} from '../../types/kubectl'
import * as fileHelper from '../../utilities/fileUtils'
jest.mock('../../types/kubectl')
describe('ingress blue green helpers', () => {
let testObjects
const betaFilepath = ['test/unit/manifests/test-ingress.yml']
const ingressFilepath = ['test/unit/manifests/test-ingress-new.yml']
beforeEach(() => {
//@ts-ignore
Kubectl.mockClear()
testObjects = getManifestObjects(ingressFilepath)
jest
.spyOn(fileHelper, 'writeObjectsToFile')
.mockImplementationOnce(() => [''])
})
test('it should correctly classify ingresses', () => {
expect(
isIngressRouted(
testObjects.ingressEntityList[0],
testObjects.serviceNameMap
)
).toBe(true)
testObjects.ingressEntityList[0].spec.rules[0].http.paths = {}
expect(
isIngressRouted(
testObjects.ingressEntityList[0],
testObjects.serviceNameMap
)
).toBe(false)
expect(
isIngressRouted(
getManifestObjects(betaFilepath).ingressEntityList[0],
testObjects.serviceNameMap
)
).toBe(true)
})
test('it should correctly update ingresses', () => {
const updatedIng = getUpdatedBlueGreenIngress(
testObjects.ingressEntityList[0],
testObjects.serviceNameMap,
GREEN_LABEL_VALUE
)
//@ts-ignore
expect(updatedIng.metadata.labels['k8s.deploy.color']).toBe('green')
//@ts-ignore
expect(updatedIng.spec.rules[0].http.paths[0].backend.service.name).toBe(
'nginx-service-green'
)
})
test('correctly prepares blue/green ingresses for deployment', () => {
const kc = new Kubectl('')
const generatedObjects = routeBlueGreenIngress(
kc,
GREEN_LABEL_VALUE,
testObjects.serviceNameMap,
testObjects.ingressEntityList
)
generatedObjects.then((value) => {
expect(value).toHaveLength(1)
//@ts-ignore
expect(value[0].metadata.name).toBe('nginx-ingress')
})
})
test('correctly deploys services', () => {
const kc = new Kubectl('')
const result = deployBlueGreenIngress(kc, ingressFilepath)
result.then((value) => {
const nol = value.newObjectsList
//@ts-ignore
expect(nol[0].metadata.name).toBe('nginx-service-green')
})
})
})
@@ -14,7 +14,7 @@ import {
} from './blueGreenHelper'
import * as core from '@actions/core'
const BACKEND = 'backend'
const BACKEND = 'BACKEND'
export async function deployBlueGreenIngress(
kubectl: Kubectl,
@@ -24,12 +24,13 @@ export async function deployBlueGreenIngress(
const manifestObjects: BlueGreenManifests = getManifestObjects(filePaths)
// create deployments with green label value
const workloadDeployment = await createWorkloadsWithLabel(
const result = createWorkloadsWithLabel(
kubectl,
manifestObjects.deploymentEntityList,
GREEN_LABEL_VALUE
)
// create new services and other objects
let newObjectsList = []
manifestObjects.serviceEntityList.forEach((inputObject) => {
const newBlueGreenObject = getNewBlueGreenObject(
@@ -45,12 +46,7 @@ export async function deployBlueGreenIngress(
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}
return result
}
export async function promoteBlueGreenIngress(
@@ -58,13 +54,14 @@ export async function promoteBlueGreenIngress(
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()
if (
!validateIngressesState(
kubectl,
manifestObjects.ingressEntityList,
manifestObjects.serviceNameMap
)
) {
throw 'Ingress not in promote state'
}
// create stable deployments with new configuration
@@ -141,18 +138,17 @@ export async function routeBlueGreenIngress(
})
}
core.debug('New objects: ' + JSON.stringify(newObjectsList))
const manifestFiles = fileHelper.writeObjectsToFile(newObjectsList)
await kubectl.apply(manifestFiles)
return newObjectsList
}
export function validateIngresses(
export function validateIngressesState(
kubectl: Kubectl,
ingressEntityList: any[],
serviceNameMap: Map<string, string>
): {areValid: boolean; invalidIngresses: string[]} {
let areValid: boolean = true
const invalidIngresses = []
): boolean {
let areIngressesTargetingNewServices: boolean = true
ingressEntityList.forEach(async (inputObject) => {
if (isIngressRouted(inputObject, serviceNameMap)) {
//querying existing ingress
@@ -162,32 +158,33 @@ export function validateIngresses(
inputObject.metadata.name
)
let isValid =
!!existingIngress &&
existingIngress?.metadata?.labels[BLUE_GREEN_VERSION_LABEL] ===
GREEN_LABEL_VALUE
if (!isValid) {
invalidIngresses.push(inputObject.metadata.name)
if (!!existingIngress) {
const currentLabel: string =
existingIngress?.metadata?.labels[BLUE_GREEN_VERSION_LABEL]
// if not green label, then wrong configuration
if (currentLabel != GREEN_LABEL_VALUE)
areIngressesTargetingNewServices = false
} else {
// no ingress at all, so nothing to promote
areIngressesTargetingNewServices = false
}
// to be valid, ingress should exist and should be green
areValid = areValid && isValid
}
})
return {areValid, invalidIngresses}
return areIngressesTargetingNewServices
}
export function isIngressRouted(
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))
if (key === 'serviceName' && serviceNameMap.has(value)) {
isIngressRouted = true
}
return value
})
@@ -209,18 +206,15 @@ export function getUpdatedBlueGreenIngress(
addBlueGreenLabelsAndAnnotations(newObject, type)
// update ingress labels
if (inputObject.apiVersion === 'networking.k8s.io/v1beta1') {
return updateIngressBackendBetaV1(newObject, serviceNameMap)
}
return updateIngressBackend(newObject, serviceNameMap)
}
export function updateIngressBackendBetaV1(
export function updateIngressBackend(
inputObject: any,
serviceNameMap: Map<string, string>
): any {
inputObject = JSON.parse(JSON.stringify(inputObject), (key, value) => {
if (key.toLowerCase() === BACKEND) {
if (key.toUpperCase() === BACKEND) {
const {serviceName} = value
if (serviceNameMap.has(serviceName)) {
// update service name with corresponding bluegreen name only if service is provied in given manifests
@@ -233,20 +227,3 @@ export function updateIngressBackendBetaV1(
return inputObject
}
export function updateIngressBackend(
inputObject: any,
serviceNameMap: Map<string, string>
): any {
inputObject = JSON.parse(JSON.stringify(inputObject), (key, value) => {
if (
key.toLowerCase() === BACKEND &&
serviceNameMap.has(value?.service?.name)
) {
value.service.name = serviceNameMap.get(value.service.name)
}
return value
})
return inputObject
}
@@ -19,21 +19,21 @@ export async function deployBlueGreenService(
const manifestObjects: BlueGreenManifests = getManifestObjects(filePaths)
// create deployments with green label value
const workloadDeployment = await createWorkloadsWithLabel(
const result = await createWorkloadsWithLabel(
kubectl,
manifestObjects.deploymentEntityList,
GREEN_LABEL_VALUE
)
// create other non deployment and non service entities
const newObjectsList = manifestObjects.otherObjects
.concat(manifestObjects.ingressEntityList)
.concat(manifestObjects.unroutedServiceEntityList)
const manifestFiles = fileHelper.writeObjectsToFile(newObjectsList)
if (manifestFiles.length > 0) await kubectl.apply(manifestFiles)
// returning deployment details to check for rollout stability
return {workloadDeployment, newObjectsList}
return result
}
export async function promoteBlueGreenService(
@@ -76,6 +76,7 @@ export async function rejectBlueGreenService(
manifestObjects.deploymentEntityList
)
}
export async function routeBlueGreenService(
kubectl: Kubectl,
nextLabel: string,
@@ -23,8 +23,7 @@ const MAX_VAL = 100
export async function deployBlueGreenSMI(
kubectl: Kubectl,
filePaths: string[],
annotations: {[key: string]: string} = {}
filePaths: string[]
) {
// get all kubernetes objects defined in manifest files
const manifestObjects: BlueGreenManifests = getManifestObjects(filePaths)
@@ -38,16 +37,14 @@ export async function deployBlueGreenSMI(
await kubectl.apply(manifestFiles)
// make extraservices and trafficsplit
await setupSMI(kubectl, manifestObjects.serviceEntityList, annotations)
await setupSMI(kubectl, manifestObjects.serviceEntityList)
// create new deloyments
const workloadDeployment = await createWorkloadsWithLabel(
return await createWorkloadsWithLabel(
kubectl,
manifestObjects.deploymentEntityList,
GREEN_LABEL_VALUE
)
return {workloadDeployment, newObjectsList}
}
export async function promoteBlueGreenSMI(kubectl: Kubectl, manifestObjects) {
@@ -71,18 +68,16 @@ export async function promoteBlueGreenSMI(kubectl: Kubectl, manifestObjects) {
export async function rejectBlueGreenSMI(
kubectl: Kubectl,
filePaths: string[],
annotations: {[key: string]: string} = {}
filePaths: string[]
) {
// get all kubernetes objects defined in manifest files
const manifestObjects: BlueGreenManifests = getManifestObjects(filePaths)
// route trafficsplit to stable deployments
// route trafficsplit to stable deploymetns
await routeBlueGreenSMI(
kubectl,
NONE_LABEL_VALUE,
manifestObjects.serviceEntityList,
annotations
manifestObjects.serviceEntityList
)
// delete rejected new bluegreen deployments
@@ -96,11 +91,7 @@ export async function rejectBlueGreenSMI(
await cleanupSMI(kubectl, manifestObjects.serviceEntityList)
}
export async function setupSMI(
kubectl: Kubectl,
serviceEntityList: any[],
annotations: {[key: string]: string} = {}
) {
export async function setupSMI(kubectl: Kubectl, serviceEntityList: any[]) {
const newObjectsList = []
const trafficObjectList = []
@@ -126,8 +117,7 @@ export async function setupSMI(
createTrafficSplitObject(
kubectl,
inputObject.metadata.name,
NONE_LABEL_VALUE,
annotations
NONE_LABEL_VALUE
)
})
}
@@ -137,8 +127,7 @@ let trafficSplitAPIVersion = ''
async function createTrafficSplitObject(
kubectl: Kubectl,
name: string,
nextLabel: string,
annotations: {[key: string]: string} = {}
nextLabel: string
): Promise<any> {
// cache traffic split api version
if (!trafficSplitAPIVersion)
@@ -156,8 +145,7 @@ async function createTrafficSplitObject(
apiVersion: trafficSplitAPIVersion,
kind: 'TrafficSplit',
metadata: {
name: getBlueGreenResourceName(name, TRAFFIC_SPLIT_OBJECT_NAME_SUFFIX),
annotations: annotations
name: getBlueGreenResourceName(name, TRAFFIC_SPLIT_OBJECT_NAME_SUFFIX)
},
spec: {
service: name,
@@ -206,16 +194,14 @@ export function getSMIServiceResource(
export async function routeBlueGreenSMI(
kubectl: Kubectl,
nextLabel: string,
serviceEntityList: any[],
annotations: {[key: string]: string} = {}
serviceEntityList: any[]
) {
for (const serviceObject of serviceEntityList) {
// route trafficsplit to given label
await createTrafficSplitObject(
kubectl,
serviceObject.metadata.name,
nextLabel,
annotations
nextLabel
)
}
}
@@ -288,8 +288,7 @@ async function getTrafficSplitObject(
name: string,
stableWeight: number,
baselineWeight: number,
canaryWeight: number,
annotations: {[key: string]: string} = {}
canaryWeight: number
): Promise<string> {
// cached version
if (!trafficSplitAPIVersion) {
@@ -302,8 +301,7 @@ async function getTrafficSplitObject(
apiVersion: trafficSplitAPIVersion,
kind: 'TrafficSplit',
metadata: {
name: getTrafficSplitResourceName(name),
annotations: annotations
name: getTrafficSplitResourceName(name)
},
spec: {
backends: [
+6 -7
View File
@@ -41,8 +41,7 @@ export async function deployManifests(
files: string[],
deploymentStrategy: DeploymentStrategy,
kubectl: Kubectl,
trafficSplitMethod: TrafficSplitMethod,
annotations: {[key: string]: string} = {}
trafficSplitMethod: TrafficSplitMethod
): Promise<string[]> {
switch (deploymentStrategy) {
case DeploymentStrategy.CANARY: {
@@ -60,16 +59,16 @@ export async function deployManifests(
core.getInput('route-method', {required: true})
)
const {workloadDeployment, newObjectsList} = await Promise.resolve(
const {result, newFilePaths} = await Promise.resolve(
(routeStrategy == RouteStrategy.INGRESS &&
deployBlueGreenIngress(kubectl, files)) ||
(routeStrategy == RouteStrategy.SMI &&
deployBlueGreenSMI(kubectl, files, annotations)) ||
deployBlueGreenSMI(kubectl, files)) ||
deployBlueGreenService(kubectl, files)
)
checkForErrors([workloadDeployment.result])
return workloadDeployment.newFilePaths
checkForErrors([result])
return newFilePaths
}
case DeploymentStrategy.BASIC: {
@@ -143,7 +142,7 @@ export async function annotateAndLabelResources(
const workflowFilePath = await getWorkflowFilePath(githubToken)
const deploymentConfig = await getDeploymentConfig()
const annotationKeyLabel = getWorkflowAnnotationKeyLabel()
const annotationKeyLabel = getWorkflowAnnotationKeyLabel(workflowFilePath)
await annotateResources(
files,
-8
View File
@@ -1,8 +0,0 @@
export function parseAnnotations(str: string) {
if (str == '') {
return {}
} else {
const annotaion = JSON.parse(str)
return new Map(annotaion)
}
}
+5 -12
View File
@@ -10,25 +10,18 @@ export interface Resource {
}
export class Kubectl {
protected readonly kubectlPath: string
protected readonly namespace: string
protected readonly ignoreSSLErrors: boolean
protected readonly resourceGroup: string
protected readonly name: string
protected isPrivateCluster: boolean
private readonly kubectlPath: string
private readonly namespace: string
private readonly ignoreSSLErrors: boolean
constructor(
kubectlPath: string,
namespace: string = 'default',
ignoreSSLErrors: boolean = false,
resourceGroup: string = '',
name: string = ''
ignoreSSLErrors: boolean = false
) {
this.kubectlPath = kubectlPath
this.ignoreSSLErrors = !!ignoreSSLErrors
this.namespace = namespace
this.resourceGroup = resourceGroup
this.name = name
}
public async apply(
@@ -162,7 +155,7 @@ export class Kubectl {
return this.execute(['delete', ...args])
}
protected async execute(args: string[], silent: boolean = false) {
private async execute(args: string[], silent: boolean = false) {
if (this.ignoreSSLErrors) {
args.push('--insecure-skip-tls-verify')
}
-135
View File
@@ -1,135 +0,0 @@
import {Kubectl} from './kubectl'
import {ExecOptions, ExecOutput, getExecOutput} from '@actions/exec'
import * as core from '@actions/core'
import * as os from 'os'
import * as fs from 'fs'
import * as path from 'path'
export class PrivateKubectl extends Kubectl {
protected async execute(args: string[], silent: boolean = false) {
args.unshift('kubectl')
let kubectlCmd = args.join(' ')
let addFileFlag = false
let eo = <ExecOptions>{silent}
if (this.containsFilenames(kubectlCmd)) {
// For private clusters, files will referenced solely by their basename
kubectlCmd = this.replaceFilnamesWithBasenames(kubectlCmd)
addFileFlag = true
}
const privateClusterArgs = [
'aks',
'command',
'invoke',
'--resource-group',
this.resourceGroup,
'--name',
this.name,
'--command',
kubectlCmd
]
if (addFileFlag) {
const filenames = this.extractFilesnames(kubectlCmd).split(' ')
const tempDirectory =
process.env['runner.tempDirectory'] || os.tmpdir() + '/manifests'
eo.cwd = tempDirectory
privateClusterArgs.push(...['--file', '.'])
let filenamesArr = filenames[0].split(',')
for (let index = 0; index < filenamesArr.length; index++) {
const file = filenamesArr[index]
if (!file) {
continue
}
this.moveFileToTempManifestDir(file)
}
}
core.debug(
`private cluster Kubectl run with invoke command: ${kubectlCmd}`
)
return await getExecOutput('az', privateClusterArgs, eo)
}
private replaceFilnamesWithBasenames(kubectlCmd: string) {
let exFilenames = this.extractFilesnames(kubectlCmd)
let filenames = exFilenames.split(' ')
let filenamesArr = filenames[0].split(',')
for (let index = 0; index < filenamesArr.length; index++) {
filenamesArr[index] = path.basename(filenamesArr[index])
}
let baseFilenames = filenamesArr.join()
let result = kubectlCmd.replace(exFilenames, baseFilenames)
return result
}
public extractFilesnames(strToParse: string) {
let start = strToParse.indexOf('-filename')
let offset = 7
if (start == -1) {
start = strToParse.indexOf('-f')
if (start == -1) {
return ''
}
offset = 0
}
let temp = strToParse.substring(start + offset)
let end = temp.indexOf(' -')
//End could be case where the -f flag was last, or -f is followed by some additonal flag and it's arguments
return temp.substring(3, end == -1 ? temp.length : end).trim()
}
private containsFilenames(str: string) {
return str.includes('-f ') || str.includes('filename ')
}
private createTempManifestsDirectory() {
const manifestsDir = '/tmp/manifests'
if (!fs.existsSync('/tmp/manifests')) {
fs.mkdirSync('/tmp/manifests', {recursive: true})
}
}
private moveFileToTempManifestDir(file: string) {
this.createTempManifestsDirectory()
if (!fs.existsSync('/tmp/' + file)) {
core.debug(
'/tmp/' +
file +
' does not exist, and therefore cannot be moved to the manifest directory'
)
}
fs.copyFile('/tmp/' + file, '/tmp/manifests/' + file, function (err) {
if (err) {
core.debug(
'Could not rename ' +
'/tmp/' +
file +
' to ' +
'/tmp/manifests/' +
file +
' ERROR: ' +
err
)
return
}
core.debug(
"Successfully moved file '" +
file +
"' from /tmp to /tmp/manifest directory"
)
})
}
}
+2 -3
View File
@@ -12,12 +12,11 @@ describe('File utils', () => {
'test/unit/manifests/manifest_test_dir/another_layer/deep-service.yaml',
'test/unit/manifests/manifest_test_dir/nested-test-service.yaml',
'test/unit/manifests/test-ingress.yml',
'test/unit/manifests/test-ingress-new.yml',
'test/unit/manifests/test-service.yml'
]
// is there a more efficient way to test equality w random order?
expect(testSearch).toHaveLength(6)
expect(testSearch).toHaveLength(5)
expectedManifests.forEach((fileName) => {
expect(testSearch).toContain(fileName)
})
@@ -54,7 +53,7 @@ describe('File utils', () => {
expect(
getFilesFromDirectories([outerPath, fileAtOuter, innerPath])
).toHaveLength(6)
).toHaveLength(5)
})
})
+1 -1
View File
@@ -57,7 +57,7 @@ export function writeManifestToFile(
}
function getManifestFileName(kind: string, name: string) {
const filePath = `${kind}_${name}_${getCurrentTime().toString()}`
const filePath = `${kind}_${name}_ ${getCurrentTime().toString()}`
const tempDirectory = getTempDirectory()
return path.join(tempDirectory, path.basename(filePath))
}
+19 -1
View File
@@ -1,6 +1,24 @@
import {cleanLabel} from '../utilities/workflowAnnotationUtils'
import {
cleanLabel,
prefixObjectKeys
} from '../utilities/workflowAnnotationUtils'
describe('WorkflowAnnotationUtils', () => {
describe('prefixObjectKeys', () => {
it('should prefix an object with a given prefix', () => {
const obj = {
foo: 'bar',
baz: 'qux'
}
const prefix = 'prefix.'
const expected = {
'prefix.foo': 'bar',
'prefix.baz': 'qux'
}
expect(prefixObjectKeys(obj, prefix)).toEqual(expected)
})
})
describe('cleanLabel', () => {
it('should clean label', () => {
const alreadyClean = 'alreadyClean'
+21 -4
View File
@@ -1,6 +1,13 @@
import {DeploymentConfig} from '../types/deploymentConfig'
const ANNOTATION_PREFIX = 'actions.github.com'
const ANNOTATION_PREFIX = 'actions.github.com/'
export function prefixObjectKeys(obj: any, prefix: string): any {
return Object.keys(obj).reduce((newObj, key) => {
newObj[prefix + key] = obj[key]
return newObj
}, {})
}
export function getWorkflowAnnotations(
lastSuccessRunSha: string,
@@ -24,11 +31,21 @@ export function getWorkflowAnnotations(
helmChartPaths: deploymentConfig.helmChartFilePaths,
provider: 'GitHub'
}
return JSON.stringify(annotationObject)
const prefixedAnnotationObject = prefixObjectKeys(
annotationObject,
ANNOTATION_PREFIX
)
return JSON.stringify(prefixedAnnotationObject)
}
export function getWorkflowAnnotationKeyLabel(): string {
return `${ANNOTATION_PREFIX}/k8s-deploy`
export function getWorkflowAnnotationKeyLabel(
workflowFilePath: string
): string {
const hashKey = require('crypto')
.createHash('MD5')
.update(`${process.env.GITHUB_REPOSITORY}/${workflowFilePath}`)
.digest('hex')
return `githubWorkflow_${hashKey}`
}
/**
-50
View File
@@ -1,50 +0,0 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /testpath
backend:
service:
name: nginx-service
port:
number: 80