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