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.k8snetworking.impl; |
| 17 | |
| 18 | import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; |
| 19 | import org.json.JSONException; |
| 20 | import org.json.JSONObject; |
| 21 | import org.onosproject.cluster.ClusterService; |
| 22 | import org.onosproject.cluster.LeadershipService; |
| 23 | import org.onosproject.cluster.NodeId; |
| 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.CoreService; |
| 26 | import org.onosproject.k8snetworking.api.K8sNetwork; |
| 27 | import org.onosproject.k8snetworking.api.K8sNetworkService; |
| 28 | import org.onosproject.k8snode.api.K8sNode; |
| 29 | import org.onosproject.k8snode.api.K8sNodeEvent; |
| 30 | import org.onosproject.k8snode.api.K8sNodeListener; |
| 31 | import org.onosproject.k8snode.api.K8sNodeService; |
| 32 | import org.osgi.service.component.annotations.Activate; |
| 33 | import org.osgi.service.component.annotations.Component; |
| 34 | import org.osgi.service.component.annotations.Deactivate; |
| 35 | import org.osgi.service.component.annotations.Reference; |
| 36 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 37 | import org.slf4j.Logger; |
| 38 | |
| 39 | import javax.ws.rs.client.Client; |
| 40 | import javax.ws.rs.client.ClientBuilder; |
| 41 | import javax.ws.rs.client.Entity; |
| 42 | import javax.ws.rs.client.WebTarget; |
| 43 | import javax.ws.rs.core.MediaType; |
| 44 | import javax.ws.rs.core.Response; |
| 45 | import java.util.Objects; |
| 46 | import java.util.concurrent.ExecutorService; |
| 47 | |
| 48 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 49 | import static org.onlab.util.Tools.groupedThreads; |
| 50 | import static org.onosproject.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID; |
| 51 | import static org.onosproject.k8snetworking.impl.OsgiPropertyConstants.SERVICE_IP_CIDR_DEFAULT; |
| 52 | import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.getBclassIpPrefixFromCidr; |
| 53 | import static org.onosproject.k8snode.api.K8sApiConfig.Mode.PASSTHROUGH; |
| 54 | import static org.slf4j.LoggerFactory.getLogger; |
| 55 | |
| 56 | /** |
| 57 | * Provides kubernetes and openstack integration feature. |
| 58 | */ |
| 59 | @Component(immediate = true) |
| 60 | public class K8sOpenstackIntegrationHandler { |
| 61 | |
| 62 | private final Logger log = getLogger(getClass()); |
| 63 | |
| 64 | private static final String K8S_NODE_IP = "k8sNodeIp"; |
| 65 | private static final String OS_K8S_INT_PORT_NAME = "osK8sIntPortName"; |
Jian Li | 6d2ffbf | 2020-11-04 15:58:18 +0900 | [diff] [blame] | 66 | private static final String OS_K8S_EXT_PORT_NAME = "osK8sExtPortName"; |
Jian Li | 4b5048a | 2020-10-08 02:57:45 +0900 | [diff] [blame] | 67 | private static final String POD_CIDR = "podCidr"; |
| 68 | private static final String SERVICE_CIDR = "serviceCidr"; |
| 69 | private static final String POD_GW_IP = "podGwIp"; |
| 70 | private static final String K8S_INT_OS_PORT_MAC = "k8sIntOsPortMac"; |
| 71 | private static final String ONOS_PORT = "8181"; |
| 72 | private static final String OS_K8S_INTEGRATION_EP = "onos/openstacknetworking/integration/"; |
| 73 | private static final String ONOS_USERNAME = "karaf"; |
| 74 | private static final String ONOS_PASSWORD = "karaf"; |
| 75 | private static final String B_CLASS_SUFFIX = ".0.0/16"; |
| 76 | |
| 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 78 | protected CoreService coreService; |
| 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 81 | protected ClusterService clusterService; |
| 82 | |
| 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 84 | protected LeadershipService leadershipService; |
| 85 | |
| 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 87 | protected K8sNodeService k8sNodeService; |
| 88 | |
| 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 90 | protected K8sNetworkService k8sNetworkService; |
| 91 | |
| 92 | private final InternalK8sNodeListener k8sNodeListener = |
| 93 | new InternalK8sNodeListener(); |
| 94 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 95 | groupedThreads(this.getClass().getSimpleName(), "event-handler", log)); |
| 96 | |
| 97 | private ApplicationId appId; |
| 98 | private NodeId localNodeId; |
| 99 | |
| 100 | @Activate |
| 101 | protected void activate() { |
| 102 | appId = coreService.registerApplication(K8S_NETWORKING_APP_ID); |
| 103 | localNodeId = clusterService.getLocalNode().id(); |
| 104 | k8sNodeService.addListener(k8sNodeListener); |
| 105 | |
| 106 | |
| 107 | log.info("Started"); |
| 108 | } |
| 109 | |
| 110 | @Deactivate |
| 111 | protected void deactivate() { |
| 112 | k8sNodeService.removeListener(k8sNodeListener); |
| 113 | |
| 114 | log.info("Stopped"); |
| 115 | } |
| 116 | |
| 117 | private void setCniPtNodeRules(K8sNode k8sNode, boolean install) { |
| 118 | K8sNetwork network = k8sNetworkService.network(k8sNode.hostname()); |
| 119 | String k8sNodeIp = k8sNode.nodeIp().toString(); |
| 120 | String gatewayIp = network.gatewayIp().toString(); |
| 121 | String nodePodCidr = network.cidr(); |
| 122 | String srcPodPrefix = getBclassIpPrefixFromCidr(nodePodCidr); |
| 123 | String podCidr = srcPodPrefix + B_CLASS_SUFFIX; |
| 124 | String osK8sIntPortName = k8sNode.osToK8sIntgPatchPortName(); |
Jian Li | 6d2ffbf | 2020-11-04 15:58:18 +0900 | [diff] [blame] | 125 | String k8sIntOsPortMac = k8sNode.portMacByName(k8sNode.intgBridge(), |
| 126 | k8sNode.k8sIntgToOsPatchPortName()).toString(); |
Jian Li | 4b5048a | 2020-10-08 02:57:45 +0900 | [diff] [blame] | 127 | |
| 128 | String path = install ? "node/pt-install" : "node/pt-uninstall"; |
| 129 | |
| 130 | String jsonString = ""; |
| 131 | |
| 132 | try { |
| 133 | jsonString = new JSONObject() |
| 134 | .put(K8S_NODE_IP, k8sNodeIp) |
| 135 | .put(POD_GW_IP, gatewayIp) |
| 136 | .put(POD_CIDR, podCidr) |
| 137 | .put(SERVICE_CIDR, SERVICE_IP_CIDR_DEFAULT) |
| 138 | .put(OS_K8S_INT_PORT_NAME, osK8sIntPortName) |
| 139 | .put(K8S_INT_OS_PORT_MAC, k8sIntOsPortMac) |
| 140 | .toString(); |
| 141 | log.info("push integration configuration {}", jsonString); |
| 142 | } catch (JSONException e) { |
| 143 | log.error("Failed to generate JSON string"); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | HttpAuthenticationFeature feature = |
| 148 | HttpAuthenticationFeature.basic(ONOS_USERNAME, ONOS_PASSWORD); |
| 149 | |
| 150 | final Client client = ClientBuilder.newClient(); |
| 151 | client.register(feature); |
| 152 | String host = "http://" + k8sNode.managementIp().toString() + ":" + ONOS_PORT + "/"; |
| 153 | String endpoint = host + OS_K8S_INTEGRATION_EP; |
| 154 | WebTarget wt = client.target(endpoint).path(path); |
| 155 | Response response = wt.request(MediaType.APPLICATION_JSON_TYPE) |
| 156 | .put(Entity.json(jsonString)); |
| 157 | final int status = response.getStatus(); |
| 158 | |
| 159 | if (status != 200) { |
| 160 | log.error("Failed to install/uninstall openstack k8s CNI PT rules."); |
| 161 | } |
| 162 | } |
| 163 | |
Jian Li | 6d2ffbf | 2020-11-04 15:58:18 +0900 | [diff] [blame] | 164 | private void setCniPtNodePortRules(K8sNode k8sNode, boolean install) { |
| 165 | String k8sNodeIp = k8sNode.nodeIp().toString(); |
| 166 | String osK8sExtPortName = k8sNode.osToK8sExtPatchPortName(); |
| 167 | |
| 168 | String path = install ? "nodeport/pt-install" : "nodeport/pt-uninstall"; |
| 169 | |
| 170 | String jsonString = ""; |
| 171 | |
| 172 | try { |
| 173 | jsonString = new JSONObject() |
| 174 | .put(K8S_NODE_IP, k8sNodeIp) |
| 175 | .put(SERVICE_CIDR, SERVICE_IP_CIDR_DEFAULT) |
| 176 | .put(OS_K8S_EXT_PORT_NAME, osK8sExtPortName) |
| 177 | .toString(); |
| 178 | log.info("push integration configuration {}", jsonString); |
| 179 | } catch (JSONException e) { |
| 180 | log.error("Failed to generate JSON string"); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | HttpAuthenticationFeature feature = |
| 185 | HttpAuthenticationFeature.basic(ONOS_USERNAME, ONOS_PASSWORD); |
| 186 | |
| 187 | final Client client = ClientBuilder.newClient(); |
| 188 | client.register(feature); |
| 189 | String host = "http://" + k8sNode.managementIp().toString() + ":" + ONOS_PORT + "/"; |
| 190 | String endpoint = host + OS_K8S_INTEGRATION_EP; |
| 191 | WebTarget wt = client.target(endpoint).path(path); |
| 192 | Response response = wt.request(MediaType.APPLICATION_JSON_TYPE) |
| 193 | .put(Entity.json(jsonString)); |
| 194 | final int status = response.getStatus(); |
| 195 | |
| 196 | if (status != 200) { |
| 197 | log.error("Failed to install/uninstall openstack k8s CNI PT node port rules."); |
| 198 | } |
| 199 | } |
| 200 | |
Jian Li | 4b5048a | 2020-10-08 02:57:45 +0900 | [diff] [blame] | 201 | private class InternalK8sNodeListener implements K8sNodeListener { |
| 202 | |
| 203 | @Override |
| 204 | public boolean isRelevant(K8sNodeEvent event) { |
| 205 | return event.subject().mode() == PASSTHROUGH; |
| 206 | } |
| 207 | |
| 208 | private boolean isRelevantHelper() { |
| 209 | return Objects.equals(localNodeId, leadershipService.getLeader(appId.name())); |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public void event(K8sNodeEvent event) { |
| 214 | switch (event.type()) { |
| 215 | case K8S_NODE_COMPLETE: |
| 216 | eventExecutor.execute(() -> processNodeCompletion(event.subject())); |
| 217 | break; |
Jian Li | dc1df64 | 2020-11-25 16:49:34 +0900 | [diff] [blame^] | 218 | case K8S_NODE_OFF_BOARDED: |
| 219 | eventExecutor.execute(() -> processNodeOffboard(event.subject())); |
Jian Li | 4b5048a | 2020-10-08 02:57:45 +0900 | [diff] [blame] | 220 | break; |
| 221 | default: |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | private void processNodeCompletion(K8sNode k8sNode) { |
| 227 | if (!isRelevantHelper()) { |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | setCniPtNodeRules(k8sNode, true); |
Jian Li | 6d2ffbf | 2020-11-04 15:58:18 +0900 | [diff] [blame] | 232 | setCniPtNodePortRules(k8sNode, true); |
Jian Li | 4b5048a | 2020-10-08 02:57:45 +0900 | [diff] [blame] | 233 | } |
| 234 | |
Jian Li | dc1df64 | 2020-11-25 16:49:34 +0900 | [diff] [blame^] | 235 | private void processNodeOffboard(K8sNode k8sNode) { |
Jian Li | 4b5048a | 2020-10-08 02:57:45 +0900 | [diff] [blame] | 236 | if (!isRelevantHelper()) { |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | setCniPtNodeRules(k8sNode, false); |
Jian Li | 6d2ffbf | 2020-11-04 15:58:18 +0900 | [diff] [blame] | 241 | setCniPtNodePortRules(k8sNode, false); |
Jian Li | 4b5048a | 2020-10-08 02:57:45 +0900 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | } |