[infra-426] | Abhishek | Refactor code
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
package com.navi.infra.portal.enums;
|
||||
|
||||
public enum KubeApplyResult{
|
||||
SUCCESSFUL,UNSUCCESSFUL
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.navi.infra.portal.events;
|
||||
|
||||
import com.navi.infra.portal.enums.KubeApplyResult;
|
||||
|
||||
public class KubeApplyEvent {
|
||||
private final String cluster;
|
||||
private final String kubeObject;
|
||||
private final String name;
|
||||
private final KubeApplyResult result;
|
||||
|
||||
public KubeApplyEvent(KubeApplyResult result,String cluster, String kubeObjectType, String name){
|
||||
this.cluster=cluster;
|
||||
this.kubeObject=kubeObjectType;
|
||||
this.name=name;
|
||||
this.result=result;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
if(this.result==KubeApplyResult.SUCCESSFUL){
|
||||
return String.format("%s %s updated in %s",name, kubeObject, cluster);
|
||||
}
|
||||
return String.format("%s %s could not be updated in %s",name, kubeObject, cluster);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.navi.infra.portal.events;
|
||||
|
||||
public class KubeApplySuccessfulEvent {
|
||||
private final String cluster;
|
||||
private final String kubeObject;
|
||||
private final String name;
|
||||
|
||||
public KubeApplySuccessfulEvent(String cluster, String kubeObjectType, String name){
|
||||
this.cluster=cluster;
|
||||
this.kubeObject=kubeObjectType;
|
||||
this.name=name;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return String.format("%s %s updated in %s",name, kubeObject, cluster);
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,8 @@ import org.springframework.stereotype.Component;
|
||||
public class PortalEventListener {
|
||||
|
||||
@EventListener
|
||||
public void handleKubeUpdateEvent(KubeApplySuccessfulEvent kubeApplySuccessfulEvent){
|
||||
log.info(kubeApplySuccessfulEvent.toString());
|
||||
public void handleKubeApplyEvent(KubeApplyEvent kubeApplyEvent){
|
||||
log.info(kubeApplyEvent.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.navi.infra.portal.events;
|
||||
|
||||
import com.navi.infra.portal.enums.KubeApplyResult;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -12,7 +13,7 @@ public class PortalEventPublisher {
|
||||
this.applicationEventPublisher = applicationEventPublisher;
|
||||
}
|
||||
|
||||
public void publishKubeUpdateEvent(String cluster, String kubeObjectType, String name) {
|
||||
applicationEventPublisher.publishEvent(new KubeApplySuccessfulEvent(cluster,kubeObjectType,name));
|
||||
public void publishKubeApplyEvent(KubeApplyResult result, String cluster, String kubeObjectType, String name) {
|
||||
applicationEventPublisher.publishEvent(new KubeApplyEvent(result,cluster,kubeObjectType,name));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,15 +3,15 @@ package com.navi.infra.portal.kubernetes.manifest;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.navi.infra.portal.bash.BashExecute;
|
||||
import com.navi.infra.portal.domain.Manifest;
|
||||
import com.navi.infra.portal.enums.KubeApplyResult;
|
||||
import com.navi.infra.portal.events.PortalEventPublisher;
|
||||
import com.navi.infra.portal.exceptions.KubernetesManifestException;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -33,6 +33,8 @@ public class KubernetesManifestService {
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
private final PortalEventPublisher portalEventPublisher;
|
||||
|
||||
public String generateManifests(Manifest manifest) {
|
||||
writeAggregatedManifestAsJson(manifest);
|
||||
int exitCode = generateKManifests(getManifestPath(manifest),
|
||||
@@ -92,9 +94,10 @@ public class KubernetesManifestService {
|
||||
log.info("Executing command {}", command);
|
||||
|
||||
if (BashExecute.execute(command) > 0) {
|
||||
portalEventPublisher.publishKubeApplyEvent(KubeApplyResult.UNSUCCESSFUL ,clusterName,manifestObject,manifest.getName());
|
||||
throw new RuntimeException(String.format("Unable to apply %s", manifestObject));
|
||||
}
|
||||
|
||||
portalEventPublisher.publishKubeApplyEvent(KubeApplyResult.SUCCESSFUL ,clusterName,manifestObject,manifest.getName());
|
||||
log.info("Successfully applied {}", manifestObject);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user