[infra-426] | Abhishek | Refactor code

This commit is contained in:
Abhishek Katiyar
2020-07-22 20:33:20 +05:30
parent 940ea4cab2
commit b783c8592e
6 changed files with 41 additions and 25 deletions

View File

@@ -0,0 +1,5 @@
package com.navi.infra.portal.enums;
public enum KubeApplyResult{
SUCCESSFUL,UNSUCCESSFUL
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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());
}
}

View File

@@ -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));
}
}

View File

@@ -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);
}