Skip to content
This repository was archived by the owner on Oct 11, 2021. It is now read-only.

Commit 8a1d275

Browse files
authored
Merge pull request #109 from arkaprovob/main
some changes in event origin and refactored event attribute
2 parents 817e9dc + 91fd5c8 commit 8a1d275

File tree

3 files changed

+191
-65
lines changed

3 files changed

+191
-65
lines changed

service/src/main/java/io/spaship/operator/controller/OperatorService.java

Lines changed: 172 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,31 @@ public WebsiteStatus initNewWebsite(Website website, boolean redeploy) {
132132
if (redeploy) {
133133
contentController.redeploy(env, website);
134134
}
135+
136+
//IMPLEMENTATION OF ISSUE 59 Start
137+
String eventPayloadEnd = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()),
138+
EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()),
139+
EventAttribute.CODE.concat(EventAttribute.EventCode.WEBSITE_CREATE.name()),
140+
EventAttribute.TRACE_ID.concat(traceId),
141+
EventAttribute.TIMESTAMP.concat(LocalDateTime.now().toString()),
142+
EventAttribute.MESSAGE.concat(website.toString()),
143+
EventAttribute.ENVIRONMENT.concat(env)
144+
);
145+
eventSourcingEngine.publishMessage(eventPayloadEnd);
146+
//IMPLEMENTATION OF ISSUE 59 End
147+
135148
} catch (RuntimeException ex) {
149+
//IMPLEMENTATION OF ISSUE 59 Start
150+
String eventPayloadEnd = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()),
151+
EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()),
152+
EventAttribute.CODE.concat(EventAttribute.EventCode.WEBSITE_INIT_FAILED.name()),
153+
EventAttribute.TRACE_ID.concat(traceId),
154+
EventAttribute.TIMESTAMP.concat(LocalDateTime.now().toString()),
155+
EventAttribute.MESSAGE.concat(website.toString()),
156+
EventAttribute.ENVIRONMENT.concat(env)
157+
);
158+
eventSourcingEngine.publishMessage(eventPayloadEnd);
159+
//IMPLEMENTATION OF ISSUE 59 End
136160
log.error("Error processing env=" + env, ex);
137161
exception = ex;
138162
// continue with processing other environments and throw exception after loop ends
@@ -142,21 +166,98 @@ public WebsiteStatus initNewWebsite(Website website, boolean redeploy) {
142166
throw exception;
143167
}
144168

145-
//IMPLEMENTATION OF ISSUE 59 Start
146-
String eventPayloadEnd = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()),
147-
EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()),
148-
EventAttribute.CODE.concat(EventAttribute.EventCode.WEBSITE_CREATE.name()),
149-
EventAttribute.TRACE_ID.concat(traceId),
150-
EventAttribute.TIMESTAMP.concat(LocalDateTime.now().toString()),
151-
EventAttribute.MESSAGE.concat(website.toString())
152-
);
153-
eventSourcingEngine.publishMessage(eventPayloadEnd);
154-
//IMPLEMENTATION OF ISSUE 59 End
169+
log.debugf("Infrastructure initialized. status=%s", status);
170+
return status;
171+
}
172+
173+
174+
175+
public WebsiteStatus initNewWebsite(Website website, boolean redeploy,String traceId) {
176+
177+
if(Objects.isNull(website.getConfig()))
178+
System.exit(1);
179+
180+
Set<String> enabledEnvs = website.getEnabledEnvs();
181+
log.infof("Init infrastructure for websiteId=%s, enabledEnvs=%s redeploy=%s", website.getId(), enabledEnvs, redeploy);
182+
websiteRepository.addWebsite(website);
183+
184+
RuntimeException exception = null;
185+
WebsiteStatus status = website.getStatus();
186+
if (status == null) {
187+
status = new WebsiteStatus();
188+
}
189+
for (String env : enabledEnvs) {
190+
try {
191+
log.debugf("Processing env=%s", env);
192+
193+
setupCoreServices(env, website);
194+
195+
String apiHost = null;
196+
Integer port = null;
197+
if (ingressController.isEnabled()) {
198+
Ingress ingress = ingressController.updateIngress(env, website);
199+
if (ingress != null) {
200+
String contentHost = ingress.getSpec().getRules().get(0).getHost();
201+
status.addEnvHost(env, contentHost);
202+
}
203+
} else if (routerController.isEnabled()) {
204+
List<Route> contentRoutes = routerController.updateWebsiteRoutes(env, website);
205+
if (contentRoutes != null && contentRoutes.size() > 0) {
206+
String contentHost = contentRoutes.get(0).getSpec().getHost();
207+
status.addEnvHost(env, contentHost);
208+
}
209+
routerController.updateApiRoute(env, website);
210+
} else {
211+
log.infof("No routing created");
212+
}
213+
214+
if (redeploy) {
215+
contentController.redeploy(env, website);
216+
}
217+
218+
//IMPLEMENTATION OF ISSUE 59 Start
219+
String eventPayloadEnd = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()),
220+
EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()),
221+
EventAttribute.CODE.concat(EventAttribute.EventCode.WEBSITE_CREATE.name()),
222+
EventAttribute.TRACE_ID.concat(traceId),
223+
EventAttribute.TIMESTAMP.concat(LocalDateTime.now().toString()),
224+
EventAttribute.MESSAGE.concat(website.toString()),
225+
EventAttribute.ENVIRONMENT.concat(env)
226+
);
227+
eventSourcingEngine.publishMessage(eventPayloadEnd);
228+
//IMPLEMENTATION OF ISSUE 59 End
229+
230+
} catch (RuntimeException ex) {
231+
//IMPLEMENTATION OF ISSUE 59 Start
232+
String eventPayloadEnd = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()),
233+
EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()),
234+
EventAttribute.CODE.concat(EventAttribute.EventCode.WEBSITE_INIT_FAILED.name()),
235+
EventAttribute.TRACE_ID.concat(traceId),
236+
EventAttribute.TIMESTAMP.concat(LocalDateTime.now().toString()),
237+
EventAttribute.MESSAGE.concat(website.toString()),
238+
EventAttribute.ENVIRONMENT.concat(env)
239+
);
240+
eventSourcingEngine.publishMessage(eventPayloadEnd);
241+
//IMPLEMENTATION OF ISSUE 59 End
242+
log.error("Error processing env=" + env, ex);
243+
exception = ex;
244+
// continue with processing other environments and throw exception after loop ends
245+
}
246+
}
247+
if (exception != null) {
248+
throw exception;
249+
}
155250

156251
log.debugf("Infrastructure initialized. status=%s", status);
157252
return status;
158253
}
159254

255+
256+
257+
258+
259+
260+
160261
private void setupCoreServices(String env, Website website) {
161262
String namespace = website.getMetadata().getNamespace();
162263
final String websiteName = Utils.getWebsiteName(website);
@@ -189,6 +290,40 @@ public void deleteInfrastructure(Website website) {
189290
}
190291
}
191292

293+
294+
295+
public void deleteInfrastructure(Website website,String traceId) {
296+
log.infof("Delete infrastructure for websiteId=%s", website.getId());
297+
298+
String namespace = website.getMetadata().getNamespace();
299+
final String websiteName = Utils.getWebsiteName(website);
300+
301+
for (String env : website.getEnabledEnvs()) {
302+
contentController.deleteDeployment(env, namespace, websiteName);
303+
contentController.deleteConfigs(env, namespace, website);
304+
305+
if (ingressController.isEnabled()) {
306+
ingressController.deleteIngress(env, website);
307+
} else if (routerController.isEnabled()) {
308+
routerController.deleteWebsiteRoutes(env, website);
309+
} else {
310+
log.infof("No routing deleted");
311+
}
312+
313+
String eventPayload = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()),
314+
EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()),
315+
EventAttribute.CODE.concat(EventAttribute.EventCode.WEBSITE_DELETED.name()),
316+
EventAttribute.TRACE_ID.concat(traceId),
317+
EventAttribute.TIMESTAMP.concat(LocalDateTime.now().toString()),
318+
EventAttribute.MESSAGE.concat(website.toString()),
319+
EventAttribute.ENVIRONMENT.concat(env)
320+
);
321+
eventSourcingEngine.publishMessage(eventPayload);
322+
323+
}
324+
}
325+
326+
192327
public List<Future> updateRelatedComponents(List<Website> authorizedWebsites, String gitUrl, String ref, Set<String> updatedWebsites) {
193328
List<Future> updates = new ArrayList<>();
194329
// Iterate over authorized websites
@@ -327,16 +462,6 @@ public static Website createWebsiteCopy(Website website, String previewId, Strin
327462
public void createOrUpdateWebsite(Website website, boolean redeploy) throws GitAPIException, IOException {
328463
log.infof("Create/Update website website_id=%s redeploy=%s", website.getId(), redeploy);
329464

330-
String traceId = UUID.randomUUID().toString();
331-
String eventPayloadStart = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()),
332-
EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()),
333-
EventAttribute.CODE.concat(EventAttribute.EventCode.WEBSITE_CREATE_OR_UPDATE_INIT.name()),
334-
EventAttribute.TRACE_ID.concat(traceId),
335-
EventAttribute.TIMESTAMP.concat(LocalDateTime.now().toString()),
336-
EventAttribute.MESSAGE.concat(website.toString())
337-
);
338-
eventSourcingEngine.publishMessage(eventPayloadStart);
339-
340465
if (!websiteController.isCrdEnabled()) {
341466
deployNewWebsite(website, true, redeploy);
342467
return;
@@ -349,40 +474,29 @@ public void createOrUpdateWebsite(Website website, boolean redeploy) throws GitA
349474
if (existingWebsite != null) {
350475
websiteController.updateStatus(existingWebsite, WebsiteStatus.STATUS.FORCE_UPDATE);
351476

352-
//IMPLEMENTATION OF ISSUE 59 Start
353-
String eventPayload = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()),
354-
EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()),
355-
EventAttribute.CODE.concat(EventAttribute.EventCode.PREVIEW_UPDATE.name()),
356-
EventAttribute.TRACE_ID.concat(traceId),
357-
EventAttribute.TIMESTAMP.concat(LocalDateTime.now().toString()),
358-
EventAttribute.MESSAGE.concat(existingWebsite.toString())
359-
);
360-
eventSourcingEngine.publishMessage(eventPayload);
361-
//IMPLEMENTATION OF ISSUE 59 End
362-
363477
} else { // This is basically creation of new website
364478
websiteController.getWebsiteClient()
365479
.inNamespace(website.getMetadata().getNamespace())
366480
.withName(website.getMetadata().getName())
367481
.createOrReplace(website);
368-
369-
//IMPLEMENTATION OF ISSUE 59 Start
370-
String eventPayload = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()),
371-
EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()),
372-
EventAttribute.CODE.concat(EventAttribute.EventCode.PREVIEW_CREATE.name()),
373-
EventAttribute.TRACE_ID.concat(traceId),
374-
EventAttribute.TIMESTAMP.concat(LocalDateTime.now().toString()),
375-
EventAttribute.MESSAGE.concat(website.toString())
376-
);
377-
eventSourcingEngine.publishMessage(eventPayload);
378-
//IMPLEMENTATION OF ISSUE 59 End
379-
380482
}
381483
}
382484

383485
public void deleteWebsite(Website website) {
384486
log.infof("Deleting website website_id=%s", website.getId());
385487

488+
String traceId = UUID.randomUUID().toString();
489+
//IMPLEMENTATION OF ISSUE 59 Start
490+
String eventPayload = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()),
491+
EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()),
492+
EventAttribute.CODE.concat(EventAttribute.EventCode.WEBSITE_DELETE_INIT.name()),
493+
EventAttribute.TRACE_ID.concat(traceId),
494+
EventAttribute.TIMESTAMP.concat(LocalDateTime.now().toString()),
495+
EventAttribute.MESSAGE.concat(website.toString())
496+
);
497+
eventSourcingEngine.publishMessage(eventPayload);
498+
//IMPLEMENTATION OF ISSUE 59 End
499+
386500
if (websiteController.isCrdEnabled()) {
387501
websiteController.getWebsiteClient().inNamespace(website.getMetadata().getNamespace()).delete(website);
388502
} else {
@@ -391,22 +505,27 @@ public void deleteWebsite(Website website) {
391505
log.error("website doesn't exists in memory");
392506
return;
393507
}
394-
deleteInfrastructure(websiteToDelete);
508+
deleteInfrastructure(websiteToDelete,traceId);
395509
}
396-
//IMPLEMENTATION OF ISSUE 59 Start
397-
String eventPayload = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()),
398-
EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()),
399-
EventAttribute.CODE.concat(EventAttribute.EventCode.PREVIEW_DELETE.name()),
400-
EventAttribute.MESSAGE.concat("website preview deleted")
401-
);
402-
eventSourcingEngine.publishMessage(eventPayload);
403-
//IMPLEMENTATION OF ISSUE 59 End
510+
404511
}
405512

406513
public WebsiteStatus deployNewWebsite(Website website, boolean updateGitIfExists, boolean redeploy) throws IOException, GitAPIException {
514+
515+
String traceId = UUID.randomUUID().toString();
516+
517+
String eventPayloadStart = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()),
518+
EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()),
519+
EventAttribute.CODE.concat(EventAttribute.EventCode.WEBSITE_CREATE_OR_UPDATE_INIT.name()),
520+
EventAttribute.TRACE_ID.concat(traceId),
521+
EventAttribute.TIMESTAMP.concat(LocalDateTime.now().toString()),
522+
EventAttribute.MESSAGE.concat(website.toString())
523+
);
524+
eventSourcingEngine.publishMessage(eventPayloadStart);
525+
407526
WebsiteConfig websiteConfig = gitWebsiteConfigService.cloneRepo(website, updateGitIfExists);
408527
website.setConfig(websiteConfig);
409-
return initNewWebsite(website, redeploy);
528+
return initNewWebsite(website, redeploy,traceId);
410529
}
411530

412531
public void updateAndRegisterWebsite(Website website, boolean updateGitIfExists) throws GitAPIException, IOException {

service/src/main/java/io/spaship/operator/controller/WebsiteController.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import javax.inject.Inject;
2929
import java.text.DateFormat;
3030
import java.text.SimpleDateFormat;
31+
import java.time.LocalDateTime;
3132
import java.util.*;
3233
import java.util.concurrent.TimeUnit;
3334

@@ -234,24 +235,29 @@ public static boolean websiteSpecGitChanged(WebsiteSpec oldSpec, WebsiteSpec new
234235
}
235236

236237
public void websiteDeleted(Website websiteToDelete) {
238+
239+
String traceId = UUID.randomUUID().toString();
240+
//IMPLEMENTATION OF ISSUE 59 Start
241+
String eventPayload = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(websiteToDelete.getMetadata().getName()),
242+
EventAttribute.NAMESPACE.concat(websiteToDelete.getMetadata().getNamespace()),
243+
EventAttribute.CODE.concat(EventAttribute.EventCode.WEBSITE_DELETE_INIT.name()),
244+
EventAttribute.TRACE_ID.concat(traceId),
245+
EventAttribute.TIMESTAMP.concat(LocalDateTime.now().toString()),
246+
EventAttribute.MESSAGE.concat(websiteToDelete.toString())
247+
);
248+
eventSourcingEngine.publishMessage(eventPayload);
249+
//IMPLEMENTATION OF ISSUE 59 End
250+
237251
log.infof("Website deleted, websiteId=%s", websiteToDelete.getId());
238252
try {
239253
Website website = websiteRepository.getWebsite(websiteToDelete.getId());
240254
if (website != null) {
241255
gitWebsiteConfigService.deleteRepo(websiteToDelete);
242-
operatorService.deleteInfrastructure(website);
256+
operatorService.deleteInfrastructure(website,traceId);
243257
websiteRepository.removeWebsite(website.getId());
244258
}
245259
removeLock(websiteToDelete.getMetadata().getNamespace(), websiteToDelete.getMetadata().getName());
246260

247-
//IMPLEMENTATION OF ISSUE 59 Start
248-
String eventPayload = Utils.buildEventPayload(EventAttribute.CR_NAME.concat(website.getMetadata().getName()),
249-
EventAttribute.NAMESPACE.concat(website.getMetadata().getNamespace()),
250-
EventAttribute.CODE.concat(EventAttribute.EventCode.WEBSITE_DELETE.name()),
251-
EventAttribute.MESSAGE.concat("website removed")
252-
);
253-
eventSourcingEngine.publishMessage(eventPayload);
254-
//IMPLEMENTATION OF ISSUE 59 End
255261

256262
} catch (Exception e) {
257263
log.error("Error on CRD deleted", e);

service/src/main/java/io/spaship/operator/utility/EventAttribute.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,20 @@ public interface EventAttribute {
1010
String TRACE_ID="traceId~";
1111
String TIMESTAMP="timestamp~";
1212
String ERROR="error~";
13-
String ENVIRONMENT="refresh-env~";
13+
String ENVIRONMENT="target-env~";
1414
enum EventCode{
1515
WEBSITE_CREATE_INIT,
1616
WEBSITE_CREATE_OR_UPDATE_INIT,
1717
WEBSITE_REFRESH_COMPONENT_INIT,
1818
WEBSITE_REFRESH_COMPONENT,
1919
WEBSITE_REFRESH_COMPONENT_FAILED,
20+
WEBSITE_INIT_FAILED,
2021
WEBSITE_CREATE,
2122
WEBSITE_UPDATE,
22-
WEBSITE_DELETE,
23+
WEBSITE_DELETE_INIT,
24+
WEBSITE_DELETED,
2325
PREVIEW_CREATE,
2426
PREVIEW_UPDATE,
25-
PREVIEW_DELETE,
2627
RELEASE_DEPLOY,
2728
RELEASE_DELETE;
2829
}

0 commit comments

Comments
 (0)