Skip to content

Commit

Permalink
Improve deployment verification
Browse files Browse the repository at this point in the history
With the exception of progress deadline, it now uses the same algorith
as "kubectl rollout status":
https://github.com/kubernetes/kubectl/blob/f89fc21e9c51d313e923eb93d1ae83754be62019/pkg/polymorphichelpers/rollout_status.go#L80-L88

While at it, rename minimum replicas to desired replicas as per
Kubernetes terminology. Minimum replicas are more used in
Horizontal Pod Autoscaling, not the Deployment configuration.
  • Loading branch information
ViliusS committed Apr 9, 2023
1 parent c5c766c commit 695feaa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,11 @@ public VerificationResult verify(KubectlWrapper kubectl, Manifests.ManifestObjec
* available replicas (status.availableReplicas).
*/
private static class DeploymentVerifier implements Verifier {
private static final String AVAILABLE_REPLICAS = "availableReplicas";
private static final String MINIMUM_REPLICAS_JSONPATH = "spec.replicas";
private static final String STATUS_JSONPATH = "status";
private static final String AVAILABLE_REPLICAS = "availableReplicas";
private static final String UPDATED_REPLICAS = "updatedReplicas";
private static final String REPLICAS = "replicas";
private static final String DESIRED_REPLICAS_JSONPATH = "spec.replicas";

/**
* Verifies that the deployment was applied to the GKE cluster.
Expand All @@ -164,19 +166,25 @@ public VerificationResult verify(KubectlWrapper kubectl, Manifests.ManifestObjec
return errorResult(e, object);
}

Integer minReplicas = JsonPath.read(json, MINIMUM_REPLICAS_JSONPATH);
Integer desiredReplicas = JsonPath.read(json, DESIRED_REPLICAS_JSONPATH);
Map<String, Object> status = JsonPath.read(json, STATUS_JSONPATH);
Integer availableReplicas = (Integer) status.getOrDefault(AVAILABLE_REPLICAS, 0);
Integer updatedReplicas = (Integer) status.getOrDefault(UPDATED_REPLICAS, 0);
Integer replicas = (Integer) status.getOrDefault(REPLICAS, 0);
boolean verified =
minReplicas != null
&& availableReplicas != null
&& minReplicas.intValue() <= availableReplicas.intValue();
desiredReplicas != null
&& updatedReplicas.intValue() >= desiredReplicas.intValue()
&& replicas.intValue() <= updatedReplicas.intValue()
&& availableReplicas.intValue() == updatedReplicas.intValue();

log.append("AvailableReplicas = ")
.append(availableReplicas)
.append(",")
.append(" MinimumReplicas = ")
.append(minReplicas)
.append(" UpdatedReplicas = ")
.append(updatedReplicas)
.append(",")
.append(" DesiredReplicas = ")
.append(desiredReplicas)
.append("\n");

return new VerificationResult(log.toString(), verified, object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public KubernetesVerifiers.VerificationResult getVerificationResult() {
* @return Self-reference after performing verify.
*/
private VerificationTask verify() {
consoleLogger.println(String.format("Verifying: %s ", manifestObject.describe()));
consoleLogger.println(
Messages.KubernetesEngineBuilder_VerifyingLogPrefix(manifestObject.describe()));
currentResult = KubernetesVerifiers.verify(kubectl, manifestObject);
if (isVerified()) {
consoleLogger.println(currentResult.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ public void testGoodDeploymentVerified() throws Exception {
KubernetesVerifiers.verify(kubectl, goodDeployment);
assertTrue(result.isVerified());
Integer availableReplicas = JsonPath.read(goodDeploymentOutput, "status.availableReplicas");
Integer minimumReplicas = JsonPath.read(goodDeploymentOutput, "spec.replicas");
Integer updatedReplicas = JsonPath.read(goodDeploymentOutput, "status.updatedReplicas");
Integer desiredReplicas = JsonPath.read(goodDeploymentOutput, "spec.replicas");
String shouldBeInLog =
String.format(
"AvailableReplicas = %s, MinimumReplicas = %s", availableReplicas, minimumReplicas);
"AvailableReplicas = %s, UpdatedReplicas = %s, DesiredReplicas = %s",
availableReplicas, updatedReplicas, desiredReplicas);
String verificationLog = result.toString();
assertTrue(verificationLog.contains(shouldBeInLog));
}
Expand All @@ -70,11 +72,13 @@ public void testBadDeploymentNotVerified() throws Exception {
KubernetesVerifiers.verify(kubectl, badDeployment);
assertFalse(result.isVerified());

Integer minimumReplicas = JsonPath.read(badDeploymentOutput, "spec.replicas");
Integer desiredReplicas = JsonPath.read(badDeploymentOutput, "spec.replicas");
Integer updatedReplicas = JsonPath.read(badDeploymentOutput, "status.updatedReplicas");
Integer availableReplicas = 0;
String shouldBeInLog =
String.format(
"AvailableReplicas = %s, MinimumReplicas = %s", availableReplicas, minimumReplicas);
"AvailableReplicas = %s, UpdatedReplicas = %s, DesiredReplicas = %s",
availableReplicas, updatedReplicas, desiredReplicas);
String verificationLog = result.toString();
assertTrue(verificationLog.contains(shouldBeInLog));
}
Expand Down

0 comments on commit 695feaa

Please sign in to comment.