Skip to content

Instantly share code, notes, and snippets.

@FreedomBen
Last active June 5, 2020 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FreedomBen/14390eecbf0a0f1b012744b4154628b3 to your computer and use it in GitHub Desktop.
Save FreedomBen/14390eecbf0a0f1b012744b4154628b3 to your computer and use it in GitHub Desktop.
Clone an OpenShift app (rough script that will need tweaking)
#!/usr/bin/env ruby
require 'json'
require 'yaml'
APP_LABEL = "mtls-nginx-example"
OUTPUT_FILENAME = "clone-output.yaml"
#OC_CONFIG = ""
OC_CONFIG = "--config ~/.kube/devcluster.conf"
def oc_get_all
all = JSON.parse(`oc #{OC_CONFIG} get all -lapp=#{APP_LABEL} -o json`.chomp)
cms = JSON.parse(`oc #{OC_CONFIG} get cm -lapp=#{APP_LABEL} -o json`.chomp)
all["items"].concat(cms["items"])
all
end
def remove_pods(all)
items = all["items"].select do |item|
item["kind"] != "Pod"
end
all["items"] = items
end
def remove_replica_sets(all)
items = all["items"].select do |item|
item["kind"] != "ReplicaSet"
end
all["items"] = items
end
def remove_replication_controllers(all)
items = all["items"].select do |item|
item["kind"] != "ReplicationController"
end
all["items"] = items
end
def remove_statuses(all)
items = all["items"].each do |item|
item.delete("status")
end
all["items"] = items
end
def clean_metadata(all)
items = all["items"]
items.map! do |item|
md = item["metadata"]
md.delete("selfLink")
md.delete("uid")
md.delete("resourceVersion")
md.delete("creationTimestamp")
md.delete("annotations")
md.delete("generation")
item["metadata"] = md
item
end
all["items"] = items
all.delete("metadata")
end
def main(args)
all = oc_get_all
# These mutate the argument
remove_pods(all)
remove_replica_sets(all)
remove_replication_controllers(all)
remove_statuses(all)
clean_metadata(all)
File.write(OUTPUT_FILENAME, all.to_yaml)
puts "Operation successful. Wrote output to file '#{OUTPUT_FILENAME}'"
end
main(ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment