New traceability fields annotation changes - design changes (#89)

* New design changes
This commit is contained in:
Jyotsna
2020-12-29 19:11:56 +05:30
committed by GitHub
parent 068b665bf3
commit d7d5d3429f
11 changed files with 201 additions and 235 deletions
+4 -4
View File
@@ -25,7 +25,7 @@ ServiceTypes.clusterIP = 'ClusterIP';
exports.deploymentTypes = ['deployment', 'replicaset', 'daemonset', 'pod', 'statefulset'];
exports.workloadTypes = ['deployment', 'replicaset', 'daemonset', 'pod', 'statefulset', 'job', 'cronjob'];
exports.workloadTypesWithRolloutStatus = ['deployment', 'daemonset', 'statefulset'];
function getWorkflowAnnotationsJson(lastSuccessRunSha, workflowFilePath, filePathConfigs) {
function getWorkflowAnnotationsJson(lastSuccessRunSha, workflowFilePath, deploymentConfig) {
let annotationObject = {};
annotationObject["run"] = process.env.GITHUB_RUN_ID;
annotationObject["repository"] = process.env.GITHUB_REPOSITORY;
@@ -38,9 +38,9 @@ function getWorkflowAnnotationsJson(lastSuccessRunSha, workflowFilePath, filePat
annotationObject["lastSuccessRunCommit"] = lastSuccessRunSha;
annotationObject["branch"] = process.env.GITHUB_REF;
annotationObject["deployTimestamp"] = Date.now();
annotationObject["dockerfilePaths"] = filePathConfigs.dockerfilePaths;
annotationObject["manifestsPaths"] = filePathConfigs.manifestFilePaths;
annotationObject["helmChartPaths"] = filePathConfigs.helmChartFilePaths;
annotationObject["dockerfilePaths"] = deploymentConfig.dockerfilePaths;
annotationObject["manifestsPaths"] = deploymentConfig.manifestFilePaths;
annotationObject["helmChartPaths"] = deploymentConfig.helmChartFilePaths;
annotationObject["provider"] = "GitHub";
return JSON.stringify(annotationObject);
}
+31
View File
@@ -0,0 +1,31 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DockerExec = void 0;
const tool_runner_1 = require("./utilities/tool-runner");
class DockerExec {
constructor(dockerPath) {
this.dockerPath = dockerPath;
}
;
pull(image, args, silent) {
args = ['pull', image, ...args];
let result = this.execute(args, silent);
if (result.stderr != '' && result.code != 0) {
throw new Error(`docker images pull failed with: ${result.error}`);
}
}
inspect(image, args, silent) {
args = ['inspect', image, ...args];
let result = this.execute(args, silent);
if (result.stderr != '' && result.code != 0) {
throw new Error(`docker inspect call failed with: ${result.error}`);
}
return result.stdout;
}
execute(args, silent) {
const command = new tool_runner_1.ToolRunner(this.dockerPath);
command.arg(args);
return command.execSync({ silent: !!silent });
}
}
exports.DockerExec = DockerExec;
-35
View File
@@ -1,35 +0,0 @@
"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()
};
});
@@ -113,16 +113,16 @@ function checkManifestStability(kubectl, resources) {
function annotateAndLabelResources(files, kubectl, resourceTypes, allPods) {
return __awaiter(this, void 0, void 0, function* () {
const workflowFilePath = yield utility_1.getWorkflowFilePath(TaskInputParameters.githubToken);
const filePathsConfig = yield utility_1.getFilePathsConfigs();
const deploymentConfig = yield utility_1.getDeploymentConfig();
const annotationKeyLabel = models.getWorkflowAnnotationKeyLabel(workflowFilePath);
annotateResources(files, kubectl, resourceTypes, allPods, annotationKeyLabel, workflowFilePath, filePathsConfig);
annotateResources(files, kubectl, resourceTypes, allPods, annotationKeyLabel, workflowFilePath, deploymentConfig);
labelResources(files, kubectl, annotationKeyLabel);
});
}
function annotateResources(files, kubectl, resourceTypes, allPods, annotationKey, workflowFilePath, filePathsConfig) {
function annotateResources(files, kubectl, resourceTypes, allPods, annotationKey, workflowFilePath, deploymentConfig) {
const annotateResults = [];
const lastSuccessSha = utility_1.getLastSuccessfulRunSha(kubectl, TaskInputParameters.namespace, annotationKey);
let annotationKeyValStr = annotationKey + '=' + models.getWorkflowAnnotationsJson(lastSuccessSha, workflowFilePath, filePathsConfig);
let annotationKeyValStr = annotationKey + '=' + models.getWorkflowAnnotationsJson(lastSuccessSha, workflowFilePath, deploymentConfig);
annotateResults.push(kubectl.annotate('namespace', TaskInputParameters.namespace, annotationKeyValStr));
annotateResults.push(kubectl.annotateFiles(files, annotationKeyValStr));
resourceTypes.forEach(resource => {
+52 -67
View File
@@ -9,13 +9,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCurrentTime = exports.getRandomInt = exports.sleep = exports.getFilePathsConfigs = exports.annotateChildPods = exports.getWorkflowFilePath = exports.getLastSuccessfulRunSha = exports.checkForErrors = exports.isEqual = exports.getExecutableExtension = void 0;
exports.getCurrentTime = exports.getRandomInt = exports.sleep = exports.getDeploymentConfig = exports.annotateChildPods = exports.getWorkflowFilePath = exports.getLastSuccessfulRunSha = exports.checkForErrors = exports.isEqual = exports.getExecutableExtension = void 0;
const os = require("os");
const core = require("@actions/core");
const githubClient_1 = require("../githubClient");
const httpClient_1 = require("./httpClient");
const exec = require("./exec");
const inputParams = require("../input-parameters");
const docker_object_model_1 = require("../docker-object-model");
const io = require("@actions/io");
function getExecutableExtension() {
if (os.type().match(/^Win/)) {
return '.exe';
@@ -139,81 +140,36 @@ function annotateChildPods(kubectl, resourceType, resourceName, annotationKeyVal
return commandExecutionResults;
}
exports.annotateChildPods = annotateChildPods;
function getFilePathsConfigs() {
function getDeploymentConfig() {
return __awaiter(this, void 0, void 0, function* () {
let filePathsConfig = {};
const MANIFEST_PATHS_KEY = 'manifestFilePaths';
const HELM_CHART_KEY = 'helmChartFilePaths';
const DOCKERFILE_PATH_KEY = 'dockerfilePaths';
const DOCKERFILE_PATH_LABEL_KEY = 'dockerfile-path';
let inputManifestFiles = inputParams.manifests || [];
filePathsConfig[MANIFEST_PATHS_KEY] = inputManifestFiles;
let helmChartPaths = (process.env.HELM_CHART_PATHS && process.env.HELM_CHART_PATHS.split('\n').filter(path => path != "")) || [];
filePathsConfig[HELM_CHART_KEY] = helmChartPaths;
//Parsing dockerfile paths for images
let imageNames = core.getInput('images').split('\n');
const inputManifestFiles = inputParams.manifests || [];
const helmChartPaths = (process.env.HELM_CHART_PATHS && process.env.HELM_CHART_PATHS.split('\n').filter(path => path != "")) || [];
const imageNames = inputParams.containers || [];
let imageDockerfilePathMap = {};
let pathKey, pathVal;
//Fetching from env var if available
let dockerfilePathsList = (process.env.DOCKERFILE_PATHS && process.env.DOCKERFILE_PATHS.split('\n')) || [];
dockerfilePathsList.forEach(path => {
if (path) {
pathKey = path.split(' ')[0];
pathVal = path.split(' ')[1];
imageDockerfilePathMap[pathKey] = pathVal;
}
});
//Fetching from image lable if available
//Fetching from image label if available
for (const image of imageNames) {
let args = [image];
let resultObj;
let containerRegistryName = image;
let imageConfig, imageInspectResult;
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]}`);
}
if (res.stdout) {
resultObj = JSON.parse(res.stdout);
}
});
yield checkDockerPath();
var dockerExec = new docker_object_model_1.DockerExec('docker');
dockerExec.pull(image, [], true);
imageInspectResult = dockerExec.inspect(image, [], true);
imageConfig = JSON.parse(imageInspectResult)[0];
imageDockerfilePathMap[image] = getDockerfilePath(imageConfig);
}
catch (ex) {
core.warning(`Failed to get dockerfile paths for image ${image.toString()} | ` + ex);
}
if (resultObj) {
resultObj = resultObj[0];
if ((resultObj.Config) && (resultObj.Config.Labels) && (resultObj.Config.Labels[DOCKERFILE_PATH_LABEL_KEY])) {
pathVal = resultObj.Config.Labels[DOCKERFILE_PATH_LABEL_KEY];
}
else {
pathVal = 'Not available';
}
if (!imageDockerfilePathMap[image]) { //If (image : someVal) does not exist from env var parsing then add
imageDockerfilePathMap[image] = pathVal;
}
core.warning(`Failed to get dockerfile path for image ${image.toString()} | ` + ex);
}
}
filePathsConfig[DOCKERFILE_PATH_KEY] = imageDockerfilePathMap;
return Promise.resolve(filePathsConfig);
const deploymentConfig = {
manifestFilePaths: inputManifestFiles,
helmChartFilePaths: helmChartPaths,
dockerfilePaths: imageDockerfilePathMap
};
return Promise.resolve(deploymentConfig);
});
}
exports.getFilePathsConfigs = getFilePathsConfigs;
exports.getDeploymentConfig = getDeploymentConfig;
function sleep(timeout) {
return new Promise(resolve => setTimeout(resolve, timeout));
}
@@ -226,3 +182,32 @@ function getCurrentTime() {
return new Date().getTime();
}
exports.getCurrentTime = getCurrentTime;
function checkDockerPath() {
return __awaiter(this, void 0, void 0, function* () {
let dockerPath = yield io.which('docker', false);
if (!dockerPath) {
throw new Error('Docker is not installed.');
}
});
}
function getDockerfilePath(imageConfig) {
const DOCKERFILE_PATH_LABEL_KEY = 'dockerfile-path';
const ref = process.env.GITHUB_REF && process.env.GITHUB_REF.replace('refs/heads/', '').replace('refs/tags/', '');
let pathLabel, pathLink, pathValue = '';
if (imageConfig) {
if ((imageConfig.Config) && (imageConfig.Config.Labels) && (imageConfig.Config.Labels[DOCKERFILE_PATH_LABEL_KEY])) {
pathLabel = imageConfig.Config.Labels[DOCKERFILE_PATH_LABEL_KEY];
if (pathValue.startsWith('./')) { //if it is relative filepath convert to link from current repo
pathLink = `https://github.com/${process.env.GITHUB_REPOSITORY}/blob/${ref}/${pathLabel}`;
pathValue = pathLink;
}
else {
pathValue = pathLabel;
}
}
else {
pathValue = '';
}
}
return pathValue;
}