mirror of
https://github.com/Azure/k8s-deploy.git
synced 2026-04-12 09:22:19 +08:00
Addressed review comments.
This commit is contained in:
parent
3a1c0b10eb
commit
65e1224846
@ -312,11 +312,11 @@ test("deployment - deploy() - Annotate & label resources", async () => {
|
|||||||
kubeCtl.labelFiles = jest.fn();
|
kubeCtl.labelFiles = jest.fn();
|
||||||
//Invoke and assert
|
//Invoke and assert
|
||||||
await expect(deployment.deploy(kubeCtl, ['manifests/deployment.yaml'], undefined)).resolves.not.toThrowError();
|
await expect(deployment.deploy(kubeCtl, ['manifests/deployment.yaml'], undefined)).resolves.not.toThrowError();
|
||||||
expect(kubeCtl.annotate).toHaveBeenNthCalledWith(1, 'namespace', 'default', [annotationKeyValStr], true);
|
expect(kubeCtl.annotate).toHaveBeenNthCalledWith(1, 'namespace', 'default', annotationKeyValStr);
|
||||||
expect(kubeCtl.annotateFiles).toBeCalledWith(["~/Deployment_testapp_currentTimestamp"], [annotationKeyValStr], true);
|
expect(kubeCtl.annotateFiles).toBeCalledWith(["~/Deployment_testapp_currentTimestamp"], annotationKeyValStr);
|
||||||
expect(kubeCtl.annotate).toBeCalledTimes(2);
|
expect(kubeCtl.annotate).toBeCalledTimes(2);
|
||||||
expect(kubeCtl.labelFiles).toBeCalledWith(["~/Deployment_testapp_currentTimestamp"],
|
expect(kubeCtl.labelFiles).toBeCalledWith(["~/Deployment_testapp_currentTimestamp"],
|
||||||
[`workflowFriendlyName=workflow.yml`, `workflow=${getWorkflowAnnotationKeyLabel(process.env.GITHUB_WORKFLOW)}`], true);
|
[`workflowFriendlyName=workflow.yml`, `workflow=${getWorkflowAnnotationKeyLabel(process.env.GITHUB_WORKFLOW)}`]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("deployment - deploy() - Annotate & label resources for a new workflow", async () => {
|
test("deployment - deploy() - Annotate & label resources for a new workflow", async () => {
|
||||||
@ -340,11 +340,11 @@ test("deployment - deploy() - Annotate & label resources for a new workflow", as
|
|||||||
kubeCtl.labelFiles = jest.fn();
|
kubeCtl.labelFiles = jest.fn();
|
||||||
//Invoke and assert
|
//Invoke and assert
|
||||||
await expect(deployment.deploy(kubeCtl, ['manifests/deployment.yaml'], undefined)).resolves.not.toThrowError();
|
await expect(deployment.deploy(kubeCtl, ['manifests/deployment.yaml'], undefined)).resolves.not.toThrowError();
|
||||||
expect(kubeCtl.annotate).toHaveBeenNthCalledWith(1, 'namespace', 'default', [annotationKeyValStr], true);
|
expect(kubeCtl.annotate).toHaveBeenNthCalledWith(1, 'namespace', 'default', annotationKeyValStr);
|
||||||
expect(kubeCtl.annotateFiles).toBeCalledWith(["~/Deployment_testapp_currentTimestamp"], [annotationKeyValStr], true);
|
expect(kubeCtl.annotateFiles).toBeCalledWith(["~/Deployment_testapp_currentTimestamp"], annotationKeyValStr);
|
||||||
expect(kubeCtl.annotate).toBeCalledTimes(2);
|
expect(kubeCtl.annotate).toBeCalledTimes(2);
|
||||||
expect(kubeCtl.labelFiles).toBeCalledWith(["~/Deployment_testapp_currentTimestamp"],
|
expect(kubeCtl.labelFiles).toBeCalledWith(["~/Deployment_testapp_currentTimestamp"],
|
||||||
[`workflowFriendlyName=${process.env.GITHUB_WORKFLOW}`, `workflow=${getWorkflowAnnotationKeyLabel(process.env.GITHUB_WORKFLOW)}`], true);
|
[`workflowFriendlyName=${process.env.GITHUB_WORKFLOW}`, `workflow=${getWorkflowAnnotationKeyLabel(process.env.GITHUB_WORKFLOW)}`]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("deployment - deploy() - Annotate resources failed", async () => {
|
test("deployment - deploy() - Annotate resources failed", async () => {
|
||||||
|
|||||||
@ -17,21 +17,18 @@ class GitHubClient {
|
|||||||
this._repository = repository;
|
this._repository = repository;
|
||||||
this._token = token;
|
this._token = token;
|
||||||
}
|
}
|
||||||
getWorkflows(force) {
|
getWorkflows() {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
if (force || !this._workflowsPromise) {
|
const getWorkflowFileNameUrl = `https://api.github.com/repos/${this._repository}/actions/workflows`;
|
||||||
const getWorkflowFileNameUrl = `https://api.github.com/repos/${this._repository}/actions/workflows`;
|
const webRequest = new httpClient_1.WebRequest();
|
||||||
const webRequest = new httpClient_1.WebRequest();
|
webRequest.method = "GET";
|
||||||
webRequest.method = "GET";
|
webRequest.uri = getWorkflowFileNameUrl;
|
||||||
webRequest.uri = getWorkflowFileNameUrl;
|
webRequest.headers = {
|
||||||
webRequest.headers = {
|
Authorization: `Bearer ${this._token}`
|
||||||
Authorization: `Bearer ${this._token}`
|
};
|
||||||
};
|
core.debug(`Getting workflows for repo: ${this._repository}`);
|
||||||
core.debug(`Getting workflows for repo: ${this._repository}`);
|
const response = yield httpClient_1.sendRequest(webRequest);
|
||||||
const response = yield httpClient_1.sendRequest(webRequest);
|
return Promise.resolve(response);
|
||||||
this._workflowsPromise = Promise.resolve(response);
|
|
||||||
}
|
|
||||||
return this._workflowsPromise;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -37,30 +37,24 @@ class Kubectl {
|
|||||||
}
|
}
|
||||||
return newReplicaSet;
|
return newReplicaSet;
|
||||||
}
|
}
|
||||||
annotate(resourceType, resourceName, annotations, overwrite) {
|
annotate(resourceType, resourceName, annotation) {
|
||||||
let args = ['annotate', resourceType, resourceName];
|
let args = ['annotate', resourceType, resourceName];
|
||||||
args = args.concat(annotations);
|
args.push(annotation);
|
||||||
if (!!overwrite) {
|
args.push(`--overwrite`);
|
||||||
args.push(`--overwrite`);
|
|
||||||
}
|
|
||||||
return this.execute(args);
|
return this.execute(args);
|
||||||
}
|
}
|
||||||
annotateFiles(files, annotations, overwrite) {
|
annotateFiles(files, annotation) {
|
||||||
let args = ['annotate'];
|
let args = ['annotate'];
|
||||||
args = args.concat(['-f', this.createInlineArray(files)]);
|
args = args.concat(['-f', this.createInlineArray(files)]);
|
||||||
args = args.concat(annotations);
|
args.push(annotation);
|
||||||
if (!!overwrite) {
|
args.push(`--overwrite`);
|
||||||
args.push(`--overwrite`);
|
|
||||||
}
|
|
||||||
return this.execute(args);
|
return this.execute(args);
|
||||||
}
|
}
|
||||||
labelFiles(files, labels, overwrite) {
|
labelFiles(files, labels) {
|
||||||
let args = ['label'];
|
let args = ['label'];
|
||||||
args = args.concat(['-f', this.createInlineArray(files)]);
|
args = args.concat(['-f', this.createInlineArray(files)]);
|
||||||
args = args.concat(labels);
|
args = args.concat(labels);
|
||||||
if (!!overwrite) {
|
args.push(`--overwrite`);
|
||||||
args.push(`--overwrite`);
|
|
||||||
}
|
|
||||||
return this.execute(args);
|
return this.execute(args);
|
||||||
}
|
}
|
||||||
getAllPods() {
|
getAllPods() {
|
||||||
|
|||||||
@ -122,8 +122,8 @@ function annotateResources(files, kubectl, resourceTypes, allPods, annotationKey
|
|||||||
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);
|
let annotationKeyValStr = annotationKey + '=' + models.getWorkflowAnnotationsJson(lastSuccessSha);
|
||||||
annotateResults.push(kubectl.annotate('namespace', TaskInputParameters.namespace, [annotationKeyValStr], true));
|
annotateResults.push(kubectl.annotate('namespace', TaskInputParameters.namespace, annotationKeyValStr));
|
||||||
annotateResults.push(kubectl.annotateFiles(files, [annotationKeyValStr], true));
|
annotateResults.push(kubectl.annotateFiles(files, annotationKeyValStr));
|
||||||
resourceTypes.forEach(resource => {
|
resourceTypes.forEach(resource => {
|
||||||
if (resource.type.toUpperCase() !== models.KubernetesWorkload.pod.toUpperCase()) {
|
if (resource.type.toUpperCase() !== models.KubernetesWorkload.pod.toUpperCase()) {
|
||||||
utility_1.annotateChildPods(kubectl, resource.type, resource.name, annotationKeyValStr, allPods)
|
utility_1.annotateChildPods(kubectl, resource.type, resource.name, annotationKeyValStr, allPods)
|
||||||
@ -137,7 +137,7 @@ function labelResources(files, kubectl, label) {
|
|||||||
workflowName = workflowName.startsWith('.github/workflows/') ?
|
workflowName = workflowName.startsWith('.github/workflows/') ?
|
||||||
workflowName.replace(".github/workflows/", "") : workflowName;
|
workflowName.replace(".github/workflows/", "") : workflowName;
|
||||||
const labels = [`workflowFriendlyName=${workflowName}`, `workflow=${label}`];
|
const labels = [`workflowFriendlyName=${workflowName}`, `workflow=${label}`];
|
||||||
utility_1.checkForErrors([kubectl.labelFiles(files, labels, true)], true);
|
utility_1.checkForErrors([kubectl.labelFiles(files, labels)], true);
|
||||||
}
|
}
|
||||||
function updateResourceObjects(filePaths, imagePullSecrets, containers) {
|
function updateResourceObjects(filePaths, imagePullSecrets, containers) {
|
||||||
const newObjectsList = [];
|
const newObjectsList = [];
|
||||||
|
|||||||
@ -28,7 +28,7 @@ function isEqual(str1, str2, ignoreCase) {
|
|||||||
if (str1 == null || str2 == null) {
|
if (str1 == null || str2 == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!!ignoreCase) {
|
if (ignoreCase) {
|
||||||
return str1.toUpperCase() === str2.toUpperCase();
|
return str1.toUpperCase() === str2.toUpperCase();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -40,7 +40,7 @@ function checkForErrors(execResults, warnIfError) {
|
|||||||
if (execResults.length !== 0) {
|
if (execResults.length !== 0) {
|
||||||
let stderr = '';
|
let stderr = '';
|
||||||
execResults.forEach(result => {
|
execResults.forEach(result => {
|
||||||
if (!!result && !!result.stderr) {
|
if (result && result.stderr) {
|
||||||
if (result.code !== 0) {
|
if (result.code !== 0) {
|
||||||
stderr += result.stderr + '\n';
|
stderr += result.stderr + '\n';
|
||||||
}
|
}
|
||||||
@ -50,7 +50,7 @@ function checkForErrors(execResults, warnIfError) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (stderr.length > 0) {
|
if (stderr.length > 0) {
|
||||||
if (!!warnIfError) {
|
if (warnIfError) {
|
||||||
core.warning(stderr.trim());
|
core.warning(stderr.trim());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -61,25 +61,27 @@ function checkForErrors(execResults, warnIfError) {
|
|||||||
}
|
}
|
||||||
exports.checkForErrors = checkForErrors;
|
exports.checkForErrors = checkForErrors;
|
||||||
function getLastSuccessfulRunSha(kubectl, namespaceName, annotationKey) {
|
function getLastSuccessfulRunSha(kubectl, namespaceName, annotationKey) {
|
||||||
const result = kubectl.getResource('namespace', namespaceName);
|
try {
|
||||||
if (!result) {
|
const result = kubectl.getResource('namespace', namespaceName);
|
||||||
core.debug(`Failed to get commits from cluster.`);
|
if (result) {
|
||||||
return '';
|
if (result.stderr) {
|
||||||
|
core.warning(`${result.stderr}`);
|
||||||
|
return process.env.GITHUB_SHA;
|
||||||
|
}
|
||||||
|
else if (result.stdout) {
|
||||||
|
const annotationsSet = JSON.parse(result.stdout).metadata.annotations;
|
||||||
|
if (annotationsSet && annotationsSet[annotationKey]) {
|
||||||
|
return JSON.parse(annotationsSet[annotationKey].replace(/'/g, '"')).commit;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return 'NA';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
catch (ex) {
|
||||||
if (!!result.stderr) {
|
core.warning(`Failed to get commits from cluster. ${JSON.stringify(ex)}`);
|
||||||
core.debug(`${result.stderr}`);
|
return '';
|
||||||
return process.env.GITHUB_SHA;
|
|
||||||
}
|
|
||||||
else if (!!result.stdout) {
|
|
||||||
const annotationsSet = JSON.parse(result.stdout).metadata.annotations;
|
|
||||||
if (!!annotationsSet && !!annotationsSet[annotationKey]) {
|
|
||||||
return JSON.parse(annotationsSet[annotationKey].replace(/'/g, '"')).commit;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return 'NA';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exports.getLastSuccessfulRunSha = getLastSuccessfulRunSha;
|
exports.getLastSuccessfulRunSha = getLastSuccessfulRunSha;
|
||||||
@ -89,20 +91,25 @@ function getWorkflowFilePath(githubToken) {
|
|||||||
if (!workflowFilePath.startsWith('.github/workflows/')) {
|
if (!workflowFilePath.startsWith('.github/workflows/')) {
|
||||||
const githubClient = new githubClient_1.GitHubClient(process.env.GITHUB_REPOSITORY, githubToken);
|
const githubClient = new githubClient_1.GitHubClient(process.env.GITHUB_REPOSITORY, githubToken);
|
||||||
const response = yield githubClient.getWorkflows();
|
const response = yield githubClient.getWorkflows();
|
||||||
if (response.statusCode == httpClient_1.StatusCodes.OK
|
if (response) {
|
||||||
&& !!response.body
|
if (response.statusCode == httpClient_1.StatusCodes.OK
|
||||||
&& !!response.body.total_count) {
|
&& response.body
|
||||||
if (response.body.total_count > 0) {
|
&& response.body.total_count) {
|
||||||
for (let workflow of response.body.workflows) {
|
if (response.body.total_count > 0) {
|
||||||
if (process.env.GITHUB_WORKFLOW === workflow.name) {
|
for (let workflow of response.body.workflows) {
|
||||||
workflowFilePath = workflow.path;
|
if (process.env.GITHUB_WORKFLOW === workflow.name) {
|
||||||
break;
|
workflowFilePath = workflow.path;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (response.statusCode != httpClient_1.StatusCodes.OK) {
|
||||||
|
core.debug(`An error occured while getting list of workflows on the repo. Statuscode: ${response.statusCode}, StatusMessage: ${response.statusMessage}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (response.statusCode != httpClient_1.StatusCodes.OK) {
|
else {
|
||||||
core.debug(`An error occured while getting list of workflows on the repo. Statuscode: ${response.statusCode}, StatusMessage: ${response.statusMessage}`);
|
core.warning(`Failed to get response from workflow list API`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Promise.resolve(workflowFilePath);
|
return Promise.resolve(workflowFilePath);
|
||||||
@ -115,13 +122,13 @@ function annotateChildPods(kubectl, resourceType, resourceName, annotationKeyVal
|
|||||||
if (resourceType.toLowerCase().indexOf('deployment') > -1) {
|
if (resourceType.toLowerCase().indexOf('deployment') > -1) {
|
||||||
owner = kubectl.getNewReplicaSet(resourceName);
|
owner = kubectl.getNewReplicaSet(resourceName);
|
||||||
}
|
}
|
||||||
if (!!allPods && !!allPods.items && allPods.items.length > 0) {
|
if (allPods && allPods.items && allPods.items.length > 0) {
|
||||||
allPods.items.forEach((pod) => {
|
allPods.items.forEach((pod) => {
|
||||||
const owners = pod.metadata.ownerReferences;
|
const owners = pod.metadata.ownerReferences;
|
||||||
if (!!owners) {
|
if (owners) {
|
||||||
owners.forEach(ownerRef => {
|
owners.forEach(ownerRef => {
|
||||||
if (ownerRef.name === owner) {
|
if (ownerRef.name === owner) {
|
||||||
commandExecutionResults.push(kubectl.annotate('pod', pod.metadata.name, [annotationKeyValStr], true));
|
commandExecutionResults.push(kubectl.annotate('pod', pod.metadata.name, annotationKeyValStr));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -40,6 +40,7 @@ export function getWorkflowAnnotationsJson(lastSuccessRunSha: string): string {
|
|||||||
+ `'provider': 'GitHub'`
|
+ `'provider': 'GitHub'`
|
||||||
+ `}`;
|
+ `}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getWorkflowAnnotationKeyLabel(workflowFilePath: string): string {
|
export function getWorkflowAnnotationKeyLabel(workflowFilePath: string): string {
|
||||||
const hashKey = require("crypto").createHash("MD5")
|
const hashKey = require("crypto").createHash("MD5")
|
||||||
.update(`${process.env.GITHUB_REPOSITORY}/${workflowFilePath}`)
|
.update(`${process.env.GITHUB_REPOSITORY}/${workflowFilePath}`)
|
||||||
|
|||||||
@ -7,24 +7,20 @@ export class GitHubClient {
|
|||||||
this._token = token;
|
this._token = token;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getWorkflows(force?: boolean): Promise<any> {
|
public async getWorkflows(): Promise<any> {
|
||||||
if (force || !this._workflowsPromise) {
|
const getWorkflowFileNameUrl = `https://api.github.com/repos/${this._repository}/actions/workflows`;
|
||||||
const getWorkflowFileNameUrl = `https://api.github.com/repos/${this._repository}/actions/workflows`;
|
const webRequest = new WebRequest();
|
||||||
const webRequest = new WebRequest();
|
webRequest.method = "GET";
|
||||||
webRequest.method = "GET";
|
webRequest.uri = getWorkflowFileNameUrl;
|
||||||
webRequest.uri = getWorkflowFileNameUrl;
|
webRequest.headers = {
|
||||||
webRequest.headers = {
|
Authorization: `Bearer ${this._token}`
|
||||||
Authorization: `Bearer ${this._token}`
|
};
|
||||||
};
|
|
||||||
|
|
||||||
core.debug(`Getting workflows for repo: ${this._repository}`);
|
core.debug(`Getting workflows for repo: ${this._repository}`);
|
||||||
const response: WebResponse = await sendRequest(webRequest);
|
const response: WebResponse = await sendRequest(webRequest);
|
||||||
this._workflowsPromise = Promise.resolve(response);
|
return Promise.resolve(response);
|
||||||
}
|
|
||||||
return this._workflowsPromise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _repository: string;
|
private _repository: string;
|
||||||
private _token: string;
|
private _token: string;
|
||||||
private _workflowsPromise: Promise<any>;
|
|
||||||
}
|
}
|
||||||
@ -50,26 +50,26 @@ export class Kubectl {
|
|||||||
return newReplicaSet;
|
return newReplicaSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
public annotate(resourceType: string, resourceName: string, annotations: string[], overwrite?: boolean): IExecSyncResult {
|
public annotate(resourceType: string, resourceName: string, annotation: string): IExecSyncResult {
|
||||||
let args = ['annotate', resourceType, resourceName];
|
let args = ['annotate', resourceType, resourceName];
|
||||||
args = args.concat(annotations);
|
args.push(annotation);
|
||||||
if (!!overwrite) { args.push(`--overwrite`); }
|
args.push(`--overwrite`);
|
||||||
return this.execute(args);
|
return this.execute(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public annotateFiles(files: string | string[], annotations: string[], overwrite?: boolean): IExecSyncResult {
|
public annotateFiles(files: string | string[], annotation: string): IExecSyncResult {
|
||||||
let args = ['annotate'];
|
let args = ['annotate'];
|
||||||
args = args.concat(['-f', this.createInlineArray(files)]);
|
args = args.concat(['-f', this.createInlineArray(files)]);
|
||||||
args = args.concat(annotations);
|
args.push(annotation);
|
||||||
if (!!overwrite) { args.push(`--overwrite`); }
|
args.push(`--overwrite`);
|
||||||
return this.execute(args);
|
return this.execute(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public labelFiles(files: string | string[], labels: string[], overwrite?: boolean): IExecSyncResult {
|
public labelFiles(files: string | string[], labels: string[]): IExecSyncResult {
|
||||||
let args = ['label'];
|
let args = ['label'];
|
||||||
args = args.concat(['-f', this.createInlineArray(files)]);
|
args = args.concat(['-f', this.createInlineArray(files)]);
|
||||||
args = args.concat(labels);
|
args = args.concat(labels);
|
||||||
if (!!overwrite) { args.push(`--overwrite`); }
|
args.push(`--overwrite`);
|
||||||
return this.execute(args);
|
return this.execute(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -123,8 +123,8 @@ function annotateResources(files: string[], kubectl: Kubectl, resourceTypes: Res
|
|||||||
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);
|
let annotationKeyValStr = annotationKey + '=' + models.getWorkflowAnnotationsJson(lastSuccessSha);
|
||||||
annotateResults.push(kubectl.annotate('namespace', TaskInputParameters.namespace, [annotationKeyValStr], true));
|
annotateResults.push(kubectl.annotate('namespace', TaskInputParameters.namespace, annotationKeyValStr));
|
||||||
annotateResults.push(kubectl.annotateFiles(files, [annotationKeyValStr], true));
|
annotateResults.push(kubectl.annotateFiles(files, annotationKeyValStr));
|
||||||
resourceTypes.forEach(resource => {
|
resourceTypes.forEach(resource => {
|
||||||
if (resource.type.toUpperCase() !== models.KubernetesWorkload.pod.toUpperCase()) {
|
if (resource.type.toUpperCase() !== models.KubernetesWorkload.pod.toUpperCase()) {
|
||||||
annotateChildPods(kubectl, resource.type, resource.name, annotationKeyValStr, allPods)
|
annotateChildPods(kubectl, resource.type, resource.name, annotationKeyValStr, allPods)
|
||||||
@ -139,7 +139,7 @@ function labelResources(files: string[], kubectl: Kubectl, label: string) {
|
|||||||
workflowName = workflowName.startsWith('.github/workflows/') ?
|
workflowName = workflowName.startsWith('.github/workflows/') ?
|
||||||
workflowName.replace(".github/workflows/", "") : workflowName;
|
workflowName.replace(".github/workflows/", "") : workflowName;
|
||||||
const labels = [`workflowFriendlyName=${workflowName}`, `workflow=${label}`];
|
const labels = [`workflowFriendlyName=${workflowName}`, `workflow=${label}`];
|
||||||
checkForErrors([kubectl.labelFiles(files, labels, true)], true);
|
checkForErrors([kubectl.labelFiles(files, labels)], true);
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateResourceObjects(filePaths: string[], imagePullSecrets: string[], containers: string[]): string[] {
|
function updateResourceObjects(filePaths: string[], imagePullSecrets: string[], containers: string[]): string[] {
|
||||||
|
|||||||
@ -22,7 +22,7 @@ export function isEqual(str1: string, str2: string, ignoreCase?: boolean): boole
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!!ignoreCase) {
|
if (ignoreCase) {
|
||||||
return str1.toUpperCase() === str2.toUpperCase();
|
return str1.toUpperCase() === str2.toUpperCase();
|
||||||
} else {
|
} else {
|
||||||
return str1 === str2;
|
return str1 === str2;
|
||||||
@ -33,7 +33,7 @@ export function checkForErrors(execResults: IExecSyncResult[], warnIfError?: boo
|
|||||||
if (execResults.length !== 0) {
|
if (execResults.length !== 0) {
|
||||||
let stderr = '';
|
let stderr = '';
|
||||||
execResults.forEach(result => {
|
execResults.forEach(result => {
|
||||||
if (!!result && !!result.stderr) {
|
if (result && result.stderr) {
|
||||||
if (result.code !== 0) {
|
if (result.code !== 0) {
|
||||||
stderr += result.stderr + '\n';
|
stderr += result.stderr + '\n';
|
||||||
} else {
|
} else {
|
||||||
@ -42,7 +42,7 @@ export function checkForErrors(execResults: IExecSyncResult[], warnIfError?: boo
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (stderr.length > 0) {
|
if (stderr.length > 0) {
|
||||||
if (!!warnIfError) {
|
if (warnIfError) {
|
||||||
core.warning(stderr.trim());
|
core.warning(stderr.trim());
|
||||||
} else {
|
} else {
|
||||||
throw new Error(stderr.trim());
|
throw new Error(stderr.trim());
|
||||||
@ -52,25 +52,27 @@ export function checkForErrors(execResults: IExecSyncResult[], warnIfError?: boo
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function getLastSuccessfulRunSha(kubectl: Kubectl, namespaceName: string, annotationKey: string): string {
|
export function getLastSuccessfulRunSha(kubectl: Kubectl, namespaceName: string, annotationKey: string): string {
|
||||||
const result = kubectl.getResource('namespace', namespaceName);
|
try {
|
||||||
if (!result) {
|
const result = kubectl.getResource('namespace', namespaceName);
|
||||||
core.debug(`Failed to get commits from cluster.`);
|
if (result) {
|
||||||
return '';
|
if (result.stderr) {
|
||||||
|
core.warning(`${result.stderr}`);
|
||||||
|
return process.env.GITHUB_SHA;
|
||||||
|
}
|
||||||
|
else if (result.stdout) {
|
||||||
|
const annotationsSet = JSON.parse(result.stdout).metadata.annotations;
|
||||||
|
if (annotationsSet && annotationsSet[annotationKey]) {
|
||||||
|
return JSON.parse(annotationsSet[annotationKey].replace(/'/g, '"')).commit;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return 'NA';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
catch (ex) {
|
||||||
if (!!result.stderr) {
|
core.warning(`Failed to get commits from cluster. ${JSON.stringify(ex)}`);
|
||||||
core.debug(`${result.stderr}`);
|
return '';
|
||||||
return process.env.GITHUB_SHA;
|
|
||||||
}
|
|
||||||
else if (!!result.stdout) {
|
|
||||||
const annotationsSet = JSON.parse(result.stdout).metadata.annotations;
|
|
||||||
if (!!annotationsSet && !!annotationsSet[annotationKey]) {
|
|
||||||
return JSON.parse(annotationsSet[annotationKey].replace(/'/g, '"')).commit;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return 'NA';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,20 +81,25 @@ export async function getWorkflowFilePath(githubToken: string): Promise<string>
|
|||||||
if (!workflowFilePath.startsWith('.github/workflows/')) {
|
if (!workflowFilePath.startsWith('.github/workflows/')) {
|
||||||
const githubClient = new GitHubClient(process.env.GITHUB_REPOSITORY, githubToken);
|
const githubClient = new GitHubClient(process.env.GITHUB_REPOSITORY, githubToken);
|
||||||
const response = await githubClient.getWorkflows();
|
const response = await githubClient.getWorkflows();
|
||||||
if (response.statusCode == StatusCodes.OK
|
if (response) {
|
||||||
&& !!response.body
|
if (response.statusCode == StatusCodes.OK
|
||||||
&& !!response.body.total_count) {
|
&& response.body
|
||||||
if (response.body.total_count > 0) {
|
&& response.body.total_count) {
|
||||||
for (let workflow of response.body.workflows) {
|
if (response.body.total_count > 0) {
|
||||||
if (process.env.GITHUB_WORKFLOW === workflow.name) {
|
for (let workflow of response.body.workflows) {
|
||||||
workflowFilePath = workflow.path;
|
if (process.env.GITHUB_WORKFLOW === workflow.name) {
|
||||||
break;
|
workflowFilePath = workflow.path;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (response.statusCode != StatusCodes.OK) {
|
||||||
|
core.debug(`An error occured while getting list of workflows on the repo. Statuscode: ${response.statusCode}, StatusMessage: ${response.statusMessage}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (response.statusCode != StatusCodes.OK) {
|
else {
|
||||||
core.debug(`An error occured while getting list of workflows on the repo. Statuscode: ${response.statusCode}, StatusMessage: ${response.statusMessage}`);
|
core.warning(`Failed to get response from workflow list API`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Promise.resolve(workflowFilePath);
|
return Promise.resolve(workflowFilePath);
|
||||||
@ -105,13 +112,13 @@ export function annotateChildPods(kubectl: Kubectl, resourceType: string, resour
|
|||||||
owner = kubectl.getNewReplicaSet(resourceName);
|
owner = kubectl.getNewReplicaSet(resourceName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!!allPods && !!allPods.items && allPods.items.length > 0) {
|
if (allPods && allPods.items && allPods.items.length > 0) {
|
||||||
allPods.items.forEach((pod) => {
|
allPods.items.forEach((pod) => {
|
||||||
const owners = pod.metadata.ownerReferences;
|
const owners = pod.metadata.ownerReferences;
|
||||||
if (!!owners) {
|
if (owners) {
|
||||||
owners.forEach(ownerRef => {
|
owners.forEach(ownerRef => {
|
||||||
if (ownerRef.name === owner) {
|
if (ownerRef.name === owner) {
|
||||||
commandExecutionResults.push(kubectl.annotate('pod', pod.metadata.name, [annotationKeyValStr], true));
|
commandExecutionResults.push(kubectl.annotate('pod', pod.metadata.name, annotationKeyValStr));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user