@@ -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 " " $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 " 
263+       --grace-period=0 --force
234264fi 
235265
236266${KUBECTL}  rollout status --namespace=" ${NAMESPACE} " 
@@ -256,10 +286,10 @@ NETBOX_API_URL="http://netbox.${NAMESPACE}.svc.cluster.local"
256286PATCHED_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
278308mv " $PATCHED_TMP_JOB_YAML " " $TMP_JOB_YAML " 
279309
0 commit comments