Daniel Park | 81bd46b | 2021-02-18 20:38:46 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2021-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.cli; |
| 17 | |
| 18 | import io.fabric8.kubernetes.api.model.Node; |
| 19 | import io.fabric8.kubernetes.client.KubernetesClient; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
| 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.kubevirtnode.api.KubevirtApiConfig; |
| 24 | import org.onosproject.kubevirtnode.api.KubevirtApiConfigService; |
| 25 | import org.onosproject.kubevirtnode.api.KubevirtNode; |
| 26 | import org.onosproject.kubevirtnode.api.KubevirtNodeAdminService; |
| 27 | |
| 28 | import java.util.Set; |
| 29 | |
| 30 | import static org.onosproject.kubevirtnode.api.KubevirtNode.Type.WORKER; |
| 31 | import static org.onosproject.kubevirtnode.api.KubevirtNodeState.INIT; |
| 32 | import static org.onosproject.kubevirtnode.util.KubevirtNodeUtil.buildKubevirtNode; |
| 33 | import static org.onosproject.kubevirtnode.util.KubevirtNodeUtil.k8sClient; |
| 34 | |
| 35 | /** |
| 36 | * Synchronizes kubevirt node states. |
| 37 | */ |
| 38 | @Service |
| 39 | @Command(scope = "onos", name = "kubevirt-sync-state", |
| 40 | description = "Synchronizes kubevirt node states.") |
| 41 | public class KubevirtSyncStateCommand extends AbstractShellCommand { |
| 42 | @Override |
| 43 | protected void doExecute() throws Exception { |
| 44 | KubevirtApiConfigService apiConfigService = get(KubevirtApiConfigService.class); |
| 45 | |
| 46 | print("Re-synchronizing Kubevirt node states.."); |
| 47 | KubevirtApiConfig config = apiConfigService.apiConfig(); |
| 48 | bootstrapKubevirtNodes(config); |
| 49 | print("Done."); |
| 50 | |
| 51 | } |
| 52 | |
| 53 | private void bootstrapKubevirtNodes(KubevirtApiConfig config) { |
| 54 | KubevirtNodeAdminService nodeAdminService = get(KubevirtNodeAdminService.class); |
| 55 | |
| 56 | Set<KubevirtNode> completeNodeSet = nodeAdminService.completeNodes(); |
| 57 | KubernetesClient k8sClient = k8sClient(config); |
| 58 | |
| 59 | if (k8sClient == null) { |
| 60 | log.warn("Failed to connect to kubernetes API server"); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | for (Node node : k8sClient.nodes().list().getItems()) { |
| 65 | KubevirtNode kubevirtNode = buildKubevirtNode(node); |
| 66 | // we always provision VMs to worker nodes, so only need to install |
| 67 | // flow rules in worker nodes |
| 68 | if (kubevirtNode.type() == WORKER) { |
| 69 | if (completeNodeSet.stream().map(KubevirtNode::hostname) |
| 70 | .filter(name -> name.equals(kubevirtNode.hostname())) |
| 71 | .findAny().isPresent()) { |
| 72 | print("Initializing %s because the node was COMPLETE state.", |
| 73 | kubevirtNode.hostname()); |
| 74 | KubevirtNode updated = kubevirtNode.updateState(INIT); |
| 75 | nodeAdminService.updateNode(updated); |
| 76 | } else { |
| 77 | nodeAdminService.updateNode(kubevirtNode); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |