Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-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.kubevirtnetworking.impl; |
| 17 | |
| 18 | import io.fabric8.kubernetes.api.model.Pod; |
| 19 | import io.fabric8.kubernetes.api.model.PodBuilder; |
| 20 | import io.fabric8.kubernetes.client.KubernetesClient; |
| 21 | import org.json.JSONArray; |
| 22 | import org.json.JSONObject; |
| 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onosproject.cluster.ClusterService; |
| 25 | import org.onosproject.cluster.LeadershipService; |
| 26 | import org.onosproject.cluster.NodeId; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreService; |
| 29 | import org.onosproject.kubevirtnetworking.api.KubevirtNetwork; |
| 30 | import org.onosproject.kubevirtnetworking.api.KubevirtNetworkAdminService; |
| 31 | import org.onosproject.kubevirtnetworking.api.KubevirtPodAdminService; |
| 32 | import org.onosproject.kubevirtnetworking.api.KubevirtPodEvent; |
| 33 | import org.onosproject.kubevirtnetworking.api.KubevirtPodListener; |
| 34 | import org.onosproject.kubevirtnetworking.api.KubevirtPort; |
| 35 | import org.onosproject.kubevirtnetworking.api.KubevirtPortAdminService; |
| 36 | import org.onosproject.kubevirtnode.api.KubevirtApiConfigService; |
| 37 | import org.onosproject.mastership.MastershipService; |
| 38 | import org.onosproject.net.device.DeviceService; |
| 39 | import org.onosproject.net.driver.DriverService; |
| 40 | import org.osgi.service.component.annotations.Activate; |
| 41 | import org.osgi.service.component.annotations.Component; |
| 42 | import org.osgi.service.component.annotations.Deactivate; |
| 43 | import org.osgi.service.component.annotations.Reference; |
| 44 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 45 | import org.slf4j.Logger; |
| 46 | |
| 47 | import java.util.Map; |
| 48 | import java.util.Objects; |
| 49 | import java.util.concurrent.ExecutorService; |
| 50 | |
| 51 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 52 | import static org.onlab.util.Tools.groupedThreads; |
| 53 | import static org.onosproject.kubevirtnetworking.api.Constants.KUBEVIRT_NETWORKING_APP_ID; |
| 54 | import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.getPort; |
| 55 | import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.k8sClient; |
| 56 | import static org.slf4j.LoggerFactory.getLogger; |
| 57 | |
| 58 | /** |
| 59 | * Associates the kubevirt container port and pod. |
| 60 | */ |
| 61 | @Component(immediate = true) |
| 62 | public class KubevirtPodPortMapper { |
| 63 | |
| 64 | private final Logger log = getLogger(getClass()); |
| 65 | |
| 66 | private static final String NETWORK_STATUS_KEY = "k8s.v1.cni.cncf.io/network-status"; |
| 67 | private static final String NAME = "name"; |
| 68 | private static final String IPS = "ips"; |
| 69 | private static final String NETWORK_PREFIX = "default/"; |
| 70 | |
| 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 72 | protected CoreService coreService; |
| 73 | |
| 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 75 | protected MastershipService mastershipService; |
| 76 | |
| 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 78 | protected ClusterService clusterService; |
| 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 81 | protected LeadershipService leadershipService; |
| 82 | |
| 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 84 | protected DeviceService deviceService; |
| 85 | |
| 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 87 | protected DriverService driverService; |
| 88 | |
| 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 90 | protected KubevirtPortAdminService kubevirtPortAdminService; |
| 91 | |
| 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 93 | protected KubevirtNetworkAdminService kubevirtNetworkAdminService; |
| 94 | |
| 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 96 | protected KubevirtPodAdminService kubevirtPodAdminService; |
| 97 | |
| 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 99 | protected KubevirtApiConfigService kubevirtApiConfigService; |
| 100 | |
| 101 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 102 | groupedThreads(this.getClass().getSimpleName(), "event-handler")); |
| 103 | |
| 104 | private final InternalKubevirtPodListener kubevirtPodListener = |
| 105 | new InternalKubevirtPodListener(); |
| 106 | |
| 107 | private ApplicationId appId; |
| 108 | private NodeId localNodeId; |
| 109 | |
| 110 | @Activate |
| 111 | protected void activate() { |
| 112 | appId = coreService.registerApplication(KUBEVIRT_NETWORKING_APP_ID); |
| 113 | localNodeId = clusterService.getLocalNode().id(); |
| 114 | leadershipService.runForLeadership(appId.name()); |
| 115 | kubevirtPodAdminService.addListener(kubevirtPodListener); |
| 116 | |
| 117 | log.info("Started"); |
| 118 | } |
| 119 | |
| 120 | @Deactivate |
| 121 | protected void deactivate() { |
| 122 | kubevirtPodAdminService.removeListener(kubevirtPodListener); |
| 123 | leadershipService.withdraw(appId.name()); |
| 124 | eventExecutor.shutdown(); |
| 125 | |
| 126 | log.info("Stopped"); |
| 127 | } |
| 128 | |
| 129 | private class InternalKubevirtPodListener implements KubevirtPodListener { |
| 130 | |
| 131 | private boolean isRelevantHelper() { |
| 132 | return Objects.equals(localNodeId, leadershipService.getLeader(appId.name())); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void event(KubevirtPodEvent event) { |
| 137 | switch (event.type()) { |
| 138 | case KUBEVIRT_POD_UPDATED: |
| 139 | eventExecutor.execute(() -> processPodUpdate(event.subject())); |
| 140 | break; |
| 141 | case KUBEVIRT_POD_REMOVED: |
| 142 | eventExecutor.execute(() -> processPodDeletion(event.subject())); |
| 143 | break; |
| 144 | case KUBEVIRT_POD_CREATED: |
Jian Li | 7a581b1 | 2021-02-18 14:24:32 +0900 | [diff] [blame] | 145 | eventExecutor.execute(() -> processPodCreation(event.subject())); |
| 146 | break; |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 147 | default: |
| 148 | // do nothing |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | |
Jian Li | 7a581b1 | 2021-02-18 14:24:32 +0900 | [diff] [blame] | 153 | private void processPodCreation(Pod pod) { |
| 154 | if (!isRelevantHelper()) { |
| 155 | return; |
| 156 | } |
| 157 | |
Jian Li | 7a581b1 | 2021-02-18 14:24:32 +0900 | [diff] [blame] | 158 | Map<String, String> annots = pod.getMetadata().getAnnotations(); |
| 159 | if (annots == null) { |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | if (!annots.containsKey(NETWORK_STATUS_KEY)) { |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | try { |
| 168 | String networkStatusStr = pod.getMetadata().getAnnotations().get(NETWORK_STATUS_KEY); |
| 169 | JSONArray networkStatus = new JSONArray(networkStatusStr); |
| 170 | for (int i = 0; i < networkStatus.length(); i++) { |
| 171 | JSONObject object = networkStatus.getJSONObject(i); |
| 172 | String name = object.getString(NAME); |
| 173 | KubevirtNetwork jsonNetwork = kubevirtNetworkAdminService.networks().stream() |
| 174 | .filter(n -> (NETWORK_PREFIX + n.name()).equals(name)) |
| 175 | .findAny().orElse(null); |
| 176 | if (jsonNetwork != null) { |
| 177 | JSONArray ips = object.getJSONArray(IPS); |
| 178 | if (ips != null && ips.length() > 0) { |
| 179 | IpAddress ip = IpAddress.valueOf(ips.getString(0)); |
| 180 | kubevirtNetworkAdminService.reserveIp(jsonNetwork.networkId(), ip); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } catch (Exception e) { |
| 185 | log.error("Failed to reserve IP address", e); |
| 186 | } |
| 187 | |
| 188 | KubevirtPort port = getPort(kubevirtNetworkAdminService.networks(), pod); |
| 189 | if (port == null) { |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | if (kubevirtPortAdminService.port(port.macAddress()) == null) { |
| 194 | kubevirtPortAdminService.createPort(port); |
| 195 | } |
| 196 | } |
| 197 | |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 198 | private void processPodUpdate(Pod pod) { |
| 199 | if (!isRelevantHelper()) { |
| 200 | return; |
| 201 | } |
| 202 | |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 203 | KubevirtPort port = getPort(kubevirtNetworkAdminService.networks(), pod); |
| 204 | if (port == null) { |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | if (kubevirtPortAdminService.port(port.macAddress()) != null) { |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | if (port.ipAddress() == null) { |
| 213 | try { |
| 214 | IpAddress ip = kubevirtNetworkAdminService.allocateIp(port.networkId()); |
| 215 | port = port.updateIpAddress(ip); |
| 216 | |
| 217 | // update the POD annotation to inject the allocated IP address |
| 218 | String networkStatusStr = pod.getMetadata().getAnnotations().get(NETWORK_STATUS_KEY); |
| 219 | JSONArray networkStatus = new JSONArray(networkStatusStr); |
| 220 | for (int i = 0; i < networkStatus.length(); i++) { |
| 221 | JSONObject object = networkStatus.getJSONObject(i); |
| 222 | String name = object.getString(NAME); |
| 223 | KubevirtNetwork jsonNetwork = kubevirtNetworkAdminService.networks().stream() |
| 224 | .filter(n -> (NETWORK_PREFIX + n.name()).equals(name)) |
| 225 | .findAny().orElse(null); |
| 226 | if (jsonNetwork != null) { |
| 227 | JSONArray ipsJson = new JSONArray(); |
| 228 | ipsJson.put(ip.toString()); |
| 229 | object.put(IPS, ipsJson); |
| 230 | } |
| 231 | } |
| 232 | Map<String, String> annots = pod.getMetadata().getAnnotations(); |
| 233 | annots.put(NETWORK_STATUS_KEY, networkStatus.toString(4)); |
| 234 | |
Jian Li | 858ccd7 | 2021-02-04 17:25:01 +0900 | [diff] [blame^] | 235 | KubernetesClient client = k8sClient(kubevirtApiConfigService); |
| 236 | |
| 237 | if (client == null) { |
| 238 | return; |
| 239 | } |
| 240 | |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 241 | client.pods().inNamespace(pod.getMetadata().getNamespace()) |
| 242 | .withName(pod.getMetadata().getName()) |
| 243 | .edit(r -> new PodBuilder(r) |
| 244 | .editMetadata() |
| 245 | .addToAnnotations(annots) |
| 246 | .endMetadata().build() |
| 247 | ); |
| 248 | } catch (Exception e) { |
| 249 | log.error("Failed to allocate IP address", e); |
| 250 | } |
| 251 | } |
| 252 | kubevirtPortAdminService.createPort(port); |
| 253 | } |
| 254 | |
| 255 | private void processPodDeletion(Pod pod) { |
| 256 | if (!isRelevantHelper()) { |
| 257 | return; |
| 258 | } |
| 259 | |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 260 | KubevirtPort port = getPort(kubevirtNetworkAdminService.networks(), pod); |
| 261 | if (port == null) { |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | if (port.ipAddress() != null) { |
| 266 | kubevirtNetworkAdminService.releaseIp(port.networkId(), port.ipAddress()); |
| 267 | } |
| 268 | |
| 269 | kubevirtPortAdminService.removePort(port.macAddress()); |
| 270 | } |
| 271 | } |
| 272 | } |