Jian Li | 810f58c | 2021-02-27 01:10:50 +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 | |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 18 | import com.google.common.base.Strings; |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 21 | import org.onlab.util.KryoNamespace; |
| 22 | import org.onosproject.core.ApplicationId; |
| 23 | import org.onosproject.core.CoreService; |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 24 | import org.onosproject.kubevirtnetworking.api.DefaultKubevirtFloatingIp; |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 25 | import org.onosproject.kubevirtnetworking.api.DefaultKubevirtRouter; |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 26 | import org.onosproject.kubevirtnetworking.api.KubevirtFloatingIp; |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame] | 27 | import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancer; |
| 28 | import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerService; |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 29 | import org.onosproject.kubevirtnetworking.api.KubevirtPeerRouter; |
| 30 | import org.onosproject.kubevirtnetworking.api.KubevirtRouter; |
| 31 | import org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent; |
| 32 | import org.onosproject.kubevirtnetworking.api.KubevirtRouterStore; |
| 33 | import org.onosproject.kubevirtnetworking.api.KubevirtRouterStoreDelegate; |
| 34 | import org.onosproject.store.AbstractStore; |
| 35 | import org.onosproject.store.serializers.KryoNamespaces; |
| 36 | import org.onosproject.store.service.ConsistentMap; |
| 37 | import org.onosproject.store.service.MapEvent; |
| 38 | import org.onosproject.store.service.MapEventListener; |
| 39 | import org.onosproject.store.service.Serializer; |
| 40 | import org.onosproject.store.service.StorageService; |
| 41 | import org.onosproject.store.service.Versioned; |
| 42 | import org.osgi.service.component.annotations.Activate; |
| 43 | import org.osgi.service.component.annotations.Component; |
| 44 | import org.osgi.service.component.annotations.Deactivate; |
| 45 | import org.osgi.service.component.annotations.Reference; |
| 46 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 47 | import org.slf4j.Logger; |
| 48 | |
| 49 | import java.util.Collection; |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 50 | import java.util.HashSet; |
Daniel Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 51 | import java.util.Objects; |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 52 | import java.util.Set; |
| 53 | import java.util.concurrent.ExecutorService; |
| 54 | |
| 55 | import static com.google.common.base.Preconditions.checkArgument; |
| 56 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 57 | import static org.onlab.util.Tools.groupedThreads; |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 58 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_ASSOCIATED; |
| 59 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_CREATED; |
| 60 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_DISASSOCIATED; |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame] | 61 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_LB_ASSOCIATED; |
| 62 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_LB_DISASSOCIATED; |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 63 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_REMOVED; |
| 64 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_UPDATED; |
Daniel Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 65 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_GATEWAY_NODE_ATTACHED; |
| 66 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_GATEWAY_NODE_CHANGED; |
| 67 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_GATEWAY_NODE_DETACHED; |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame] | 68 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_PEER_ROUTER_MAC_RETRIEVED; |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 69 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_CREATED; |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 70 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_EXTERNAL_NETWORK_ATTACHED; |
| 71 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_EXTERNAL_NETWORK_DETACHED; |
| 72 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_INTERNAL_NETWORKS_ATTACHED; |
| 73 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_INTERNAL_NETWORKS_DETACHED; |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 74 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_REMOVED; |
| 75 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_UPDATED; |
Daniel Park | 8ad7c3b | 2021-04-09 15:45:59 +0900 | [diff] [blame] | 76 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_SNAT_STATUS_DISABLED; |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame] | 77 | import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.getLoadBalancerSetForRouter; |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 78 | import static org.slf4j.LoggerFactory.getLogger; |
| 79 | |
| 80 | /** |
| 81 | * Implementation of kubevirt router store using consistent map. |
| 82 | */ |
| 83 | @Component(immediate = true, service = KubevirtRouterStore.class) |
| 84 | public class DistributedKubevirtRouterStore |
| 85 | extends AbstractStore<KubevirtRouterEvent, KubevirtRouterStoreDelegate> |
| 86 | implements KubevirtRouterStore { |
| 87 | |
| 88 | private final Logger log = getLogger(getClass()); |
| 89 | |
| 90 | private static final String ERR_NOT_FOUND = " does not exist"; |
| 91 | private static final String ERR_DUPLICATE = " already exists"; |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 92 | private static final String MSG_FLOATING_IP = "Kubevirt floating IP %s %s with %s"; |
| 93 | private static final String MSG_ASSOCIATED = "associated"; |
| 94 | private static final String MSG_DISASSOCIATED = "disassociated"; |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame] | 95 | private static final String MSG_ASSOCIATED_LB = "associated LB VIP"; |
| 96 | private static final String MSG_DISASSOCIATED_LB = "disassociated LB VIP"; |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 97 | |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 98 | private static final String APP_ID = "org.onosproject.kubevirtnetwork"; |
| 99 | |
| 100 | private static final KryoNamespace |
| 101 | SERIALIZER_KUBEVIRT_ROUTER = KryoNamespace.newBuilder() |
| 102 | .register(KryoNamespaces.API) |
| 103 | .register(KubevirtRouter.class) |
| 104 | .register(DefaultKubevirtRouter.class) |
| 105 | .register(KubevirtPeerRouter.class) |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 106 | .register(KubevirtFloatingIp.class) |
| 107 | .register(DefaultKubevirtFloatingIp.class) |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 108 | .register(Collection.class) |
| 109 | .build(); |
| 110 | |
| 111 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 112 | protected CoreService coreService; |
| 113 | |
| 114 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 115 | protected StorageService storageService; |
| 116 | |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame] | 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 118 | protected KubevirtLoadBalancerService loadBalancerService; |
| 119 | |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 120 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 121 | groupedThreads(this.getClass().getSimpleName(), "event-handler", log)); |
| 122 | |
| 123 | private final MapEventListener<String, KubevirtRouter> routerMapListener = |
| 124 | new KubevirtRouterMapListener(); |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 125 | private final MapEventListener<String, KubevirtFloatingIp> fipMapListener = |
| 126 | new KubevirtFloatingIpMapListener(); |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 127 | |
| 128 | private ConsistentMap<String, KubevirtRouter> routerStore; |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 129 | private ConsistentMap<String, KubevirtFloatingIp> fipStore; |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 130 | |
| 131 | @Activate |
| 132 | protected void activate() { |
| 133 | ApplicationId appId = coreService.registerApplication(APP_ID); |
| 134 | routerStore = storageService.<String, KubevirtRouter>consistentMapBuilder() |
| 135 | .withSerializer(Serializer.using(SERIALIZER_KUBEVIRT_ROUTER)) |
| 136 | .withName("kubevirt-routerstore") |
| 137 | .withApplicationId(appId) |
| 138 | .build(); |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 139 | fipStore = storageService.<String, KubevirtFloatingIp>consistentMapBuilder() |
| 140 | .withSerializer(Serializer.using(SERIALIZER_KUBEVIRT_ROUTER)) |
| 141 | .withName("kubevirt-fipstore") |
| 142 | .withApplicationId(appId) |
| 143 | .build(); |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 144 | routerStore.addListener(routerMapListener); |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 145 | fipStore.addListener(fipMapListener); |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 146 | log.info("Started"); |
| 147 | } |
| 148 | |
| 149 | @Deactivate |
| 150 | protected void deactivate() { |
| 151 | routerStore.removeListener(routerMapListener); |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 152 | fipStore.removeListener(fipMapListener); |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 153 | eventExecutor.shutdown(); |
| 154 | log.info("Stopped"); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void createRouter(KubevirtRouter router) { |
| 159 | routerStore.compute(router.name(), (name, existing) -> { |
| 160 | final String error = router.name() + ERR_DUPLICATE; |
| 161 | checkArgument(existing == null, error); |
| 162 | return router; |
| 163 | }); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public void updateRouter(KubevirtRouter router) { |
| 168 | routerStore.compute(router.name(), (name, existing) -> { |
| 169 | final String error = router.name() + ERR_NOT_FOUND; |
| 170 | checkArgument(existing != null, error); |
| 171 | return router; |
| 172 | }); |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public KubevirtRouter removeRouter(String name) { |
| 177 | Versioned<KubevirtRouter> router = routerStore.remove(name); |
| 178 | if (router == null) { |
| 179 | final String error = name + ERR_NOT_FOUND; |
| 180 | throw new IllegalArgumentException(error); |
| 181 | } |
| 182 | return router.value(); |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public KubevirtRouter router(String name) { |
| 187 | return routerStore.asJavaMap().get(name); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public Set<KubevirtRouter> routers() { |
| 192 | return ImmutableSet.copyOf(routerStore.asJavaMap().values()); |
| 193 | } |
| 194 | |
| 195 | @Override |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 196 | public void createFloatingIp(KubevirtFloatingIp floatingIp) { |
| 197 | fipStore.compute(floatingIp.id(), (id, existing) -> { |
| 198 | final String error = floatingIp.id() + ERR_DUPLICATE; |
| 199 | checkArgument(existing == null, error); |
| 200 | return floatingIp; |
| 201 | }); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public void updateFloatingIp(KubevirtFloatingIp floatingIp) { |
| 206 | fipStore.compute(floatingIp.id(), (id, existing) -> { |
| 207 | final String error = floatingIp.id() + ERR_NOT_FOUND; |
| 208 | checkArgument(existing != null, error); |
| 209 | return floatingIp; |
| 210 | }); |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public KubevirtFloatingIp removeFloatingIp(String id) { |
| 215 | Versioned<KubevirtFloatingIp> floatingIp = fipStore.remove(id); |
| 216 | if (floatingIp == null) { |
| 217 | final String error = id + ERR_NOT_FOUND; |
| 218 | throw new IllegalArgumentException(error); |
| 219 | } |
| 220 | return floatingIp.value(); |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public KubevirtFloatingIp floatingIp(String id) { |
| 225 | return fipStore.asJavaMap().get(id); |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public Set<KubevirtFloatingIp> floatingIps() { |
| 230 | return ImmutableSet.copyOf(fipStore.asJavaMap().values()); |
| 231 | } |
| 232 | |
| 233 | |
| 234 | @Override |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 235 | public void clear() { |
| 236 | routerStore.clear(); |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 237 | fipStore.clear(); |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | private class KubevirtRouterMapListener implements MapEventListener<String, KubevirtRouter> { |
| 241 | |
| 242 | @Override |
| 243 | public void event(MapEvent<String, KubevirtRouter> event) { |
| 244 | switch (event.type()) { |
| 245 | case INSERT: |
| 246 | log.debug("Kubevirt router created"); |
| 247 | eventExecutor.execute(() -> |
| 248 | notifyDelegate(new KubevirtRouterEvent( |
| 249 | KUBEVIRT_ROUTER_CREATED, event.newValue().value()))); |
| 250 | break; |
| 251 | case UPDATE: |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 252 | eventExecutor.execute(() -> processRouterMapUpdate(event)); |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 253 | break; |
| 254 | case REMOVE: |
| 255 | log.debug("Kubevirt router removed"); |
| 256 | eventExecutor.execute(() -> |
| 257 | notifyDelegate(new KubevirtRouterEvent( |
| 258 | KUBEVIRT_ROUTER_REMOVED, event.oldValue().value()))); |
| 259 | break; |
| 260 | default: |
| 261 | // do nothing |
| 262 | break; |
| 263 | } |
| 264 | } |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 265 | |
| 266 | private void processRouterMapUpdate(MapEvent<String, KubevirtRouter> event) { |
| 267 | log.debug("Kubevirt router updated"); |
| 268 | eventExecutor.execute(() -> |
| 269 | notifyDelegate(new KubevirtRouterEvent( |
| 270 | KUBEVIRT_ROUTER_UPDATED, event.newValue().value()))); |
| 271 | |
| 272 | KubevirtRouter router = Strings.isNullOrEmpty( |
| 273 | event.newValue().value().name()) ? |
| 274 | null : |
| 275 | router(event.newValue().value().name()); |
| 276 | |
| 277 | KubevirtRouter oldValue = event.oldValue().value(); |
| 278 | KubevirtRouter newValue = event.newValue().value(); |
| 279 | |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame] | 280 | if (oldValue.peerRouter() != null |
| 281 | && oldValue.peerRouter().macAddress() == null |
| 282 | && newValue.peerRouter() != null |
| 283 | && newValue.peerRouter().macAddress() != null) { |
| 284 | notifyDelegate(new KubevirtRouterEvent( |
| 285 | KUBEVIRT_PEER_ROUTER_MAC_RETRIEVED, |
| 286 | event.newValue().value())); |
| 287 | } |
| 288 | |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 289 | if (oldValue.external().size() == 0 && newValue.external().size() > 0) { |
| 290 | newValue.external().entrySet().stream().findAny() |
| 291 | .ifPresent(entry -> |
| 292 | notifyDelegate(new KubevirtRouterEvent( |
| 293 | KUBEVIRT_ROUTER_EXTERNAL_NETWORK_ATTACHED, |
| 294 | router, entry.getKey(), entry.getValue(), |
Daniel Park | 5a3e939 | 2021-03-23 08:00:00 +0900 | [diff] [blame] | 295 | newValue.peerRouter().ipAddress().toString(), |
| 296 | newValue.peerRouter().macAddress()))); |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | if (oldValue.external().size() > 0 && newValue.external().size() == 0) { |
| 300 | oldValue.external().entrySet().stream().findAny() |
| 301 | .ifPresent(entry -> |
| 302 | notifyDelegate(new KubevirtRouterEvent( |
| 303 | KUBEVIRT_ROUTER_EXTERNAL_NETWORK_DETACHED, |
| 304 | router, entry.getKey(), entry.getValue(), |
Daniel Park | 5a3e939 | 2021-03-23 08:00:00 +0900 | [diff] [blame] | 305 | oldValue.peerRouter().ipAddress().toString(), |
| 306 | oldValue.peerRouter().macAddress()))); |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | Set<String> added = new HashSet<>(newValue.internal()); |
| 310 | Set<String> oldset = oldValue.internal(); |
| 311 | added.removeAll(oldset); |
| 312 | |
| 313 | Set<String> removed = new HashSet<>(oldValue.internal()); |
| 314 | Set<String> newset = newValue.internal(); |
| 315 | removed.removeAll(newset); |
| 316 | |
| 317 | if (added.size() > 0) { |
| 318 | notifyDelegate(new KubevirtRouterEvent( |
| 319 | KUBEVIRT_ROUTER_INTERNAL_NETWORKS_ATTACHED, |
| 320 | router, added)); |
| 321 | } |
| 322 | |
| 323 | if (removed.size() > 0) { |
| 324 | notifyDelegate(new KubevirtRouterEvent( |
| 325 | KUBEVIRT_ROUTER_INTERNAL_NETWORKS_DETACHED, |
| 326 | router, removed)); |
| 327 | } |
Daniel Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 328 | if (oldValue.electedGateway() == null |
| 329 | && newValue.electedGateway() != null) { |
| 330 | notifyDelegate(new KubevirtRouterEvent( |
| 331 | KUBEVIRT_GATEWAY_NODE_ATTACHED, |
| 332 | router, newValue.electedGateway())); |
| 333 | } |
| 334 | |
| 335 | if (oldValue.electedGateway() != null |
| 336 | && newValue.electedGateway() == null) { |
| 337 | notifyDelegate(new KubevirtRouterEvent( |
| 338 | KUBEVIRT_GATEWAY_NODE_DETACHED, |
| 339 | router, oldValue.electedGateway())); |
| 340 | } |
| 341 | |
| 342 | if (oldValue.electedGateway() != null |
| 343 | && newValue.electedGateway() != null |
| 344 | && !Objects.equals(oldValue.electedGateway(), newValue.electedGateway())) { |
| 345 | notifyDelegate(new KubevirtRouterEvent( |
| 346 | KUBEVIRT_GATEWAY_NODE_CHANGED, |
| 347 | router, oldValue.electedGateway())); |
| 348 | } |
Daniel Park | 8ad7c3b | 2021-04-09 15:45:59 +0900 | [diff] [blame] | 349 | |
| 350 | if (oldValue.enableSnat() && !newValue.enableSnat()) { |
| 351 | notifyDelegate(new KubevirtRouterEvent( |
| 352 | KUBEVIRT_SNAT_STATUS_DISABLED, router)); |
| 353 | } |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 354 | } |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 355 | } |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 356 | |
| 357 | private class KubevirtFloatingIpMapListener implements MapEventListener<String, KubevirtFloatingIp> { |
| 358 | |
| 359 | @Override |
| 360 | public void event(MapEvent<String, KubevirtFloatingIp> event) { |
| 361 | switch (event.type()) { |
| 362 | case INSERT: |
| 363 | eventExecutor.execute(() -> processFloatingIpMapInsertion(event)); |
| 364 | break; |
| 365 | case UPDATE: |
| 366 | eventExecutor.execute(() -> processFloatingIpMapUpdate(event)); |
| 367 | break; |
| 368 | case REMOVE: |
| 369 | eventExecutor.execute(() -> processFloatingIpMapRemoval(event)); |
| 370 | break; |
| 371 | default: |
| 372 | break; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | private void processFloatingIpMapInsertion(MapEvent<String, KubevirtFloatingIp> event) { |
| 377 | log.debug("Kubevirt floating IP created"); |
| 378 | KubevirtRouter router = Strings.isNullOrEmpty( |
| 379 | event.newValue().value().routerName()) ? |
| 380 | null : |
| 381 | router(event.newValue().value().routerName()); |
| 382 | notifyDelegate(new KubevirtRouterEvent( |
| 383 | KUBEVIRT_FLOATING_IP_CREATED, |
| 384 | router, |
| 385 | event.newValue().value())); |
| 386 | } |
| 387 | |
| 388 | private void processFloatingIpMapUpdate(MapEvent<String, KubevirtFloatingIp> event) { |
| 389 | log.debug("Kubevirt floating IP updated"); |
| 390 | KubevirtRouter router = Strings.isNullOrEmpty( |
| 391 | event.newValue().value().routerName()) ? |
| 392 | null : |
| 393 | router(event.newValue().value().routerName()); |
| 394 | notifyDelegate(new KubevirtRouterEvent( |
| 395 | KUBEVIRT_FLOATING_IP_UPDATED, |
| 396 | router, |
| 397 | event.newValue().value())); |
| 398 | processFloatingIpUpdate(event, router); |
| 399 | } |
| 400 | |
| 401 | private void processFloatingIpMapRemoval(MapEvent<String, KubevirtFloatingIp> event) { |
| 402 | log.debug("Kubevirt floating IP removed"); |
| 403 | KubevirtRouter router = Strings.isNullOrEmpty( |
| 404 | event.oldValue().value().routerName()) ? |
| 405 | null : |
| 406 | router(event.oldValue().value().routerName()); |
| 407 | notifyDelegate(new KubevirtRouterEvent( |
| 408 | KUBEVIRT_FLOATING_IP_REMOVED, |
| 409 | router, |
| 410 | event.oldValue().value())); |
| 411 | } |
| 412 | |
| 413 | private void processFloatingIpUpdate(MapEvent<String, KubevirtFloatingIp> event, |
| 414 | KubevirtRouter router) { |
| 415 | String oldPodName = event.oldValue().value().podName(); |
| 416 | String newPodName = event.newValue().value().podName(); |
| 417 | |
| 418 | if (Strings.isNullOrEmpty(oldPodName) && !Strings.isNullOrEmpty(newPodName)) { |
| 419 | notifyDelegate(new KubevirtRouterEvent( |
| 420 | KUBEVIRT_FLOATING_IP_ASSOCIATED, |
| 421 | router, |
| 422 | event.newValue().value(), newPodName)); |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 423 | log.info(String.format(MSG_FLOATING_IP, |
| 424 | event.newValue().value().floatingIp(), MSG_ASSOCIATED, newPodName)); |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | if (!Strings.isNullOrEmpty(oldPodName) && Strings.isNullOrEmpty(newPodName)) { |
| 428 | notifyDelegate(new KubevirtRouterEvent( |
| 429 | KUBEVIRT_FLOATING_IP_DISASSOCIATED, |
| 430 | router, |
Daniel Park | f313604 | 2021-03-10 07:49:11 +0900 | [diff] [blame] | 431 | event.oldValue().value(), oldPodName)); |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 432 | log.info(String.format(MSG_FLOATING_IP, |
| 433 | event.newValue().value().floatingIp(), MSG_DISASSOCIATED, oldPodName)); |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 434 | } |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame] | 435 | |
| 436 | IpAddress oldFixedIp = event.oldValue().value().fixedIp(); |
| 437 | IpAddress newFixedIp = event.newValue().value().fixedIp(); |
| 438 | |
| 439 | getLoadBalancerSetForRouter(router, loadBalancerService) |
| 440 | .stream() |
| 441 | .map(KubevirtLoadBalancer::vip) |
| 442 | .forEach(vip -> { |
| 443 | if (oldFixedIp == null |
| 444 | && newFixedIp != null |
| 445 | && newFixedIp.equals(vip)) { |
| 446 | notifyDelegate(new KubevirtRouterEvent( |
| 447 | KUBEVIRT_FLOATING_IP_LB_ASSOCIATED, |
| 448 | router, |
| 449 | event.newValue().value())); |
| 450 | log.info(String.format(MSG_FLOATING_IP, |
| 451 | event.newValue().value().floatingIp(), MSG_ASSOCIATED_LB, |
| 452 | event.newValue().value().fixedIp())); |
| 453 | } |
| 454 | |
| 455 | if (oldFixedIp != null |
| 456 | && newFixedIp == null |
| 457 | && oldFixedIp.equals(vip)) { |
| 458 | notifyDelegate(new KubevirtRouterEvent( |
| 459 | KUBEVIRT_FLOATING_IP_LB_DISASSOCIATED, |
| 460 | router, |
| 461 | event.oldValue().value())); |
| 462 | log.info(String.format(MSG_FLOATING_IP, |
| 463 | event.oldValue().value().floatingIp(), MSG_DISASSOCIATED_LB, |
| 464 | event.oldValue().value().fixedIp())); |
| 465 | } |
| 466 | }); |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 467 | } |
| 468 | } |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 469 | } |