Merge pull request #22 from navi-infra/infra-426

[infra-426] | Abhishek | Add changes to support publishing of events
This commit is contained in:
Abhishek Katiyar
2020-07-23 12:00:10 +05:30
committed by GitHub Enterprise
5 changed files with 64 additions and 4 deletions

View File

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

View File

@@ -0,0 +1,19 @@
package com.navi.infra.portal.events;
import com.navi.infra.portal.enums.KubeApplyResult;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class KubeApplyEvent {
private final KubeApplyResult result;
private final String cluster;
private final String kubeObject;
private final String name;
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

@@ -0,0 +1,16 @@
package com.navi.infra.portal.events;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class PortalEventListener {
@EventListener
public void handleKubeApplyEvent(KubeApplyEvent kubeApplyEvent){
log.info(kubeApplyEvent.toString());
}
}

View File

@@ -0,0 +1,17 @@
package com.navi.infra.portal.events;
import com.navi.infra.portal.enums.KubeApplyResult;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public class PortalEventPublisher {
private final ApplicationEventPublisher applicationEventPublisher;
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);
}