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