diff --git a/service/src/main/java/io/spaship/operator/controller/OperatorService.java b/service/src/main/java/io/spaship/operator/controller/OperatorService.java index a30558a2..b87bcd49 100644 --- a/service/src/main/java/io/spaship/operator/controller/OperatorService.java +++ b/service/src/main/java/io/spaship/operator/controller/OperatorService.java @@ -132,7 +132,31 @@ public WebsiteStatus initNewWebsite(Website website, boolean redeploy) { if (redeploy) { contentController.redeploy(env, website); } + + //IMPLEMENTATION OF ISSUE 59 Start + String eventPayloadEnd = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()), + EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()), + EventAttribute.CODE.concat(EventAttribute.EventCode.WEBSITE_CREATE.name()), + EventAttribute.TRACE_ID.concat(traceId), + EventAttribute.TIMESTAMP.concat(LocalDateTime.now().toString()), + EventAttribute.MESSAGE.concat(website.toString()), + EventAttribute.ENVIRONMENT.concat(env) + ); + eventSourcingEngine.publishMessage(eventPayloadEnd); + //IMPLEMENTATION OF ISSUE 59 End + } catch (RuntimeException ex) { + //IMPLEMENTATION OF ISSUE 59 Start + String eventPayloadEnd = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()), + EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()), + EventAttribute.CODE.concat(EventAttribute.EventCode.WEBSITE_INIT_FAILED.name()), + EventAttribute.TRACE_ID.concat(traceId), + EventAttribute.TIMESTAMP.concat(LocalDateTime.now().toString()), + EventAttribute.MESSAGE.concat(website.toString()), + EventAttribute.ENVIRONMENT.concat(env) + ); + eventSourcingEngine.publishMessage(eventPayloadEnd); + //IMPLEMENTATION OF ISSUE 59 End log.error("Error processing env=" + env, ex); exception = ex; // continue with processing other environments and throw exception after loop ends @@ -142,21 +166,98 @@ public WebsiteStatus initNewWebsite(Website website, boolean redeploy) { throw exception; } - //IMPLEMENTATION OF ISSUE 59 Start - String eventPayloadEnd = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()), - EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()), - EventAttribute.CODE.concat(EventAttribute.EventCode.WEBSITE_CREATE.name()), - EventAttribute.TRACE_ID.concat(traceId), - EventAttribute.TIMESTAMP.concat(LocalDateTime.now().toString()), - EventAttribute.MESSAGE.concat(website.toString()) - ); - eventSourcingEngine.publishMessage(eventPayloadEnd); - //IMPLEMENTATION OF ISSUE 59 End + log.debugf("Infrastructure initialized. status=%s", status); + return status; + } + + + + public WebsiteStatus initNewWebsite(Website website, boolean redeploy,String traceId) { + + if(Objects.isNull(website.getConfig())) + System.exit(1); + + Set enabledEnvs = website.getEnabledEnvs(); + log.infof("Init infrastructure for websiteId=%s, enabledEnvs=%s redeploy=%s", website.getId(), enabledEnvs, redeploy); + websiteRepository.addWebsite(website); + + RuntimeException exception = null; + WebsiteStatus status = website.getStatus(); + if (status == null) { + status = new WebsiteStatus(); + } + for (String env : enabledEnvs) { + try { + log.debugf("Processing env=%s", env); + + setupCoreServices(env, website); + + String apiHost = null; + Integer port = null; + if (ingressController.isEnabled()) { + Ingress ingress = ingressController.updateIngress(env, website); + if (ingress != null) { + String contentHost = ingress.getSpec().getRules().get(0).getHost(); + status.addEnvHost(env, contentHost); + } + } else if (routerController.isEnabled()) { + List contentRoutes = routerController.updateWebsiteRoutes(env, website); + if (contentRoutes != null && contentRoutes.size() > 0) { + String contentHost = contentRoutes.get(0).getSpec().getHost(); + status.addEnvHost(env, contentHost); + } + routerController.updateApiRoute(env, website); + } else { + log.infof("No routing created"); + } + + if (redeploy) { + contentController.redeploy(env, website); + } + + //IMPLEMENTATION OF ISSUE 59 Start + String eventPayloadEnd = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()), + EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()), + EventAttribute.CODE.concat(EventAttribute.EventCode.WEBSITE_CREATE.name()), + EventAttribute.TRACE_ID.concat(traceId), + EventAttribute.TIMESTAMP.concat(LocalDateTime.now().toString()), + EventAttribute.MESSAGE.concat(website.toString()), + EventAttribute.ENVIRONMENT.concat(env) + ); + eventSourcingEngine.publishMessage(eventPayloadEnd); + //IMPLEMENTATION OF ISSUE 59 End + + } catch (RuntimeException ex) { + //IMPLEMENTATION OF ISSUE 59 Start + String eventPayloadEnd = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()), + EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()), + EventAttribute.CODE.concat(EventAttribute.EventCode.WEBSITE_INIT_FAILED.name()), + EventAttribute.TRACE_ID.concat(traceId), + EventAttribute.TIMESTAMP.concat(LocalDateTime.now().toString()), + EventAttribute.MESSAGE.concat(website.toString()), + EventAttribute.ENVIRONMENT.concat(env) + ); + eventSourcingEngine.publishMessage(eventPayloadEnd); + //IMPLEMENTATION OF ISSUE 59 End + log.error("Error processing env=" + env, ex); + exception = ex; + // continue with processing other environments and throw exception after loop ends + } + } + if (exception != null) { + throw exception; + } log.debugf("Infrastructure initialized. status=%s", status); return status; } + + + + + + private void setupCoreServices(String env, Website website) { String namespace = website.getMetadata().getNamespace(); final String websiteName = Utils.getWebsiteName(website); @@ -189,6 +290,40 @@ public void deleteInfrastructure(Website website) { } } + + + public void deleteInfrastructure(Website website,String traceId) { + log.infof("Delete infrastructure for websiteId=%s", website.getId()); + + String namespace = website.getMetadata().getNamespace(); + final String websiteName = Utils.getWebsiteName(website); + + for (String env : website.getEnabledEnvs()) { + contentController.deleteDeployment(env, namespace, websiteName); + contentController.deleteConfigs(env, namespace, website); + + if (ingressController.isEnabled()) { + ingressController.deleteIngress(env, website); + } else if (routerController.isEnabled()) { + routerController.deleteWebsiteRoutes(env, website); + } else { + log.infof("No routing deleted"); + } + + String eventPayload = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()), + EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()), + EventAttribute.CODE.concat(EventAttribute.EventCode.WEBSITE_DELETED.name()), + EventAttribute.TRACE_ID.concat(traceId), + EventAttribute.TIMESTAMP.concat(LocalDateTime.now().toString()), + EventAttribute.MESSAGE.concat(website.toString()), + EventAttribute.ENVIRONMENT.concat(env) + ); + eventSourcingEngine.publishMessage(eventPayload); + + } + } + + public List updateRelatedComponents(List authorizedWebsites, String gitUrl, String ref, Set updatedWebsites) { List updates = new ArrayList<>(); // Iterate over authorized websites @@ -327,16 +462,6 @@ public static Website createWebsiteCopy(Website website, String previewId, Strin public void createOrUpdateWebsite(Website website, boolean redeploy) throws GitAPIException, IOException { log.infof("Create/Update website website_id=%s redeploy=%s", website.getId(), redeploy); - String traceId = UUID.randomUUID().toString(); - String eventPayloadStart = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()), - EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()), - EventAttribute.CODE.concat(EventAttribute.EventCode.WEBSITE_CREATE_OR_UPDATE_INIT.name()), - EventAttribute.TRACE_ID.concat(traceId), - EventAttribute.TIMESTAMP.concat(LocalDateTime.now().toString()), - EventAttribute.MESSAGE.concat(website.toString()) - ); - eventSourcingEngine.publishMessage(eventPayloadStart); - if (!websiteController.isCrdEnabled()) { deployNewWebsite(website, true, redeploy); return; @@ -349,40 +474,29 @@ public void createOrUpdateWebsite(Website website, boolean redeploy) throws GitA if (existingWebsite != null) { websiteController.updateStatus(existingWebsite, WebsiteStatus.STATUS.FORCE_UPDATE); - //IMPLEMENTATION OF ISSUE 59 Start - String eventPayload = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()), - EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()), - EventAttribute.CODE.concat(EventAttribute.EventCode.PREVIEW_UPDATE.name()), - EventAttribute.TRACE_ID.concat(traceId), - EventAttribute.TIMESTAMP.concat(LocalDateTime.now().toString()), - EventAttribute.MESSAGE.concat(existingWebsite.toString()) - ); - eventSourcingEngine.publishMessage(eventPayload); - //IMPLEMENTATION OF ISSUE 59 End - } else { // This is basically creation of new website websiteController.getWebsiteClient() .inNamespace(website.getMetadata().getNamespace()) .withName(website.getMetadata().getName()) .createOrReplace(website); - - //IMPLEMENTATION OF ISSUE 59 Start - String eventPayload = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()), - EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()), - EventAttribute.CODE.concat(EventAttribute.EventCode.PREVIEW_CREATE.name()), - EventAttribute.TRACE_ID.concat(traceId), - EventAttribute.TIMESTAMP.concat(LocalDateTime.now().toString()), - EventAttribute.MESSAGE.concat(website.toString()) - ); - eventSourcingEngine.publishMessage(eventPayload); - //IMPLEMENTATION OF ISSUE 59 End - } } public void deleteWebsite(Website website) { log.infof("Deleting website website_id=%s", website.getId()); + String traceId = UUID.randomUUID().toString(); + //IMPLEMENTATION OF ISSUE 59 Start + String eventPayload = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()), + EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()), + EventAttribute.CODE.concat(EventAttribute.EventCode.WEBSITE_DELETE_INIT.name()), + EventAttribute.TRACE_ID.concat(traceId), + EventAttribute.TIMESTAMP.concat(LocalDateTime.now().toString()), + EventAttribute.MESSAGE.concat(website.toString()) + ); + eventSourcingEngine.publishMessage(eventPayload); + //IMPLEMENTATION OF ISSUE 59 End + if (websiteController.isCrdEnabled()) { websiteController.getWebsiteClient().inNamespace(website.getMetadata().getNamespace()).delete(website); } else { @@ -391,22 +505,27 @@ public void deleteWebsite(Website website) { log.error("website doesn't exists in memory"); return; } - deleteInfrastructure(websiteToDelete); + deleteInfrastructure(websiteToDelete,traceId); } - //IMPLEMENTATION OF ISSUE 59 Start - String eventPayload = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()), - EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()), - EventAttribute.CODE.concat(EventAttribute.EventCode.PREVIEW_DELETE.name()), - EventAttribute.MESSAGE.concat("website preview deleted") - ); - eventSourcingEngine.publishMessage(eventPayload); - //IMPLEMENTATION OF ISSUE 59 End + } public WebsiteStatus deployNewWebsite(Website website, boolean updateGitIfExists, boolean redeploy) throws IOException, GitAPIException { + + String traceId = UUID.randomUUID().toString(); + + String eventPayloadStart = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()), + EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()), + EventAttribute.CODE.concat(EventAttribute.EventCode.WEBSITE_CREATE_OR_UPDATE_INIT.name()), + EventAttribute.TRACE_ID.concat(traceId), + EventAttribute.TIMESTAMP.concat(LocalDateTime.now().toString()), + EventAttribute.MESSAGE.concat(website.toString()) + ); + eventSourcingEngine.publishMessage(eventPayloadStart); + WebsiteConfig websiteConfig = gitWebsiteConfigService.cloneRepo(website, updateGitIfExists); website.setConfig(websiteConfig); - return initNewWebsite(website, redeploy); + return initNewWebsite(website, redeploy,traceId); } public void updateAndRegisterWebsite(Website website, boolean updateGitIfExists) throws GitAPIException, IOException { diff --git a/service/src/main/java/io/spaship/operator/controller/WebsiteController.java b/service/src/main/java/io/spaship/operator/controller/WebsiteController.java index 1ec3eff7..b9f35726 100644 --- a/service/src/main/java/io/spaship/operator/controller/WebsiteController.java +++ b/service/src/main/java/io/spaship/operator/controller/WebsiteController.java @@ -28,6 +28,7 @@ import javax.inject.Inject; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.time.LocalDateTime; import java.util.*; import java.util.concurrent.TimeUnit; @@ -234,24 +235,29 @@ public static boolean websiteSpecGitChanged(WebsiteSpec oldSpec, WebsiteSpec new } public void websiteDeleted(Website websiteToDelete) { + + String traceId = UUID.randomUUID().toString(); + //IMPLEMENTATION OF ISSUE 59 Start + String eventPayload = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(websiteToDelete.getMetadata().getName()), + EventAttribute.NAMESPACE.concat(websiteToDelete.getMetadata().getNamespace()), + EventAttribute.CODE.concat(EventAttribute.EventCode.WEBSITE_DELETE_INIT.name()), + EventAttribute.TRACE_ID.concat(traceId), + EventAttribute.TIMESTAMP.concat(LocalDateTime.now().toString()), + EventAttribute.MESSAGE.concat(websiteToDelete.toString()) + ); + eventSourcingEngine.publishMessage(eventPayload); + //IMPLEMENTATION OF ISSUE 59 End + log.infof("Website deleted, websiteId=%s", websiteToDelete.getId()); try { Website website = websiteRepository.getWebsite(websiteToDelete.getId()); if (website != null) { gitWebsiteConfigService.deleteRepo(websiteToDelete); - operatorService.deleteInfrastructure(website); + operatorService.deleteInfrastructure(website,traceId); websiteRepository.removeWebsite(website.getId()); } removeLock(websiteToDelete.getMetadata().getNamespace(), websiteToDelete.getMetadata().getName()); - //IMPLEMENTATION OF ISSUE 59 Start - String eventPayload = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()), - EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()), - EventAttribute.CODE.concat(EventAttribute.EventCode.WEBSITE_DELETE.name()), - EventAttribute.MESSAGE.concat("website removed") - ); - eventSourcingEngine.publishMessage(eventPayload); - //IMPLEMENTATION OF ISSUE 59 End } catch (Exception e) { log.error("Error on CRD deleted", e); diff --git a/service/src/main/java/io/spaship/operator/utility/EventAttribute.java b/service/src/main/java/io/spaship/operator/utility/EventAttribute.java index 3a9048a7..98ef4251 100644 --- a/service/src/main/java/io/spaship/operator/utility/EventAttribute.java +++ b/service/src/main/java/io/spaship/operator/utility/EventAttribute.java @@ -10,19 +10,20 @@ public interface EventAttribute { String TRACE_ID="traceId~"; String TIMESTAMP="timestamp~"; String ERROR="error~"; - String ENVIRONMENT="refresh-env~"; + String ENVIRONMENT="target-env~"; enum EventCode{ WEBSITE_CREATE_INIT, WEBSITE_CREATE_OR_UPDATE_INIT, WEBSITE_REFRESH_COMPONENT_INIT, WEBSITE_REFRESH_COMPONENT, WEBSITE_REFRESH_COMPONENT_FAILED, + WEBSITE_INIT_FAILED, WEBSITE_CREATE, WEBSITE_UPDATE, - WEBSITE_DELETE, + WEBSITE_DELETE_INIT, + WEBSITE_DELETED, PREVIEW_CREATE, PREVIEW_UPDATE, - PREVIEW_DELETE, RELEASE_DEPLOY, RELEASE_DELETE; }