diff --git a/controllerconfig/controllerconfig/upgrade-scripts/50-remove-calico-ipip.py b/controllerconfig/controllerconfig/upgrade-scripts/50-remove-calico-ipip.py index 359cce8754..9afba4a7a0 100644 --- a/controllerconfig/controllerconfig/upgrade-scripts/50-remove-calico-ipip.py +++ b/controllerconfig/controllerconfig/upgrade-scripts/50-remove-calico-ipip.py @@ -37,11 +37,10 @@ def main(): '%(filename)s(%(lineno)s): %(levelname)s: %(message)s') LOG.basicConfig(filename="/var/log/software.log", format=log_format, level=LOG.INFO, datefmt="%FT%T") - - LOG.info("%s invoked from_release = %s to_release = %s action = %s" - % (sys.argv[0], from_release, to_release, action)) res = 0 if action == "activate" and from_release == '22.12': + LOG.info("%s invoked from_release = %s to_release = %s action = %s" + % (sys.argv[0], from_release, to_release, action)) try: res = do_update(from_release, to_release) except Exception as e: @@ -52,19 +51,33 @@ def main(): def do_update(from_release, to_release): - # Disable IPIP overlay by setting default IPv4 ippool's ipipMode to Never - LOG.info("Disabling Calico's IPIP tunnel") - cmd = (KUBE_CMD + "patch ippools.crd.projectcalico.org" + - " default-ipv4-ippool" + - " --type=merge -p '{\"spec\":{\"ipipMode\":\"Never\"}}'") + # Check if IPv4 IPIP pool exists + cmd = (KUBE_CMD + "get ippools.crd.projectcalico.org" + + " --no-headers" + + " -o custom-columns=NAME:.metadata.name") + stdout, _ = run_cmd(cmd) + # Disable IPIP overlay by setting default IPv4 ippool's ipipMode to Never + if stdout.rstrip('\n') == "default-ipv4-ippool": + LOG.info("Disabling Calico's IPIP tunnel") + cmd = (KUBE_CMD + "patch ippools.crd.projectcalico.org" + + " default-ipv4-ippool" + + " --type=merge -p" + + " '{\"spec\":{\"ipipMode\":\"Never\"}}'") + run_cmd(cmd) + else: + LOG.info("Skipping, default-ipv4-ippool not found") + + +def run_cmd(cmd): sub = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = sub.communicate() if sub.returncode != 0: LOG.error('Command failed:\n %s\n. %s\n%s\n' % (cmd, stdout.decode('utf-8'), stderr.decode('utf-8'))) - raise Exception('Cannot change default-ipv4-ippool') + raise Exception(f'Cannot run cmd: "{cmd}"') + return stdout.decode('utf-8'), stderr.decode('utf-8') if __name__ == "__main__":