-
Notifications
You must be signed in to change notification settings - Fork 776
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stopping control plane on HA (#1406)
- Loading branch information
1 parent
e7653bf
commit 8cf2616
Showing
5 changed files
with
135 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -eu | ||
|
||
export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH" | ||
ARCH="$($SNAP/bin/uname -m)" | ||
export IN_SNAP_LD_LIBRARY_PATH="$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/$ARCH-linux-gnu:$SNAP/usr/lib/$ARCH-linux-gnu" | ||
export PYTHONNOUSERSITE=false | ||
|
||
source $SNAP/actions/common/utils.sh | ||
|
||
LD_LIBRARY_PATH=$IN_SNAP_LD_LIBRARY_PATH ${SNAP}/usr/bin/python3 ${SNAP}/scripts/wrappers/control-plane-kicker.py $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#!/usr/bin/python3 | ||
import netifaces | ||
import subprocess | ||
|
||
from time import sleep | ||
|
||
from common.utils import ( | ||
is_cluster_ready, | ||
get_dqlite_info, | ||
is_ha_enabled, | ||
is_service_expected_to_start, | ||
set_service_expected_to_start, | ||
) | ||
|
||
services = [ | ||
'controller-manager', | ||
'scheduler', | ||
] | ||
|
||
|
||
def start_control_plane_services(): | ||
""" | ||
Start the control plane services | ||
""" | ||
for service in services: | ||
if not is_service_expected_to_start(service): | ||
systemd_service_name = "microk8s.daemon-{}".format(service) | ||
print("Starting {}".format(systemd_service_name), flush=True) | ||
cmd = "snapctl start {}".format(systemd_service_name) | ||
subprocess.check_output((cmd.split())) | ||
set_service_expected_to_start(service, True) | ||
|
||
|
||
def stop_control_plane_services(): | ||
""" | ||
Stop the control plane services | ||
""" | ||
for service in services: | ||
if is_service_expected_to_start(service): | ||
systemd_service_name = "microk8s.daemon-{}".format(service) | ||
print("Stopping {}".format(systemd_service_name), flush=True) | ||
cmd = "snapctl stop {}".format(systemd_service_name) | ||
subprocess.check_output(cmd.split()) | ||
set_service_expected_to_start(service, False) | ||
|
||
|
||
if __name__ == '__main__': | ||
while True: | ||
# Check for changes every 10 seconds | ||
sleep(10) | ||
try: | ||
# We will not attempt to stop services if: | ||
# 1. The cluster is not ready | ||
# 2. We are not on an HA cluster | ||
# 3. The control plane kicker is disabled | ||
# 4. dqlite has less than 4 nodes | ||
if ( | ||
not is_cluster_ready() | ||
or not is_ha_enabled() | ||
or not is_service_expected_to_start('control-plane-kicker') | ||
): | ||
start_control_plane_services() | ||
continue | ||
|
||
info = get_dqlite_info() | ||
if len(info) <= 3: | ||
start_control_plane_services() | ||
continue | ||
|
||
local_ips = [] | ||
for interface in netifaces.interfaces(): | ||
if netifaces.AF_INET not in netifaces.ifaddresses(interface): | ||
continue | ||
for link in netifaces.ifaddresses(interface)[netifaces.AF_INET]: | ||
local_ips.append(link['addr']) | ||
|
||
voter_ips = [] | ||
for node in info: | ||
if node[1] == "voter": | ||
ip_parts = node[0].split(':') | ||
voter_ips.append(ip_parts[0]) | ||
|
||
should_run = False | ||
for ip in local_ips: | ||
if ip in voter_ips: | ||
should_run = True | ||
start_control_plane_services() | ||
break | ||
|
||
if not should_run: | ||
stop_control_plane_services() | ||
|
||
except Exception as e: | ||
print(e, flush=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters