Initial commit for config files annotations

This commit is contained in:
Jyotsna 2020-11-17 18:15:45 +05:30
parent b371791f3a
commit bfca26e368
10 changed files with 11699 additions and 5158 deletions

View File

@ -25,7 +25,7 @@ ServiceTypes.clusterIP = 'ClusterIP';
exports.deploymentTypes = ['deployment', 'replicaset', 'daemonset', 'pod', 'statefulset']; exports.deploymentTypes = ['deployment', 'replicaset', 'daemonset', 'pod', 'statefulset'];
exports.workloadTypes = ['deployment', 'replicaset', 'daemonset', 'pod', 'statefulset', 'job', 'cronjob']; exports.workloadTypes = ['deployment', 'replicaset', 'daemonset', 'pod', 'statefulset', 'job', 'cronjob'];
exports.workloadTypesWithRolloutStatus = ['deployment', 'daemonset', 'statefulset']; exports.workloadTypesWithRolloutStatus = ['deployment', 'daemonset', 'statefulset'];
function getWorkflowAnnotationsJson(lastSuccessRunSha, workflowFilePath) { function getWorkflowAnnotationsJson(lastSuccessRunSha, workflowFilePath, filePathConfigs) {
return `{` return `{`
+ `'run': '${process.env.GITHUB_RUN_ID}',` + `'run': '${process.env.GITHUB_RUN_ID}',`
+ `'repository': '${process.env.GITHUB_REPOSITORY}',` + `'repository': '${process.env.GITHUB_REPOSITORY}',`
@ -38,6 +38,7 @@ function getWorkflowAnnotationsJson(lastSuccessRunSha, workflowFilePath) {
+ `'lastSuccessRunCommit': '${lastSuccessRunSha}',` + `'lastSuccessRunCommit': '${lastSuccessRunSha}',`
+ `'branch': '${process.env.GITHUB_REF}',` + `'branch': '${process.env.GITHUB_REF}',`
+ `'deployTimestamp': '${Date.now()}',` + `'deployTimestamp': '${Date.now()}',`
+ `'filePathConfigs': '${JSON.stringify(filePathConfigs)}',`
+ `'provider': 'GitHub'` + `'provider': 'GitHub'`
+ `}`; + `}`;
} }

35
lib/utilities/exec.js Normal file
View File

@ -0,0 +1,35 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.exec = void 0;
const aexec = require("@actions/exec");
exports.exec = (command, args = [], silent) => __awaiter(void 0, void 0, void 0, function* () {
let stdout = '';
let stderr = '';
const options = {
silent: silent,
ignoreReturnCode: true
};
options.listeners = {
stdout: (data) => {
stdout += data.toString();
},
stderr: (data) => {
stderr += data.toString();
}
};
const returnCode = yield aexec.exec(command, args, options);
return {
success: returnCode === 0,
stdout: stdout.trim(),
stderr: stderr.trim()
};
});

View File

@ -113,15 +113,16 @@ function checkManifestStability(kubectl, resources) {
function annotateAndLabelResources(files, kubectl, resourceTypes, allPods) { function annotateAndLabelResources(files, kubectl, resourceTypes, allPods) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const workflowFilePath = yield utility_1.getWorkflowFilePath(TaskInputParameters.githubToken); const workflowFilePath = yield utility_1.getWorkflowFilePath(TaskInputParameters.githubToken);
const filePathsConfig = yield utility_1.getFilePathsConfigs();
const annotationKeyLabel = models.getWorkflowAnnotationKeyLabel(workflowFilePath); const annotationKeyLabel = models.getWorkflowAnnotationKeyLabel(workflowFilePath);
annotateResources(files, kubectl, resourceTypes, allPods, annotationKeyLabel, workflowFilePath); annotateResources(files, kubectl, resourceTypes, allPods, annotationKeyLabel, workflowFilePath, filePathsConfig);
labelResources(files, kubectl, annotationKeyLabel); labelResources(files, kubectl, annotationKeyLabel);
}); });
} }
function annotateResources(files, kubectl, resourceTypes, allPods, annotationKey, workflowFilePath) { function annotateResources(files, kubectl, resourceTypes, allPods, annotationKey, workflowFilePath, filePathsConfig) {
const annotateResults = []; const annotateResults = [];
const lastSuccessSha = utility_1.getLastSuccessfulRunSha(kubectl, TaskInputParameters.namespace, annotationKey); const lastSuccessSha = utility_1.getLastSuccessfulRunSha(kubectl, TaskInputParameters.namespace, annotationKey);
let annotationKeyValStr = annotationKey + '=' + models.getWorkflowAnnotationsJson(lastSuccessSha, workflowFilePath); let annotationKeyValStr = annotationKey + '=' + models.getWorkflowAnnotationsJson(lastSuccessSha, workflowFilePath, filePathsConfig);
annotateResults.push(kubectl.annotate('namespace', TaskInputParameters.namespace, annotationKeyValStr)); annotateResults.push(kubectl.annotate('namespace', TaskInputParameters.namespace, annotationKeyValStr));
annotateResults.push(kubectl.annotateFiles(files, annotationKeyValStr)); annotateResults.push(kubectl.annotateFiles(files, annotationKeyValStr));
resourceTypes.forEach(resource => { resourceTypes.forEach(resource => {

View File

@ -9,11 +9,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
}); });
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.getCurrentTime = exports.getRandomInt = exports.sleep = exports.annotateChildPods = exports.getWorkflowFilePath = exports.getLastSuccessfulRunSha = exports.checkForErrors = exports.isEqual = exports.getExecutableExtension = void 0; exports.getCurrentTime = exports.getRandomInt = exports.sleep = exports.getFilePathsConfigs = exports.annotateChildPods = exports.getWorkflowFilePath = exports.getLastSuccessfulRunSha = exports.checkForErrors = exports.isEqual = exports.getExecutableExtension = void 0;
const os = require("os"); const os = require("os");
const core = require("@actions/core"); const core = require("@actions/core");
const githubClient_1 = require("../githubClient"); const githubClient_1 = require("../githubClient");
const httpClient_1 = require("./httpClient"); const httpClient_1 = require("./httpClient");
const exec = require("./exec");
const inputParams = require("../input-parameters");
function getExecutableExtension() { function getExecutableExtension() {
if (os.type().match(/^Win/)) { if (os.type().match(/^Win/)) {
return '.exe'; return '.exe';
@ -137,6 +139,67 @@ function annotateChildPods(kubectl, resourceType, resourceName, annotationKeyVal
return commandExecutionResults; return commandExecutionResults;
} }
exports.annotateChildPods = annotateChildPods; exports.annotateChildPods = annotateChildPods;
function getFilePathsConfigs() {
return __awaiter(this, void 0, void 0, function* () {
let filePathsConfig = {};
const BUILD_CONFIG_KEY = 'buildConfigs';
const MANIFEST_PATHS_KEY = 'manifestFilePaths';
const HELM_CHART_KEY = 'helmChartFilePaths';
const DOCKERFILE_PATH_LABEL_KEY = 'dockerfile-path';
const DOCKERFILE_PATH_KEY = 'dockerfilePath';
const CONTAINER_REG_KEY = 'containerRegistryServer';
let inputManifestFiles = inputParams.manifests;
filePathsConfig[MANIFEST_PATHS_KEY] = inputManifestFiles || '';
let helmChartPath = process.env.HELM_CHART_PATH || '';
filePathsConfig[HELM_CHART_KEY] = helmChartPath;
//Fetch labels from each image
let imageToBuildConfigMap = {};
let imageNames = core.getInput('images').split('\n');
for (const image of imageNames) {
let args = [image];
let resultObj;
let buildConfigMap = {};
let containerRegistryName = image.toString().split('/')[0];
try {
let usrname = process.env.CR_USERNAME || null;
let pwd = process.env.CR_PASSWORD || null;
if (pwd && usrname) {
let loginArgs = [containerRegistryName, '--username', usrname, '--password', pwd];
yield exec.exec('docker login ', loginArgs, true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(`docker login failed with: ${res.stderr.match(/(.*)\s*$/)[0]}`);
}
});
}
yield exec.exec('docker pull ', args, true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(`docker images pull failed with: ${res.stderr.match(/(.*)\s*$/)[0]}`);
}
});
yield exec.exec('docker inspect --type=image', args, true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(`docker inspect call failed with: ${res.stderr.match(/(.*)\s*$/)[0]}`);
}
resultObj = JSON.parse(res.stdout)[0];
});
}
catch (ex) {
core.warning(`Failed to get dockerfile paths for image ${image.toString()} | ` + ex);
}
if (resultObj != null && resultObj.Config != null && resultObj.Config.Labels != null) {
if (resultObj.Config.Labels[DOCKERFILE_PATH_LABEL_KEY] != null) {
buildConfigMap[DOCKERFILE_PATH_KEY] = resultObj.Config.Labels[DOCKERFILE_PATH_LABEL_KEY];
}
//Add CR server name to build config
buildConfigMap[CONTAINER_REG_KEY] = containerRegistryName;
imageToBuildConfigMap[resultObj.Id] = buildConfigMap;
}
}
filePathsConfig[BUILD_CONFIG_KEY] = imageToBuildConfigMap;
return Promise.resolve(filePathsConfig);
});
}
exports.getFilePathsConfigs = getFilePathsConfigs;
function sleep(timeout) { function sleep(timeout) {
return new Promise(resolve => setTimeout(resolve, timeout)); return new Promise(resolve => setTimeout(resolve, timeout));
} }

7237
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -8,16 +8,16 @@
"test": "jest" "test": "jest"
}, },
"dependencies": { "dependencies": {
"@actions/tool-cache": "^1.0.0",
"@actions/io": "^1.0.0",
"@actions/core": "^1.2.6", "@actions/core": "^1.2.6",
"@actions/exec": "^1.0.0", "@actions/exec": "^1.0.4",
"@actions/io": "^1.0.0",
"@actions/tool-cache": "^1.0.0",
"js-yaml": "3.13.1" "js-yaml": "3.13.1"
}, },
"devDependencies": { "devDependencies": {
"@types/jest": "^25.2.2",
"@types/node": "^12.0.10", "@types/node": "^12.0.10",
"jest": "^25.0.0", "jest": "^25.0.0",
"@types/jest": "^25.2.2",
"ts-jest": "^25.5.1", "ts-jest": "^25.5.1",
"typescript": "3.9.5" "typescript": "3.9.5"
} }

View File

@ -25,7 +25,7 @@ export const deploymentTypes: string[] = ['deployment', 'replicaset', 'daemonset
export const workloadTypes: string[] = ['deployment', 'replicaset', 'daemonset', 'pod', 'statefulset', 'job', 'cronjob']; export const workloadTypes: string[] = ['deployment', 'replicaset', 'daemonset', 'pod', 'statefulset', 'job', 'cronjob'];
export const workloadTypesWithRolloutStatus: string[] = ['deployment', 'daemonset', 'statefulset']; export const workloadTypesWithRolloutStatus: string[] = ['deployment', 'daemonset', 'statefulset'];
export function getWorkflowAnnotationsJson(lastSuccessRunSha: string, workflowFilePath: string): string { export function getWorkflowAnnotationsJson(lastSuccessRunSha: string, workflowFilePath: string, filePathConfigs: any): string {
return `{` return `{`
+ `'run': '${process.env.GITHUB_RUN_ID}',` + `'run': '${process.env.GITHUB_RUN_ID}',`
+ `'repository': '${process.env.GITHUB_REPOSITORY}',` + `'repository': '${process.env.GITHUB_REPOSITORY}',`
@ -38,6 +38,7 @@ export function getWorkflowAnnotationsJson(lastSuccessRunSha: string, workflowFi
+ `'lastSuccessRunCommit': '${lastSuccessRunSha}',` + `'lastSuccessRunCommit': '${lastSuccessRunSha}',`
+ `'branch': '${process.env.GITHUB_REF}',` + `'branch': '${process.env.GITHUB_REF}',`
+ `'deployTimestamp': '${Date.now()}',` + `'deployTimestamp': '${Date.now()}',`
+ `'filePathConfigs': '${JSON.stringify(filePathConfigs)}',`
+ `'provider': 'GitHub'` + `'provider': 'GitHub'`
+ `}`; + `}`;
} }

34
src/utilities/exec.ts Normal file
View File

@ -0,0 +1,34 @@
import * as aexec from '@actions/exec';
import {ExecOptions} from '@actions/exec';
export interface ExecResult {
success: boolean;
stdout: string;
stderr: string;
}
export const exec = async (command: string, args: string[] = [], silent?: boolean): Promise<ExecResult> => {
let stdout: string = '';
let stderr: string = '';
const options: ExecOptions = {
silent: silent,
ignoreReturnCode: true
};
options.listeners = {
stdout: (data: Buffer) => {
stdout += data.toString();
},
stderr: (data: Buffer) => {
stderr += data.toString();
}
};
const returnCode: number = await aexec.exec(command, args, options);
return {
success: returnCode === 0,
stdout: stdout.trim(),
stderr: stderr.trim()
};
};

View File

@ -17,7 +17,7 @@ import { IExecSyncResult } from '../../utilities/tool-runner';
import { deployPodCanary } from './pod-canary-deployment-helper'; import { deployPodCanary } from './pod-canary-deployment-helper';
import { deploySMICanary } from './smi-canary-deployment-helper'; import { deploySMICanary } from './smi-canary-deployment-helper';
import { checkForErrors, annotateChildPods, getWorkflowFilePath, getLastSuccessfulRunSha } from "../utility"; import { checkForErrors, annotateChildPods, getWorkflowFilePath, getLastSuccessfulRunSha, getFilePathsConfigs } from "../utility";
export async function deploy(kubectl: Kubectl, manifestFilePaths: string[], deploymentStrategy: string) { export async function deploy(kubectl: Kubectl, manifestFilePaths: string[], deploymentStrategy: string) {
@ -114,15 +114,16 @@ async function checkManifestStability(kubectl: Kubectl, resources: Resource[]):
async function annotateAndLabelResources(files: string[], kubectl: Kubectl, resourceTypes: Resource[], allPods: any) { async function annotateAndLabelResources(files: string[], kubectl: Kubectl, resourceTypes: Resource[], allPods: any) {
const workflowFilePath = await getWorkflowFilePath(TaskInputParameters.githubToken); const workflowFilePath = await getWorkflowFilePath(TaskInputParameters.githubToken);
const filePathsConfig = await getFilePathsConfigs();
const annotationKeyLabel = models.getWorkflowAnnotationKeyLabel(workflowFilePath); const annotationKeyLabel = models.getWorkflowAnnotationKeyLabel(workflowFilePath);
annotateResources(files, kubectl, resourceTypes, allPods, annotationKeyLabel, workflowFilePath); annotateResources(files, kubectl, resourceTypes, allPods, annotationKeyLabel, workflowFilePath, filePathsConfig);
labelResources(files, kubectl, annotationKeyLabel); labelResources(files, kubectl, annotationKeyLabel);
} }
function annotateResources(files: string[], kubectl: Kubectl, resourceTypes: Resource[], allPods: any, annotationKey: string, workflowFilePath: string) { function annotateResources(files: string[], kubectl: Kubectl, resourceTypes: Resource[], allPods: any, annotationKey: string, workflowFilePath: string, filePathsConfig: string) {
const annotateResults: IExecSyncResult[] = []; const annotateResults: IExecSyncResult[] = [];
const lastSuccessSha = getLastSuccessfulRunSha(kubectl, TaskInputParameters.namespace, annotationKey); const lastSuccessSha = getLastSuccessfulRunSha(kubectl, TaskInputParameters.namespace, annotationKey);
let annotationKeyValStr = annotationKey + '=' + models.getWorkflowAnnotationsJson(lastSuccessSha, workflowFilePath); let annotationKeyValStr = annotationKey + '=' + models.getWorkflowAnnotationsJson(lastSuccessSha, workflowFilePath, filePathsConfig);
annotateResults.push(kubectl.annotate('namespace', TaskInputParameters.namespace, annotationKeyValStr)); annotateResults.push(kubectl.annotate('namespace', TaskInputParameters.namespace, annotationKeyValStr));
annotateResults.push(kubectl.annotateFiles(files, annotationKeyValStr)); annotateResults.push(kubectl.annotateFiles(files, annotationKeyValStr));
resourceTypes.forEach(resource => { resourceTypes.forEach(resource => {

View File

@ -4,6 +4,8 @@ import { IExecSyncResult } from './tool-runner';
import { Kubectl } from '../kubectl-object-model'; import { Kubectl } from '../kubectl-object-model';
import { GitHubClient } from '../githubClient'; import { GitHubClient } from '../githubClient';
import { StatusCodes } from "./httpClient"; import { StatusCodes } from "./httpClient";
import * as exec from "./exec";
import * as inputParams from "../input-parameters";
export function getExecutableExtension(): string { export function getExecutableExtension(): string {
if (os.type().match(/^Win/)) { if (os.type().match(/^Win/)) {
@ -127,6 +129,78 @@ export function annotateChildPods(kubectl: Kubectl, resourceType: string, resour
return commandExecutionResults; return commandExecutionResults;
} }
export async function getFilePathsConfigs(): Promise<any> {
let filePathsConfig: any = {};
const BUILD_CONFIG_KEY = 'buildConfigs';
const MANIFEST_PATHS_KEY = 'manifestFilePaths';
const HELM_CHART_KEY = 'helmChartFilePaths';
const DOCKERFILE_PATH_LABEL_KEY = 'dockerfile-path';
const DOCKERFILE_PATH_KEY = 'dockerfilePath';
const CONTAINER_REG_KEY = 'containerRegistryServer';
let inputManifestFiles = inputParams.manifests;
filePathsConfig[MANIFEST_PATHS_KEY] = inputManifestFiles || '';
let helmChartPath = process.env.HELM_CHART_PATH || '';
filePathsConfig[HELM_CHART_KEY] = helmChartPath;
//Fetch labels from each image
let imageToBuildConfigMap: any = {};
let imageNames = core.getInput('images').split('\n');
for(const image of imageNames){
let args: string[] = [image];
let resultObj: any;
let buildConfigMap : any = {};
let containerRegistryName = image.toString().split('/')[0];
try{
let usrname = process.env.CR_USERNAME || null;
let pwd = process.env.CR_PASSWORD || null;
if(pwd && usrname)
{
let loginArgs: string[] = [containerRegistryName, '--username', usrname, '--password', pwd];
await exec.exec('docker login ', loginArgs, true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(`docker login failed with: ${res.stderr.match(/(.*)\s*$/)![0]}`);
}
});
}
await exec.exec('docker pull ', args, true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(`docker images pull failed with: ${res.stderr.match(/(.*)\s*$/)![0]}`);
}
});
await exec.exec('docker inspect --type=image', args, true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(`docker inspect call failed with: ${res.stderr.match(/(.*)\s*$/)![0]}`);
}
resultObj = JSON.parse(res.stdout)[0];
});
}
catch (ex) {
core.warning(`Failed to get dockerfile paths for image ${image.toString()} | ` + ex);
}
if(resultObj != null && resultObj.Config != null && resultObj.Config.Labels != null ){
if(resultObj.Config.Labels[DOCKERFILE_PATH_LABEL_KEY] !=null){
buildConfigMap[DOCKERFILE_PATH_KEY] = resultObj.Config.Labels[DOCKERFILE_PATH_LABEL_KEY];
}
//Add CR server name to build config
buildConfigMap[CONTAINER_REG_KEY] = containerRegistryName;
imageToBuildConfigMap[resultObj.Id] = buildConfigMap;
}
}
filePathsConfig[BUILD_CONFIG_KEY] = imageToBuildConfigMap;
return Promise.resolve(filePathsConfig);
}
export function sleep(timeout: number) { export function sleep(timeout: number) {
return new Promise(resolve => setTimeout(resolve, timeout)); return new Promise(resolve => setTimeout(resolve, timeout));
} }