Skip to content

Pr 1194 #1217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 14, 2024
Merged

Pr 1194 #1217

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
/*
* Copyright 2013-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package org.cloudfoundry.operations.routes;

import static org.cloudfoundry.util.tuple.TupleUtils.function;
import static org.cloudfoundry.util.tuple.TupleUtils.predicate;

import java.time.Duration;
import java.util.Collections;
Expand All @@ -38,10 +35,8 @@
import org.cloudfoundry.client.v2.privatedomains.PrivateDomainResource;
import org.cloudfoundry.client.v2.routes.AbstractRouteResource;
import org.cloudfoundry.client.v2.routes.CreateRouteResponse;
import org.cloudfoundry.client.v2.routes.DeleteRouteResponse;
import org.cloudfoundry.client.v2.routes.ListRouteApplicationsRequest;
import org.cloudfoundry.client.v2.routes.RouteEntity;
import org.cloudfoundry.client.v2.routes.RouteExistsRequest;
import org.cloudfoundry.client.v2.routes.RouteResource;
import org.cloudfoundry.client.v2.serviceinstances.UnionServiceInstanceResource;
import org.cloudfoundry.client.v2.shareddomains.ListSharedDomainsRequest;
Expand All @@ -50,6 +45,10 @@
import org.cloudfoundry.client.v2.spaces.ListSpaceRoutesRequest;
import org.cloudfoundry.client.v2.spaces.ListSpaceServiceInstancesRequest;
import org.cloudfoundry.client.v2.spaces.SpaceResource;
import org.cloudfoundry.client.v3.domains.CheckReservedRoutesRequest;
import org.cloudfoundry.client.v3.domains.DomainResource;
import org.cloudfoundry.client.v3.organizations.ListOrganizationDomainsRequest;
import org.cloudfoundry.client.v3.spaces.DeleteUnmappedRoutesRequest;
import org.cloudfoundry.operations.util.OperationsLogging;
import org.cloudfoundry.util.ExceptionUtils;
import org.cloudfoundry.util.JobUtils;
Expand Down Expand Up @@ -81,18 +80,18 @@ public Mono<Boolean> check(CheckRouteRequest request) {
return Mono.zip(this.cloudFoundryClient, this.organizationId)
.flatMap(
function(
(cloudFoundryClient, organizationId) ->
(client, organizationId) ->
Mono.zip(
Mono.just(cloudFoundryClient),
getOptionalDomainId(
cloudFoundryClient,
this.cloudFoundryClient,
getOptionalDomainIdByName(
client,
organizationId,
request.getDomain()))))
.flatMap(
function(
(cloudFoundryClient, domainId) ->
requestRouteExists(
cloudFoundryClient,
(client, domainId) ->
routeExists(
client,
domainId,
request.getHost(),
request.getPath())))
Expand All @@ -101,6 +100,42 @@ public Mono<Boolean> check(CheckRouteRequest request) {
.checkpoint();
}

private static Mono<Boolean> routeExists(
CloudFoundryClient cloudFoundryClient, String domainId, String host, String path) {
return cloudFoundryClient
.domainsV3()
.checkReservedRoutes(
CheckReservedRoutesRequest.builder()
.domainId(domainId)
.host(host)
.path(path)
.build())
.flatMap(response -> Mono.just(response.getMatchingRoute()));
}

private static Mono<String> getOptionalDomainIdByName(
CloudFoundryClient cloudFoundryClient, String organizationId, String domain) {
return listDomains(cloudFoundryClient, organizationId, new String[] {domain})
.singleOrEmpty()
.map(resource -> resource.getId());
}

private static Flux<DomainResource> listDomains(
CloudFoundryClient cloudFoundryClient,
String organizationId,
String[] domainNamesFilter) {
return PaginationUtils.requestClientV3Resources(
page ->
cloudFoundryClient
.organizationsV3()
.listDomains(
ListOrganizationDomainsRequest.builder()
.names(domainNamesFilter)
.page(page)
.organizationId(organizationId)
.build()));
}

@Override
public Mono<Integer> create(CreateRouteRequest request) {
return Mono.zip(this.cloudFoundryClient, this.organizationId)
Expand Down Expand Up @@ -170,43 +205,22 @@ public Mono<Void> delete(DeleteRouteRequest request) {
@Override
public Mono<Void> deleteOrphanedRoutes(DeleteOrphanedRoutesRequest request) {
return Mono.zip(this.cloudFoundryClient, this.spaceId)
.flatMapMany(
function(
(cloudFoundryClient, spaceId) ->
requestSpaceRoutes(cloudFoundryClient, spaceId)
.filter(
route ->
isRouteOrphan(
ResourceUtils.getEntity(
route)))
.map(ResourceUtils::getId)
.map(
routeId ->
Tuples.of(
cloudFoundryClient,
routeId))))
.flatMap(
function(
(cloudFoundryClient, routeId) ->
getApplications(cloudFoundryClient, routeId)
.map(
applicationResources ->
Tuples.of(
cloudFoundryClient,
applicationResources,
routeId))))
.filter(
predicate(
(cloudFoundryClient, applicationResources, routeId) ->
isApplicationOrphan(applicationResources)))
(client, spaceId) ->
Mono.zip(
this.cloudFoundryClient,
client.spacesV3()
.deleteUnmappedRoutes(
DeleteUnmappedRoutesRequest
.builder()
.spaceId(spaceId)
.build()))))
.flatMap(
function(
(cloudFoundryClient, applicationResources, routeId) ->
deleteRoute(
cloudFoundryClient,
request.getCompletionTimeout(),
routeId)))
.then()
(client, job) ->
JobUtils.waitForCompletion(
client, request.getCompletionTimeout(), job)))
.transform(OperationsLogging.log("Delete Orphaned Routes"))
.checkpoint();
}
Expand Down Expand Up @@ -324,15 +338,6 @@ public Mono<Void> unmap(UnmapRouteRequest request) {
.checkpoint();
}

private static Mono<Void> deleteRoute(
CloudFoundryClient cloudFoundryClient, Duration completionTimeout, String routeId) {
return requestDeleteRoute(cloudFoundryClient, routeId)
.flatMap(
job ->
JobUtils.waitForCompletion(
cloudFoundryClient, completionTimeout, job));
}

private static Mono<Map<String, String>> getAllDomains(
CloudFoundryClient cloudFoundryClient, String organizationId) {
return requestAllPrivateDomains(cloudFoundryClient, organizationId)
Expand Down Expand Up @@ -386,11 +391,6 @@ private static Mono<List<String>> getApplicationNames(
.collectList();
}

private static Mono<List<ApplicationResource>> getApplications(
CloudFoundryClient cloudFoundryClient, String routeId) {
return requestApplications(cloudFoundryClient, routeId).collectList();
}

private static Mono<Resource<?>> getDomain(
CloudFoundryClient cloudFoundryClient, String organizationId, String domain) {
return getDomains(cloudFoundryClient, organizationId, domain)
Expand All @@ -416,13 +416,6 @@ private static Flux<Resource<?>> getDomains(
.switchIfEmpty(requestSharedDomains(cloudFoundryClient, domain));
}

private static Mono<String> getOptionalDomainId(
CloudFoundryClient cloudFoundryClient, String organizationId, String domain) {
return getDomains(cloudFoundryClient, organizationId, domain)
.singleOrEmpty()
.map(ResourceUtils::getId);
}

private static Mono<AbstractRouteResource> getOrCreateRoute(
CloudFoundryClient cloudFoundryClient,
String organizationId,
Expand Down Expand Up @@ -562,10 +555,6 @@ private static Mono<String> getSpaceName(Map<String, String> spaces, String spac
return Mono.just(spaces.get(spaceId));
}

private static boolean isApplicationOrphan(List<ApplicationResource> applications) {
return applications.isEmpty();
}

private static boolean isIdentical(String s, String t) {
return s == null ? t == null : s.equals(t);
}
Expand Down Expand Up @@ -673,13 +662,21 @@ private static Mono<CreateRouteResponse> requestCreateRoute(
.create(builder.domainId(domainId).spaceId(spaceId).build());
}

private static Mono<DeleteRouteResponse> requestDeleteRoute(
private static Mono<Void> deleteRoute(
CloudFoundryClient cloudFoundryClient, Duration completionTimeout, String routeId) {
return requestDeleteRoute(cloudFoundryClient, routeId)
.flatMap(
job ->
JobUtils.waitForCompletion(
cloudFoundryClient, completionTimeout, job));
}

private static Mono<String> requestDeleteRoute(
CloudFoundryClient cloudFoundryClient, String routeId) {
return cloudFoundryClient
.routes()
.routesV3()
.delete(
org.cloudfoundry.client.v2.routes.DeleteRouteRequest.builder()
.async(true)
org.cloudfoundry.client.v3.routes.DeleteRouteRequest.builder()
.routeId(routeId)
.build());
}
Expand Down Expand Up @@ -723,18 +720,6 @@ private static Mono<Void> requestRemoveRouteFromApplication(
.build());
}

private static Mono<Boolean> requestRouteExists(
CloudFoundryClient cloudFoundryClient, String domainId, String host, String path) {
return cloudFoundryClient
.routes()
.exists(
RouteExistsRequest.builder()
.domainId(domainId)
.host(host)
.path(path)
.build());
}

private static Flux<RouteResource> requestRoutes(
CloudFoundryClient cloudFoundryClient,
UnaryOperator<org.cloudfoundry.client.v2.routes.ListRoutesRequest.Builder> modifier) {
Expand Down Expand Up @@ -827,8 +812,4 @@ private static Route toRoute(

return builder.build();
}

private boolean isRouteOrphan(RouteEntity entity) {
return entity.getServiceInstanceId() == null || entity.getServiceInstanceId().isEmpty();
}
}
Loading