Skip to content

Commit

Permalink
Update kubernetes and use NetworkingV1Api for Ingress (#142)
Browse files Browse the repository at this point in the history
- Updated `kubernetes` library
- Updated ingress codepaths
- Updated helm, aws-cli, docker-compose v2
  • Loading branch information
jarojasm95 committed Sep 19, 2022
1 parent 82f4b04 commit 40416be
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 150 deletions.
13 changes: 7 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ RUN python -m venv /root/.venv

ENV PATH /root/.venv/bin:$PATH
# also specified around line 48
ARG POETRY_VERSION=1.1.14
ARG POETRY_VERSION=1.2.1
ENV PATH /root/.local/bin:$PATH
RUN pip install --upgrade pip setuptools wheel && \
curl -sSL https://install.python-poetry.org -o install-poetry.py && \
Expand Down Expand Up @@ -43,18 +43,19 @@ RUN apt-get update && \
vim-nox \
curl \
ssh \
unzip
unzip \
wget

# also specified around line 15
ARG POETRY_VERSION=1.1.14
ARG POETRY_VERSION=1.2.1
ENV PATH /root/.local/bin:$PATH
RUN pip install --upgrade pip setuptools wheel && \
curl -sSL https://install.python-poetry.org -o install-poetry.py && \
python install-poetry.py --version $POETRY_VERSION

# Update all needed tool versions here

ARG AWS_CLI_VERSION=2.7.1
ARG AWS_CLI_VERSION=2.7.24
RUN curl https://awscli.amazonaws.com/awscli-exe-linux-$(uname -m)-$AWS_CLI_VERSION.zip -o awscliv2.zip && \
unzip awscliv2.zip && \
./aws/install && \
Expand All @@ -74,15 +75,15 @@ ARG DOCKER_COMPOSE_VERSION=1.29.2
RUN curl -L "https://github.com/docker/compose/releases/download/$DOCKER_COMPOSE_VERSION/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && \
chmod 755 /usr/local/bin/docker-compose

ARG DOCKER_COMPOSE_2_VERSION=v2.9.0
ARG DOCKER_COMPOSE_2_VERSION=v2.11.0
RUN curl -L "https://github.com/docker/compose/releases/download/$DOCKER_COMPOSE_2_VERSION/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose-2 && \
chmod 755 /usr/local/bin/docker-compose-2

ARG KUBE_VERSION=1.19.7
RUN curl -L -o /usr/local/bin/kubectl https://storage.googleapis.com/kubernetes-release/release/v$KUBE_VERSION/bin/linux/$(dpkg --print-architecture)/kubectl && \
chmod 755 /usr/local/bin/kubectl

ARG HELM_VERSION=3.9.2
ARG HELM_VERSION=3.9.4
RUN curl -fsSL https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 -o get-helm-3.sh && \
chmod 700 get-helm-3.sh && \
./get-helm-3.sh --version v${HELM_VERSION}
Expand Down
10 changes: 6 additions & 4 deletions aladdin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ function check_and_handle_init() {
if "$ALADDIN_MANAGE_SOFTWARE_DEPENDENCIES"; then
"$SCRIPT_DIR"/infra_k8s_check.sh $infra_k8s_check_args
fi
check_or_start_k3d
if "$IS_LOCAL"; then
check_or_start_k3d
fi
}

function _start_k3d() {
Expand Down Expand Up @@ -321,13 +323,13 @@ while [[ $# -gt 0 ]]; do
shift # past argument or value
done

exec_host_command "$@"
set_cluster_helper_vars
get_host_addr
exec_host_plugin "$@"
check_cluster_alias
get_config_variables
exec_host_command "$@"
exec_host_plugin "$@"
check_and_handle_init
set_cluster_helper_vars
handle_ostypes
prepare_volume_mount_options
prepare_ssh_options
Expand Down
4 changes: 2 additions & 2 deletions aladdin/lib/arg_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import functools
import json
import os
import subprocess
import sys
import pkg_resources
from contextlib import suppress
Expand Down Expand Up @@ -43,7 +42,8 @@ def add_namespace_argument(arg_parser):
def bash_wrapper():
_, *args = sys.argv
handler = PROJECT_ROOT / "aladdin.sh"
subprocess.run([str(handler), *args])
os.environ["PYTHONPATH"] = ":".join(sys.path)
os.execv(str(handler), ["aladdin.sh", *args])


def container_command(func=None):
Expand Down
42 changes: 29 additions & 13 deletions aladdin/lib/k8s/ingress.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def _create_ingress_service_tuples(services, dual_dns_prefix_annotation_name):
if dual_dns_prefix_annotation_name:
try:
# create service tuple from annotation dns
annotation_name = service.metadata.annotations[dual_dns_prefix_annotation_name]
annotation_name = service.metadata.annotations[
dual_dns_prefix_annotation_name
]
except (AttributeError, KeyError, TypeError):
pass # no annotation found
else:
Expand All @@ -26,33 +28,47 @@ def _create_ingress_service_tuples(services, dual_dns_prefix_annotation_name):


def build_ingress(services, dns_suffix, dual_dns_prefix_annotation_name, ingress_info):
ingress = client.NetworkingV1beta1Ingress()
ingress = client.V1Ingress()
# init metadata
ingress.metadata = client.V1ObjectMeta()
ingress.metadata.name = ingress_info["ingress_name"]
# init spec
ingress.spec = client.NetworkingV1beta1IngressSpec()
ingress.spec = client.V1IngressSpec()
ingress.spec.rules = []
service_tuples = _create_ingress_service_tuples(services, dual_dns_prefix_annotation_name)
service_tuples = _create_ingress_service_tuples(
services, dual_dns_prefix_annotation_name
)
# A little bit of a hack to have the ingress put the port:443 entries before the port:80 entries,
# so that the port:80 entries take precedence if the service name is the same. Without this
# we get ssl errors when accessing services behind the ingress locally because of k3d internals
service_tuples = sorted(service_tuples, key = lambda x: x[1], reverse=True)
service_tuples = sorted(service_tuples, key=lambda x: x[1], reverse=True)
for dns_prefix, port, service in service_tuples:
ingress_rule = client.NetworkingV1beta1IngressRule()
ingress_rule = client.V1IngressRule()
ingress_rule.host = "%s.%s" % (dns_prefix, dns_suffix)
backend = client.NetworkingV1beta1IngressBackend(
service_name=service.metadata.name, service_port=port
backend = client.V1IngressBackend(
service=client.V1IngressServiceBackend(
port=client.V1ServiceBackendPort(
number=port,
),
name=service.metadata.name,
)
)
ingress_path = [client.NetworkingV1beta1HTTPIngressPath(path="/", backend=backend)]
ingress_rule.http = client.NetworkingV1beta1HTTPIngressRuleValue(ingress_path)
ingress_rule.http = client.V1HTTPIngressRuleValue([
client.V1HTTPIngressPath(
path="/", backend=backend, path_type="ImplementationSpecific"
)
])

ingress.spec.rules.append(ingress_rule)

if not ingress.spec.rules:
ingress_dummy_backend = client.NetworkingV1beta1IngressBackend(
service_name=ingress_info["ingress_controller_service_name"], service_port=80
ingress.spec.backend = client.V1IngressBackend(
service=client.V1IngressServiceBackend(
port=client.V1ServiceBackendPort(
number=80,
),
name=ingress_info["ingress_controller_service_name"],
)
)
ingress.spec.backend = ingress_dummy_backend

return ingress
10 changes: 5 additions & 5 deletions aladdin/lib/k8s/kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(
configuration.assert_hostname = False
self.core_v1_api = client.CoreV1Api()
self.apps_v1_api = client.AppsV1Api()
self.networking_v1_beta1_api = client.NetworkingV1beta1Api()
self.networking_v1_api = client.NetworkingV1Api()
self.namespace = namespace or get_current_namespace()

def _kub_cmd(self, *args):
Expand Down Expand Up @@ -109,7 +109,7 @@ def get_objects(self, obj_type, label_val=None, label_key=None):
if obj_type == "deployment":
get_func = getattr(self.apps_v1_api, get_func_name)
elif obj_type == "ingress":
get_func = getattr(self.networking_v1_beta1_api, get_func_name)
get_func = getattr(self.networking_v1_api, get_func_name)
else:
get_func = getattr(self.core_v1_api, get_func_name)
# Create a label selector filter if label_val was specified
Expand Down Expand Up @@ -260,13 +260,13 @@ def get_ingress(self, label_val=None, label_key=None, default=None):
return (self.get_ingresses(label_val, label_key) + [default])[0]

def create_ingress(self, body):
self.networking_v1_beta1_api.create_namespaced_ingress(self.namespace, body)
self.networking_v1_api.create_namespaced_ingress(self.namespace, body)

def update_ingress(self, name, body):
self.networking_v1_beta1_api.patch_namespaced_ingress(name, self.namespace, body)
self.networking_v1_api.patch_namespaced_ingress(name, self.namespace, body)

def delete_ingress(self, name):
self.networking_v1_beta1_api.delete_namespaced_ingress(
self.networking_v1_api.delete_namespaced_ingress(
name, self.namespace, body=client.V1DeleteOptions()
)

Expand Down
Loading

0 comments on commit 40416be

Please sign in to comment.