Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[KubernetesManifest] Check manifest stability for Pods #10684

Merged
merged 4 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,7 @@
"loc.messages.NullInputObjectMetadata": "Input object metadata is null.",
"loc.messages.CanaryDeploymentAlreadyExistErrorMessage": "Canary deployment already exists. Rejecting this deployment.",
"loc.messages.InvalidRejectActionDeploymentStrategy": "Reject action works only with strategy: canary",
"loc.messages.InvalidPromotetActionDeploymentStrategy": "Promote action works only with strategy: canary"
"loc.messages.InvalidPromotetActionDeploymentStrategy": "Promote action works only with strategy: canary",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

End with '.'. Add for below messages.

"loc.messages.AllContainersNotInReadyState": "All the containers are not in a ready state",
"loc.messages.CouldNotDeterminePodStatus": "Could not determine the pod's status due to the error: %s"
}
66 changes: 65 additions & 1 deletion Tasks/KubernetesManifestV0/src/utils/DeploymentHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as fileHelper from '../utils/FileHelper';
import * as utils from '../utils/utilities';
import { IExecSyncResult } from 'azure-pipelines-task-lib/toolrunner';
import { Kubectl, Resource } from 'kubernetes-common-v2/kubectl-object-model';
import { isEqual, StringComparer } from './StringComparison';

export function deploy(kubectl: Kubectl, manifestFilePaths: string[], deploymentStrategy: string) {

Expand Down Expand Up @@ -40,7 +41,7 @@ function getManifestFiles(manifestFilePaths: string[]): string[] {
const files: string[] = utils.getManifestFiles(manifestFilePaths);

if (files == null || files.length === 0) {
throw (tl.loc('ManifestFileNotFound'));
throw (tl.loc('ManifestFileNotFound', manifestFilePaths));
}

return files;
Expand All @@ -65,6 +66,13 @@ function checkManifestStability(kubectl: Kubectl, resourceTypes: Resource[]) {
if (models.recognizedWorkloadTypesWithRolloutStatus.indexOf(resource.type.toLowerCase()) >= 0) {
rolloutStatusResults.push(kubectl.checkRolloutStatus(resource.type, resource.name));
}
if (isEqual(resource.type, constants.KubernetesWorkload.Pod, StringComparer.OrdinalIgnoreCase)) {
thesattiraju marked this conversation as resolved.
Show resolved Hide resolved
try {
checkPodStatus(kubectl, resource.name);
} catch (ex) {
tl.warning(tl.loc('CouldNotDeterminePodStatus', JSON.stringify(ex)));
}
}
});
utils.checkForErrors(rolloutStatusResults);
}
Expand Down Expand Up @@ -137,3 +145,59 @@ function updateImagePullSecretsInManifestFiles(filePaths: string[], imagePullSec
function isCanaryDeploymentStrategy(deploymentStrategy: string): boolean {
return deploymentStrategy != null && deploymentStrategy.toUpperCase() == canaryDeploymentHelper.CANARY_DEPLOYMENT_STRATEGY.toUpperCase();
}

function checkPodStatus(kubectl: Kubectl, podName: string) {
const startTime = new Date();
const timeOut = 5 * 60 * 1000; // Timeout 5 min
let currentTime = new Date();
let podStatus;
while (currentTime.getTime() - startTime.getTime() < timeOut) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is busy loop. Can you add sleep and Instead of time use Iteration.

tl.debug(`Polling for pod status: ${podName}`);
podStatus = getPodStatus(kubectl, podName);
if (podStatus.phase && podStatus.phase !== 'Pending') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of 'Unknown' status do you want to retry?

break;
}
currentTime = new Date();
}
podStatus = getPodStatus(kubectl, podName);
switch (podStatus.phase) {
case 'Succeeded':
thesattiraju marked this conversation as resolved.
Show resolved Hide resolved
case 'Running':
if (isPodReady(podStatus)) {
console.log(`pod/${podName} is successfully rolled out`);
}
thesattiraju marked this conversation as resolved.
Show resolved Hide resolved
break;
case 'Pending':
if (!isPodReady(podStatus)) {
tl.warning(`pod/${podName} rollout status check timedout`);
}
break;
case 'Failed':
tl.error(`pod/${podName} rollout failed`);
break;
default:
tl.warning(`pod/${podName} rollout status: ${podStatus.phase}`);
}
}

function getPodStatus(kubectl: Kubectl, podName: string): any {
const podResult = kubectl.getResource('pod', podName);
utils.checkForErrors([podResult]);
const podStatus = JSON.parse(podResult.stdout).status;
tl.debug(`Pod Status: ${JSON.stringify(podStatus)}`);
return podStatus;
}

function isPodReady(podStatus: any): boolean {
let allContainersAreReady = true;
podStatus.containerStatuses.forEach(container => {
if (container.ready === false) {
console.log(`'${container.name}' status: ${JSON.stringify(container.state)}`);
allContainersAreReady = false;
}
});
if (!allContainersAreReady) {
tl.warning(tl.loc('AllContainersNotInReadyState'));
}
return allContainersAreReady;
}
6 changes: 4 additions & 2 deletions Tasks/KubernetesManifestV0/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 0,
"Minor": 154,
"Patch": 4
"Patch": 5
},
"demands": [],
"groups": [],
Expand Down Expand Up @@ -310,6 +310,8 @@
"NullInputObjectMetadata": "Input object metadata is null.",
"CanaryDeploymentAlreadyExistErrorMessage": "Canary deployment already exists. Rejecting this deployment.",
"InvalidRejectActionDeploymentStrategy": "Reject action works only with strategy: canary",
"InvalidPromotetActionDeploymentStrategy": "Promote action works only with strategy: canary"
"InvalidPromotetActionDeploymentStrategy": "Promote action works only with strategy: canary",
"AllContainersNotInReadyState": "All the containers are not in a ready state",
"CouldNotDeterminePodStatus": "Could not determine the pod's status due to the error: %s"
}
}
6 changes: 4 additions & 2 deletions Tasks/KubernetesManifestV0/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 0,
"Minor": 154,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0.155.0

"Patch": 4
"Patch": 5
},
"demands": [],
"groups": [],
Expand Down Expand Up @@ -310,6 +310,8 @@
"NullInputObjectMetadata": "ms-resource:loc.messages.NullInputObjectMetadata",
"CanaryDeploymentAlreadyExistErrorMessage": "ms-resource:loc.messages.CanaryDeploymentAlreadyExistErrorMessage",
"InvalidRejectActionDeploymentStrategy": "ms-resource:loc.messages.InvalidRejectActionDeploymentStrategy",
"InvalidPromotetActionDeploymentStrategy": "ms-resource:loc.messages.InvalidPromotetActionDeploymentStrategy"
"InvalidPromotetActionDeploymentStrategy": "ms-resource:loc.messages.InvalidPromotetActionDeploymentStrategy",
"AllContainersNotInReadyState": "ms-resource:loc.messages.AllContainersNotInReadyState",
"CouldNotDeterminePodStatus": "ms-resource:loc.messages.CouldNotDeterminePodStatus"
}
}