blob: b4b5da45dbc858bc375be70e921925ca64749be8 [file] [log] [blame]
Jian Li85387732019-02-19 23:56:18 +09001/*
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 */
16package org.onosproject.k8snetworking.impl;
17
18import io.fabric8.kubernetes.api.model.Service;
19import io.fabric8.kubernetes.client.KubernetesClient;
Jian Li85387732019-02-19 23:56:18 +090020import io.fabric8.kubernetes.client.Watcher;
Jian Li9ac73952021-01-15 14:53:22 +090021import io.fabric8.kubernetes.client.WatcherException;
Jian Li85387732019-02-19 23:56:18 +090022import org.onosproject.cluster.ClusterService;
23import org.onosproject.cluster.LeadershipService;
24import org.onosproject.cluster.NodeId;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
27import org.onosproject.k8snetworking.api.K8sServiceAdminService;
Jian Li3d1111e2019-02-22 02:02:13 +090028import org.onosproject.k8snode.api.K8sApiConfigEvent;
29import org.onosproject.k8snode.api.K8sApiConfigListener;
Jian Li85387732019-02-19 23:56:18 +090030import org.onosproject.k8snode.api.K8sApiConfigService;
31import org.onosproject.mastership.MastershipService;
32import org.osgi.service.component.annotations.Activate;
33import org.osgi.service.component.annotations.Component;
34import org.osgi.service.component.annotations.Deactivate;
35import org.osgi.service.component.annotations.Reference;
36import org.osgi.service.component.annotations.ReferenceCardinality;
37import org.slf4j.Logger;
38
39import java.util.Objects;
40import java.util.concurrent.ExecutorService;
41
42import static java.util.concurrent.Executors.newSingleThreadExecutor;
43import static org.onlab.util.Tools.groupedThreads;
44import static org.onosproject.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID;
45import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.k8sClient;
46import static org.slf4j.LoggerFactory.getLogger;
47
48/**
49 * Kubernetes service watcher used for feeding service information.
50 */
51@Component(immediate = true)
52public class K8sServiceWatcher {
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 K8sServiceAdminService k8sServiceAdminService;
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
77 private final InternalK8sServiceWatcher
78 internalK8sServiceWatcher = new InternalK8sServiceWatcher();
Jian Li3d1111e2019-02-22 02:02:13 +090079 private final InternalK8sApiConfigListener
80 internalK8sApiConfigListener = new InternalK8sApiConfigListener();
Jian Li85387732019-02-19 23:56:18 +090081
82 private ApplicationId appId;
83 private NodeId localNodeId;
84
85 @Activate
86 protected void activate() {
87 appId = coreService.registerApplication(K8S_NETWORKING_APP_ID);
88 localNodeId = clusterService.getLocalNode().id();
89 leadershipService.runForLeadership(appId.name());
Jian Li3d1111e2019-02-22 02:02:13 +090090 k8sApiConfigService.addListener(internalK8sApiConfigListener);
Jian Li85387732019-02-19 23:56:18 +090091
92 log.info("Started");
93 }
94
95 @Deactivate
96 protected void deactivate() {
Jian Li3d1111e2019-02-22 02:02:13 +090097 k8sApiConfigService.removeListener(internalK8sApiConfigListener);
Jian Li85387732019-02-19 23:56:18 +090098 leadershipService.withdraw(appId.name());
99 eventExecutor.shutdown();
100
101 log.info("Stopped");
102 }
103
Jian Li3d1111e2019-02-22 02:02:13 +0900104 private class InternalK8sApiConfigListener implements K8sApiConfigListener {
105
106 private boolean isRelevantHelper() {
107 return Objects.equals(localNodeId, leadershipService.getLeader(appId.name()));
Jian Li85387732019-02-19 23:56:18 +0900108 }
109
Jian Li3d1111e2019-02-22 02:02:13 +0900110 @Override
111 public void event(K8sApiConfigEvent event) {
Jian Li85387732019-02-19 23:56:18 +0900112
Jian Li3d1111e2019-02-22 02:02:13 +0900113 switch (event.type()) {
114 case K8S_API_CONFIG_UPDATED:
115 eventExecutor.execute(this::processConfigUpdating);
116 break;
117 case K8S_API_CONFIG_CREATED:
118 case K8S_API_CONFIG_REMOVED:
119 default:
120 // do nothing
121 break;
122 }
Jian Li85387732019-02-19 23:56:18 +0900123 }
124
Jian Li3d1111e2019-02-22 02:02:13 +0900125 private void processConfigUpdating() {
126 if (!isRelevantHelper()) {
127 return;
128 }
129
130 KubernetesClient client = k8sClient(k8sApiConfigService);
131
132 if (client != null) {
Jian Liaf081522019-06-19 20:53:07 +0900133 client.services().inAnyNamespace().watch(internalK8sServiceWatcher);
Jian Li3d1111e2019-02-22 02:02:13 +0900134 }
135 }
Jian Li85387732019-02-19 23:56:18 +0900136 }
137
138 private class InternalK8sServiceWatcher implements Watcher<Service> {
139
140 @Override
141 public void eventReceived(Action action, Service service) {
142 switch (action) {
143 case ADDED:
144 eventExecutor.execute(() -> processAddition(service));
145 break;
146 case MODIFIED:
147 eventExecutor.execute(() -> processModification(service));
148 break;
149 case DELETED:
150 eventExecutor.execute(() -> processDeletion(service));
151 break;
152 case ERROR:
153 log.warn("Failures processing service manipulation.");
154 break;
155 default:
156 // do nothing
157 break;
158 }
159 }
160
161 @Override
Jian Li9ac73952021-01-15 14:53:22 +0900162 public void onClose(WatcherException e) {
Jian Li1ea027142019-08-26 23:19:38 +0900163 log.warn("Service watcher OnClose", e);
Jian Li85387732019-02-19 23:56:18 +0900164 }
165
166 private void processAddition(Service service) {
167 if (!isMaster()) {
168 return;
169 }
170
Jian Li3d1111e2019-02-22 02:02:13 +0900171 log.trace("Process service {} creating event from API server.",
Jian Li85387732019-02-19 23:56:18 +0900172 service.getMetadata().getName());
173
Jian Li8143c3b2019-12-24 15:58:25 +0900174 if (k8sServiceAdminService.service(
175 service.getMetadata().getUid()) == null) {
176 k8sServiceAdminService.createService(service);
177 }
Jian Li85387732019-02-19 23:56:18 +0900178 }
179
180 private void processModification(Service service) {
181 if (!isMaster()) {
182 return;
183 }
184
Jian Li3d1111e2019-02-22 02:02:13 +0900185 log.trace("Process service {} updating event from API server.",
Jian Li85387732019-02-19 23:56:18 +0900186 service.getMetadata().getName());
187
Jian Li3d1111e2019-02-22 02:02:13 +0900188 if (k8sServiceAdminService.service(
189 service.getMetadata().getUid()) != null) {
190 k8sServiceAdminService.updateService(service);
191 }
Jian Li85387732019-02-19 23:56:18 +0900192 }
193
194 private void processDeletion(Service service) {
195 if (!isMaster()) {
196 return;
197 }
198
Jian Li3d1111e2019-02-22 02:02:13 +0900199 log.trace("Process service {} removal event from API server.",
Jian Li85387732019-02-19 23:56:18 +0900200 service.getMetadata().getName());
201
202 k8sServiceAdminService.removeService(service.getMetadata().getUid());
203 }
204
205 private boolean isMaster() {
206 return Objects.equals(localNodeId, leadershipService.getLeader(appId.name()));
207 }
208 }
209}