Skip to content

Commit 93d9d22

Browse files
committed
extensions: let NetworkOrchestrator extensions handle VPC gateways and routes
NetworkExtensionElement implements every other VpcProvider method by delegating to the extension script, but four of them throw UnsupportedOperationException: createPrivateGateway, deletePrivateGateway, applyStaticRoutes and applyACLItemsToPrivateGw. The restriction is at the framework level, so it applies to every network extension whatever its backend can do, and an extension-backed VPC cannot reach parity with the VPC virtual router. This wires them up the same way as their neighbours, adding four script commands: create-private-gateway delete-private-gateway apply-static-routes apply-private-gw-acl Private gateway operations resolve the owning VPC and go through the existing VPC-level script path, carrying the gateway's id, uuid, address, netmask, broadcast URI and derived VLAN alongside the usual VPC fields. applyStaticRoutes and applyACLItemsToPrivateGw are full rebuilds that send the complete non-revoked set on every call, matching applyFWRules and applyNetworkACLs rather than the per-object style of add-static-nat; a backend reconciles instead of tracking deltas. An extension that does not implement these verbs is unaffected: the commands are new, and before this change the operations failed with UnsupportedOperationException regardless of the script, so no previously working behaviour changes. addPrivateGatewayToPayload documents gateway_vlan as conditional but emitted it unconditionally whenever a broadcast URI was present. BroadcastDomainType returns null for a URI whose scheme it does not recognise, so a non-VLAN gateway shipped "gateway_vlan": "" to the script. It is now emitted only when it has a value, which is what the javadoc already claimed and what every neighbouring field does. Adds NetworkExtensionElementTest, the first unit test this class has had. 30 cases over the four methods: the payload fields each sends, the VPC and gateway identity the script needs to find the router, the omit-when-blank branch of every optional field, both failure styles (createPrivateGateway and deletePrivateGateway return false, applyStaticRoutes and applyACLItemsToPrivateGw throw ResourceUnavailableException), full serialisation of a static route and of an ACL rule, rule ordering by number and routes by CIDR, a null CIDR through the route comparator, a null gateway handed to each of the three public entry points, and a route whose state is not yet set. The command name constants are asserted against their literals, since they are the wire contract with every deployed script. Adds NetworkExtensionElementPayloadContractTest, 6 more. It asserts the keys the script cannot run without -- a transcription of a reference implementation's require_arg calls, with the source named in each test -- and writes the payloads this class actually produces to target/extension-payloads/. The unit tests assert what this class writes and an extension's own tests assert what it reads, each checked against a fixture written by the same hand, so both would pass if a key name drifted between them. Replaying the real payloads against a real backend is what closes that: the six captured payloads pass 21 assertions against a live OVN northbound database, including that ovn-northd resolves the static routes this class emitted. 381 tests pass in framework/extensions.
1 parent 6a0fcd6 commit 93d9d22

3 files changed

Lines changed: 1109 additions & 7 deletions

File tree

‎framework/extensions/src/main/java/org/apache/cloudstack/framework/extensions/network/NetworkExtensionElement.java‎

Lines changed: 204 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import com.cloud.network.element.VpcProvider;
7777
import com.cloud.network.vpc.NetworkACLItem;
7878
import com.cloud.network.vpc.PrivateGateway;
79+
import com.cloud.network.vpc.StaticRoute;
7980
import com.cloud.network.vpc.StaticRouteProfile;
8081
import com.cloud.network.vpc.Vpc;
8182
import com.cloud.network.lb.LoadBalancingRule;
@@ -246,6 +247,10 @@ public class NetworkExtensionElement extends AdapterBase implements
246247
public static final String CMD_CUSTOM_ACTION = "custom-action";
247248
public static final String CMD_PREPARE_NIC = "prepare-nic";
248249
public static final String CMD_RELEASE_NIC = "release-nic";
250+
public static final String CMD_CREATE_PRIVATE_GATEWAY = "create-private-gateway";
251+
public static final String CMD_DELETE_PRIVATE_GATEWAY = "delete-private-gateway";
252+
public static final String CMD_APPLY_STATIC_ROUTES = "apply-static-routes";
253+
public static final String CMD_APPLY_PRIVATE_GW_ACL = "apply-private-gw-acl";
249254

250255
// ---- Network detail key ----
251256

@@ -2459,31 +2464,223 @@ public boolean shutdownVpc(Vpc vpc, ReservationContext context)
24592464
return result;
24602465
}
24612466

2467+
/**
2468+
* Resolves the {@link Vpc} a private gateway belongs to, so gateway operations can
2469+
* go through the VPC-level script path like every other VPC operation.
2470+
*/
2471+
private Vpc getVpcForPrivateGateway(PrivateGateway gateway, String action) {
2472+
if (gateway == null || gateway.getVpcId() == null) {
2473+
logger.warn("{}: private gateway has no VPC", action);
2474+
return null;
2475+
}
2476+
Vpc vpc = vpcDao.findById(gateway.getVpcId());
2477+
if (vpc == null) {
2478+
logger.warn("{}: could not find VPC {} for private gateway {}", action, gateway.getVpcId(), gateway.getUuid());
2479+
}
2480+
return vpc;
2481+
}
2482+
2483+
/**
2484+
* Creates the private gateway on the external network system via the script's
2485+
* {@code create-private-gateway} command.
2486+
*
2487+
* <p>Script command: {@code create-private-gateway}</p>
2488+
*/
24622489
@Override
24632490
public boolean createPrivateGateway(PrivateGateway gateway)
24642491
throws ConcurrentOperationException, ResourceUnavailableException {
2465-
throw new UnsupportedOperationException("Private gateways are not supported by the network extension element.");
2492+
final Vpc vpc = getVpcForPrivateGateway(gateway, CMD_CREATE_PRIVATE_GATEWAY);
2493+
if (vpc == null) {
2494+
return false;
2495+
}
2496+
2497+
logger.info("Creating private gateway {} for VPC {}", gateway.getUuid(), vpc);
2498+
2499+
final JsonObject payload = new JsonObject();
2500+
addVpcToPayload(payload, vpc);
2501+
addPrivateGatewayToPayload(payload, gateway);
2502+
2503+
return executeVpcScript(vpc, CMD_CREATE_PRIVATE_GATEWAY, payload);
24662504
}
24672505

2468-
/** Private gateways are not supported by the network extension element. */
2506+
/**
2507+
* Removes the private gateway via the script's {@code delete-private-gateway} command.
2508+
*
2509+
* <p>Script command: {@code delete-private-gateway}</p>
2510+
*/
24692511
@Override
24702512
public boolean deletePrivateGateway(PrivateGateway gateway)
24712513
throws ConcurrentOperationException, ResourceUnavailableException {
2472-
throw new UnsupportedOperationException("Private gateways are not supported by the network extension element.");
2514+
final Vpc vpc = getVpcForPrivateGateway(gateway, CMD_DELETE_PRIVATE_GATEWAY);
2515+
if (vpc == null) {
2516+
return false;
2517+
}
2518+
2519+
logger.info("Deleting private gateway {} for VPC {}", gateway.getUuid(), vpc);
2520+
2521+
final JsonObject payload = new JsonObject();
2522+
addVpcToPayload(payload, vpc);
2523+
addPrivateGatewayToPayload(payload, gateway);
2524+
2525+
return executeVpcScript(vpc, CMD_DELETE_PRIVATE_GATEWAY, payload);
24732526
}
24742527

2475-
/** Static routes are not supported by the network extension element. */
2528+
/**
2529+
* Applies the VPC's static routes via the script's {@code apply-static-routes}
2530+
* command.
2531+
*
2532+
* <p>Like {@link #applyFWRules} and {@link #applyNetworkACLs}, this is a full
2533+
* rebuild: the complete set of non-revoked routes is sent on every call, so the
2534+
* script can reconcile rather than track individual additions and removals.</p>
2535+
*
2536+
* <p>Script command: {@code apply-static-routes}</p>
2537+
*/
24762538
@Override
24772539
public boolean applyStaticRoutes(Vpc vpc, List<StaticRouteProfile> routes)
24782540
throws ResourceUnavailableException {
2479-
throw new UnsupportedOperationException("Static routes are not supported by the network extension element.");
2541+
if (vpc == null) {
2542+
logger.warn("applyStaticRoutes: no VPC given");
2543+
return false;
2544+
}
2545+
2546+
final List<StaticRouteProfile> activeRoutes = routes == null ? List.of() :
2547+
routes.stream()
2548+
.filter(r -> !StaticRoute.State.Revoke.equals(r.getState()))
2549+
.collect(Collectors.toList());
2550+
2551+
logger.info("applyStaticRoutes: vpc={} activeRoutes={}", vpc, activeRoutes.size());
2552+
2553+
final JsonObject payload = new JsonObject();
2554+
addVpcToPayload(payload, vpc);
2555+
payload.add("static_routes", buildStaticRoutesArray(activeRoutes));
2556+
2557+
final boolean result = executeVpcScript(vpc, CMD_APPLY_STATIC_ROUTES, payload);
2558+
if (!result) {
2559+
throw new ResourceUnavailableException(
2560+
"Failed to apply static routes for VPC " + vpc.getUuid(),
2561+
Vpc.class, vpc.getId());
2562+
}
2563+
return true;
24802564
}
24812565

2482-
/** ACL items on private gateways are not supported by the network extension element. */
2566+
/**
2567+
* Applies network ACL items to a private gateway via the script's
2568+
* {@code apply-private-gw-acl} command.
2569+
*
2570+
* <p>A full rebuild, matching {@link #applyNetworkACLs}.</p>
2571+
*
2572+
* <p>Script command: {@code apply-private-gw-acl}</p>
2573+
*/
24832574
@Override
24842575
public boolean applyACLItemsToPrivateGw(PrivateGateway gateway, List<? extends NetworkACLItem> rules)
24852576
throws ResourceUnavailableException {
2486-
throw new UnsupportedOperationException("ACL items on private gateways are not supported by the network extension element.");
2577+
final Vpc vpc = getVpcForPrivateGateway(gateway, CMD_APPLY_PRIVATE_GW_ACL);
2578+
if (vpc == null) {
2579+
return false;
2580+
}
2581+
2582+
final List<? extends NetworkACLItem> activeRules = rules == null ? List.of() :
2583+
rules.stream()
2584+
.filter(r -> !NetworkACLItem.State.Revoke.equals(r.getState()))
2585+
.collect(Collectors.toList());
2586+
2587+
logger.info("applyACLItemsToPrivateGw: gateway={} activeRules={}", gateway.getUuid(), activeRules.size());
2588+
2589+
final JsonObject payload = new JsonObject();
2590+
addVpcToPayload(payload, vpc);
2591+
addPrivateGatewayToPayload(payload, gateway);
2592+
payload.add("acl_rules", buildAclRulesArray(activeRules));
2593+
2594+
final boolean result = executeVpcScript(vpc, CMD_APPLY_PRIVATE_GW_ACL, payload);
2595+
if (!result) {
2596+
throw new ResourceUnavailableException(
2597+
"Failed to apply ACL items to private gateway " + gateway.getUuid(),
2598+
Vpc.class, vpc.getId());
2599+
}
2600+
return true;
2601+
}
2602+
2603+
/**
2604+
* Adds the private gateway fields to the payload.
2605+
* Always present: gateway_id, gateway_uuid, gateway_network_id, gateway_zone_id,
2606+
* gateway_physical_network_id, gateway_source_nat, gateway_network_acl_id.
2607+
* Conditional (only when non-blank): gateway_ip_address, gateway_gateway,
2608+
* gateway_netmask, gateway_broadcast_uri, gateway_vlan, gateway_state.
2609+
*/
2610+
private void addPrivateGatewayToPayload(JsonObject payload, PrivateGateway gateway) {
2611+
if (payload == null || gateway == null) {
2612+
return;
2613+
}
2614+
payload.addProperty("gateway_id", String.valueOf(gateway.getId()));
2615+
payload.addProperty("gateway_uuid", safeStr(gateway.getUuid()));
2616+
payload.addProperty("gateway_network_id", String.valueOf(gateway.getNetworkId()));
2617+
payload.addProperty("gateway_zone_id", String.valueOf(gateway.getZoneId()));
2618+
payload.addProperty("gateway_physical_network_id", String.valueOf(gateway.getPhysicalNetworkId()));
2619+
payload.addProperty("gateway_source_nat", String.valueOf(gateway.getSourceNat()));
2620+
payload.addProperty("gateway_network_acl_id", String.valueOf(gateway.getNetworkACLId()));
2621+
if (StringUtils.isNotBlank(gateway.getIp4Address())) {
2622+
payload.addProperty("gateway_ip_address", safeStr(gateway.getIp4Address()));
2623+
}
2624+
if (StringUtils.isNotBlank(gateway.getGateway())) {
2625+
payload.addProperty("gateway_gateway", safeStr(gateway.getGateway()));
2626+
}
2627+
if (StringUtils.isNotBlank(gateway.getNetmask())) {
2628+
payload.addProperty("gateway_netmask", safeStr(gateway.getNetmask()));
2629+
}
2630+
if (StringUtils.isNotBlank(gateway.getBroadcastUri())) {
2631+
payload.addProperty("gateway_broadcast_uri", safeStr(gateway.getBroadcastUri()));
2632+
try {
2633+
String vlan = Networks.BroadcastDomainType.getValue(new URI(gateway.getBroadcastUri()));
2634+
if (StringUtils.isNotBlank(vlan)) {
2635+
payload.addProperty("gateway_vlan", vlan);
2636+
}
2637+
} catch (Exception e) {
2638+
logger.debug("Could not derive a VLAN from broadcast URI {} of private gateway {}",
2639+
gateway.getBroadcastUri(), gateway.getUuid());
2640+
}
2641+
}
2642+
if (gateway.getState() != null) {
2643+
payload.addProperty("gateway_state", gateway.getState().toString().toLowerCase());
2644+
}
2645+
}
2646+
2647+
/**
2648+
* Serialises static routes to a {@link JsonArray} for the
2649+
* {@code apply-static-routes} script command. Routes are sorted by CIDR so the
2650+
* payload is stable between calls that carry the same set.
2651+
*/
2652+
private JsonArray buildStaticRoutesArray(List<StaticRouteProfile> routes) {
2653+
JsonArray array = new JsonArray();
2654+
List<StaticRouteProfile> sorted = routes.stream()
2655+
.sorted(java.util.Comparator.comparing(
2656+
r -> StringUtils.defaultString(r.getCidr())))
2657+
.collect(Collectors.toList());
2658+
for (StaticRouteProfile route : sorted) {
2659+
JsonObject routeObj = new JsonObject();
2660+
routeObj.addProperty("id", route.getId());
2661+
routeObj.addProperty("cidr", safeStr(route.getCidr()));
2662+
routeObj.addProperty("next_hop", safeStr(route.getNextHop()));
2663+
if (route.getState() != null) {
2664+
routeObj.addProperty("state", route.getState().toString().toLowerCase());
2665+
}
2666+
if (route.getVpcGatewayId() != null) {
2667+
routeObj.addProperty("vpc_gateway_id", String.valueOf(route.getVpcGatewayId()));
2668+
}
2669+
if (StringUtils.isNotBlank(route.getIp4Address())) {
2670+
routeObj.addProperty("gateway_ip_address", safeStr(route.getIp4Address()));
2671+
}
2672+
if (StringUtils.isNotBlank(route.getGateway())) {
2673+
routeObj.addProperty("gateway", safeStr(route.getGateway()));
2674+
}
2675+
if (StringUtils.isNotBlank(route.getNetmask())) {
2676+
routeObj.addProperty("netmask", safeStr(route.getNetmask()));
2677+
}
2678+
if (StringUtils.isNotBlank(route.getVlanTag())) {
2679+
routeObj.addProperty("vlan", safeStr(route.getVlanTag()));
2680+
}
2681+
array.add(routeObj);
2682+
}
2683+
return array;
24872684
}
24882685

24892686
@Override

0 commit comments

Comments
 (0)