mirror of
https://github.com/Azure/k8s-deploy.git
synced 2026-06-26 14:49:26 +08:00
New traceability fields annotation changes - design changes (#89)
* New design changes
This commit is contained in:
@@ -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
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user