Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nginx ingress addon #139

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions addons.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,13 @@ microk8s-addons:
supported_architectures:
- amd64
- arm64

- name: "nginx-ingress"
description: "NGINX Ingress controller from the NGINX Team."
version: "0.16.2"
check_status: "deployment.apps/nginx-ingress-nginx-ingress"
supported_architectures:
- amd64
- arm64
- s390x
- ppc64le
17 changes: 17 additions & 0 deletions addons/nginx-ingress/disable
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
set -e

source $SNAP/actions/common/utils.sh
CURRENT_DIR=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)

NAMESPACE="nginx-ingress"
KUBECTL="$SNAP/microk8s-kubectl.wrapper"
HELM="${SNAP}/microk8s-helm3.wrapper"
echo "Disabling NGINX ingress"

# unload the the manifests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# unload the the manifests
# unload the manifests

$HELM uninstall nginx-ingress -n $NAMESPACE > /dev/null 2>&1
$KUBECTL delete secret nginx-regcred -n $NAMESPACE > /dev/null 2>&1 || true
$KUBECTL delete namespace $NAMESPACE

echo "NGINX ingress is disabled"
81 changes: 81 additions & 0 deletions addons/nginx-ingress/enable
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env bash

set -e

source $SNAP/actions/common/utils.sh
CURRENT_DIR=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
source $CURRENT_DIR/../common/utils.sh

NAMESPACE="nginx-ingress"
CHART_VERSION="0.16.2"
KUBECTL="$SNAP/microk8s-kubectl.wrapper"
HELM="${SNAP}/microk8s-helm3.wrapper"

TEMP=$(getopt -o "h" \
--long help,tls-secret:,plus-token:,values: \
-n "$(basename "$0")" -- "$@")

if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

eval set -- "$TEMP"

CERT_SECRET="${CERT_SECRET:-}"
PLUS_TOKEN="${PLUS_TOKEN:-}"
NGINX_VALUES="${NGINX_VALUES:-}"

while true; do
case "$1" in
--tls-secret ) CERT_SECRET="$2"; shift 2 ;;
--plus-token ) PLUS_TOKEN="$2"; shift 2 ;;
--values ) NGINX_VALUES="$2"; shift 2 ;;
-h | --help )
prog=$(basename -s.wrapper "$0")
echo "Usage: $prog [options...]"
echo " --tls-secret <secret> TLS Secret that will be used by the controller"
echo " Example: namespace/secret_name"
echo " --plus-token <token> JWT Image Pull Token for Nginx Plus"
echo " --values <distro> Values file to override for the nginx helm chart"
echo
exit ;;
-- ) shift; break ;;
* ) break ;;
esac
done

echo "Enabling NGINX ingress"

$HELM repo add nginx-stable https://helm.nginx.com/stable
$HELM repo update

HELM_OPTS=
if [ -n "${CERT_SECRET}" ]; then
HELM_OPTS+="--set controller.defaultTLS.secret=${CERT_SECRET} "
fi

if [ -n "${NGINX_VALUES}" ]; then
HELM_OPTS+="--values ${NGINX_VALUES} "
fi

if [ -n "${PLUS_TOKEN}" ]; then
$KUBECTL create namespace $NAMESPACE
$KUBECTL create secret docker-registry nginx-regcred --docker-server=private-registry.nginx.com --docker-username=$PLUS_TOKEN --docker-password=none -n $NAMESPACE

$HELM upgrade --install nginx-ingress nginx-stable/nginx-ingress \
--namespace $NAMESPACE \
--set controller.config.entries.external-status-address="127.0.0.1" \
--set controller.hostNetwork=true \
--set controller.image.repository=private-registry.nginx.com/nginx-ic/nginx-plus-ingress \
--set controller.serviceAccount.imagePullSecretName=nginx-regcred \
--set controller.nginxplus=true \
--version $CHART_VERSION \
${HELM_OPTS}
else
$HELM upgrade --install nginx-ingress nginx-stable/nginx-ingress \
--namespace $NAMESPACE --create-namespace \
--set controller.config.entries.external-status-address="127.0.0.1" \
--set controller.hostNetwork=true \
--version $CHART_VERSION \
${HELM_OPTS}
fi

echo "NGINX ingress is enabled"
76 changes: 76 additions & 0 deletions tests/test_nginx_ingress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import time
import os

import requests

from utils import (
kubectl,
microk8s_disable,
microk8s_enable,
update_yaml_with_arch,
wait_for_pod_state,
)


class TestNginxIngress(object):
def test_nginx_ingress(self):
"""
Sets up and validates nginx-ingress.
"""
print("Enabling nginx-ingress")
microk8s_enable("nginx-ingress")
print("Validating nginx-ingress")
self.validate_nginx_ingress()
print("Disabling nginx-ingress")
microk8s_disable("nginx-ingress")

def common_ingress(self):
"""
Perform the Ingress validations that are common for all
the Ingress controllers.
"""
attempt = 50
while attempt >= 0:
output = kubectl("get ing")
if "microbot.127.0.0.1.nip.io" in output:
break
time.sleep(5)
attempt -= 1
assert "microbot.127.0.0.1.nip.io" in output

service_ok = False
attempt = 50
while attempt >= 0:
try:
resp = requests.get("http://microbot.127.0.0.1.nip.io/")
if resp.status_code == 200 and "microbot.png" in resp.content.decode(
"utf-8"
):
service_ok = True
break
except requests.RequestException:
time.sleep(5)
attempt -= 1

assert service_ok

def validate_nginx_ingress(self):
"""
Validate nginx-ingress
"""
kubectl(
"annotate ingressclass nginx ingressclass.kubernetes.io/is-default-class=true"
)
wait_for_pod_state(
"", "nginx-ingress", "running", label="app=nginx-ingress-nginx-ingress"
)

here = os.path.dirname(os.path.abspath(__file__))
manifest = os.path.join(here, "templates", "ingress.yaml")
update_yaml_with_arch(manifest)
kubectl("apply -f {}".format(manifest))
wait_for_pod_state("", "default", "running", label="app=microbot")

self.common_ingress()

kubectl("delete -f {}".format(manifest))