Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-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.kubevirtnode.util; |
| 17 | |
Daniel Park | 515f5f3 | 2021-02-22 17:12:20 +0900 | [diff] [blame] | 18 | import com.fasterxml.jackson.core.JsonProcessingException; |
Daniel Park | 515f5f3 | 2021-02-22 17:12:20 +0900 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.JsonNode; |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
Jian Li | 4fe40e5 | 2021-01-06 03:29:58 +0900 | [diff] [blame] | 21 | import com.google.common.base.Strings; |
Daniel Park | 80c6f1b | 2021-02-18 20:38:46 +0900 | [diff] [blame] | 22 | import io.fabric8.kubernetes.api.model.Node; |
| 23 | import io.fabric8.kubernetes.api.model.NodeAddress; |
Jian Li | 1e0f3e8 | 2021-06-17 11:06:36 +0900 | [diff] [blame] | 24 | import io.fabric8.kubernetes.api.model.NodeSpec; |
| 25 | import io.fabric8.kubernetes.api.model.Taint; |
Jian Li | aaf44b5 | 2020-12-27 23:22:46 +0900 | [diff] [blame] | 26 | import io.fabric8.kubernetes.client.ConfigBuilder; |
| 27 | import io.fabric8.kubernetes.client.DefaultKubernetesClient; |
| 28 | import io.fabric8.kubernetes.client.KubernetesClient; |
Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 29 | import org.apache.commons.lang.StringUtils; |
Andrea Campanella | 775e5f8 | 2022-06-09 08:11:25 -0700 | [diff] [blame] | 30 | import com.eclipsesource.json.JsonArray; |
| 31 | import com.eclipsesource.json.JsonObject; |
Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 32 | import org.onlab.packet.IpAddress; |
Daniel Park | 80c6f1b | 2021-02-18 20:38:46 +0900 | [diff] [blame] | 33 | import org.onosproject.kubevirtnode.api.DefaultKubevirtNode; |
| 34 | import org.onosproject.kubevirtnode.api.DefaultKubevirtPhyInterface; |
Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 35 | import org.onosproject.kubevirtnode.api.KubevirtApiConfig; |
Jian Li | 4fe40e5 | 2021-01-06 03:29:58 +0900 | [diff] [blame] | 36 | import org.onosproject.kubevirtnode.api.KubevirtNode; |
Daniel Park | 80c6f1b | 2021-02-18 20:38:46 +0900 | [diff] [blame] | 37 | import org.onosproject.kubevirtnode.api.KubevirtNodeState; |
| 38 | import org.onosproject.kubevirtnode.api.KubevirtPhyInterface; |
Jian Li | 4fe40e5 | 2021-01-06 03:29:58 +0900 | [diff] [blame] | 39 | import org.onosproject.net.Device; |
Daniel Park | 17fe798 | 2022-04-04 17:48:01 +0900 | [diff] [blame] | 40 | import org.onosproject.net.DeviceId; |
Jian Li | 4fe40e5 | 2021-01-06 03:29:58 +0900 | [diff] [blame] | 41 | import org.onosproject.net.behaviour.BridgeConfig; |
| 42 | import org.onosproject.net.behaviour.BridgeName; |
| 43 | import org.onosproject.net.device.DeviceService; |
| 44 | import org.onosproject.ovsdb.controller.OvsdbClientService; |
| 45 | import org.onosproject.ovsdb.controller.OvsdbController; |
| 46 | import org.onosproject.ovsdb.controller.OvsdbNodeId; |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 47 | import org.slf4j.Logger; |
| 48 | import org.slf4j.LoggerFactory; |
Jian Li | 94b6d16 | 2021-04-15 17:09:11 +0900 | [diff] [blame] | 49 | import org.xbill.DNS.Address; |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 50 | |
| 51 | import java.io.IOException; |
Jian Li | 94b6d16 | 2021-04-15 17:09:11 +0900 | [diff] [blame] | 52 | import java.net.InetAddress; |
| 53 | import java.net.UnknownHostException; |
Jian Li | 4fe40e5 | 2021-01-06 03:29:58 +0900 | [diff] [blame] | 54 | import java.util.Dictionary; |
Daniel Park | 80c6f1b | 2021-02-18 20:38:46 +0900 | [diff] [blame] | 55 | import java.util.HashSet; |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 56 | import java.util.List; |
Daniel Park | 80c6f1b | 2021-02-18 20:38:46 +0900 | [diff] [blame] | 57 | import java.util.Map; |
| 58 | import java.util.Set; |
| 59 | import java.util.stream.Collectors; |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 60 | |
Jian Li | 4fe40e5 | 2021-01-06 03:29:58 +0900 | [diff] [blame] | 61 | import static org.onlab.util.Tools.get; |
Jian Li | 4b24970 | 2021-02-19 18:13:10 +0900 | [diff] [blame] | 62 | import static org.onosproject.kubevirtnode.api.Constants.SONA_PROJECT_DOMAIN; |
Daniel Park | 515f5f3 | 2021-02-22 17:12:20 +0900 | [diff] [blame] | 63 | import static org.onosproject.kubevirtnode.api.KubevirtNode.Type.GATEWAY; |
Daniel Park | 80c6f1b | 2021-02-18 20:38:46 +0900 | [diff] [blame] | 64 | import static org.onosproject.kubevirtnode.api.KubevirtNode.Type.MASTER; |
Jian Li | 1e0f3e8 | 2021-06-17 11:06:36 +0900 | [diff] [blame] | 65 | import static org.onosproject.kubevirtnode.api.KubevirtNode.Type.OTHER; |
Daniel Park | 80c6f1b | 2021-02-18 20:38:46 +0900 | [diff] [blame] | 66 | import static org.onosproject.kubevirtnode.api.KubevirtNode.Type.WORKER; |
Jian Li | 4fe40e5 | 2021-01-06 03:29:58 +0900 | [diff] [blame] | 67 | |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 68 | /** |
| 69 | * An utility that used in KubeVirt node app. |
| 70 | */ |
| 71 | public final class KubevirtNodeUtil { |
| 72 | |
| 73 | private static final Logger log = LoggerFactory.getLogger(KubevirtNodeUtil.class); |
| 74 | |
| 75 | private static final String COLON_SLASH = "://"; |
| 76 | private static final String COLON = ":"; |
| 77 | |
| 78 | private static final int HEX_LENGTH = 16; |
| 79 | private static final String OF_PREFIX = "of:"; |
| 80 | private static final String ZERO = "0"; |
Daniel Park | 80c6f1b | 2021-02-18 20:38:46 +0900 | [diff] [blame] | 81 | private static final String INTERNAL_IP = "InternalIP"; |
| 82 | private static final String K8S_ROLE = "node-role.kubernetes.io"; |
Jian Li | 4b24970 | 2021-02-19 18:13:10 +0900 | [diff] [blame] | 83 | private static final String PHYSNET_CONFIG_KEY = SONA_PROJECT_DOMAIN + "/physnet-config"; |
| 84 | private static final String DATA_IP_KEY = SONA_PROJECT_DOMAIN + "/data-ip"; |
Daniel Park | 515f5f3 | 2021-02-22 17:12:20 +0900 | [diff] [blame] | 85 | private static final String GATEWAY_CONFIG_KEY = SONA_PROJECT_DOMAIN + "/gateway-config"; |
| 86 | private static final String GATEWAY_BRIDGE_NAME = "gatewayBridgeName"; |
Daniel Park | 80c6f1b | 2021-02-18 20:38:46 +0900 | [diff] [blame] | 87 | private static final String NETWORK_KEY = "network"; |
| 88 | private static final String INTERFACE_KEY = "interface"; |
Daniel Park | 17fe798 | 2022-04-04 17:48:01 +0900 | [diff] [blame] | 89 | private static final String PHYS_BRIDGE_ID = "physBridgeId"; |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 90 | |
Jian Li | 4fe40e5 | 2021-01-06 03:29:58 +0900 | [diff] [blame] | 91 | private static final int PORT_NAME_MAX_LENGTH = 15; |
Jian Li | 1e0f3e8 | 2021-06-17 11:06:36 +0900 | [diff] [blame] | 92 | |
| 93 | private static final String NO_SCHEDULE_EFFECT = "NoSchedule"; |
| 94 | private static final String KUBEVIRT_IO_KEY = "kubevirt.io/drain"; |
| 95 | private static final String DRAINING_VALUE = "draining"; |
Jian Li | 4fe40e5 | 2021-01-06 03:29:58 +0900 | [diff] [blame] | 96 | |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 97 | /** |
| 98 | * Prevents object installation from external. |
| 99 | */ |
| 100 | private KubevirtNodeUtil() { |
| 101 | } |
| 102 | |
| 103 | /** |
Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 104 | * Generates endpoint URL by referring to scheme, ipAddress and port. |
| 105 | * |
| 106 | * @param apiConfig kubernetes API config |
| 107 | * @return generated endpoint URL |
| 108 | */ |
| 109 | public static String endpoint(KubevirtApiConfig apiConfig) { |
| 110 | return endpoint(apiConfig.scheme(), apiConfig.ipAddress(), apiConfig.port()); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Generates endpoint URL by referring to scheme, ipAddress and port. |
| 115 | * |
| 116 | * @param scheme scheme |
| 117 | * @param ipAddress IP address |
| 118 | * @param port port number |
| 119 | * @return generated endpoint URL |
| 120 | */ |
| 121 | public static String endpoint(KubevirtApiConfig.Scheme scheme, IpAddress ipAddress, int port) { |
| 122 | StringBuilder endpoint = new StringBuilder(); |
| 123 | String protocol = StringUtils.lowerCase(scheme.name()); |
| 124 | |
| 125 | endpoint.append(protocol); |
| 126 | endpoint.append(COLON_SLASH); |
| 127 | endpoint.append(ipAddress.toString()); |
| 128 | endpoint.append(COLON); |
| 129 | endpoint.append(port); |
| 130 | |
| 131 | return endpoint.toString(); |
| 132 | } |
| 133 | |
| 134 | /** |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 135 | * Generates a DPID (of:0000000000000001) from an index value. |
| 136 | * |
| 137 | * @param index index value |
| 138 | * @return generated DPID |
| 139 | */ |
| 140 | public static String genDpid(long index) { |
| 141 | if (index < 0) { |
| 142 | return null; |
| 143 | } |
| 144 | |
| 145 | String hexStr = Long.toHexString(index); |
| 146 | |
| 147 | StringBuilder zeroPadding = new StringBuilder(); |
| 148 | for (int i = 0; i < HEX_LENGTH - hexStr.length(); i++) { |
| 149 | zeroPadding.append(ZERO); |
| 150 | } |
| 151 | |
| 152 | return OF_PREFIX + zeroPadding.toString() + hexStr; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Generates string format based on the given string length list. |
| 157 | * |
| 158 | * @param stringLengths a list of string lengths |
| 159 | * @return string format (e.g., %-28s%-15s%-24s%-20s%-15s) |
| 160 | */ |
| 161 | public static String genFormatString(List<Integer> stringLengths) { |
| 162 | StringBuilder fsb = new StringBuilder(); |
| 163 | stringLengths.forEach(length -> { |
| 164 | fsb.append("%-"); |
| 165 | fsb.append(length); |
| 166 | fsb.append("s"); |
| 167 | }); |
| 168 | return fsb.toString(); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Prints out the JSON string in pretty format. |
| 173 | * |
| 174 | * @param mapper Object mapper |
| 175 | * @param jsonString JSON string |
| 176 | * @return pretty formatted JSON string |
| 177 | */ |
| 178 | public static String prettyJson(ObjectMapper mapper, String jsonString) { |
| 179 | try { |
| 180 | Object jsonObject = mapper.readValue(jsonString, Object.class); |
| 181 | return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject); |
| 182 | } catch (IOException e) { |
| 183 | log.debug("Json string parsing exception caused by {}", e); |
| 184 | } |
| 185 | return null; |
| 186 | } |
Jian Li | aaf44b5 | 2020-12-27 23:22:46 +0900 | [diff] [blame] | 187 | |
| 188 | /** |
| 189 | * Obtains workable kubernetes client. |
| 190 | * |
| 191 | * @param config kubernetes API config |
| 192 | * @return kubernetes client |
| 193 | */ |
| 194 | public static KubernetesClient k8sClient(KubevirtApiConfig config) { |
| 195 | if (config == null) { |
| 196 | log.warn("Kubernetes API server config is empty."); |
| 197 | return null; |
| 198 | } |
| 199 | |
| 200 | String endpoint = endpoint(config); |
| 201 | |
| 202 | ConfigBuilder configBuilder = new ConfigBuilder().withMasterUrl(endpoint); |
| 203 | |
| 204 | if (config.scheme() == KubevirtApiConfig.Scheme.HTTPS) { |
| 205 | configBuilder.withTrustCerts(true) |
| 206 | .withCaCertData(config.caCertData()) |
| 207 | .withClientCertData(config.clientCertData()) |
| 208 | .withClientKeyData(config.clientKeyData()); |
| 209 | |
| 210 | if (StringUtils.isNotEmpty(config.token())) { |
| 211 | configBuilder.withOauthToken(config.token()); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | return new DefaultKubernetesClient(configBuilder.build()); |
| 216 | } |
Jian Li | 4fe40e5 | 2021-01-06 03:29:58 +0900 | [diff] [blame] | 217 | |
| 218 | /** |
| 219 | * Gets the ovsdb client with supplied openstack node. |
| 220 | * |
| 221 | * @param node kubernetes node |
| 222 | * @param ovsdbPort ovsdb port |
| 223 | * @param ovsdbController ovsdb controller |
| 224 | * @return ovsdb client |
| 225 | */ |
| 226 | public static OvsdbClientService getOvsdbClient(KubevirtNode node, |
| 227 | int ovsdbPort, |
| 228 | OvsdbController ovsdbController) { |
| 229 | OvsdbNodeId ovsdb = new OvsdbNodeId(node.managementIp(), ovsdbPort); |
| 230 | return ovsdbController.getOvsdbClient(ovsdb); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Checks whether the controller has a connection with an OVSDB that resides |
| 235 | * inside the given kubernetes node. |
| 236 | * |
| 237 | * @param node kubernetes node |
| 238 | * @param ovsdbPort OVSDB port |
| 239 | * @param ovsdbController OVSDB controller |
| 240 | * @param deviceService device service |
| 241 | * @return true if the controller is connected to the OVSDB, false otherwise |
| 242 | */ |
| 243 | public static boolean isOvsdbConnected(KubevirtNode node, |
| 244 | int ovsdbPort, |
| 245 | OvsdbController ovsdbController, |
| 246 | DeviceService deviceService) { |
| 247 | OvsdbClientService client = getOvsdbClient(node, ovsdbPort, ovsdbController); |
| 248 | return deviceService.isAvailable(node.ovsdb()) && |
| 249 | client != null && |
| 250 | client.isConnected(); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Adds or removes a network interface (aka port) into a given bridge of kubernetes node. |
| 255 | * |
| 256 | * @param k8sNode kubernetes node |
| 257 | * @param bridgeName bridge name |
| 258 | * @param intfName interface name |
| 259 | * @param deviceService device service |
| 260 | * @param addOrRemove add port is true, remove it otherwise |
| 261 | */ |
| 262 | public static synchronized void addOrRemoveSystemInterface(KubevirtNode k8sNode, |
| 263 | String bridgeName, |
| 264 | String intfName, |
| 265 | DeviceService deviceService, |
| 266 | boolean addOrRemove) { |
| 267 | |
| 268 | |
| 269 | Device device = deviceService.getDevice(k8sNode.ovsdb()); |
| 270 | if (device == null || !device.is(BridgeConfig.class)) { |
| 271 | log.info("device is null or this device if not ovsdb device"); |
| 272 | return; |
| 273 | } |
| 274 | BridgeConfig bridgeConfig = device.as(BridgeConfig.class); |
| 275 | |
| 276 | if (addOrRemove) { |
| 277 | bridgeConfig.addPort(BridgeName.bridgeName(bridgeName), intfName); |
| 278 | } else { |
| 279 | bridgeConfig.deletePort(BridgeName.bridgeName(bridgeName), intfName); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Re-structures the OVS port name. |
| 285 | * The length of OVS port name should be not large than 15. |
| 286 | * |
| 287 | * @param portName original port name |
| 288 | * @return re-structured OVS port name |
| 289 | */ |
| 290 | public static String structurePortName(String portName) { |
| 291 | |
| 292 | // The size of OVS port name should not be larger than 15 |
| 293 | if (portName.length() > PORT_NAME_MAX_LENGTH) { |
| 294 | return StringUtils.substring(portName, 0, PORT_NAME_MAX_LENGTH); |
| 295 | } |
| 296 | |
| 297 | return portName; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Gets Boolean property from the propertyName |
| 302 | * Return null if propertyName is not found. |
| 303 | * |
| 304 | * @param properties properties to be looked up |
| 305 | * @param propertyName the name of the property to look up |
| 306 | * @return value when the propertyName is defined or return null |
| 307 | */ |
| 308 | public static Boolean getBooleanProperty(Dictionary<?, ?> properties, |
| 309 | String propertyName) { |
| 310 | Boolean value; |
| 311 | try { |
| 312 | String s = get(properties, propertyName); |
| 313 | value = Strings.isNullOrEmpty(s) ? null : Boolean.valueOf(s); |
| 314 | } catch (ClassCastException e) { |
| 315 | value = null; |
| 316 | } |
| 317 | return value; |
| 318 | } |
Daniel Park | 80c6f1b | 2021-02-18 20:38:46 +0900 | [diff] [blame] | 319 | |
| 320 | /** |
| 321 | * Returns the kubevirt node from the node. |
| 322 | * |
| 323 | * @param node a raw node object returned from a k8s client |
| 324 | * @return kubevirt node |
| 325 | */ |
| 326 | public static KubevirtNode buildKubevirtNode(Node node) { |
| 327 | String hostname = node.getMetadata().getName(); |
| 328 | IpAddress managementIp = null; |
| 329 | IpAddress dataIp = null; |
| 330 | |
| 331 | for (NodeAddress nodeAddress:node.getStatus().getAddresses()) { |
| 332 | if (nodeAddress.getType().equals(INTERNAL_IP)) { |
| 333 | managementIp = IpAddress.valueOf(nodeAddress.getAddress()); |
| 334 | dataIp = IpAddress.valueOf(nodeAddress.getAddress()); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | Set<String> rolesFull = node.getMetadata().getLabels().keySet().stream() |
| 339 | .filter(l -> l.contains(K8S_ROLE)) |
| 340 | .collect(Collectors.toSet()); |
| 341 | |
| 342 | KubevirtNode.Type nodeType = WORKER; |
| 343 | |
| 344 | for (String roleStr : rolesFull) { |
| 345 | String role = roleStr.split("/")[1]; |
| 346 | if (MASTER.name().equalsIgnoreCase(role)) { |
| 347 | nodeType = MASTER; |
| 348 | break; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | // start to parse kubernetes annotation |
| 353 | Map<String, String> annots = node.getMetadata().getAnnotations(); |
| 354 | String physnetConfig = annots.get(PHYSNET_CONFIG_KEY); |
Daniel Park | 515f5f3 | 2021-02-22 17:12:20 +0900 | [diff] [blame] | 355 | String gatewayConfig = annots.get(GATEWAY_CONFIG_KEY); |
Jian Li | 4b24970 | 2021-02-19 18:13:10 +0900 | [diff] [blame] | 356 | String dataIpStr = annots.get(DATA_IP_KEY); |
Daniel Park | 80c6f1b | 2021-02-18 20:38:46 +0900 | [diff] [blame] | 357 | Set<KubevirtPhyInterface> phys = new HashSet<>(); |
Daniel Park | 515f5f3 | 2021-02-22 17:12:20 +0900 | [diff] [blame] | 358 | String gatewayBridgeName = null; |
Daniel Park | 80c6f1b | 2021-02-18 20:38:46 +0900 | [diff] [blame] | 359 | try { |
| 360 | if (physnetConfig != null) { |
Andrea Campanella | 775e5f8 | 2022-06-09 08:11:25 -0700 | [diff] [blame] | 361 | JsonArray configJson = JsonArray.readFrom(physnetConfig); |
Daniel Park | 80c6f1b | 2021-02-18 20:38:46 +0900 | [diff] [blame] | 362 | |
Andrea Campanella | 775e5f8 | 2022-06-09 08:11:25 -0700 | [diff] [blame] | 363 | for (int i = 0; i < configJson.size(); i++) { |
| 364 | JsonObject object = configJson.get(i).asObject(); |
| 365 | String network = object.get(NETWORK_KEY).asString(); |
| 366 | String intf = object.get(INTERFACE_KEY).asString(); |
Daniel Park | 80c6f1b | 2021-02-18 20:38:46 +0900 | [diff] [blame] | 367 | |
| 368 | if (network != null && intf != null) { |
Daniel Park | 17fe798 | 2022-04-04 17:48:01 +0900 | [diff] [blame] | 369 | String physBridgeId; |
Andrea Campanella | 775e5f8 | 2022-06-09 08:11:25 -0700 | [diff] [blame] | 370 | if (object.get(PHYS_BRIDGE_ID) != null) { |
| 371 | physBridgeId = object.get(PHYS_BRIDGE_ID).asString(); |
Daniel Park | 17fe798 | 2022-04-04 17:48:01 +0900 | [diff] [blame] | 372 | } else { |
| 373 | physBridgeId = genDpidFromName(network + intf + hostname); |
| 374 | log.trace("host {} physnet dpid for network {} intf {} is null so generate dpid {}", |
| 375 | hostname, network, intf, physBridgeId); |
| 376 | } |
| 377 | |
Daniel Park | 80c6f1b | 2021-02-18 20:38:46 +0900 | [diff] [blame] | 378 | phys.add(DefaultKubevirtPhyInterface.builder() |
Daniel Park | 17fe798 | 2022-04-04 17:48:01 +0900 | [diff] [blame] | 379 | .network(network) |
| 380 | .intf(intf) |
| 381 | .physBridge(DeviceId.deviceId(physBridgeId)) |
| 382 | .build()); |
Daniel Park | 80c6f1b | 2021-02-18 20:38:46 +0900 | [diff] [blame] | 383 | } |
Daniel Park | 80c6f1b | 2021-02-18 20:38:46 +0900 | [diff] [blame] | 384 | } |
| 385 | } |
Jian Li | 4b24970 | 2021-02-19 18:13:10 +0900 | [diff] [blame] | 386 | |
| 387 | if (dataIpStr != null) { |
| 388 | dataIp = IpAddress.valueOf(dataIpStr); |
| 389 | } |
Daniel Park | 515f5f3 | 2021-02-22 17:12:20 +0900 | [diff] [blame] | 390 | |
| 391 | if (gatewayConfig != null) { |
| 392 | JsonNode jsonNode = new ObjectMapper().readTree(gatewayConfig); |
| 393 | |
| 394 | nodeType = GATEWAY; |
| 395 | gatewayBridgeName = jsonNode.get(GATEWAY_BRIDGE_NAME).asText(); |
| 396 | } |
Andrea Campanella | 775e5f8 | 2022-06-09 08:11:25 -0700 | [diff] [blame] | 397 | } catch (JsonProcessingException e) { |
Jian Li | 9793ec4 | 2021-03-19 15:03:32 +0900 | [diff] [blame] | 398 | log.error("Failed to parse physnet config or gateway config object", e); |
Daniel Park | 80c6f1b | 2021-02-18 20:38:46 +0900 | [diff] [blame] | 399 | } |
| 400 | |
Jian Li | 1e0f3e8 | 2021-06-17 11:06:36 +0900 | [diff] [blame] | 401 | // if the node is taint with kubevirt.io key configured, |
| 402 | // we mark this node as OTHER type, and do not add it into the cluster |
| 403 | NodeSpec spec = node.getSpec(); |
| 404 | if (spec.getTaints() != null) { |
| 405 | for (Taint taint : spec.getTaints()) { |
| 406 | String effect = taint.getEffect(); |
| 407 | String key = taint.getKey(); |
| 408 | String value = taint.getValue(); |
| 409 | |
| 410 | if (StringUtils.equals(effect, NO_SCHEDULE_EFFECT) && |
| 411 | StringUtils.equals(key, KUBEVIRT_IO_KEY) && |
| 412 | StringUtils.equals(value, DRAINING_VALUE)) { |
| 413 | nodeType = OTHER; |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
Daniel Park | 80c6f1b | 2021-02-18 20:38:46 +0900 | [diff] [blame] | 418 | return DefaultKubevirtNode.builder() |
| 419 | .hostname(hostname) |
| 420 | .managementIp(managementIp) |
| 421 | .dataIp(dataIp) |
| 422 | .type(nodeType) |
| 423 | .state(KubevirtNodeState.ON_BOARDED) |
| 424 | .phyIntfs(phys) |
Daniel Park | 515f5f3 | 2021-02-22 17:12:20 +0900 | [diff] [blame] | 425 | .gatewayBridgeName(gatewayBridgeName) |
Daniel Park | 80c6f1b | 2021-02-18 20:38:46 +0900 | [diff] [blame] | 426 | .build(); |
| 427 | } |
Jian Li | 94b6d16 | 2021-04-15 17:09:11 +0900 | [diff] [blame] | 428 | |
| 429 | /** |
Daniel Park | 17fe798 | 2022-04-04 17:48:01 +0900 | [diff] [blame] | 430 | * Generates a unique dpid from given name. |
| 431 | * |
| 432 | * @param name name |
| 433 | * @return device id in string |
| 434 | */ |
| 435 | public static String genDpidFromName(String name) { |
| 436 | if (name != null) { |
| 437 | String hexString = Integer.toHexString(name.hashCode()); |
| 438 | return OF_PREFIX + Strings.padStart(hexString, 16, '0'); |
| 439 | } |
| 440 | return null; |
| 441 | } |
| 442 | |
| 443 | /** |
Jian Li | 94b6d16 | 2021-04-15 17:09:11 +0900 | [diff] [blame] | 444 | * Resolve a DNS with the given DNS server and hostname. |
| 445 | * |
| 446 | * @param hostname hostname to be resolved |
| 447 | * @return resolved IP address |
| 448 | */ |
| 449 | public static IpAddress resolveHostname(String hostname) { |
| 450 | try { |
| 451 | InetAddress addr = Address.getByName(hostname); |
| 452 | return IpAddress.valueOf(IpAddress.Version.INET, addr.getAddress()); |
| 453 | } catch (UnknownHostException e) { |
| 454 | log.warn("Failed to resolve IP address of host {}", hostname); |
| 455 | } |
| 456 | return null; |
| 457 | } |
Jian Li | 528335e | 2021-07-08 20:52:50 +0900 | [diff] [blame] | 458 | |
| 459 | /** |
| 460 | * Waits for the given length of time. |
| 461 | * |
| 462 | * @param timeSecond the amount of time for wait in second unit |
| 463 | */ |
| 464 | public static void waitFor(int timeSecond) { |
| 465 | try { |
| 466 | Thread.sleep(timeSecond * 1000L); |
| 467 | } catch (Exception e) { |
| 468 | log.error(e.toString()); |
| 469 | } |
| 470 | } |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 471 | } |