Skip to content

Commit 0b5793b

Browse files
Cleanup old ReplicaSets after NetBox deployment patch to prevent volume Multi-Attach errors (#345)
* Cleanup old ReplicaSets after NetBox deployment patch to prevent volume Multi-Attach errors * Updating size of NetBox persistent volume --------- Co-authored-by: Pablo Garcia Miranda <pablo.garciamiranda@swisscom.com>
1 parent 7cd6103 commit 0b5793b

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

kind/deploy-netbox.sh

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,36 @@ if [[ "$FORCE_NETBOX_NGINX_IPV4" == "true" ]]; then
231231
}
232232
]'
233233

234+
# Cleanup old ReplicaSets after NetBox deployment patch to prevent volume Multi-Attach errors
235+
DEPLOYMENT_NAME="netbox"
236+
# Get all ReplicaSets in JSON
237+
RS_JSON=$(eval "$KUBECTL get rs -n $NAMESPACE -l app.kubernetes.io/component=netbox -o json" | sed '/^{/,$!d')
238+
239+
# Extract the latest one
240+
LATEST_RS=$(echo "$RS_JSON" | jq -r --arg DEPLOYMENT "$DEPLOYMENT_NAME" '
241+
.items
242+
| map(select(.metadata.ownerReferences[]?.kind == "Deployment" and .metadata.ownerReferences[]?.name == $DEPLOYMENT))
243+
| sort_by(.metadata.creationTimestamp)
244+
| last
245+
| .metadata.name
246+
')
247+
248+
echo "Current (latest) ReplicaSet is: $LATEST_RS"
249+
250+
# Delete older ones
251+
echo "$RS_JSON" | jq -r --arg DEPLOYMENT "$DEPLOYMENT_NAME" --arg LATEST "$LATEST_RS" '
252+
.items
253+
| map(select(
254+
.metadata.ownerReferences[]?.kind == "Deployment"
255+
and .metadata.ownerReferences[]?.name == $DEPLOYMENT
256+
and .metadata.name != $LATEST
257+
))
258+
| .[].metadata.name
259+
' | xargs -r -I{} ${KUBECTL} delete rs {} -n "$NAMESPACE"
260+
261+
echo "Forcing restart of netbox pod to reattach volume cleanly..."
262+
${KUBECTL} delete pods -n "$NAMESPACE" -l app.kubernetes.io/component=netbox \
263+
--grace-period=0 --force
234264
fi
235265

236266
${KUBECTL} rollout status --namespace="${NAMESPACE}" deployment netbox
@@ -256,10 +286,10 @@ NETBOX_API_URL="http://netbox.${NAMESPACE}.svc.cluster.local"
256286
PATCHED_TMP_JOB_YAML="$(mktemp)"
257287

258288
# Convert YAML to JSON and inject variables if containers exist
259-
yq -o=json "$TMP_JOB_YAML" | jq \
289+
yq eval -o=json "$TMP_JOB_YAML" | jq \
260290
--arg netboxApi "$NETBOX_API_URL" \
261-
--arg pypiUrl "$PYPI_REPOSITORY_URL" \
262-
--arg artifactoryHost "$ARTIFACTORY_TRUSTED_HOST" \
291+
--arg pypiUrl "${PYPI_REPOSITORY_URL:-}" \
292+
--arg artifactoryHost "${ARTIFACTORY_TRUSTED_HOST:-}" \
263293
--arg imageRegistry "${IMAGE_REGISTRY:-docker.io}" '
264294
.spec.template.spec.containers[0].env //= [] |
265295
.spec.template.spec.containers[0].image = $imageRegistry+"/python:3.12-slim" |
@@ -273,7 +303,7 @@ yq -o=json "$TMP_JOB_YAML" | jq \
273303
]
274304
else [] end
275305
)
276-
' | yq -P > "$PATCHED_TMP_JOB_YAML"
306+
' | yq eval -P - > "$PATCHED_TMP_JOB_YAML"
277307

278308
mv "$PATCHED_TMP_JOB_YAML" "$TMP_JOB_YAML"
279309

kind/netbox-db/netbox-db.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
spec:
66
teamId: "netbox"
77
volume:
8-
size: 100Mi
8+
size: 5Gi
99
numberOfInstances: 1
1010
enableMasterLoadBalancer: true
1111
users:

0 commit comments

Comments
 (0)