#!/usr/bin/env ruby require 'json' require_relative '../lib/configgin' options = {} parser = Cli.make_option_parser(options) parser.parse!(ARGV) begin Cli.check_opts(options) rescue ArgMissingError => e STDERR.puts("missing argument: #{e}") exit(1) end job_configs = JSON.parse(File.read(options[:jobs])) templates = YAML.load_file(options[:env2conf]) # SVC_ACC_PATH is the location of the service account secrets SVC_ACC_PATH = '/var/run/secrets/kubernetes.io/serviceaccount'.freeze kube_client = Kubeclient::Client.new( URI::HTTPS.build(host: ENV['KUBERNETES_SERVICE_HOST'], port: ENV['KUBERNETES_SERVICE_PORT_HTTPS']), 'v1', ssl_options: { ca_file: "#{SVC_ACC_PATH}/ca.crt", verify_ssl: OpenSSL::SSL::VERIFY_PEER }, auth_options: { bearer_token: File.read("#{SVC_ACC_PATH}/token") } ) kube_client_stateful_set = Kubeclient::Client.new( URI::HTTPS.build(host: ENV['KUBERNETES_SERVICE_HOST'], port: ENV['KUBERNETES_SERVICE_PORT_HTTPS'], path: '/apis/apps'), 'v1beta1', ssl_options: { ca_file: "#{SVC_ACC_PATH}/ca.crt", verify_ssl: OpenSSL::SSL::VERIFY_PEER }, auth_options: { bearer_token: File.read("#{SVC_ACC_PATH}/token") } ) kube_namespace = File.read("#{SVC_ACC_PATH}/namespace") jobs = {} job_configs.each do |job, job_config| base_config = JSON.parse(File.read(job_config['base'])) begin bosh_spec = EnvironmentConfigTransmogrifier.transmogrify(base_config, templates, secrets: '/etc/secrets') rescue NonHashValueOverride => e STDERR.puts e.to_s STDERR.puts "Error generating #{job}: #{outfile} from #{infile}" exit 1 end jobs[job] = Job.new(bosh_spec, kube_namespace, kube_client, kube_client_stateful_set) end exported_properties = Hash[jobs.map { |name, job| [name, job.exported_properties] }] kube_client.patch_pod( ENV['HOSTNAME'], { metadata: { annotations: { :'skiff-exported-properties' => exported_properties.to_json } } }, kube_namespace ) jobs.each do |job_name, job| dns_encoder = KubeDNSEncoder.new(job.spec['links']) job_configs[job_name]['files'].each do |infile, outfile| job.generate(infile, outfile, dns_encoder) end end