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; |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 19 | import org.onosproject.cluster.ClusterService; |
| 20 | import org.onosproject.cluster.LeadershipService; |
| 21 | import org.onosproject.cluster.NodeId; |
| 22 | import org.onosproject.core.ApplicationId; |
| 23 | import org.onosproject.core.CoreService; |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 24 | import org.onosproject.kubevirtnetworking.api.KubevirtNetworkAdminService; |
| 25 | import org.onosproject.kubevirtnetworking.api.KubevirtPodAdminService; |
| 26 | import org.onosproject.kubevirtnetworking.api.KubevirtPodEvent; |
| 27 | import org.onosproject.kubevirtnetworking.api.KubevirtPodListener; |
| 28 | import org.onosproject.kubevirtnetworking.api.KubevirtPort; |
| 29 | import org.onosproject.kubevirtnetworking.api.KubevirtPortAdminService; |
| 30 | import org.onosproject.kubevirtnode.api.KubevirtApiConfigService; |
Jian Li | b6dc08f | 2021-03-24 15:24:18 +0900 | [diff] [blame] | 31 | import org.onosproject.kubevirtnode.api.KubevirtNode; |
Daniel Park | f313604 | 2021-03-10 07:49:11 +0900 | [diff] [blame] | 32 | import org.onosproject.kubevirtnode.api.KubevirtNodeService; |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 33 | import org.onosproject.mastership.MastershipService; |
| 34 | import org.onosproject.net.device.DeviceService; |
| 35 | import org.onosproject.net.driver.DriverService; |
| 36 | import org.osgi.service.component.annotations.Activate; |
| 37 | import org.osgi.service.component.annotations.Component; |
| 38 | import org.osgi.service.component.annotations.Deactivate; |
| 39 | import org.osgi.service.component.annotations.Reference; |
| 40 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 41 | import org.slf4j.Logger; |
| 42 | |
| 43 | import java.util.Map; |
| 44 | import java.util.Objects; |
Jian Li | d4296d0 | 2021-03-12 18:03:58 +0900 | [diff] [blame] | 45 | import java.util.Set; |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 46 | import java.util.concurrent.ExecutorService; |
| 47 | |
Jian Li | b6dc08f | 2021-03-24 15:24:18 +0900 | [diff] [blame] | 48 | import static java.lang.Thread.sleep; |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 49 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 50 | import static org.onlab.util.Tools.groupedThreads; |
| 51 | import static org.onosproject.kubevirtnetworking.api.Constants.KUBEVIRT_NETWORKING_APP_ID; |
Jian Li | d4296d0 | 2021-03-12 18:03:58 +0900 | [diff] [blame] | 52 | import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.getPorts; |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 53 | import static org.slf4j.LoggerFactory.getLogger; |
| 54 | |
| 55 | /** |
| 56 | * Associates the kubevirt container port and pod. |
| 57 | */ |
| 58 | @Component(immediate = true) |
| 59 | public class KubevirtPodPortMapper { |
| 60 | |
| 61 | private final Logger log = getLogger(getClass()); |
| 62 | |
| 63 | private static final String NETWORK_STATUS_KEY = "k8s.v1.cni.cncf.io/network-status"; |
| 64 | private static final String NAME = "name"; |
| 65 | private static final String IPS = "ips"; |
| 66 | private static final String NETWORK_PREFIX = "default/"; |
Jian Li | b6dc08f | 2021-03-24 15:24:18 +0900 | [diff] [blame] | 67 | private static final long SLEEP_MS = 2000; // we wait 2s |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 68 | |
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 70 | protected CoreService coreService; |
| 71 | |
| 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 73 | protected MastershipService mastershipService; |
| 74 | |
| 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 76 | protected ClusterService clusterService; |
| 77 | |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 79 | protected LeadershipService leadershipService; |
| 80 | |
| 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 82 | protected DeviceService deviceService; |
| 83 | |
| 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 85 | protected DriverService driverService; |
| 86 | |
| 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 88 | protected KubevirtPortAdminService kubevirtPortAdminService; |
| 89 | |
| 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 91 | protected KubevirtNetworkAdminService kubevirtNetworkAdminService; |
| 92 | |
| 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 94 | protected KubevirtPodAdminService kubevirtPodAdminService; |
| 95 | |
| 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 97 | protected KubevirtApiConfigService kubevirtApiConfigService; |
| 98 | |
Daniel Park | f313604 | 2021-03-10 07:49:11 +0900 | [diff] [blame] | 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 100 | protected KubevirtNodeService kubevirtNodeService; |
| 101 | |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 102 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 103 | groupedThreads(this.getClass().getSimpleName(), "event-handler")); |
| 104 | |
| 105 | private final InternalKubevirtPodListener kubevirtPodListener = |
| 106 | new InternalKubevirtPodListener(); |
| 107 | |
| 108 | private ApplicationId appId; |
| 109 | private NodeId localNodeId; |
| 110 | |
| 111 | @Activate |
| 112 | protected void activate() { |
| 113 | appId = coreService.registerApplication(KUBEVIRT_NETWORKING_APP_ID); |
| 114 | localNodeId = clusterService.getLocalNode().id(); |
| 115 | leadershipService.runForLeadership(appId.name()); |
| 116 | kubevirtPodAdminService.addListener(kubevirtPodListener); |
| 117 | |
| 118 | log.info("Started"); |
| 119 | } |
| 120 | |
| 121 | @Deactivate |
| 122 | protected void deactivate() { |
| 123 | kubevirtPodAdminService.removeListener(kubevirtPodListener); |
| 124 | leadershipService.withdraw(appId.name()); |
| 125 | eventExecutor.shutdown(); |
| 126 | |
| 127 | log.info("Stopped"); |
| 128 | } |
| 129 | |
| 130 | private class InternalKubevirtPodListener implements KubevirtPodListener { |
| 131 | |
| 132 | private boolean isRelevantHelper() { |
| 133 | return Objects.equals(localNodeId, leadershipService.getLeader(appId.name())); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void event(KubevirtPodEvent event) { |
| 138 | switch (event.type()) { |
| 139 | case KUBEVIRT_POD_UPDATED: |
| 140 | eventExecutor.execute(() -> processPodUpdate(event.subject())); |
| 141 | break; |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 142 | case KUBEVIRT_POD_CREATED: |
Jian Li | b6dc08f | 2021-03-24 15:24:18 +0900 | [diff] [blame] | 143 | case KUBEVIRT_POD_REMOVED: |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 144 | default: |
| 145 | // do nothing |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | |
Jian Li | b6dc08f | 2021-03-24 15:24:18 +0900 | [diff] [blame] | 150 | private void processPodUpdate(Pod pod) { |
Jian Li | 7a581b1 | 2021-02-18 14:24:32 +0900 | [diff] [blame] | 151 | if (!isRelevantHelper()) { |
| 152 | return; |
| 153 | } |
| 154 | |
Jian Li | 7a581b1 | 2021-02-18 14:24:32 +0900 | [diff] [blame] | 155 | Map<String, String> annots = pod.getMetadata().getAnnotations(); |
| 156 | if (annots == null) { |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | if (!annots.containsKey(NETWORK_STATUS_KEY)) { |
| 161 | return; |
| 162 | } |
| 163 | |
Jian Li | b6dc08f | 2021-03-24 15:24:18 +0900 | [diff] [blame] | 164 | KubevirtNode node = kubevirtNodeService.node(pod.getSpec().getNodeName()); |
| 165 | |
| 166 | if (node == null) { |
| 167 | try { |
| 168 | // we wait until all k8s nodes are available |
| 169 | sleep(SLEEP_MS); |
| 170 | } catch (InterruptedException e) { |
| 171 | e.printStackTrace(); |
Jian Li | 7a581b1 | 2021-02-18 14:24:32 +0900 | [diff] [blame] | 172 | } |
Jian Li | 7a581b1 | 2021-02-18 14:24:32 +0900 | [diff] [blame] | 173 | } |
| 174 | |
Jian Li | b6dc08f | 2021-03-24 15:24:18 +0900 | [diff] [blame] | 175 | Set<KubevirtPort> ports = |
| 176 | getPorts(kubevirtNodeService, kubevirtNetworkAdminService.networks(), pod); |
Jian Li | d4296d0 | 2021-03-12 18:03:58 +0900 | [diff] [blame] | 177 | if (ports.size() == 0) { |
Jian Li | 7a581b1 | 2021-02-18 14:24:32 +0900 | [diff] [blame] | 178 | return; |
| 179 | } |
| 180 | |
Jian Li | d4296d0 | 2021-03-12 18:03:58 +0900 | [diff] [blame] | 181 | ports.forEach(port -> { |
Jian Li | b6dc08f | 2021-03-24 15:24:18 +0900 | [diff] [blame] | 182 | KubevirtPort existing = kubevirtPortAdminService.port(port.macAddress()); |
Jian Li | 7a581b1 | 2021-02-18 14:24:32 +0900 | [diff] [blame] | 183 | |
Jian Li | b6dc08f | 2021-03-24 15:24:18 +0900 | [diff] [blame] | 184 | if (existing != null) { |
| 185 | if (port.deviceId() != null && existing.deviceId() == null) { |
| 186 | KubevirtPort updated = existing.updateDeviceId(port.deviceId()); |
| 187 | // internal we update device ID of kubevirt port |
| 188 | kubevirtPortAdminService.updatePort(updated); |
Jian Li | d4296d0 | 2021-03-12 18:03:58 +0900 | [diff] [blame] | 189 | } |
| 190 | } |
Jian Li | d4296d0 | 2021-03-12 18:03:58 +0900 | [diff] [blame] | 191 | }); |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | } |