I have a demo where I build a Kubernetes cluster on Container Engine to run a LAMP app. In the demo, I script out a complete build process from an empty project to the full running app. Testing this requires a clean up that takes me all the way back to an empty project with no cluster.
There is one thing I do not tear down – static IP addresses. I don’t tear these down because they are locked to host names in Google Domains, and I use those IPs in my Kubernetes setup to make sure that my cluster app is available at a nice URL and not just a randomly assigned IP.
But I have been running into a problem with this. Sometimes the static IPs hold on to Forwarding Rules that are autogenerated with crazy randomized names by Container Engine. It appears to happen only when I do a full clean. I suspect that I am deleting the cluster before it has a chance to issue the command to delete the forwarding rules itself.
In any case, I got tired of dealing with this manually, so I made a Makefile solution. First I get the dynamic list of crazy random forwarding rule names using the Makefile technique I outlined earlier. Then I pass that list to a gcloud command:
RULELIST = $(shell gcloud compute forwarding-rules list --format='value[terminator=" "](name)')
-gcloud compute forwarding-rules delete $(RULELIST) --region $(REGION) -q
Note that I had to make sure I passed a region, otherwise the command would have prompted me to enter it manually.