Jian Li | 1cee988 | 2019-02-13 11:25:25 +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.k8snode.impl; |
| 17 | |
Jian Li | 077b07e | 2020-09-01 16:55:25 +0900 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 19 | import io.fabric8.kubernetes.api.model.Node; |
| 20 | import io.fabric8.kubernetes.api.model.NodeAddress; |
| 21 | import io.fabric8.kubernetes.client.KubernetesClient; |
| 22 | import org.onlab.packet.IpAddress; |
Jian Li | c2242bd | 2020-09-03 13:12:14 +0900 | [diff] [blame] | 23 | import org.onlab.packet.MacAddress; |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 24 | import org.onosproject.cluster.ClusterService; |
| 25 | import org.onosproject.cluster.LeadershipService; |
| 26 | import org.onosproject.cluster.NodeId; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreService; |
Jian Li | 077b07e | 2020-09-01 16:55:25 +0900 | [diff] [blame] | 29 | import org.onosproject.k8snode.api.DefaultK8sHost; |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 30 | import org.onosproject.k8snode.api.DefaultK8sNode; |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 31 | import org.onosproject.k8snode.api.ExternalNetworkService; |
| 32 | import org.onosproject.k8snode.api.HostNodesInfo; |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 33 | import org.onosproject.k8snode.api.K8sApiConfig; |
| 34 | import org.onosproject.k8snode.api.K8sApiConfigAdminService; |
| 35 | import org.onosproject.k8snode.api.K8sApiConfigEvent; |
| 36 | import org.onosproject.k8snode.api.K8sApiConfigListener; |
Jian Li | 077b07e | 2020-09-01 16:55:25 +0900 | [diff] [blame] | 37 | import org.onosproject.k8snode.api.K8sHost; |
| 38 | import org.onosproject.k8snode.api.K8sHostAdminService; |
| 39 | import org.onosproject.k8snode.api.K8sHostState; |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 40 | import org.onosproject.k8snode.api.K8sNode; |
| 41 | import org.onosproject.k8snode.api.K8sNodeAdminService; |
Jian Li | 019ce6a | 2020-09-09 10:23:21 +0900 | [diff] [blame] | 42 | import org.onosproject.k8snode.api.K8sRouterBridge; |
Jian Li | 077b07e | 2020-09-01 16:55:25 +0900 | [diff] [blame] | 43 | import org.onosproject.k8snode.api.K8sTunnelBridge; |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 44 | import org.osgi.service.component.annotations.Activate; |
| 45 | import org.osgi.service.component.annotations.Component; |
| 46 | import org.osgi.service.component.annotations.Deactivate; |
| 47 | import org.osgi.service.component.annotations.Reference; |
| 48 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 49 | import org.slf4j.Logger; |
| 50 | |
Jian Li | 0c63272 | 2019-05-08 15:58:04 +0900 | [diff] [blame] | 51 | import java.util.Map; |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 52 | import java.util.Objects; |
| 53 | import java.util.concurrent.ExecutorService; |
| 54 | |
Jian Li | 077b07e | 2020-09-01 16:55:25 +0900 | [diff] [blame] | 55 | import static java.lang.Thread.sleep; |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 56 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 57 | import static org.onlab.util.Tools.groupedThreads; |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 58 | import static org.onosproject.k8snode.api.Constants.DEFAULT_CLUSTER_NAME; |
Jian Li | c2242bd | 2020-09-03 13:12:14 +0900 | [diff] [blame] | 59 | import static org.onosproject.k8snode.api.Constants.DEFAULT_EXTERNAL_GATEWAY_MAC; |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 60 | import static org.onosproject.k8snode.api.Constants.EXTERNAL_TO_ROUTER; |
| 61 | import static org.onosproject.k8snode.api.K8sApiConfig.Mode.PASSTHROUGH; |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 62 | import static org.onosproject.k8snode.api.K8sNode.Type.MASTER; |
| 63 | import static org.onosproject.k8snode.api.K8sNode.Type.MINION; |
| 64 | import static org.onosproject.k8snode.api.K8sNodeService.APP_ID; |
Jian Li | 732c342 | 2020-09-07 17:01:11 +0900 | [diff] [blame] | 65 | import static org.onosproject.k8snode.api.K8sNodeState.ON_BOARDED; |
Jian Li | 0ee8d0e | 2019-12-18 11:35:05 +0900 | [diff] [blame] | 66 | import static org.onosproject.k8snode.api.K8sNodeState.PRE_ON_BOARD; |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 67 | import static org.onosproject.k8snode.util.K8sNodeUtil.k8sClient; |
| 68 | import static org.slf4j.LoggerFactory.getLogger; |
| 69 | |
| 70 | /** |
| 71 | * Handles the state of kubernetes API server configuration. |
| 72 | */ |
| 73 | @Component(immediate = true) |
| 74 | public class DefaultK8sApiConfigHandler { |
| 75 | |
| 76 | private final Logger log = getLogger(getClass()); |
| 77 | |
| 78 | private static final String INTERNAL_IP = "InternalIP"; |
| 79 | private static final String K8S_ROLE = "node-role.kubernetes.io"; |
Jian Li | 0c63272 | 2019-05-08 15:58:04 +0900 | [diff] [blame] | 80 | private static final String EXT_BRIDGE_IP = "external.bridge.ip"; |
| 81 | private static final String EXT_GATEWAY_IP = "external.gateway.ip"; |
| 82 | private static final String EXT_INTF_NAME = "external.interface.name"; |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 83 | |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 84 | private static final String DEFAULT_GATEWAY_IP = "127.0.0.1"; |
| 85 | private static final String DEFAULT_BRIDGE_IP = "127.0.0.1"; |
| 86 | |
Jian Li | 732c342 | 2020-09-07 17:01:11 +0900 | [diff] [blame] | 87 | private static final long SLEEP_MS = 10000; // we wait 10s |
Jian Li | 077b07e | 2020-09-01 16:55:25 +0900 | [diff] [blame] | 88 | |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 90 | protected CoreService coreService; |
| 91 | |
| 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 93 | protected LeadershipService leadershipService; |
| 94 | |
| 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 96 | protected ClusterService clusterService; |
| 97 | |
| 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 99 | protected K8sApiConfigAdminService k8sApiConfigAdminService; |
| 100 | |
| 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 102 | protected K8sNodeAdminService k8sNodeAdminService; |
| 103 | |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 077b07e | 2020-09-01 16:55:25 +0900 | [diff] [blame] | 105 | protected K8sHostAdminService k8sHostAdminService; |
| 106 | |
| 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 108 | protected ExternalNetworkService extNetworkService; |
| 109 | |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 110 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 111 | groupedThreads(this.getClass().getSimpleName(), "event-handler", log)); |
| 112 | |
| 113 | private final K8sApiConfigListener k8sApiConfigListener = new InternalK8sApiConfigListener(); |
| 114 | |
| 115 | private ApplicationId appId; |
| 116 | private NodeId localNode; |
| 117 | |
| 118 | @Activate |
| 119 | protected void activate() { |
| 120 | appId = coreService.getAppId(APP_ID); |
| 121 | localNode = clusterService.getLocalNode().id(); |
| 122 | leadershipService.runForLeadership(appId.name()); |
| 123 | k8sApiConfigAdminService.addListener(k8sApiConfigListener); |
| 124 | |
| 125 | log.info("Started"); |
| 126 | } |
| 127 | |
| 128 | @Deactivate |
| 129 | protected void deactivate() { |
| 130 | k8sApiConfigAdminService.removeListener(k8sApiConfigListener); |
| 131 | leadershipService.withdraw(appId.name()); |
| 132 | eventExecutor.shutdown(); |
| 133 | |
| 134 | log.info("Stopped"); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Checks the validity of the given kubernetes API server configuration. |
| 139 | * |
| 140 | * @param config kubernetes API server configuration |
| 141 | * @return validity result |
| 142 | */ |
| 143 | private boolean checkApiServerConfig(K8sApiConfig config) { |
| 144 | KubernetesClient k8sClient = k8sClient(config); |
| 145 | return k8sClient != null && k8sClient.getApiVersion() != null; |
| 146 | } |
| 147 | |
| 148 | private void bootstrapK8sNodes(K8sApiConfig config) { |
| 149 | KubernetesClient k8sClient = k8sClient(config); |
| 150 | |
| 151 | if (k8sClient == null) { |
| 152 | log.warn("Failed to connect to kubernetes API server"); |
| 153 | return; |
| 154 | } |
| 155 | |
Jian Li | 732c342 | 2020-09-07 17:01:11 +0900 | [diff] [blame] | 156 | for (Node node : k8sClient.nodes().list().getItems()) { |
| 157 | K8sNode k8sNode = buildK8sNode(node, config); |
| 158 | k8sNodeAdminService.createNode(k8sNode); |
| 159 | |
| 160 | while (k8sNodeAdminService.node(k8sNode.hostname()).state() != ON_BOARDED) { |
| 161 | try { |
| 162 | sleep(SLEEP_MS); |
| 163 | } catch (InterruptedException e) { |
| 164 | log.error("Exception caused during on-boarding state checking..."); |
| 165 | } |
| 166 | |
| 167 | if (k8sNodeAdminService.node(k8sNode.hostname()).state() == ON_BOARDED) { |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | } |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 172 | } |
| 173 | |
Jian Li | 077b07e | 2020-09-01 16:55:25 +0900 | [diff] [blame] | 174 | private void bootstrapK8sHosts(K8sApiConfig config) { |
| 175 | KubernetesClient k8sClient = k8sClient(config); |
| 176 | |
| 177 | if (k8sClient == null) { |
| 178 | log.warn("Failed to connect to kubernetes API server"); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | config.infos().forEach(h -> { |
| 183 | k8sHostAdminService.createHost(buildK8sHost(h, config)); |
| 184 | }); |
| 185 | |
| 186 | } |
| 187 | |
| 188 | private K8sHost buildK8sHost(HostNodesInfo hostNodesInfo, K8sApiConfig config) { |
| 189 | int segmentId = config.segmentId(); |
Jian Li | 019ce6a | 2020-09-09 10:23:21 +0900 | [diff] [blame] | 190 | K8sTunnelBridge tBridge = new K8sTunnelBridge(segmentId); |
| 191 | K8sRouterBridge rBridge = new K8sRouterBridge(segmentId); |
Jian Li | 077b07e | 2020-09-01 16:55:25 +0900 | [diff] [blame] | 192 | |
| 193 | return DefaultK8sHost.builder() |
| 194 | .hostIp(hostNodesInfo.hostIp()) |
| 195 | .state(K8sHostState.INIT) |
Jian Li | 019ce6a | 2020-09-09 10:23:21 +0900 | [diff] [blame] | 196 | .tunBridges(ImmutableSet.of(tBridge)) |
| 197 | .routerBridges(ImmutableSet.of(rBridge)) |
Jian Li | 077b07e | 2020-09-01 16:55:25 +0900 | [diff] [blame] | 198 | .nodeNames(hostNodesInfo.nodes()) |
| 199 | .build(); |
| 200 | } |
| 201 | |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 202 | private K8sNode buildK8sNode(Node node, K8sApiConfig config) { |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 203 | String hostname = node.getMetadata().getName(); |
| 204 | IpAddress managementIp = null; |
| 205 | IpAddress dataIp = null; |
| 206 | |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 207 | // pass-through mode: we use host IP as the management and data IP |
| 208 | // normal mode: we use K8S node's internal IP as the management and data IP |
| 209 | if (config.mode() == PASSTHROUGH) { |
| 210 | HostNodesInfo info = config.infos().stream().filter(h -> h.nodes() |
| 211 | .contains(hostname)).findAny().orElse(null); |
| 212 | if (info == null) { |
| 213 | log.error("None of the nodes were found in the host nodes info mapping list"); |
| 214 | } else { |
| 215 | managementIp = info.hostIp(); |
| 216 | dataIp = info.hostIp(); |
| 217 | } |
| 218 | } else { |
| 219 | for (NodeAddress nodeAddress:node.getStatus().getAddresses()) { |
| 220 | if (nodeAddress.getType().equals(INTERNAL_IP)) { |
| 221 | managementIp = IpAddress.valueOf(nodeAddress.getAddress()); |
| 222 | dataIp = IpAddress.valueOf(nodeAddress.getAddress()); |
| 223 | } |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | String roleStr = node.getMetadata().getLabels().keySet().stream() |
| 228 | .filter(l -> l.contains(K8S_ROLE)) |
| 229 | .findFirst().orElse(null); |
| 230 | |
Jian Li | 8a98804 | 2019-05-03 23:41:19 +0900 | [diff] [blame] | 231 | K8sNode.Type nodeType = MINION; |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 232 | |
| 233 | if (roleStr != null) { |
| 234 | String role = roleStr.split("/")[1]; |
| 235 | if (MASTER.name().equalsIgnoreCase(role)) { |
| 236 | nodeType = MASTER; |
| 237 | } else { |
| 238 | nodeType = MINION; |
| 239 | } |
| 240 | } |
| 241 | |
Jian Li | 0c63272 | 2019-05-08 15:58:04 +0900 | [diff] [blame] | 242 | Map<String, String> annots = node.getMetadata().getAnnotations(); |
| 243 | |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 244 | String extIntf = ""; |
| 245 | String extGatewayIpStr = DEFAULT_GATEWAY_IP; |
| 246 | String extBridgeIpStr = DEFAULT_BRIDGE_IP; |
| 247 | |
| 248 | if (config.mode() == PASSTHROUGH) { |
| 249 | extNetworkService.registerNetwork(config.extNetworkCidr()); |
| 250 | extIntf = EXTERNAL_TO_ROUTER + "-" + config.clusterShortName(); |
| 251 | IpAddress gatewayIp = extNetworkService.getGatewayIp(config.extNetworkCidr()); |
| 252 | IpAddress bridgeIp = extNetworkService.allocateIp(config.extNetworkCidr()); |
| 253 | if (gatewayIp != null) { |
| 254 | extGatewayIpStr = gatewayIp.toString(); |
| 255 | } |
| 256 | if (bridgeIp != null) { |
| 257 | extBridgeIpStr = bridgeIp.toString(); |
| 258 | } |
| 259 | } else { |
| 260 | extIntf = annots.get(EXT_INTF_NAME); |
| 261 | extGatewayIpStr = annots.get(EXT_GATEWAY_IP); |
| 262 | extBridgeIpStr = annots.get(EXT_BRIDGE_IP); |
| 263 | } |
Jian Li | 0c63272 | 2019-05-08 15:58:04 +0900 | [diff] [blame] | 264 | |
Jian Li | c2242bd | 2020-09-03 13:12:14 +0900 | [diff] [blame] | 265 | K8sNode.Builder builder = DefaultK8sNode.builder() |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 266 | .clusterName(DEFAULT_CLUSTER_NAME) |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 267 | .hostname(hostname) |
| 268 | .managementIp(managementIp) |
| 269 | .dataIp(dataIp) |
Jian Li | 0c63272 | 2019-05-08 15:58:04 +0900 | [diff] [blame] | 270 | .extIntf(extIntf) |
| 271 | .type(nodeType) |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 272 | .segmentId(config.segmentId()) |
Jian Li | 0ee8d0e | 2019-12-18 11:35:05 +0900 | [diff] [blame] | 273 | .state(PRE_ON_BOARD) |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 274 | .mode(config.mode()) |
Jian Li | 0c63272 | 2019-05-08 15:58:04 +0900 | [diff] [blame] | 275 | .extBridgeIp(IpAddress.valueOf(extBridgeIpStr)) |
| 276 | .extGatewayIp(IpAddress.valueOf(extGatewayIpStr)) |
Jian Li | c2242bd | 2020-09-03 13:12:14 +0900 | [diff] [blame] | 277 | .podCidr(node.getSpec().getPodCIDR()); |
| 278 | |
| 279 | if (config.dvr()) { |
| 280 | builder.extGatewayMac(MacAddress.valueOf(DEFAULT_EXTERNAL_GATEWAY_MAC)); |
| 281 | } |
| 282 | |
| 283 | return builder.build(); |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /** |
| 287 | * An internal kubernetes API server config listener. |
| 288 | * The notification is triggered by K8sApiConfigStore. |
| 289 | */ |
| 290 | private class InternalK8sApiConfigListener implements K8sApiConfigListener { |
| 291 | |
| 292 | private boolean isRelevantHelper() { |
| 293 | return Objects.equals(localNode, leadershipService.getLeader(appId.name())); |
| 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public void event(K8sApiConfigEvent event) { |
| 298 | |
| 299 | switch (event.type()) { |
| 300 | case K8S_API_CONFIG_CREATED: |
| 301 | eventExecutor.execute(() -> processConfigCreation(event.subject())); |
| 302 | break; |
| 303 | default: |
| 304 | break; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | private void processConfigCreation(K8sApiConfig config) { |
| 309 | if (!isRelevantHelper()) { |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | if (checkApiServerConfig(config)) { |
| 314 | K8sApiConfig newConfig = config.updateState(K8sApiConfig.State.CONNECTED); |
| 315 | k8sApiConfigAdminService.updateApiConfig(newConfig); |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 316 | |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 317 | bootstrapK8sNodes(config); |
Jian Li | 077b07e | 2020-09-01 16:55:25 +0900 | [diff] [blame] | 318 | |
Jian Li | 732c342 | 2020-09-07 17:01:11 +0900 | [diff] [blame] | 319 | if (config.infos().size() > 0) { |
| 320 | bootstrapK8sHosts(config); |
Jian Li | 077b07e | 2020-09-01 16:55:25 +0900 | [diff] [blame] | 321 | } |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | } |
| 325 | } |