Jian Li | 4b24970 | 2021-02-19 18:13:10 +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.kubevirtnode.impl; |
| 17 | |
| 18 | import io.fabric8.kubernetes.api.model.Node; |
| 19 | import io.fabric8.kubernetes.client.KubernetesClient; |
| 20 | import io.fabric8.kubernetes.client.Watcher; |
| 21 | import io.fabric8.kubernetes.client.WatcherException; |
| 22 | import org.onosproject.cluster.ClusterService; |
| 23 | import org.onosproject.cluster.LeadershipService; |
| 24 | import org.onosproject.cluster.NodeId; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.kubevirtnode.api.KubevirtApiConfig; |
| 28 | import org.onosproject.kubevirtnode.api.KubevirtApiConfigEvent; |
| 29 | import org.onosproject.kubevirtnode.api.KubevirtApiConfigListener; |
| 30 | import org.onosproject.kubevirtnode.api.KubevirtApiConfigService; |
| 31 | import org.onosproject.kubevirtnode.api.KubevirtNode; |
| 32 | import org.onosproject.kubevirtnode.api.KubevirtNodeAdminService; |
| 33 | import org.onosproject.mastership.MastershipService; |
| 34 | import org.osgi.service.component.annotations.Activate; |
| 35 | import org.osgi.service.component.annotations.Component; |
| 36 | import org.osgi.service.component.annotations.Deactivate; |
| 37 | import org.osgi.service.component.annotations.Reference; |
| 38 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 39 | import org.slf4j.Logger; |
| 40 | |
| 41 | import java.util.Objects; |
| 42 | import java.util.concurrent.ExecutorService; |
| 43 | |
| 44 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 45 | import static org.onlab.util.Tools.groupedThreads; |
Daniel Park | 515f5f3 | 2021-02-22 17:12:20 +0900 | [diff] [blame] | 46 | import static org.onosproject.kubevirtnode.api.KubevirtNode.Type.GATEWAY; |
Jian Li | 517597a | 2021-03-22 11:04:52 +0900 | [diff] [blame] | 47 | import static org.onosproject.kubevirtnode.api.KubevirtNode.Type.MASTER; |
Jian Li | 4b24970 | 2021-02-19 18:13:10 +0900 | [diff] [blame] | 48 | import static org.onosproject.kubevirtnode.api.KubevirtNode.Type.WORKER; |
| 49 | import static org.onosproject.kubevirtnode.api.KubevirtNodeService.APP_ID; |
| 50 | import static org.onosproject.kubevirtnode.api.KubevirtNodeState.INIT; |
| 51 | import static org.onosproject.kubevirtnode.util.KubevirtNodeUtil.buildKubevirtNode; |
| 52 | import static org.onosproject.kubevirtnode.util.KubevirtNodeUtil.k8sClient; |
| 53 | import static org.slf4j.LoggerFactory.getLogger; |
| 54 | |
| 55 | /** |
| 56 | * Kubernetes node watcher used for feeding node information. |
| 57 | */ |
| 58 | @Component(immediate = true) |
| 59 | public class KubevirtNodeWatcher { |
| 60 | |
| 61 | private final Logger log = getLogger(getClass()); |
| 62 | |
| 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 64 | protected CoreService coreService; |
| 65 | |
| 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 67 | protected MastershipService mastershipService; |
| 68 | |
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 70 | protected ClusterService clusterService; |
| 71 | |
| 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 73 | protected LeadershipService leadershipService; |
| 74 | |
| 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 76 | protected KubevirtNodeAdminService kubevirtNodeAdminService; |
| 77 | |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 79 | protected KubevirtApiConfigService kubevirtApiConfigService; |
| 80 | |
| 81 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 82 | groupedThreads(this.getClass().getSimpleName(), "event-handler")); |
| 83 | private final Watcher<Node> internalKubevirtNodeWatcher = new InternalKubevirtNodeWatcher(); |
| 84 | private final InternalKubevirtApiConfigListener |
| 85 | internalKubevirtApiConfigListener = new InternalKubevirtApiConfigListener(); |
| 86 | |
| 87 | private ApplicationId appId; |
| 88 | private NodeId localNodeId; |
| 89 | |
| 90 | @Activate |
| 91 | protected void activate() { |
| 92 | appId = coreService.registerApplication(APP_ID); |
| 93 | localNodeId = clusterService.getLocalNode().id(); |
| 94 | leadershipService.runForLeadership(appId.name()); |
| 95 | kubevirtApiConfigService.addListener(internalKubevirtApiConfigListener); |
| 96 | |
| 97 | log.info("Started"); |
| 98 | } |
| 99 | |
| 100 | @Deactivate |
| 101 | protected void deactivate() { |
| 102 | kubevirtApiConfigService.removeListener(internalKubevirtApiConfigListener); |
| 103 | leadershipService.withdraw(appId.name()); |
| 104 | eventExecutor.shutdown(); |
| 105 | |
| 106 | log.info("Stopped"); |
| 107 | } |
| 108 | |
| 109 | private void instantiateNodeWatcher() { |
| 110 | KubevirtApiConfig config = kubevirtApiConfigService.apiConfig(); |
| 111 | if (config == null) { |
| 112 | return; |
| 113 | } |
| 114 | KubernetesClient client = k8sClient(config); |
| 115 | |
| 116 | if (client != null) { |
| 117 | client.nodes().watch(internalKubevirtNodeWatcher); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | private class InternalKubevirtApiConfigListener implements KubevirtApiConfigListener { |
| 122 | |
| 123 | private boolean isRelevantHelper() { |
| 124 | return Objects.equals(localNodeId, leadershipService.getLeader(appId.name())); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void event(KubevirtApiConfigEvent event) { |
| 129 | |
| 130 | switch (event.type()) { |
| 131 | case KUBEVIRT_API_CONFIG_UPDATED: |
| 132 | eventExecutor.execute(this::processConfigUpdating); |
| 133 | break; |
| 134 | case KUBEVIRT_API_CONFIG_CREATED: |
| 135 | case KUBEVIRT_API_CONFIG_REMOVED: |
| 136 | default: |
| 137 | // do nothing |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | private void processConfigUpdating() { |
| 143 | if (!isRelevantHelper()) { |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | instantiateNodeWatcher(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | private class InternalKubevirtNodeWatcher implements Watcher<Node> { |
| 152 | |
| 153 | @Override |
| 154 | public void eventReceived(Action action, Node node) { |
| 155 | switch (action) { |
| 156 | case ADDED: |
| 157 | eventExecutor.execute(() -> processAddition(node)); |
| 158 | break; |
| 159 | case MODIFIED: |
| 160 | eventExecutor.execute(() -> processModification(node)); |
| 161 | break; |
| 162 | case DELETED: |
| 163 | eventExecutor.execute(() -> processDeletion(node)); |
| 164 | break; |
| 165 | case ERROR: |
| 166 | log.warn("Failures processing node manipulation."); |
| 167 | break; |
| 168 | default: |
| 169 | // do nothing |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public void onClose(WatcherException e) { |
| 176 | // due to the bugs in fabric8, node watcher might be closed, |
| 177 | // we will re-instantiate the node watcher in this case |
| 178 | // FIXME: https://github.com/fabric8io/kubernetes-client/issues/2135 |
Jian Li | c9e2924 | 2022-12-20 15:07:04 +0900 | [diff] [blame^] | 179 | log.info("Node watcher OnClose, re-instantiate the node watcher..."); |
Jian Li | 4b24970 | 2021-02-19 18:13:10 +0900 | [diff] [blame] | 180 | instantiateNodeWatcher(); |
| 181 | } |
| 182 | |
| 183 | private void processAddition(Node node) { |
| 184 | if (!isMaster()) { |
| 185 | return; |
| 186 | } |
| 187 | |
Jian Li | 4b24970 | 2021-02-19 18:13:10 +0900 | [diff] [blame] | 188 | KubevirtNode kubevirtNode = buildKubevirtNode(node); |
Daniel Park | d846cb7 | 2022-09-26 22:58:53 +0900 | [diff] [blame] | 189 | log.info("buildKubevirtNode: {}", kubevirtNode); |
Daniel Park | 515f5f3 | 2021-02-22 17:12:20 +0900 | [diff] [blame] | 190 | if (kubevirtNode.type() == WORKER || kubevirtNode.type() == GATEWAY) { |
Jian Li | 4b24970 | 2021-02-19 18:13:10 +0900 | [diff] [blame] | 191 | if (!kubevirtNodeAdminService.hasNode(kubevirtNode.hostname())) { |
| 192 | kubevirtNodeAdminService.createNode(kubevirtNode); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | private void processModification(Node node) { |
| 198 | if (!isMaster()) { |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | log.trace("Process node {} updating event from API server.", |
| 203 | node.getMetadata().getName()); |
| 204 | |
Jian Li | 517597a | 2021-03-22 11:04:52 +0900 | [diff] [blame] | 205 | KubevirtNode original = buildKubevirtNode(node); |
Jian Li | 4b24970 | 2021-02-19 18:13:10 +0900 | [diff] [blame] | 206 | KubevirtNode existing = kubevirtNodeAdminService.node(node.getMetadata().getName()); |
| 207 | |
Jian Li | 517597a | 2021-03-22 11:04:52 +0900 | [diff] [blame] | 208 | // if a master node is annotated as a gateway node, we simply add |
| 209 | // the node into the cluster |
| 210 | if (original.type() == GATEWAY && existing == null) { |
| 211 | kubevirtNodeAdminService.createNode(original); |
| 212 | } |
Jian Li | 4b24970 | 2021-02-19 18:13:10 +0900 | [diff] [blame] | 213 | |
Jian Li | 517597a | 2021-03-22 11:04:52 +0900 | [diff] [blame] | 214 | // if a gateway annotation removed from the master node, we simply remove |
| 215 | // the node from the cluster |
| 216 | if (original.type() == MASTER && existing != null && existing.type() == GATEWAY) { |
| 217 | kubevirtNodeAdminService.removeNode(original.hostname()); |
| 218 | } |
| 219 | |
| 220 | if (existing != null) { |
Jian Li | 4b24970 | 2021-02-19 18:13:10 +0900 | [diff] [blame] | 221 | // we update the kubevirt node and re-run bootstrapping, |
Jian Li | 517597a | 2021-03-22 11:04:52 +0900 | [diff] [blame] | 222 | // if the updated node has different phyInts and data IP |
Jian Li | 4b24970 | 2021-02-19 18:13:10 +0900 | [diff] [blame] | 223 | // this means we assume that the node's hostname, type and mgmt IP |
| 224 | // are immutable |
Jian Li | 517597a | 2021-03-22 11:04:52 +0900 | [diff] [blame] | 225 | if (!original.phyIntfs().equals(existing.phyIntfs()) || |
| 226 | !original.dataIp().equals(existing.dataIp())) { |
| 227 | kubevirtNodeAdminService.updateNode(original.updateState(INIT)); |
Jian Li | 4b24970 | 2021-02-19 18:13:10 +0900 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | private void processDeletion(Node node) { |
| 233 | if (!isMaster()) { |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | log.trace("Process node {} removal event from API server.", |
| 238 | node.getMetadata().getName()); |
| 239 | |
| 240 | KubevirtNode existing = kubevirtNodeAdminService.node(node.getMetadata().getName()); |
| 241 | |
| 242 | if (existing != null) { |
| 243 | kubevirtNodeAdminService.removeNode(node.getMetadata().getName()); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | private boolean isMaster() { |
| 248 | return Objects.equals(localNodeId, leadershipService.getLeader(appId.name())); |
| 249 | } |
| 250 | } |
| 251 | } |