Jian Li | 9b19916 | 2019-02-10 18:00:35 +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 org.onlab.packet.IpAddress; |
| 19 | import org.onosproject.cluster.ClusterService; |
| 20 | import org.onosproject.cluster.LeadershipService; |
| 21 | import org.onosproject.cluster.NodeId; |
| 22 | import org.onosproject.core.ApplicationId; |
| 23 | import org.onosproject.core.CoreService; |
| 24 | import org.onosproject.k8snetworking.api.K8sIpamAdminService; |
| 25 | import org.onosproject.k8snetworking.api.K8sNetworkEvent; |
| 26 | import org.onosproject.k8snetworking.api.K8sNetworkListener; |
| 27 | import org.onosproject.k8snetworking.api.K8sNetworkService; |
| 28 | import org.onosproject.mastership.MastershipService; |
| 29 | import org.osgi.service.component.annotations.Activate; |
| 30 | import org.osgi.service.component.annotations.Component; |
| 31 | import org.osgi.service.component.annotations.Deactivate; |
| 32 | import org.osgi.service.component.annotations.Reference; |
| 33 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 34 | import org.slf4j.Logger; |
| 35 | |
| 36 | import java.util.Objects; |
| 37 | import java.util.Set; |
| 38 | import java.util.concurrent.ExecutorService; |
| 39 | |
| 40 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 41 | import static org.onlab.util.Tools.groupedThreads; |
| 42 | import static org.onosproject.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID; |
| 43 | import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.getSubnetIps; |
| 44 | import static org.slf4j.LoggerFactory.getLogger; |
| 45 | |
| 46 | /** |
| 47 | * Initializes and purges the kubernetes IPAM. |
| 48 | */ |
| 49 | @Component(immediate = true) |
| 50 | public class K8sIpamHandler { |
| 51 | |
| 52 | private final Logger log = getLogger(getClass()); |
| 53 | |
| 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 55 | protected CoreService coreService; |
| 56 | |
| 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 58 | protected MastershipService mastershipService; |
| 59 | |
| 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 61 | protected ClusterService clusterService; |
| 62 | |
| 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 64 | protected LeadershipService leadershipService; |
| 65 | |
| 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 67 | protected K8sNetworkService k8sNetworkService; |
| 68 | |
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 70 | protected K8sIpamAdminService k8sIpamAdminService; |
| 71 | |
| 72 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 73 | groupedThreads(this.getClass().getSimpleName(), "event-handler")); |
| 74 | private final InternalK8sNetworkListener k8sNetworkListener = |
| 75 | new InternalK8sNetworkListener(); |
| 76 | |
| 77 | private ApplicationId appId; |
| 78 | private NodeId localNodeId; |
| 79 | |
| 80 | @Activate |
| 81 | protected void activate() { |
| 82 | appId = coreService.registerApplication(K8S_NETWORKING_APP_ID); |
| 83 | localNodeId = clusterService.getLocalNode().id(); |
| 84 | leadershipService.runForLeadership(appId.name()); |
| 85 | k8sNetworkService.addListener(k8sNetworkListener); |
| 86 | |
| 87 | log.info("Started"); |
| 88 | } |
| 89 | |
| 90 | @Deactivate |
| 91 | protected void deactivate() { |
| 92 | k8sNetworkService.removeListener(k8sNetworkListener); |
| 93 | leadershipService.withdraw(appId.name()); |
| 94 | eventExecutor.shutdown(); |
| 95 | |
| 96 | log.info("Stopped"); |
| 97 | } |
| 98 | |
| 99 | private class InternalK8sNetworkListener implements K8sNetworkListener { |
| 100 | |
| 101 | private boolean isRelevantHelper() { |
| 102 | return Objects.equals(localNodeId, leadershipService.getLeader(appId.name())); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void event(K8sNetworkEvent event) { |
| 107 | switch (event.type()) { |
| 108 | case K8S_NETWORK_CREATED: |
| 109 | eventExecutor.execute(() -> processNetworkAddition(event)); |
| 110 | break; |
| 111 | case K8S_NETWORK_REMOVED: |
| 112 | eventExecutor.execute(() -> processNetworkRemoval(event)); |
| 113 | break; |
| 114 | default: |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | private void processNetworkAddition(K8sNetworkEvent event) { |
| 120 | if (!isRelevantHelper()) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | Set<IpAddress> ips = getSubnetIps(event.subject().cidr()); |
| 125 | k8sIpamAdminService.initializeIpPool(event.subject().networkId(), ips); |
| 126 | } |
| 127 | |
| 128 | private void processNetworkRemoval(K8sNetworkEvent event) { |
| 129 | if (!isRelevantHelper()) { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | k8sIpamAdminService.purgeIpPool(event.subject().networkId()); |
| 134 | } |
| 135 | } |
| 136 | } |