From 9167347580c804eb230fe370542611c65aba6fba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vilius=20=C5=A0umskas?= Date: Mon, 15 Aug 2022 23:56:21 +0300 Subject: [PATCH] Improve deployment verification 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. --- .../k8sengine/KubernetesVerifiers.java | 24 ++++++++++++------- .../plugins/k8sengine/VerificationTask.java | 3 ++- .../k8sengine/KubernetesVerifiersTest.java | 14 +++++++---- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/google/jenkins/plugins/k8sengine/KubernetesVerifiers.java b/src/main/java/com/google/jenkins/plugins/k8sengine/KubernetesVerifiers.java index 9dd27ec..fd3d35f 100644 --- a/src/main/java/com/google/jenkins/plugins/k8sengine/KubernetesVerifiers.java +++ b/src/main/java/com/google/jenkins/plugins/k8sengine/KubernetesVerifiers.java @@ -137,9 +137,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. @@ -162,18 +164,24 @@ 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 status = JsonPath.read(json, STATUS_JSONPATH); Integer availableReplicas = (Integer) status.getOrDefault(AVAILABLE_REPLICAS, 0); - boolean verified = minReplicas != null - && availableReplicas != null - && minReplicas.intValue() <= availableReplicas.intValue(); + Integer updatedReplicas = (Integer) status.getOrDefault(UPDATED_REPLICAS, 0); + Integer replicas = (Integer) status.getOrDefault(REPLICAS, 0); + boolean verified = 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); diff --git a/src/main/java/com/google/jenkins/plugins/k8sengine/VerificationTask.java b/src/main/java/com/google/jenkins/plugins/k8sengine/VerificationTask.java index 20ecdc3..ca26008 100644 --- a/src/main/java/com/google/jenkins/plugins/k8sengine/VerificationTask.java +++ b/src/main/java/com/google/jenkins/plugins/k8sengine/VerificationTask.java @@ -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()); diff --git a/src/test/java/com/google/jenkins/plugins/k8sengine/KubernetesVerifiersTest.java b/src/test/java/com/google/jenkins/plugins/k8sengine/KubernetesVerifiersTest.java index 2816314..af6771a 100644 --- a/src/test/java/com/google/jenkins/plugins/k8sengine/KubernetesVerifiersTest.java +++ b/src/test/java/com/google/jenkins/plugins/k8sengine/KubernetesVerifiersTest.java @@ -44,9 +44,12 @@ public void testGoodDeploymentVerified() throws Exception { KubernetesVerifiers.VerificationResult result = 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); + String.format( + "AvailableReplicas = %s, UpdatedReplicas = %s, DesiredReplicas = %s", + availableReplicas, updatedReplicas, desiredReplicas); String verificationLog = result.toString(); assertTrue(verificationLog.contains(shouldBeInLog)); } @@ -65,10 +68,13 @@ public void testBadDeploymentNotVerified() throws Exception { KubernetesVerifiers.VerificationResult result = 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); + String.format( + "AvailableReplicas = %s, UpdatedReplicas = %s, DesiredReplicas = %s", + availableReplicas, updatedReplicas, desiredReplicas); String verificationLog = result.toString(); assertTrue(verificationLog.contains(shouldBeInLog)); }