Skip to content

Allow overriding image registry and Helm chart sources in NetBox deploy script #296

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

Draft
wants to merge 2 commits into
base: feature/deploy-netbox-vcluster
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
51 changes: 44 additions & 7 deletions kind/deploy-netbox.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ set -e -u -o pipefail
# • a local kind cluster (preloading images)
# • a virtual cluster using vcluster: https://github.com/loft-sh/vcluster ( used for testing pipeline, loading of images not needed )

NETBOX_HELM_CHART="https://github.com/netbox-community/netbox-chart/releases/download/netbox-5.0.0-beta.169/netbox-5.0.0-beta.169.tgz" # default value
# Allow override via environment variable, otherwise fallback to default
NETBOX_HELM_CHART="${NETBOX_HELM_CHART:-https://github.com/netbox-community/netbox-chart/releases/download/netbox-5.0.0-beta.169/netbox-5.0.0-beta.169.tgz}"

if [[ $# -lt 3 || $# -gt 4 ]]; then
echo "Usage: $0 <CLUSTER> <VERSION> <NAMESPACE> [--vcluster]"
Expand Down Expand Up @@ -42,7 +43,8 @@ if [[ "${VERSION}" == "3.7.8" ]] ;then
"ghcr.io/zalando/postgres-operator:v1.12.2" \
"ghcr.io/zalando/spilo-16:3.2-p3" \
)
NETBOX_HELM_CHART="https://github.com/netbox-community/netbox-chart/releases/download/netbox-5.0.0-beta5/netbox-5.0.0-beta5.tgz"
# Allow override via environment variable, otherwise fallback to default
NETBOX_HELM_CHART="${NETBOX_HELM_CHART:-https://github.com/netbox-community/netbox-chart/releases/download/netbox-5.0.0-beta5/netbox-5.0.0-beta5.tgz}"

# patch load-data.sh
sed 's/netbox-demo-v4.1.sql/netbox-demo-v3.7.sql/g' $(dirname "$0")/load-data-job/load-data.orig.sh > $(dirname "$0")/load-data-job/load-data.sh && chmod +x $(dirname "$0")/load-data-job/load-data.sh
Expand All @@ -59,7 +61,8 @@ elif [[ "${VERSION}" == "4.0.11" ]] ;then
"ghcr.io/zalando/postgres-operator:v1.12.2" \
"ghcr.io/zalando/spilo-16:3.2-p3" \
)
NETBOX_HELM_CHART="https://github.com/netbox-community/netbox-chart/releases/download/netbox-5.0.0-beta.84/netbox-5.0.0-beta.84.tgz"
# Allow override via environment variable, otherwise fallback to default
NETBOX_HELM_CHART="${NETBOX_HELM_CHART:-https://github.com/netbox-community/netbox-chart/releases/download/netbox-5.0.0-beta.84/netbox-5.0.0-beta.84.tgz}"

# patch load-data.sh
sed 's/netbox-demo-v4.1.sql/netbox-demo-v4.0.sql/g' $(dirname "$0")/load-data-job/load-data.orig.sh > $(dirname "$0")/load-data-job/load-data.sh && chmod +x $(dirname "$0")/load-data-job/load-data.sh
Expand Down Expand Up @@ -98,7 +101,12 @@ fi

# build image for loading local data via NetBox API
cd "$(dirname "$0")/load-data-job"
docker build -t netbox-load-local-data:1.0 --load --no-cache --progress=plain -f ./dockerfile .
docker build -t netbox-load-local-data:1.0 \
--load --no-cache --progress=plain \
--build-arg PYTHON_BASE_IMAGE="${PYTHON_BASE_IMAGE:-python:3.12}" \
--build-arg ARTIFACTORY_PYPI_URL="${ARTIFACTORY_PYPI_URL:-}" \
--build-arg ARTIFACTORY_TRUSTED_HOST="${ARTIFACTORY_TRUSTED_HOST:-}" \
-f ./dockerfile .
cd -

# Load local images into Kind only if not vCluster
Expand All @@ -115,13 +123,15 @@ else
fi

# Install Postgres Operator
# Allow override via environment variable, otherwise fallback to default
POSTGRESS_OPERATOR_HELM_CHART="${POSTGRESS_OPERATOR_HELM_CHART:-https://opensource.zalando.com/postgres-operator/charts/postgres-operator/postgres-operator-1.12.2.tgz}"
${HELM} upgrade --install postgres-operator \
--namespace="${NAMESPACE}" \
--create-namespace \
--set podPriorityClassName.create=false \
--set podServiceAccount.name="postgres-pod-${NAMESPACE}" \
--set serviceAccount.name="postgres-operator-${NAMESPACE}" \
https://opensource.zalando.com/postgres-operator/charts/postgres-operator/postgres-operator-1.12.2.tgz
"${POSTGRESS_OPERATOR_HELM_CHART}"

# Deploy the database
${KUBECTL} apply --namespace="${NAMESPACE}" -f "$(dirname "$0")/netbox-db.yaml"
Expand Down Expand Up @@ -150,8 +160,35 @@ else
| ${KUBECTL} apply -f -
fi

${KUBECTL} apply -n "${NAMESPACE}" \
-f "$(dirname "$0")/load-data-job.yaml"
JOB_DIR="$(dirname "$0")/job"

cd "$JOB_DIR"
kustomize edit set image ghcr.io/zalando/spilo-16="${SPILO_IMAGE:-ghcr.io/zalando/spilo-16:3.2-p3}"

# Create a patch file to inject NETBOX_SQL_DUMP_URL (from env or default)
SQL_DUMP_URL="${NETBOX_SQL_DUMP_URL:-https://raw.githubusercontent.com/netbox-community/netbox-demo-data/master/sql/netbox-demo-v4.1.sql}"

# Create patch
cat > sql-env-patch.yaml <<EOF
apiVersion: batch/v1
kind: Job
metadata:
name: netbox-demo-data-load-job
spec:
template:
spec:
containers:
- name: netbox-demo-data-load
env:
- name: NETBOX_SQL_DUMP_URL
value: "${SQL_DUMP_URL}"
EOF

# Add the patch
kustomize edit add patch --path sql-env-patch.yaml

# Apply the customized job
kustomize build . | ${KUBECTL} apply -n "${NAMESPACE}" -f -

${KUBECTL} wait \
-n "${NAMESPACE}" --for=condition=complete --timeout=600s job/netbox-demo-data-load-job
Expand Down
11 changes: 11 additions & 0 deletions kind/job/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resources:
- load-data-job.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: ghcr.io/zalando/spilo-16
newName: mobile-docker-virtual.artifactory.swisscom.com/zalando/spilo-16
newTag: 3.2-p3
patches:
- path: sql-env-patch.yaml
File renamed without changes.
27 changes: 24 additions & 3 deletions kind/load-data-job/dockerfile.orig
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
FROM python:3.12
ARG PYTHON_BASE_IMAGE=python:3.12
FROM ${PYTHON_BASE_IMAGE}

# Optional Artifactory pip index
ARG ARTIFACTORY_PYPI_URL=""
ARG ARTIFACTORY_TRUSTED_HOST=""

# Set envs so they’re usable in RUN commands
ENV ARTIFACTORY_PYPI_URL=${ARTIFACTORY_PYPI_URL}
ENV ARTIFACTORY_TRUSTED_HOST=${ARTIFACTORY_TRUSTED_HOST}

ADD main.py .
RUN pip install -Iv pynetbox==7.4.1
CMD ["python", "./main.py"]

# Conditional pip install using Artifactory if set
RUN if [ -n "$ARTIFACTORY_PYPI_URL" ]; then \
echo "Using Artifactory pip index: $ARTIFACTORY_PYPI_URL"; \
pip install -Iv pynetbox==7.4.1 \
--index-url="$ARTIFACTORY_PYPI_URL" \
--trusted-host="$ARTIFACTORY_TRUSTED_HOST"; \
else \
echo "Using public PyPI"; \
pip install -Iv pynetbox==7.4.1; \
fi

CMD ["python", "./main.py"]
11 changes: 10 additions & 1 deletion kind/load-data-job/load-data.orig.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
#!/bin/sh
set -o errexit

# Allow override of demo SQL file URL
NETBOX_SQL_DUMP_URL="${NETBOX_SQL_DUMP_URL:-https://raw.githubusercontent.com/netbox-community/netbox-demo-data/master/sql/netbox-demo-v4.1.sql}"

TMP_SQL_FILE=$(mktemp /tmp/netbox-data-dump.XXXXXXX.sql) || exit 1
curl -k https://raw.githubusercontent.com/netbox-community/netbox-demo-data/master/sql/netbox-demo-v4.1.sql > "${TMP_SQL_FILE}"

# Download the SQL dump
curl -k "${NETBOX_SQL_DUMP_URL}" > "${TMP_SQL_FILE}"

# Load it into the database
psql "user=netbox host=netbox-db.${NAMESPACE}.svc.cluster.local" netbox -q -f "${TMP_SQL_FILE}"
rm "${TMP_SQL_FILE}"

# Load additional local data
psql "user=netbox host=netbox-db.${NAMESPACE}.svc.cluster.local" netbox -q -f /load-data-job/local-data-setup.sql
Loading