Jian Li | 073f1ba | 2021-02-28 03:50:15 +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 com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import io.fabric8.kubernetes.client.KubernetesClient; |
| 22 | import io.fabric8.kubernetes.client.Watcher; |
| 23 | import io.fabric8.kubernetes.client.WatcherException; |
| 24 | import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext; |
| 25 | import org.onosproject.cluster.ClusterService; |
| 26 | import org.onosproject.cluster.LeadershipService; |
| 27 | import org.onosproject.cluster.NodeId; |
| 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.CoreService; |
| 30 | import org.onosproject.kubevirtnetworking.api.AbstractWatcher; |
| 31 | import org.onosproject.kubevirtnetworking.api.KubevirtFloatingIp; |
| 32 | import org.onosproject.kubevirtnetworking.api.KubevirtRouterAdminService; |
| 33 | import org.onosproject.kubevirtnode.api.KubevirtApiConfigEvent; |
| 34 | import org.onosproject.kubevirtnode.api.KubevirtApiConfigListener; |
| 35 | import org.onosproject.kubevirtnode.api.KubevirtApiConfigService; |
| 36 | import org.onosproject.mastership.MastershipService; |
| 37 | import org.osgi.service.component.annotations.Activate; |
| 38 | import org.osgi.service.component.annotations.Component; |
| 39 | import org.osgi.service.component.annotations.Deactivate; |
| 40 | import org.osgi.service.component.annotations.Reference; |
| 41 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 42 | import org.slf4j.Logger; |
| 43 | |
| 44 | import java.io.IOException; |
| 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.kubevirtnetworking.api.Constants.KUBEVIRT_NETWORKING_APP_ID; |
| 51 | import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.k8sClient; |
| 52 | import static org.slf4j.LoggerFactory.getLogger; |
| 53 | |
| 54 | /** |
| 55 | * Kubevirt floating IP watcher used for feeding kubevirt floating IP information. |
| 56 | */ |
| 57 | @Component(immediate = true) |
| 58 | public class KubevirtFloatingIpWatcher extends AbstractWatcher { |
| 59 | |
| 60 | private final Logger log = getLogger(getClass()); |
| 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 63 | protected CoreService coreService; |
| 64 | |
| 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 66 | protected MastershipService mastershipService; |
| 67 | |
| 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 69 | protected ClusterService clusterService; |
| 70 | |
| 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 72 | protected LeadershipService leadershipService; |
| 73 | |
| 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 75 | protected KubevirtRouterAdminService adminService; |
| 76 | |
| 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 78 | protected KubevirtApiConfigService configService; |
| 79 | |
| 80 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 81 | groupedThreads(this.getClass().getSimpleName(), "event-handler")); |
| 82 | |
| 83 | private final InternalFloatingIpWatcher watcher = new InternalFloatingIpWatcher(); |
| 84 | private final InternalKubevirtApiConfigListener configListener = |
| 85 | new InternalKubevirtApiConfigListener(); |
| 86 | |
| 87 | CustomResourceDefinitionContext fipCrdCxt = new CustomResourceDefinitionContext |
| 88 | .Builder() |
| 89 | .withGroup("kubevirt.io") |
| 90 | .withScope("Cluster") |
| 91 | .withVersion("v1") |
| 92 | .withPlural("floatingips") |
| 93 | .build(); |
| 94 | |
| 95 | private ApplicationId appId; |
| 96 | private NodeId localNodeId; |
| 97 | |
| 98 | @Activate |
| 99 | protected void activate() { |
| 100 | appId = coreService.registerApplication(KUBEVIRT_NETWORKING_APP_ID); |
| 101 | localNodeId = clusterService.getLocalNode().id(); |
| 102 | leadershipService.runForLeadership(appId.name()); |
| 103 | configService.addListener(configListener); |
| 104 | |
| 105 | log.info("Started"); |
| 106 | } |
| 107 | |
| 108 | @Deactivate |
| 109 | protected void deactivate() { |
| 110 | configService.removeListener(configListener); |
| 111 | leadershipService.withdraw(appId.name()); |
| 112 | eventExecutor.shutdown(); |
| 113 | |
| 114 | log.info("Stopped"); |
| 115 | } |
| 116 | |
| 117 | private void instantiateWatcher() { |
| 118 | KubernetesClient client = k8sClient(configService); |
| 119 | |
| 120 | if (client != null) { |
| 121 | try { |
| 122 | client.customResource(fipCrdCxt).watch(watcher); |
| 123 | } catch (IOException e) { |
| 124 | e.printStackTrace(); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | private KubevirtFloatingIp parseKubevirtFloatingIp(String resource) { |
| 130 | try { |
| 131 | ObjectMapper mapper = new ObjectMapper(); |
| 132 | JsonNode json = mapper.readTree(resource); |
| 133 | ObjectNode spec = (ObjectNode) json.get("spec"); |
| 134 | return codec(KubevirtFloatingIp.class).decode(spec, this); |
| 135 | } catch (IOException e) { |
| 136 | log.error("Failed to parse kubevirt floating IP object"); |
| 137 | } |
| 138 | |
| 139 | return null; |
| 140 | } |
| 141 | |
| 142 | private class InternalKubevirtApiConfigListener implements KubevirtApiConfigListener { |
| 143 | |
| 144 | private boolean isRelevantHelper() { |
| 145 | return Objects.equals(localNodeId, leadershipService.getLeader(appId.name())); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void event(KubevirtApiConfigEvent event) { |
| 150 | |
| 151 | switch (event.type()) { |
| 152 | case KUBEVIRT_API_CONFIG_UPDATED: |
| 153 | eventExecutor.execute(this::processConfigUpdate); |
| 154 | break; |
| 155 | case KUBEVIRT_API_CONFIG_CREATED: |
| 156 | case KUBEVIRT_API_CONFIG_REMOVED: |
| 157 | default: |
| 158 | // do nothing |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | private void processConfigUpdate() { |
| 164 | if (!isRelevantHelper()) { |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | instantiateWatcher(); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | private class InternalFloatingIpWatcher implements Watcher<String> { |
| 173 | |
| 174 | @Override |
| 175 | public void eventReceived(Action action, String resource) { |
| 176 | switch (action) { |
| 177 | case ADDED: |
| 178 | eventExecutor.execute(() -> processAddition(resource)); |
| 179 | break; |
| 180 | case MODIFIED: |
| 181 | eventExecutor.execute(() -> processModification(resource)); |
| 182 | break; |
| 183 | case DELETED: |
| 184 | eventExecutor.execute(() -> processDeletion(resource)); |
| 185 | break; |
| 186 | case ERROR: |
| 187 | log.warn("Failures processing floating IP manipulation."); |
| 188 | break; |
| 189 | default: |
| 190 | // do nothing |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public void onClose(WatcherException e) { |
| 197 | |
| 198 | } |
| 199 | |
| 200 | private void processAddition(String resource) { |
| 201 | if (!isMaster()) { |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | KubevirtFloatingIp fip = parseKubevirtFloatingIp(resource); |
| 206 | |
| 207 | if (fip != null) { |
| 208 | log.trace("Process Floating IP {} creating event from API server.", fip.floatingIp()); |
| 209 | |
| 210 | if (adminService.floatingIp(fip.id()) == null) { |
| 211 | adminService.createFloatingIp(fip); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | private void processModification(String resource) { |
| 217 | if (!isMaster()) { |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | KubevirtFloatingIp fip = parseKubevirtFloatingIp(resource); |
| 222 | |
| 223 | if (fip != null) { |
| 224 | log.trace("Process Floating IP {} updating event from API server.", fip.floatingIp()); |
| 225 | |
| 226 | if (adminService.floatingIp(fip.id()) != null) { |
| 227 | adminService.updateFloatingIp(fip); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | private void processDeletion(String resource) { |
| 233 | if (!isMaster()) { |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | KubevirtFloatingIp fip = parseKubevirtFloatingIp(resource); |
| 238 | |
| 239 | if (fip != null) { |
| 240 | log.trace("Process floating IP {} removal event from API server.", fip.floatingIp()); |
| 241 | |
| 242 | adminService.removeFloatingIp(fip.id()); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | private boolean isMaster() { |
| 247 | return Objects.equals(localNodeId, leadershipService.getLeader(appId.name())); |
| 248 | } |
| 249 | } |
| 250 | } |