From 3520f63d666a7c1b940e4fb17cf393ccaeec4581 Mon Sep 17 00:00:00 2001 From: Johan Lore Date: Mon, 27 Nov 2023 12:01:53 +0100 Subject: [PATCH 1/3] feat(AdditionnalRef): implement multi ref --- .../model/v1beta1/GitHubDeploymentSpec.java | 2 + .../service/CaptureService.java | 45 ++++++++++++++----- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/decathlon/github/kubernetesstatus/model/v1beta1/GitHubDeploymentSpec.java b/src/main/java/com/decathlon/github/kubernetesstatus/model/v1beta1/GitHubDeploymentSpec.java index abba779..a74cea2 100644 --- a/src/main/java/com/decathlon/github/kubernetesstatus/model/v1beta1/GitHubDeploymentSpec.java +++ b/src/main/java/com/decathlon/github/kubernetesstatus/model/v1beta1/GitHubDeploymentSpec.java @@ -6,6 +6,7 @@ import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import java.util.List; @Data @@ -15,6 +16,7 @@ @ApiModel(description = "GitHubDeployment spec part.") public class GitHubDeploymentSpec { private NamespacedObject sourceRef; + private List additionnalRef; private ExtractRule extract; private RepositoryDetail repository; diff --git a/src/main/java/com/decathlon/github/kubernetesstatus/service/CaptureService.java b/src/main/java/com/decathlon/github/kubernetesstatus/service/CaptureService.java index 4de6f77..ffc6e3e 100644 --- a/src/main/java/com/decathlon/github/kubernetesstatus/service/CaptureService.java +++ b/src/main/java/com/decathlon/github/kubernetesstatus/service/CaptureService.java @@ -1,9 +1,11 @@ package com.decathlon.github.kubernetesstatus.service; import com.decathlon.github.kubernetesstatus.model.v1beta1.GitHubDeployment; +import com.decathlon.github.kubernetesstatus.model.v1beta1.GitHubDeploymentSpec; import com.decathlon.github.kubernetesstatus.model.v1beta1.GitHubDeploymentStatus; import com.decathlon.github.kubernetesstatus.service.kstatus.Status; import com.decathlon.github.kubernetesstatus.service.kstatus.data.KubeObjectResult; +import com.decathlon.github.kubernetesstatus.service.kstatus.data.KubeObjectStatus; import com.decathlon.github.kubernetesstatus.service.kubernetes.DynamicObjectExtractor; import com.decathlon.github.kubernetesstatus.service.kubernetes.EventManager; import com.google.gson.JsonArray; @@ -18,9 +20,7 @@ import org.apache.logging.log4j.util.Strings; import org.springframework.stereotype.Service; -import java.util.Collections; -import java.util.HashMap; -import java.util.Objects; +import java.util.*; import java.util.regex.Pattern; @Service @@ -35,26 +35,51 @@ public class CaptureService { public void capture(GitHubDeployment deployment) { + List dynamicKubernetesObjects = new ArrayList<>(); + KubeObjectResult status = null; + + log.debug("[{}/{}] Observing GitHub Deployment", deployment.getMetadata().getNamespace(), deployment.getMetadata().getName()); - var kobj=dynamicObjectExtractor.extractKubeObject(deployment.getSpec().getSourceRef()); - if (kobj==null) { + dynamicKubernetesObjects.add(dynamicObjectExtractor.extractKubeObject(deployment.getSpec().getSourceRef())); + if (dynamicKubernetesObjects.isEmpty() || dynamicKubernetesObjects.get(0) ==null) { return; } - log.info("[{}/{}] Will check {}/{} kubernetes object", deployment.getMetadata().getNamespace(), deployment.getMetadata().getName(), kobj.getMetadata().getNamespace(), kobj.getMetadata().getName()); + for(GitHubDeploymentSpec.NamespacedObject namespacedObject : deployment.getSpec().getAdditionnalRef()) { + dynamicKubernetesObjects.add(dynamicObjectExtractor.extractKubeObject(namespacedObject)); + } + + for(DynamicKubernetesObject dynamicKubernetesObject : dynamicKubernetesObjects) { + log.info("[{}/{}] Will check {}/{} kubernetes object", deployment.getMetadata().getNamespace(), deployment.getMetadata().getName(), dynamicKubernetesObject.getMetadata().getNamespace(), dynamicKubernetesObject.getMetadata().getName()); + boolean exit = false; + KubeObjectResult statusTemp = Status.compute(dynamicKubernetesObject); + switch (statusTemp.status()) { + case FAILED: + exit = true; + status = statusTemp; + break; + case CURRENT: + if(status == null) { + status = statusTemp; + } + default: + status = statusTemp; + break; + } + if(exit) break; + } + - var ref=extractRef(deployment, kobj); + var ref=extractRef(deployment, dynamicKubernetesObjects.get(0)); if (ref==null){ log.warn("[{}/{}] Cannot extract ref from kube object with extract rule {}", deployment.getMetadata().getNamespace(), deployment.getMetadata().getName(), deployment.getSpec().getExtract()); return; } - var status=Status.compute(kobj); - log.info("[{}/{}] ref found is {}, computed status is {}", deployment.getMetadata().getNamespace(), deployment.getMetadata().getName(), ref, status); - updateGhd(deployment, kobj.getMetadata(), status, ref); + updateGhd(deployment, dynamicKubernetesObjects.get(0).getMetadata(), status, ref); } private void updateGhd(GitHubDeployment deployment, V1ObjectMeta metadata, KubeObjectResult status, String ref) { From e7de20771d9de429eacaaab841588ca7eb053ad1 Mon Sep 17 00:00:00 2001 From: Johan Lore Date: Mon, 27 Nov 2023 12:04:09 +0100 Subject: [PATCH 2/3] feat(AdditionnalRef): implement multi ref --- .../github/kubernetesstatus/service/CaptureService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/decathlon/github/kubernetesstatus/service/CaptureService.java b/src/main/java/com/decathlon/github/kubernetesstatus/service/CaptureService.java index ffc6e3e..d391847 100644 --- a/src/main/java/com/decathlon/github/kubernetesstatus/service/CaptureService.java +++ b/src/main/java/com/decathlon/github/kubernetesstatus/service/CaptureService.java @@ -60,7 +60,7 @@ public void capture(GitHubDeployment deployment) { status = statusTemp; break; case CURRENT: - if(status == null) { + if(status.status().compareTo(KubeObjectStatus.CURRENT) == 0) { status = statusTemp; } default: From 992ae9d3ce809b0df2ffc9d9ebd2e761c5ba3660 Mon Sep 17 00:00:00 2001 From: Johan Lore Date: Mon, 27 Nov 2023 14:03:30 +0100 Subject: [PATCH 3/3] feat(AdditionnalRef): implement multi ref --- .../service/CaptureService.java | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/decathlon/github/kubernetesstatus/service/CaptureService.java b/src/main/java/com/decathlon/github/kubernetesstatus/service/CaptureService.java index d391847..eba8307 100644 --- a/src/main/java/com/decathlon/github/kubernetesstatus/service/CaptureService.java +++ b/src/main/java/com/decathlon/github/kubernetesstatus/service/CaptureService.java @@ -52,22 +52,12 @@ public void capture(GitHubDeployment deployment) { for(DynamicKubernetesObject dynamicKubernetesObject : dynamicKubernetesObjects) { log.info("[{}/{}] Will check {}/{} kubernetes object", deployment.getMetadata().getNamespace(), deployment.getMetadata().getName(), dynamicKubernetesObject.getMetadata().getNamespace(), dynamicKubernetesObject.getMetadata().getName()); - boolean exit = false; KubeObjectResult statusTemp = Status.compute(dynamicKubernetesObject); - switch (statusTemp.status()) { - case FAILED: - exit = true; - status = statusTemp; - break; - case CURRENT: - if(status.status().compareTo(KubeObjectStatus.CURRENT) == 0) { - status = statusTemp; - } - default: - status = statusTemp; - break; + + if(status == null || status.status().canMoveTo(statusTemp.status())) { + status = statusTemp; } - if(exit) break; + if(status.status().compareTo(KubeObjectStatus.CURRENT) != 0 ) break; }