Created
March 1, 2019 12:03
-
-
Save digitalronin/42daeb6a5f6272cf2258fce998808e10 to your computer and use it in GitHub Desktop.
Ruby script to find k8s namespaces which might be old sandboxes, and where we could ask the developer if it's still needed. ww
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Script to find namespaces which are candidates for deletion (because they might be developer | |
# sandbox namespaces). | |
# | |
# USAGE: | |
# | |
# kubectl get namespaces --output json | ./find-namespaces-to-delete.rb | |
# | |
require 'json' | |
require 'date' | |
AGE_THRESHOLD = 100 # days | |
def main | |
hash = JSON.parse(STDIN.read) | |
namespaces = hash['items'].find_all { |namespace| is_deletion_candidate?(namespace) } | |
if namespaces.any? | |
puts | |
puts "The following namespaces are non-production, have dev-ish names, and are > #{AGE_THRESHOLD} days old:" | |
puts | |
namespaces.each {|n| puts ' ' + n['metadata']['name']} | |
puts | |
end | |
end | |
def is_deletion_candidate?(namespace) | |
!(is_production?(namespace)) \ | |
&& looks_like_sandbox?(namespace) \ | |
&& is_old?(namespace) | |
end | |
def is_production?(namespace) | |
labels = namespace['metadata']['labels'] | |
labels && labels['cloud-platform.justice.gov.uk/is-production'] == 'true' | |
end | |
def looks_like_sandbox?(namespace) | |
namespace['metadata']['name'] =~ /myapp|dev|example|dummy/ | |
end | |
def is_old?(namespace) | |
age_in_days(namespace) > AGE_THRESHOLD | |
end | |
def age_in_days(namespace) | |
(Date.today - Date.parse(namespace['metadata']['creationTimestamp'])).to_i | |
end | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment