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

Garbage collect LB resources for GCE #71812

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 27 additions & 12 deletions cluster/gce/delete-stranded-load-balancers.sh
Expand Up @@ -14,22 +14,37 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# A utility for deleting target pools and forwarding rules that are unattached to VMs
# A utility for deleting stranded load balancer resources in GCE
PROJECT=${PROJECT:-kubernetes-jenkins}
REGION=${REGION:-us-central1}

# Deleting external load balancer resources
Copy link
Contributor

Choose a reason for hiding this comment

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

This is external L4 right?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes. basically it's the gce resources created when user creates an external k8s LB.

LIST=$(gcloud --project=${PROJECT} compute target-pools list --format='value(name)')

result=0
for x in ${LIST}; do
if ! gcloud compute --project=${PROJECT} target-pools get-health "${x}" --region=${REGION} 2>/dev/null >/dev/null; then
echo DELETING "${x}"
gcloud compute --project=${PROJECT} firewall-rules delete "k8s-fw-${x}" -q
gcloud compute --project=${PROJECT} forwarding-rules delete "${x}" --region=${REGION} -q
gcloud compute --project=${PROJECT} addresses delete "${x}" --region=${REGION} -q
gcloud compute --project=${PROJECT} target-pools delete "${x}" --region=${REGION} -q
result=1
fi
# Check the existence of vm instance to see if the load balancer resources are
# actively used. Only delete them if they are not.
if ! gcloud compute --project=${PROJECT} target-pools get-health "${x}" --region=${REGION} 2>/dev/null >/dev/null; then
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add some comments here? Its hard to follow the logic.

Copy link
Member Author

Choose a reason for hiding this comment

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

added comments.

echo DELETING LB "${x}"
gcloud compute --project=${PROJECT} firewall-rules delete "k8s-fw-${x}" -q
gcloud compute --project=${PROJECT} forwarding-rules delete "${x}" --region=${REGION} -q
gcloud compute --project=${PROJECT} addresses delete "${x}" --region=${REGION} -q
gcloud compute --project=${PROJECT} target-pools delete "${x}" --region=${REGION} -q
fi
done

# Deleting internal load balancer resources
Copy link
Contributor

Choose a reason for hiding this comment

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

This is external L7 right?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's an internal L4.

ILB_LIST=$(gcloud --project=${PROJECT} compute backend-services list --format='value(name)')
for x in ${ILB_LIST}; do
# Check the existence of vm instance to see if the load balancer resources are
# actively used. Only delete them if they are not.
if ! gcloud compute --project=${PROJECT} backend-services get-health "${x}" --region=${REGION} 2>/dev/null >/dev/null; then
echo DELETING internal LB "${x}"
ig=$(gcloud compute --project=${PROJECT} backend-services list --regions=${REGION} 2>/dev/null | grep "${x}" | awk '{print $2}' | cut -d'/' -f3)
zone=$(gcloud compute --project=${PROJECT} instance-groups unmanaged list | grep ${ig} | awk '{print $2}')
gcloud compute --project=${PROJECT} firewall-rules delete "${x}" -q
gcloud compute --project=${PROJECT} forwarding-rules delete "${x}" --region=${REGION} -q
gcloud compute --project=${PROJECT} backend-services delete "${x}" --region=${REGION} -q
gcloud compute --project=${PROJECT} instance-groups unmanaged delete "${ig}" --zone=${zone} -q
fi
done
exit ${result}