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.Service; |
| 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.K8sServiceAdminService; |
| 28 | import org.onosproject.k8snode.api.K8sApiConfig; |
| 29 | import org.onosproject.k8snode.api.K8sApiConfigService; |
| 30 | import org.onosproject.mastership.MastershipService; |
| 31 | import org.osgi.service.component.annotations.Activate; |
| 32 | import org.osgi.service.component.annotations.Component; |
| 33 | import org.osgi.service.component.annotations.Deactivate; |
| 34 | import org.osgi.service.component.annotations.Reference; |
| 35 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 36 | import org.slf4j.Logger; |
| 37 | |
| 38 | import java.util.Objects; |
| 39 | import java.util.concurrent.ExecutorService; |
| 40 | |
| 41 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 42 | import static org.onlab.util.Tools.groupedThreads; |
| 43 | import static org.onosproject.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID; |
| 44 | import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.k8sClient; |
| 45 | import static org.slf4j.LoggerFactory.getLogger; |
| 46 | |
| 47 | /** |
| 48 | * Kubernetes service watcher used for feeding service information. |
| 49 | */ |
| 50 | @Component(immediate = true) |
| 51 | public class K8sServiceWatcher { |
| 52 | |
| 53 | private final Logger log = getLogger(getClass()); |
| 54 | |
| 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 56 | protected CoreService coreService; |
| 57 | |
| 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 59 | protected MastershipService mastershipService; |
| 60 | |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 62 | protected ClusterService clusterService; |
| 63 | |
| 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 65 | protected LeadershipService leadershipService; |
| 66 | |
| 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 68 | protected K8sServiceAdminService k8sServiceAdminService; |
| 69 | |
| 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 71 | protected K8sApiConfigService k8sApiConfigService; |
| 72 | |
| 73 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 74 | groupedThreads(this.getClass().getSimpleName(), "event-handler")); |
| 75 | |
| 76 | private final InternalK8sServiceWatcher |
| 77 | internalK8sServiceWatcher = new InternalK8sServiceWatcher(); |
| 78 | |
| 79 | private ApplicationId appId; |
| 80 | private NodeId localNodeId; |
| 81 | |
| 82 | @Activate |
| 83 | protected void activate() { |
| 84 | appId = coreService.registerApplication(K8S_NETWORKING_APP_ID); |
| 85 | localNodeId = clusterService.getLocalNode().id(); |
| 86 | leadershipService.runForLeadership(appId.name()); |
| 87 | |
| 88 | initWatcher(); |
| 89 | |
| 90 | log.info("Started"); |
| 91 | } |
| 92 | |
| 93 | @Deactivate |
| 94 | protected void deactivate() { |
| 95 | leadershipService.withdraw(appId.name()); |
| 96 | eventExecutor.shutdown(); |
| 97 | |
| 98 | log.info("Stopped"); |
| 99 | } |
| 100 | |
| 101 | private void initWatcher() { |
| 102 | K8sApiConfig config = |
| 103 | k8sApiConfigService.apiConfigs().stream().findAny().orElse(null); |
| 104 | if (config == null) { |
| 105 | log.error("Failed to find valid kubernetes API configuration."); |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | KubernetesClient client = k8sClient(config); |
| 110 | |
| 111 | if (client == null) { |
| 112 | log.error("Failed to connect to kubernetes API server."); |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | client.services().watch(internalK8sServiceWatcher); |
| 117 | } |
| 118 | |
| 119 | private class InternalK8sServiceWatcher implements Watcher<Service> { |
| 120 | |
| 121 | @Override |
| 122 | public void eventReceived(Action action, Service service) { |
| 123 | switch (action) { |
| 124 | case ADDED: |
| 125 | eventExecutor.execute(() -> processAddition(service)); |
| 126 | break; |
| 127 | case MODIFIED: |
| 128 | eventExecutor.execute(() -> processModification(service)); |
| 129 | break; |
| 130 | case DELETED: |
| 131 | eventExecutor.execute(() -> processDeletion(service)); |
| 132 | break; |
| 133 | case ERROR: |
| 134 | log.warn("Failures processing service manipulation."); |
| 135 | break; |
| 136 | default: |
| 137 | // do nothing |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void onClose(KubernetesClientException e) { |
| 144 | log.info("Service watcher OnClose: {}" + e); |
| 145 | } |
| 146 | |
| 147 | private void processAddition(Service service) { |
| 148 | if (!isMaster()) { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | log.info("Process service {} creating event from API server.", |
| 153 | service.getMetadata().getName()); |
| 154 | |
| 155 | k8sServiceAdminService.createService(service); |
| 156 | } |
| 157 | |
| 158 | private void processModification(Service service) { |
| 159 | if (!isMaster()) { |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | log.info("Process service {} updating event from API server.", |
| 164 | service.getMetadata().getName()); |
| 165 | |
| 166 | k8sServiceAdminService.updateService(service); |
| 167 | } |
| 168 | |
| 169 | private void processDeletion(Service service) { |
| 170 | if (!isMaster()) { |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | log.info("Process service {} removal event from API server.", |
| 175 | service.getMetadata().getName()); |
| 176 | |
| 177 | k8sServiceAdminService.removeService(service.getMetadata().getUid()); |
| 178 | } |
| 179 | |
| 180 | private boolean isMaster() { |
| 181 | return Objects.equals(localNodeId, leadershipService.getLeader(appId.name())); |
| 182 | } |
| 183 | } |
| 184 | } |