Skip to content

Commit

Permalink
fix(cloudfoundry): skip API calls for apps that won't be cached (back…
Browse files Browse the repository at this point in the history
…port #5577) (#5590)

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: Matt <6519811+mattgogerly@users.noreply.github.com>
Co-authored-by: Cameron Motevasselani <cmotevasselani@gmail.com>
  • Loading branch information
3 people authored Dec 22, 2021
1 parent 0887714 commit 8f983a9
Showing 2 changed files with 43 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -140,8 +140,13 @@ public List<CloudFoundryApplication> all(List<String> spaceGuids) {
newCloudFoundryAppList.size(),
this.account);

List<Application> cacheableApplications =
newCloudFoundryAppList.stream()
.filter(this::shouldCacheApplication)
.collect(Collectors.toUnmodifiableList());

List<String> availableAppIds =
newCloudFoundryAppList.stream().map(Application::getGuid).collect(toList());
cacheableApplications.stream().map(Application::getGuid).collect(toList());

long invalidatedServerGroups =
serverGroupCache.asMap().keySet().parallelStream()
@@ -161,7 +166,7 @@ public List<CloudFoundryApplication> all(List<String> spaceGuids) {
forkJoinPool
.submit(
() ->
newCloudFoundryAppList.parallelStream()
cacheableApplications.parallelStream()
.filter(
app -> {
CloudFoundryServerGroup cachedApp = findById(app.getGuid());
@@ -196,7 +201,7 @@ public List<CloudFoundryApplication> all(List<String> spaceGuids) {
() ->
// execute health check on instances, set number of available instances and health
// status
newCloudFoundryAppList.parallelStream()
cacheableApplications.parallelStream()
.forEach(
a ->
serverGroupCache.put(
@@ -212,24 +217,10 @@ public List<CloudFoundryApplication> all(List<String> spaceGuids) {
for (CloudFoundryServerGroup serverGroup : serverGroupCache.asMap().values()) {
Names names = Names.parseName(serverGroup.getName());

if (onlySpinnakerManaged && names.getSequence() == null) {
log.debug(
"Skipping app '{}' from foundation '{}' because onlySpinnakerManaged is true and it has no version.",
serverGroup.getName(),
this.account);
continue;
}

if (names.getCluster() == null) {
log.debug(
"Skipping app '{}' from foundation '{}' because the name isn't following the frigga naming schema.",
serverGroup.getName(),
this.account);
continue;
}
serverGroupsByClusters
.computeIfAbsent(names.getCluster(), clusterName -> new HashSet<>())
.add(serverGroup);

clustersByApps
.computeIfAbsent(names.getApp(), appName -> new HashSet<>())
.add(names.getCluster());
@@ -299,6 +290,28 @@ public String findServerGroupId(String name, String spaceId) {
.orElse(null));
}

private boolean shouldCacheApplication(Application application) {
Names names = Names.parseName(application.getName());

if (names.getCluster() == null) {
log.debug(
"Skipping app '{}' from foundation '{}' because the name isn't following the frigga naming schema.",
application.getName(),
this.account);
return false;
}

if (onlySpinnakerManaged && names.getSequence() == null) {
log.debug(
"Skipping app '{}' from foundation '{}' because onlySpinnakerManaged is true and it has no version.",
application.getName(),
this.account);
return false;
}

return true;
}

private Optional<CloudFoundryServerGroup> map(Application application) {
CloudFoundryServerGroup.State state =
CloudFoundryServerGroup.State.valueOf(application.getState());
Original file line number Diff line number Diff line change
@@ -197,11 +197,12 @@ void findByIdIfInputsAreValid() {

@Test
void allDoesNotSkipVersionedAppWhenOnlySpinnakerManagedTrue() {
String guid = "guid";
Application application =
new Application()
.setCreatedAt(ZonedDateTime.now())
.setUpdatedAt(ZonedDateTime.now())
.setGuid("guid")
.setGuid(guid)
.setName("my-app-v000")
.setState("STARTED")
.setLinks(
@@ -226,11 +227,12 @@ void allDoesNotSkipVersionedAppWhenOnlySpinnakerManagedTrue() {

@Test
void allSkipsUnversionedAppWhenOnlySpinnakerManagedTrue() {
String guid = "guid";
Application application =
new Application()
.setCreatedAt(ZonedDateTime.now())
.setUpdatedAt(ZonedDateTime.now())
.setGuid("guid")
.setGuid(guid)
.setName("my-app")
.setState("STARTED")
.setLinks(
@@ -245,12 +247,19 @@ void allSkipsUnversionedAppWhenOnlySpinnakerManagedTrue() {
when(applicationService.all(any(), any(), any(), any()))
.thenReturn(Calls.response(Response.success(applicationPagination)));
when(applicationService.findById(anyString())).thenReturn(Calls.response(application));
mockMap(cloudFoundrySpace, "droplet-guid");

List<CloudFoundryApplication> result = apps.all(List.of(spaceId));
assertThat(result.size()).isEqualTo(0);

verify(applicationService).all(null, resultsPerPage, null, spaceId);

// these methods should never be called if the app is skipped
verify(applicationService, never()).findApplicationEnvById(guid);
verify(spaces, never()).findById(guid);
verify(processesService, never()).findProcessById(guid);
verify(applicationService, never()).instances(guid);
verify(applicationService, never()).findPackagesByAppId(guid);
verify(applicationService, never()).findDropletByApplicationGuid(guid);
}

@Test

0 comments on commit 8f983a9

Please sign in to comment.