Jian Li | 3d1111e | 2019-02-22 02:02:13 +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.k8snetworking.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.api.action.Command; |
| 19 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 20 | import org.onosproject.cli.AbstractShellCommand; |
| 21 | import org.onosproject.k8snode.api.K8sNode; |
| 22 | import org.onosproject.k8snode.api.K8sNodeAdminService; |
| 23 | |
| 24 | import static java.lang.Thread.sleep; |
| 25 | import static org.onosproject.k8snode.api.K8sNodeState.COMPLETE; |
| 26 | import static org.onosproject.k8snode.api.K8sNodeState.INIT; |
| 27 | |
| 28 | /** |
| 29 | * Synchronizes flow rules to provide connectivity for kubernetes pods. |
| 30 | */ |
| 31 | @Service |
| 32 | @Command(scope = "onos", name = "k8s-sync-rules", |
| 33 | description = "Synchronizes all kubernetes flow rules") |
| 34 | public class K8sSyncRulesCommand extends AbstractShellCommand { |
| 35 | |
Jian Li | 8916418 | 2020-09-04 19:38:37 +0900 | [diff] [blame] | 36 | private static final long SLEEP_MS = 10000; // we wait 10s for init each node |
| 37 | private static final long TIMEOUT_MS = 30000; // we wait 30s |
Jian Li | 3d1111e | 2019-02-22 02:02:13 +0900 | [diff] [blame] | 38 | |
| 39 | private static final String SUCCESS_MSG = "Successfully synchronize flow rules for node %s!"; |
| 40 | private static final String FAIL_MSG = "Failed to synchronize flow rules for node %s."; |
| 41 | |
| 42 | @Override |
| 43 | protected void doExecute() { |
| 44 | |
| 45 | K8sNodeAdminService adminService = get(K8sNodeAdminService.class); |
| 46 | if (adminService == null) { |
| 47 | error("Failed to re-install flow rules for kubernetes networking."); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | adminService.completeNodes().forEach(node -> |
| 52 | syncRulesBaseForNode(adminService, node)); |
| 53 | |
| 54 | print("Successfully requested re-installing flow rules."); |
| 55 | } |
| 56 | |
| 57 | private void syncRulesBaseForNode(K8sNodeAdminService adminService, |
| 58 | K8sNode k8sNode) { |
| 59 | K8sNode updated = k8sNode.updateState(INIT); |
| 60 | adminService.updateNode(updated); |
| 61 | |
| 62 | boolean result = true; |
| 63 | long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS; |
| 64 | |
| 65 | while (adminService.node(k8sNode.hostname()).state() != COMPLETE) { |
| 66 | |
| 67 | long waitMs = timeoutExpiredMs - System.currentTimeMillis(); |
| 68 | |
| 69 | try { |
| 70 | sleep(SLEEP_MS); |
| 71 | } catch (InterruptedException e) { |
| 72 | error("Exception caused during node synchronization..."); |
| 73 | } |
| 74 | |
| 75 | if (adminService.node(k8sNode.hostname()).state() == COMPLETE) { |
| 76 | break; |
| 77 | } else { |
| 78 | adminService.updateNode(updated); |
| 79 | print("Failed to synchronize flow rules, retrying..."); |
| 80 | } |
| 81 | |
| 82 | if (waitMs <= 0) { |
| 83 | result = false; |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | if (result) { |
| 89 | print(SUCCESS_MSG, k8sNode.hostname()); |
| 90 | } else { |
| 91 | error(FAIL_MSG, k8sNode.hostname()); |
| 92 | } |
| 93 | } |
| 94 | } |