Skip to content
This repository has been archived by the owner on Aug 13, 2024. It is now read-only.

Commit

Permalink
Enable passing custom calico version (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsturtevant authored Jun 1, 2023
1 parent 1e404e5 commit 51f5aa5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/capi/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Release History

0.1.5
+++++

* Calico versions in helm charts are pinned to 3.25.1 and can be overridden with `CALICO_VERSION` environment variable

0.1.4
+++++
Expand Down
34 changes: 25 additions & 9 deletions src/capi/azext_capi/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""This module implements the behavior of `az capi` commands."""

# pylint: disable=missing-docstring
# pylint: disable=line-too-long

from collections import namedtuple
import base64
Expand Down Expand Up @@ -35,7 +36,7 @@

from ._format import output_for_tsv, output_list_for_tsv
from .helpers.binary import check_clusterctl, check_helm, check_kubectl, check_kind
from .helpers.constants import MANAGEMENT_RG_NAME, AKS_INFRA_RG_NAME, AKS_VNET_NAME
from .helpers.constants import MANAGEMENT_RG_NAME, AKS_INFRA_RG_NAME, AKS_VNET_NAME, CAPZ_BASE_CONTENT_URL, DEFAULT_CALICO_VERSION
from .helpers.generic import has_kind_prefix, match_output, is_clusterctl_compatible
from .helpers.kubectl import create_configmap, get_configmap
from .helpers.logger import logger
Expand Down Expand Up @@ -598,27 +599,26 @@ def install_cni(cmd, cluster_name, workload_cfg, windows, args):
pass # This is a best-effort configuration, so don't fail if we can't find the CIDR.

# Install Calico CNI using the official Helm chart.
file_base = "https://raw.githubusercontent.com/kubernetes-sigs/cluster-api-provider-azure/release-1.7"
values_file = f"{file_base}/templates/addons/calico/values.yaml"
values_file = f"{CAPZ_BASE_CONTENT_URL}/templates/addons/calico/values.yaml"
if cidr0 and not cidr1:
interface0 = ipaddress.ip_interface(cidr0)
if interface0.version == 6:
values_file = f"{file_base}/templates/addons/calico-ipv6/values.yaml"
values_file = f"{CAPZ_BASE_CONTENT_URL}/templates/addons/calico-ipv6/values.yaml"
elif cidr0 and cidr1:
interface0 = ipaddress.ip_interface(cidr0)
interface1 = ipaddress.ip_interface(cidr1)
if interface0.version == 6 and interface1.version == 4:
cidr0, cidr1 = cidr1, cidr0
values_file = f"{file_base}/templates/addons/calico-dual-stack/values.yaml"
values_file = f"{CAPZ_BASE_CONTENT_URL}/templates/addons/calico-dual-stack/values.yaml"
if interface0.version == 4 and interface1.version == 6:
values_file = f"{file_base}/templates/addons/calico-dual-stack/values.yaml"
values_file = f"{CAPZ_BASE_CONTENT_URL}/templates/addons/calico-dual-stack/values.yaml"
helminfo = HelmInfo(
repo_name="projectcalico",
repo_url="https://docs.tigera.io/calico/charts",
chart_name="calico",
chart="projectcalico/tigera-operator",
values_file=values_file,
args=["--namespace", "tigera-operator", "--create-namespace"]
args=["--namespace", "tigera-operator", "--create-namespace", "--version", os.environ.get("CALICO_VERSION", DEFAULT_CALICO_VERSION)]
)
if cidr0:
helminfo.args.extend(["--set-string", f"installation.calicoNetwork.ipPools[0].cidr={cidr0}"])
Expand All @@ -639,12 +639,17 @@ def install_cni_windows(cmd, workload_cfg, msg):
configmap = get_configmap(workload_cfg, "kubeadm-config", "kube-system")
configmap = configmap.replace("namespace: kube-system", "namespace: calico-system")
create_configmap(workload_cfg, configmap)
calico_manifest = "https://raw.githubusercontent.com/kubernetes-sigs/cluster-api-provider-azure/release-1.7/templates/addons/windows/calico/calico.yaml" # pylint: disable=line-too-long
calico_manifest = f"{CAPZ_BASE_CONTENT_URL}/templates/addons/windows/calico/calico.yaml" # pylint: disable=line-too-long
apply_kubernetes_manifest(cmd, calico_manifest, workload_cfg, msg)

calico_version = os.environ.get("CALICO_VERSION", DEFAULT_CALICO_VERSION)
update_kubernetes_image(cmd, "calico-system", "daemonSet/calico-node-windows", "install-cni", f"sigwindowstools/calico-install:{calico_version}-hostprocess", workload_cfg, msg)
update_kubernetes_image(cmd, "calico-system", "daemonSet/calico-node-windows", "calico-node-startup", f"sigwindowstools/calico-node:{calico_version}-hostprocess", workload_cfg, msg)
update_kubernetes_image(cmd, "calico-system", "daemonSet/calico-node-windows", "calico-node-felix", f"sigwindowstools/calico-node:{calico_version}-hostprocess", workload_cfg, msg)


def install_windows_kubeproxy(cmd, args, workload_cfg, msg):
kubeproxy_manifest_url = "https://raw.githubusercontent.com/kubernetes-sigs/cluster-api-provider-azure/release-1.7/templates/addons/windows/calico/kube-proxy-windows.yaml" # pylint: disable=line-too-long
kubeproxy_manifest_url = f"{CAPZ_BASE_CONTENT_URL}/templates/addons/windows/calico/kube-proxy-windows.yaml" # pylint: disable=line-too-long
kubeproxy_manifest_file = "kube-proxy-windows.yaml"
manifest = render_custom_cluster_template(kubeproxy_manifest_url, kubeproxy_manifest_file, args)
write_to_file(kubeproxy_manifest_file, manifest)
Expand Down Expand Up @@ -724,6 +729,17 @@ def apply_kubernetes_manifest(cmd, manifest, workload_cfg, msg):
raise ResourceNotFoundError(err_msg) from err


def update_kubernetes_image(cmd, namespace, resource_name, container_name, new_image_name, workload_cfg, msg):
begin_msg, end_msg, err_msg = message_variants(msg)
attempts, delay = 100, 3
with Spinner(cmd, begin_msg, end_msg):
command = ["kubectl", "set", "image", "-n", namespace, resource_name, f"{container_name}={new_image_name}", "--kubeconfig", workload_cfg]
try:
retry_shell_command(command, attempts, delay)
except subprocess.CalledProcessError as err:
raise ResourceNotFoundError(err_msg) from err


def install_helm_chart(cmd, helminfo, workload_cfg, msg):
begin_msg, end_msg, err_msg = message_variants(msg)
attempts, delay = 100, 3
Expand Down
2 changes: 2 additions & 0 deletions src/capi/azext_capi/helpers/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
KUBECONFIG = "KUBECONFIG"
AKS_INFRA_RG_NAME = "AKS_INFRA_RG_NAME"
AKS_VNET_NAME = "AKS_VNET_NAME"
CAPZ_BASE_CONTENT_URL = "https://raw.githubusercontent.com/kubernetes-sigs/cluster-api-provider-azure/release-1.7"
DEFAULT_CALICO_VERSION = "v3.25.1"

0 comments on commit 51f5aa5

Please sign in to comment.