Jian Li | 4b5048a | 2020-10-08 02:57:45 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.openstacknetworking.web; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.packet.IpPrefix; |
| 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onosproject.openstacknetworking.api.OpenstackK8sIntegrationService; |
| 23 | import org.onosproject.rest.AbstractWebResource; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | import javax.ws.rs.Consumes; |
| 28 | import javax.ws.rs.PUT; |
| 29 | import javax.ws.rs.Path; |
| 30 | import javax.ws.rs.Produces; |
| 31 | import javax.ws.rs.core.MediaType; |
| 32 | import javax.ws.rs.core.Response; |
| 33 | import java.io.IOException; |
| 34 | import java.io.InputStream; |
| 35 | |
| 36 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 37 | import static org.onlab.util.Tools.readTreeFromStream; |
| 38 | |
| 39 | /** |
| 40 | * REST interface for integrating openstack and kubernetes. |
| 41 | */ |
| 42 | @Path("integration") |
| 43 | public class OpenstackK8sIntegrationWebResource extends AbstractWebResource { |
| 44 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 45 | |
| 46 | private static final String K8S_NODE_IP = "k8sNodeIp"; |
| 47 | private static final String OS_K8S_INT_PORT_NAME = "osK8sIntPortName"; |
Jian Li | 6d2ffbf | 2020-11-04 15:58:18 +0900 | [diff] [blame^] | 48 | private static final String OS_K8S_EXT_PORT_NAME = "osK8sExtPortName"; |
Jian Li | 4b5048a | 2020-10-08 02:57:45 +0900 | [diff] [blame] | 49 | private static final String POD_CIDR = "podCidr"; |
| 50 | private static final String SERVICE_CIDR = "serviceCidr"; |
| 51 | private static final String POD_GW_IP = "podGwIp"; |
| 52 | private static final String K8S_INT_OS_PORT_MAC = "k8sIntOsPortMac"; |
| 53 | |
| 54 | private final OpenstackK8sIntegrationService intService = |
| 55 | get(OpenstackK8sIntegrationService.class); |
| 56 | |
| 57 | /** |
| 58 | * Installs CNI pass-through related flow rules for each kubernetes nodes. |
| 59 | * |
Jian Li | 6d2ffbf | 2020-11-04 15:58:18 +0900 | [diff] [blame^] | 60 | * @param input JSON string |
Jian Li | 4b5048a | 2020-10-08 02:57:45 +0900 | [diff] [blame] | 61 | * @return 200 ok, 400 BAD_REQUEST if the json is malformed |
| 62 | * @throws IOException exception |
| 63 | */ |
| 64 | @PUT |
| 65 | @Path("node/pt-install") |
| 66 | @Consumes(MediaType.APPLICATION_JSON) |
| 67 | @Produces(MediaType.APPLICATION_JSON) |
| 68 | public Response installCniPtNodeRules(InputStream input) throws IOException { |
| 69 | log.trace("Install K8S CNI pass-through node rules"); |
| 70 | |
| 71 | JsonNode json = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input); |
| 72 | IpAddress k8sNodeIp = IpAddress.valueOf(json.get(K8S_NODE_IP).asText()); |
| 73 | IpPrefix podCidr = IpPrefix.valueOf(json.get(POD_CIDR).asText()); |
| 74 | IpPrefix serviceCidr = IpPrefix.valueOf(json.get(SERVICE_CIDR).asText()); |
| 75 | IpAddress podGwIp = IpAddress.valueOf(json.get(POD_GW_IP).asText()); |
| 76 | String osK8sIntPortName = json.get(OS_K8S_INT_PORT_NAME).asText(); |
| 77 | MacAddress k8sIntOsPortMac = MacAddress.valueOf(json.get(K8S_INT_OS_PORT_MAC).asText()); |
| 78 | |
| 79 | intService.installCniPtNodeRules(k8sNodeIp, podCidr, serviceCidr, |
| 80 | podGwIp, osK8sIntPortName, k8sIntOsPortMac); |
| 81 | |
| 82 | return Response.ok().build(); |
| 83 | } |
| 84 | |
| 85 | /** |
Jian Li | 6d2ffbf | 2020-11-04 15:58:18 +0900 | [diff] [blame^] | 86 | * Uninstalls CNI pass-through related flow rules for each kubernetes nodes. |
Jian Li | 4b5048a | 2020-10-08 02:57:45 +0900 | [diff] [blame] | 87 | * |
Jian Li | 6d2ffbf | 2020-11-04 15:58:18 +0900 | [diff] [blame^] | 88 | * @param input JSON string |
Jian Li | 4b5048a | 2020-10-08 02:57:45 +0900 | [diff] [blame] | 89 | * @return 200 ok, 400 BAD_REQUEST if the json is malformed |
| 90 | * @throws IOException exception |
| 91 | */ |
| 92 | @PUT |
| 93 | @Path("node/pt-uninstall") |
| 94 | @Consumes(MediaType.APPLICATION_JSON) |
| 95 | @Produces(MediaType.APPLICATION_JSON) |
| 96 | public Response uninstallCniPtNodeRules(InputStream input) throws IOException { |
| 97 | log.trace("Uninstall K8S CNI pass-through node rules"); |
| 98 | |
| 99 | JsonNode json = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input); |
| 100 | IpAddress k8sNodeIp = IpAddress.valueOf(json.get(K8S_NODE_IP).asText()); |
| 101 | IpPrefix podCidr = IpPrefix.valueOf(json.get(POD_CIDR).asText()); |
| 102 | IpPrefix serviceCidr = IpPrefix.valueOf(json.get(SERVICE_CIDR).asText()); |
| 103 | IpAddress podGwIp = IpAddress.valueOf(json.get(POD_GW_IP).asText()); |
| 104 | String osK8sIntPortName = json.get(OS_K8S_INT_PORT_NAME).asText(); |
| 105 | MacAddress k8sIntOsPortMac = MacAddress.valueOf(json.get(K8S_INT_OS_PORT_MAC).asText()); |
| 106 | |
| 107 | intService.uninstallCniPtNodeRules(k8sNodeIp, podCidr, serviceCidr, |
| 108 | podGwIp, osK8sIntPortName, k8sIntOsPortMac); |
| 109 | |
| 110 | return Response.ok().build(); |
| 111 | } |
Jian Li | 6d2ffbf | 2020-11-04 15:58:18 +0900 | [diff] [blame^] | 112 | |
| 113 | /** |
| 114 | * Installs CNI pass-through related node port flow rules. |
| 115 | * |
| 116 | * @param input JSON string |
| 117 | * @return 200 ok, 400 BAD_REQUEST if the json is malformed |
| 118 | * @throws IOException exception |
| 119 | */ |
| 120 | @PUT |
| 121 | @Path("nodeport/pt-install") |
| 122 | @Consumes(MediaType.APPLICATION_JSON) |
| 123 | @Produces(MediaType.APPLICATION_JSON) |
| 124 | public Response installCniPtNodePortRules(InputStream input) throws IOException { |
| 125 | log.trace("Install K8S CNI pass-through node port rules"); |
| 126 | |
| 127 | JsonNode json = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input); |
| 128 | IpAddress k8sNodeIp = IpAddress.valueOf(json.get(K8S_NODE_IP).asText()); |
| 129 | String osK8sExtPortName = json.get(OS_K8S_EXT_PORT_NAME).asText(); |
| 130 | |
| 131 | intService.installCniPtNodePortRules(k8sNodeIp, osK8sExtPortName); |
| 132 | |
| 133 | return Response.ok().build(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Uninstalls CNI pass-through related node port flow rules. |
| 138 | * |
| 139 | * @param input JSON string |
| 140 | * @return 200 ok, 400 BAD_REQUEST if the json is malformed |
| 141 | * @throws IOException exception |
| 142 | */ |
| 143 | @PUT |
| 144 | @Path("nodeport/pt-uninstall") |
| 145 | @Consumes(MediaType.APPLICATION_JSON) |
| 146 | @Produces(MediaType.APPLICATION_JSON) |
| 147 | public Response uninstallCniPtNodePortRules(InputStream input) throws IOException { |
| 148 | log.trace("Uninstall K8S CNI pass-through node port rules"); |
| 149 | |
| 150 | JsonNode json = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input); |
| 151 | IpAddress k8sNodeIp = IpAddress.valueOf(json.get(K8S_NODE_IP).asText()); |
| 152 | String osK8sExtPortName = json.get(OS_K8S_EXT_PORT_NAME).asText(); |
| 153 | |
| 154 | intService.uninstallCniPtNodePortRules(k8sNodeIp, osK8sExtPortName); |
| 155 | |
| 156 | return Response.ok().build(); |
| 157 | } |
Jian Li | 4b5048a | 2020-10-08 02:57:45 +0900 | [diff] [blame] | 158 | } |