Skip to content

Instantly share code, notes, and snippets.

@joemiller
Last active April 12, 2016 03:22
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 joemiller/d25f45481758338f47416f1b5ef412b8 to your computer and use it in GitHub Desktop.
Save joemiller/d25f45481758338f47416f1b5ef412b8 to your computer and use it in GitHub Desktop.
helper for making kubernetes secrets yaml files. Supports key/val strings and file contents
#!/usr/bin/env ruby
#
# helper for making kubernetes secrets yaml files. Supports key/val strings and file contents
#
# Example:
# --------
#
# $ echo 'top secret stuff right here' >secret.txt
# $ ./k8s-secrets.rb secret-volume @secret.txt foo=bar biz=bop
#
# ---
# apiVersion: v1
# kind: Secret
# metadata:
# name: secret-volume
# type: Opaque
# data:
# secret.txt: |
# dG9wIHNlY3JldCBzaGl0IHJpZ2h0IGhlcmUK
# foo: |
# YmFy
# biz: |
# Ym9w
#
# Author: joe miller / https://github.com/joemiller
require 'base64'
require 'yaml'
secret_spec = {
'apiVersion' => 'v1',
'kind' => 'Secret',
'metadata' => {
'name' => ''
},
'type' => 'Opaque',
'data' => {}
}
if ARGV.length < 2
puts "Usage: k8s-secrets <secret-vol-name> <key1=val1> <@file1> ..."
exit(1)
end
secret_spec['metadata']['name'] = ARGV.shift
ARGV.each do |i|
case i
when /^@/
key = i[1..-1]
data = File.open(key).read()
when /=/
key, data = i.split(/=/)
else
puts "Unrecognized format '#{i}'. Should be either 'key=value' or '@filename'."
exit(1)
end
secret_spec['data'][key] = Base64.encode64(data)
end
puts YAML.dump(secret_spec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment