Jian Li | 8538773 | 2019-02-19 23:56:18 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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.k8snetworking.impl; |
| 17 | |
| 18 | import io.fabric8.kubernetes.api.model.Endpoints; |
| 19 | import io.fabric8.kubernetes.client.KubernetesClient; |
| 20 | import io.fabric8.kubernetes.client.KubernetesClientException; |
| 21 | import io.fabric8.kubernetes.client.Watcher; |
| 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.k8snetworking.api.K8sEndpointsAdminService; |
Jian Li | 3d1111e | 2019-02-22 02:02:13 +0900 | [diff] [blame] | 28 | import org.onosproject.k8snode.api.K8sApiConfigEvent; |
| 29 | import org.onosproject.k8snode.api.K8sApiConfigListener; |
Jian Li | 8538773 | 2019-02-19 23:56:18 +0900 | [diff] [blame] | 30 | import org.onosproject.k8snode.api.K8sApiConfigService; |
| 31 | import org.onosproject.mastership.MastershipService; |
| 32 | import org.osgi.service.component.annotations.Activate; |
| 33 | import org.osgi.service.component.annotations.Component; |
| 34 | import org.osgi.service.component.annotations.Deactivate; |
| 35 | import org.osgi.service.component.annotations.Reference; |
| 36 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 37 | import org.slf4j.Logger; |
| 38 | |
| 39 | import java.util.Objects; |
| 40 | import java.util.concurrent.ExecutorService; |
| 41 | |
| 42 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 43 | import static org.onlab.util.Tools.groupedThreads; |
| 44 | import static org.onosproject.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID; |
| 45 | import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.k8sClient; |
| 46 | import static org.slf4j.LoggerFactory.getLogger; |
| 47 | |
| 48 | /** |
| 49 | * Kubernetes endpoints watcher used for feeding endpoints information. |
| 50 | */ |
| 51 | @Component(immediate = true) |
| 52 | public class K8sEndpointsWatcher { |
| 53 | |
| 54 | private final Logger log = getLogger(getClass()); |
| 55 | |
| 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 57 | protected CoreService coreService; |
| 58 | |
| 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 60 | protected MastershipService mastershipService; |
| 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 63 | protected ClusterService clusterService; |
| 64 | |
| 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 66 | protected LeadershipService leadershipService; |
| 67 | |
| 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 69 | protected K8sEndpointsAdminService k8sEndpointsAdminService; |
| 70 | |
| 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 72 | protected K8sApiConfigService k8sApiConfigService; |
| 73 | |
| 74 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 75 | groupedThreads(this.getClass().getSimpleName(), "event-handler")); |
| 76 | private final Watcher<Endpoints> |
| 77 | internalEndpointsWatcher = new InternalK8sEndpointsWatcher(); |
Jian Li | 3d1111e | 2019-02-22 02:02:13 +0900 | [diff] [blame] | 78 | private final InternalK8sApiConfigListener |
| 79 | internalK8sApiConfigListener = new InternalK8sApiConfigListener(); |
Jian Li | 8538773 | 2019-02-19 23:56:18 +0900 | [diff] [blame] | 80 | |
| 81 | private ApplicationId appId; |
| 82 | private NodeId localNodeId; |
| 83 | |
| 84 | @Activate |
| 85 | protected void activate() { |
| 86 | appId = coreService.registerApplication(K8S_NETWORKING_APP_ID); |
| 87 | localNodeId = clusterService.getLocalNode().id(); |
| 88 | leadershipService.runForLeadership(appId.name()); |
Jian Li | 3d1111e | 2019-02-22 02:02:13 +0900 | [diff] [blame] | 89 | k8sApiConfigService.addListener(internalK8sApiConfigListener); |
Jian Li | 8538773 | 2019-02-19 23:56:18 +0900 | [diff] [blame] | 90 | |
| 91 | log.info("Started"); |
| 92 | } |
| 93 | |
| 94 | @Deactivate |
| 95 | protected void deactivate() { |
Jian Li | 3d1111e | 2019-02-22 02:02:13 +0900 | [diff] [blame] | 96 | k8sApiConfigService.removeListener(internalK8sApiConfigListener); |
Jian Li | 8538773 | 2019-02-19 23:56:18 +0900 | [diff] [blame] | 97 | leadershipService.withdraw(appId.name()); |
| 98 | eventExecutor.shutdown(); |
| 99 | |
| 100 | log.info("Stopped"); |
| 101 | } |
| 102 | |
Jian Li | 3d1111e | 2019-02-22 02:02:13 +0900 | [diff] [blame] | 103 | private class InternalK8sApiConfigListener implements K8sApiConfigListener { |
| 104 | |
| 105 | private boolean isRelevantHelper() { |
| 106 | return Objects.equals(localNodeId, leadershipService.getLeader(appId.name())); |
Jian Li | 8538773 | 2019-02-19 23:56:18 +0900 | [diff] [blame] | 107 | } |
| 108 | |
Jian Li | 3d1111e | 2019-02-22 02:02:13 +0900 | [diff] [blame] | 109 | @Override |
| 110 | public void event(K8sApiConfigEvent event) { |
Jian Li | 8538773 | 2019-02-19 23:56:18 +0900 | [diff] [blame] | 111 | |
Jian Li | 3d1111e | 2019-02-22 02:02:13 +0900 | [diff] [blame] | 112 | switch (event.type()) { |
| 113 | case K8S_API_CONFIG_UPDATED: |
| 114 | eventExecutor.execute(this::processConfigUpdating); |
| 115 | break; |
| 116 | case K8S_API_CONFIG_CREATED: |
| 117 | case K8S_API_CONFIG_REMOVED: |
| 118 | default: |
| 119 | // do nothing |
| 120 | break; |
| 121 | } |
Jian Li | 8538773 | 2019-02-19 23:56:18 +0900 | [diff] [blame] | 122 | } |
| 123 | |
Jian Li | 3d1111e | 2019-02-22 02:02:13 +0900 | [diff] [blame] | 124 | private void processConfigUpdating() { |
| 125 | if (!isRelevantHelper()) { |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | KubernetesClient client = k8sClient(k8sApiConfigService); |
| 130 | |
| 131 | if (client != null) { |
Jian Li | af08152 | 2019-06-19 20:53:07 +0900 | [diff] [blame] | 132 | client.endpoints().inAnyNamespace().watch(internalEndpointsWatcher); |
Jian Li | 3d1111e | 2019-02-22 02:02:13 +0900 | [diff] [blame] | 133 | } |
| 134 | } |
Jian Li | 8538773 | 2019-02-19 23:56:18 +0900 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | private class InternalK8sEndpointsWatcher implements Watcher<Endpoints> { |
| 138 | |
| 139 | @Override |
| 140 | public void eventReceived(Action action, Endpoints endpoints) { |
| 141 | switch (action) { |
| 142 | case ADDED: |
| 143 | eventExecutor.execute(() -> processAddition(endpoints)); |
| 144 | break; |
| 145 | case MODIFIED: |
Jian Li | 4a7ce67 | 2019-04-09 15:20:25 +0900 | [diff] [blame] | 146 | eventExecutor.execute(() -> processModification(endpoints)); |
Jian Li | 8538773 | 2019-02-19 23:56:18 +0900 | [diff] [blame] | 147 | break; |
| 148 | case DELETED: |
| 149 | eventExecutor.execute(() -> processDeletion(endpoints)); |
| 150 | break; |
| 151 | case ERROR: |
| 152 | log.warn("Failures processing endpoints manipulation."); |
| 153 | break; |
| 154 | default: |
| 155 | // do nothing |
| 156 | break; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void onClose(KubernetesClientException e) { |
Jian Li | 003d569 | 2019-08-26 23:19:38 +0900 | [diff] [blame] | 162 | log.warn("Endpoints watcher OnClose", e); |
Jian Li | 8538773 | 2019-02-19 23:56:18 +0900 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | private void processAddition(Endpoints endpoints) { |
| 166 | if (!isMaster()) { |
| 167 | return; |
| 168 | } |
| 169 | |
Jian Li | 3d1111e | 2019-02-22 02:02:13 +0900 | [diff] [blame] | 170 | log.trace("Process endpoints {} creating event from API server.", |
Jian Li | 8538773 | 2019-02-19 23:56:18 +0900 | [diff] [blame] | 171 | endpoints.getMetadata().getName()); |
| 172 | |
Jian Li | 8108f22 | 2019-12-24 15:58:25 +0900 | [diff] [blame] | 173 | if (k8sEndpointsAdminService.endpoints( |
| 174 | endpoints.getMetadata().getUid()) == null) { |
| 175 | k8sEndpointsAdminService.createEndpoints(endpoints); |
| 176 | } |
Jian Li | 8538773 | 2019-02-19 23:56:18 +0900 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | private void processModification(Endpoints endpoints) { |
| 180 | if (!isMaster()) { |
| 181 | return; |
| 182 | } |
| 183 | |
Jian Li | 3d1111e | 2019-02-22 02:02:13 +0900 | [diff] [blame] | 184 | log.trace("Process endpoints {} updating event from API server.", |
Jian Li | 8538773 | 2019-02-19 23:56:18 +0900 | [diff] [blame] | 185 | endpoints.getMetadata().getName()); |
| 186 | |
Jian Li | 3d1111e | 2019-02-22 02:02:13 +0900 | [diff] [blame] | 187 | if (k8sEndpointsAdminService.endpoints( |
| 188 | endpoints.getMetadata().getUid()) != null) { |
| 189 | k8sEndpointsAdminService.updateEndpoints(endpoints); |
| 190 | } |
Jian Li | 8538773 | 2019-02-19 23:56:18 +0900 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | private void processDeletion(Endpoints endpoints) { |
| 194 | if (!isMaster()) { |
| 195 | return; |
| 196 | } |
| 197 | |
Jian Li | 3d1111e | 2019-02-22 02:02:13 +0900 | [diff] [blame] | 198 | log.trace("Process endpoints {} removal event from API server.", |
Jian Li | 8538773 | 2019-02-19 23:56:18 +0900 | [diff] [blame] | 199 | endpoints.getMetadata().getName()); |
| 200 | |
| 201 | k8sEndpointsAdminService.removeEndpoints(endpoints.getMetadata().getUid()); |
| 202 | } |
| 203 | |
| 204 | private boolean isMaster() { |
| 205 | return Objects.equals(localNodeId, leadershipService.getLeader(appId.name())); |
| 206 | } |
| 207 | } |
| 208 | } |