зеркало из https://github.com/Azure/k8s-deploy.git
Checks in annotateNamespace to not error during failed annotation
This commit is contained in:
Родитель
15e04b8f7e
Коммит
a4ebc55d69
|
@ -8,7 +8,6 @@ import * as io from '@actions/io';
|
|||
import * as toolCache from '@actions/tool-cache';
|
||||
import * as fileHelper from '../src/utilities/files-helper';
|
||||
import { workflowAnnotations } from '../src/constants';
|
||||
import * as utility from '../src/utilities/utility';
|
||||
import * as inputParam from '../src/input-parameters';
|
||||
|
||||
import { Kubectl, Resource } from '../src/kubectl-object-model';
|
||||
|
@ -21,7 +20,6 @@ const os = require("os");
|
|||
|
||||
const coreMock = mocked(core, true);
|
||||
const ioMock = mocked(io, true);
|
||||
const utilityMock = mocked(utility, true);
|
||||
const inputParamMock = mocked(inputParam, true);
|
||||
|
||||
const toolCacheMock = mocked(toolCache, true);
|
||||
|
@ -43,7 +41,7 @@ const getNamespaceMock = {
|
|||
|
||||
const resources: Resource[] = [{ type: "Deployment", name: "AppName" }];
|
||||
|
||||
beforeAll(() => {
|
||||
beforeEach(() => {
|
||||
deploymentYaml = fs.readFileSync(path.join(__dirname, 'manifests', 'deployment.yml'), 'utf8');
|
||||
|
||||
process.env["KUBECONFIG"] = 'kubeConfig';
|
||||
|
@ -272,10 +270,37 @@ test("deployment - deploy() - Annotate resources", async () => {
|
|||
|
||||
//Invoke and assert
|
||||
await expect(deployment.deploy(kubeCtl, ['manifests/deployment.yaml'], undefined)).resolves.not.toThrowError();
|
||||
expect(kubeCtl.annotateFiles).toBeCalledWith(["Local\\Temp\\deployment.yaml"], workflowAnnotations, true);
|
||||
expect(kubeCtl.annotateFiles).toBeCalledWith(["Local/Temp/deployment.yaml"], workflowAnnotations, true);
|
||||
expect(kubeCtl.annotate).toBeCalledTimes(2);
|
||||
});
|
||||
|
||||
test("deployment - deploy() - Skip Annotate namespace", async () => {
|
||||
process.env['GITHUB_REPOSITORY'] = 'test1Repo';
|
||||
const KubernetesManifestUtilityMock = mocked(KubernetesManifestUtility, true);
|
||||
KubernetesManifestUtilityMock.checkManifestStability = jest.fn().mockReturnValue("");
|
||||
const KubernetesObjectUtilityMock = mocked(KubernetesObjectUtility, true);
|
||||
KubernetesObjectUtilityMock.getResources = jest.fn().mockReturnValue(resources);
|
||||
const fileHelperMock = mocked(fileHelper, true);
|
||||
const fsMock = (mocked(fs, true));
|
||||
fileHelperMock.getTempDirectory = jest.fn().mockReturnValue("Local/Temp/");
|
||||
fsMock.writeFileSync =jest.fn().mockReturnValue("");
|
||||
const kubeCtl: jest.Mocked<Kubectl> = new Kubectl("") as any;
|
||||
kubeCtl.apply = jest.fn().mockReturnValue("");
|
||||
kubeCtl.getResource = jest.fn().mockReturnValue(getNamespaceMock);
|
||||
kubeCtl.getAllPods = jest.fn().mockReturnValue(getAllPodsMock);
|
||||
kubeCtl.getNewReplicaSet = jest.fn().mockReturnValue("testpod-776cbc86f9");
|
||||
kubeCtl.annotateFiles = jest.fn().mockReturnValue("");
|
||||
kubeCtl.annotate = jest.fn().mockReturnValue("");
|
||||
|
||||
const consoleOutputSpy = jest.spyOn(process.stdout, "write").mockImplementation();
|
||||
|
||||
//Invoke and assert
|
||||
await expect(deployment.deploy(kubeCtl, ['manifests/deployment.yaml'], undefined)).resolves.not.toThrowError();
|
||||
expect(kubeCtl.annotateFiles).toBeCalledWith(["Local/Temp/deployment.yaml"], workflowAnnotations, true);
|
||||
expect(kubeCtl.annotate).toBeCalledTimes(1);
|
||||
expect(consoleOutputSpy).toHaveBeenNthCalledWith(1, `##[debug]Skipping 'annotate namespace' as namespace annotated by other workflow` + os.EOL)
|
||||
});
|
||||
|
||||
test("deployment - deploy() - Annotate resources failed", async () => {
|
||||
//Mocks
|
||||
inputParamMock.forceDeployment = true;
|
||||
|
@ -300,5 +325,5 @@ test("deployment - deploy() - Annotate resources failed", async () => {
|
|||
const consoleOutputSpy = jest.spyOn(process.stdout, "write").mockImplementation();
|
||||
//Invoke and assert
|
||||
await expect(deployment.deploy(kubeCtl, ['manifests/deployment.yaml'], undefined)).resolves.not.toThrowError();
|
||||
expect(consoleOutputSpy).toHaveBeenNthCalledWith(1, '::warning::kubectl annotate failed' + os.EOL)
|
||||
expect(consoleOutputSpy).toHaveBeenNthCalledWith(1, '##[warning]kubectl annotate failed' + os.EOL)
|
||||
});
|
|
@ -30,7 +30,7 @@ function checkForErrors(execResults, warnIfError) {
|
|||
if (execResults.length !== 0) {
|
||||
let stderr = '';
|
||||
execResults.forEach(result => {
|
||||
if (result.stderr) {
|
||||
if (result && result.stderr) {
|
||||
if (result.code !== 0) {
|
||||
stderr += result.stderr + '\n';
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ function checkForErrors(execResults, warnIfError) {
|
|||
}
|
||||
});
|
||||
if (stderr.length > 0) {
|
||||
if (!!warnIfError) {
|
||||
if (warnIfError) {
|
||||
core.warning(stderr.trim());
|
||||
}
|
||||
else {
|
||||
|
@ -56,10 +56,10 @@ function annotateChildPods(kubectl, resourceType, resourceName, allPods) {
|
|||
if (resourceType.toLowerCase().indexOf('deployment') > -1) {
|
||||
owner = kubectl.getNewReplicaSet(resourceName);
|
||||
}
|
||||
if (!!allPods && !!allPods.items && allPods.items.length > 0) {
|
||||
if (allPods && allPods.items && allPods.items.length > 0) {
|
||||
allPods.items.forEach((pod) => {
|
||||
const owners = pod.metadata.ownerReferences;
|
||||
if (!!owners) {
|
||||
if (owners) {
|
||||
owners.forEach(ownerRef => {
|
||||
if (ownerRef.name === owner) {
|
||||
commandExecutionResults.push(kubectl.annotate('pod', pod.metadata.name, constants_1.workflowAnnotations, true));
|
||||
|
@ -72,15 +72,21 @@ function annotateChildPods(kubectl, resourceType, resourceName, allPods) {
|
|||
}
|
||||
exports.annotateChildPods = annotateChildPods;
|
||||
function annotateNamespace(kubectl, namespaceName) {
|
||||
let annotate = true;
|
||||
const result = kubectl.getResource('namespace', namespaceName);
|
||||
this.checkForErrors([result]);
|
||||
const annotationsSet = JSON.parse(result.stdout).metadata.annotations;
|
||||
if (!!annotationsSet && !!annotationsSet.runUri && annotationsSet.runUri.indexOf(process.env['GITHUB_REPOSITORY']) == -1) {
|
||||
annotate = false;
|
||||
core.debug(`Skipping 'annotate namespace' as namespace annotated by other workflow`);
|
||||
if (!result) {
|
||||
return { code: -1, stderr: 'Failed to get resource' };
|
||||
}
|
||||
if (annotate) {
|
||||
else if (result && result.stderr) {
|
||||
return result;
|
||||
}
|
||||
if (result && result.stdout) {
|
||||
const annotationsSet = JSON.parse(result.stdout).metadata.annotations;
|
||||
if (annotationsSet && annotationsSet.runUri) {
|
||||
if (annotationsSet.runUri.indexOf(process.env['GITHUB_REPOSITORY']) == -1) {
|
||||
core.debug(`Skipping 'annotate namespace' as namespace annotated by other workflow`);
|
||||
return { code: 0, stdout: '' };
|
||||
}
|
||||
}
|
||||
return kubectl.annotate('namespace', namespaceName, constants_1.workflowAnnotations, true);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ export function checkForErrors(execResults: IExecSyncResult[], warnIfError?: boo
|
|||
if (execResults.length !== 0) {
|
||||
let stderr = '';
|
||||
execResults.forEach(result => {
|
||||
if (result.stderr) {
|
||||
if (result && result.stderr) {
|
||||
if (result.code !== 0) {
|
||||
stderr += result.stderr + '\n';
|
||||
} else {
|
||||
|
@ -41,7 +41,7 @@ export function checkForErrors(execResults: IExecSyncResult[], warnIfError?: boo
|
|||
}
|
||||
});
|
||||
if (stderr.length > 0) {
|
||||
if (!!warnIfError) {
|
||||
if (warnIfError) {
|
||||
core.warning(stderr.trim());
|
||||
} else {
|
||||
throw new Error(stderr.trim());
|
||||
|
@ -57,10 +57,10 @@ export function annotateChildPods(kubectl: Kubectl, resourceType: string, resour
|
|||
owner = kubectl.getNewReplicaSet(resourceName);
|
||||
}
|
||||
|
||||
if (!!allPods && !!allPods.items && allPods.items.length > 0) {
|
||||
if (allPods && allPods.items && allPods.items.length > 0) {
|
||||
allPods.items.forEach((pod) => {
|
||||
const owners = pod.metadata.ownerReferences;
|
||||
if (!!owners) {
|
||||
if (owners) {
|
||||
owners.forEach(ownerRef => {
|
||||
if (ownerRef.name === owner) {
|
||||
commandExecutionResults.push(kubectl.annotate('pod', pod.metadata.name, workflowAnnotations, true));
|
||||
|
@ -74,15 +74,22 @@ export function annotateChildPods(kubectl: Kubectl, resourceType: string, resour
|
|||
}
|
||||
|
||||
export function annotateNamespace(kubectl: Kubectl, namespaceName: string): IExecSyncResult {
|
||||
let annotate = true;
|
||||
const result = kubectl.getResource('namespace', namespaceName);
|
||||
this.checkForErrors([result]);
|
||||
const annotationsSet = JSON.parse(result.stdout).metadata.annotations;
|
||||
if (!!annotationsSet && !!annotationsSet.runUri && annotationsSet.runUri.indexOf(process.env['GITHUB_REPOSITORY']) == -1) {
|
||||
annotate = false;
|
||||
core.debug(`Skipping 'annotate namespace' as namespace annotated by other workflow`);
|
||||
if (!result) {
|
||||
return { code: -1, stderr: 'Failed to get resource' } as IExecSyncResult;
|
||||
}
|
||||
if (annotate) {
|
||||
else if (result && result.stderr) {
|
||||
return result;
|
||||
}
|
||||
|
||||
if (result && result.stdout) {
|
||||
const annotationsSet = JSON.parse(result.stdout).metadata.annotations;
|
||||
if (annotationsSet && annotationsSet.runUri) {
|
||||
if (annotationsSet.runUri.indexOf(process.env['GITHUB_REPOSITORY']) == -1) {
|
||||
core.debug(`Skipping 'annotate namespace' as namespace annotated by other workflow`);
|
||||
return { code: 0, stdout: '' } as IExecSyncResult;
|
||||
}
|
||||
}
|
||||
return kubectl.annotate('namespace', namespaceName, workflowAnnotations, true);
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче