Skip to content

Commit cc6cd96

Browse files
authored
Merge pull request #593 from deric/slop
Rewrite command line arguments parsing
2 parents c0857cd + d67dc90 commit cc6cd96

File tree

6 files changed

+119
-160
lines changed

6 files changed

+119
-160
lines changed

tooling/Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ RUN set -x && \
1010
git clone https://github.com/cloudflare/cfssl_trust.git /go/src/github.com/cloudflare/cfssl_trust && \
1111
echo "Build complete."
1212

13-
FROM ruby:2.3.5-alpine
13+
FROM ruby:3.0-alpine3.16
1414
COPY --from=0 /go/src/github.com/cloudflare/cfssl_trust /etc/cfssl
1515
COPY --from=0 /go/src/github.com/cloudflare/cfssl/bin/ /usr/bin
16+
RUN gem install slop
1617
COPY . /etc/k8s
1718

1819
RUN set -x && \
1920
apk --no-cache add git openssl
2021

2122
WORKDIR /mnt
2223

23-
ENTRYPOINT ["sh", "-c", "/etc/k8s/start-kubetool.sh"]
24+
ENTRYPOINT ["ruby", "/etc/k8s/kube_tool.rb"]

tooling/kube_tool.rb

Lines changed: 51 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,64 @@
11
#!/usr/bin/env ruby
22

3-
require 'optparse'
3+
require 'slop'
44
require_relative 'kube_tool/pre_checks.rb'
55
require_relative 'kube_tool/create_certs.rb'
66
require_relative 'kube_tool/clean_up.rb'
77
require_relative 'kube_tool/other_params.rb'
88

9-
options = {:os => nil,
10-
:version => nil,
11-
:container_runtime => nil,
12-
:cni_provider => nil,
13-
:cni_provider_version => nil,
14-
:etcd_initial_cluster => nil,
15-
:kube_api_advertise_address => nil,
16-
:install_dashboard => nil,
17-
:key_size => nil,
18-
}
19-
20-
parser = OptionParser.new do|opts|
21-
22-
opts.on('-o', '--os-type os-type', 'the os that kubernetes will run on') do |os|
23-
options[:os] = os;
24-
end
25-
26-
opts.on('-v', '--version version', 'the kubernetes version to install') do |version|
27-
options[:version] = version;
28-
end
29-
30-
opts.on('-r', '--container_runtime container runtime', 'the container runtime to use. this can only be docker or cri_containerd') do |container_runtime|
31-
options[:container_runtime] = container_runtime;
32-
end
33-
34-
opts.on('-c', '--cni-provider cni-provider', 'the networking provider to use, flannel, weave, calico or cilium are supported') do |cni_provider|
35-
options[:cni_provider] = cni_provider;
36-
end
37-
opts.on('-p', '--cni-provider-version [cni_provider_version]', 'the networking provider version to use, calico and cilium will use this to reference the correct deployment download link') do |cni_provider_version|
38-
options[:cni_provider_version] = cni_provider_version;
39-
end
40-
41-
opts.on('-i', '--etcd-initial-cluster etcd-initial-cluster', 'the list of servers in the etcd cluster') do | etcd_initial_cluster |
42-
options[:etcd_initial_cluster] = etcd_initial_cluster
43-
end
44-
45-
opts.on('-t', '--etcd-ip etcd_ip', 'ip address etcd will listen on') do |etcd_ip|
46-
options[:etcd_ip] = etcd_ip;
47-
end
48-
49-
opts.on('-a', '--api-address api_address', 'the ip address that kube api will listen on') do |api_address|
50-
options[:kube_api_advertise_address] = api_address;
51-
end
52-
53-
opts.on('-b', '--key-size key_size', 'Specifies the number of bits in the key to create') do |key_size|
54-
options[:key_size] = key_size
55-
end
56-
57-
opts.on('-d', '--install-dashboard dashboard', 'install the kube dashboard') do |dashboard|
58-
options[:install_dashboard] = dashboard;
59-
end
60-
61-
opts.on('-h', '--help', 'Displays Help') do
62-
puts opts
63-
exit
9+
class Kube_tool
10+
def self.parse_args
11+
begin
12+
opts = Slop.parse do |o|
13+
o.string '-o', '--os', 'The OS that Kubernetes will run on', default: ENV['OS']
14+
o.string '-v', '--version', 'The Kubernetes version to install', default: ENV['VERSION']
15+
o.string '-r', '--container_runtime', 'The container runtime to use. This can only be "docker" or "cri_containerd"', default: ENV['CONTAINER_RUNTIME']
16+
o.string '-c', '--cni_provider', 'The networking provider to use, flannel, weave, calico, calico-tigera or cilium are supported', default: ENV['CNI_PROVIDER']
17+
o.string '-p', '--cni_provider_version', 'The networking provider version to use, calico and cilium will use this to reference the correct deployment download link', default: ENV['CNI_PROVIDER_VERSION']
18+
o.string '-t', '--etcd_ip', 'The IP address etcd will listen on', default: ENV['ETCD_IP']
19+
o.string '-i', '--etcd_initial_cluster', 'The list of servers in the etcd cluster', default: ENV['ETCD_INITIAL_CLUSTER']
20+
o.string '-a', '--api_address', 'The IP address (or fact) that kube api will listen on', default: ENV['KUBE_API_ADVERTISE_ADDRESS']
21+
o.int '-b', '--key_size', 'Specifies the number of bits in the key to create', default: ENV['KEY_SIZE'].to_i
22+
o.int '--ca_algo', 'Algorithm to generate CA certificates, default: ecdsa', default: ENV['CA_ALGO']
23+
o.int '--sa_size', 'Service account key size', default: ENV['SA_SIZE'].to_i
24+
o.bool '-d', '--install_dashboard', 'Whether install the kube dashboard', default: ENV['INSTALL_DASHBOARD']
25+
o.on '-h','--help', 'print the help' do
26+
puts o
27+
exit
28+
end
29+
end
30+
31+
options = opts.to_hash
32+
options[:key_size] = 256 if options[:key_size] < 1
33+
options[:sa_size] = 2048 if options[:sa_size] < 1
34+
options[:ca_algo] ||= 'ecdsa'
35+
options[:container_runtime] ||= 'cri_containerd'
36+
options[:version] ||= '1.25.4'
37+
options[:os] ||= 'Debian'
38+
if options[:etcd_initial_cluster].nil?
39+
abort('Please provide IP addresses for etcd initial cluster -i/--etcd_initial_cluster (ENV ETCD_INITIAL_CLUSTER)')
40+
end
41+
puts options
42+
return options
43+
44+
rescue Slop::Error => e
45+
puts "ERROR: #{e.message}"
46+
exit 1
47+
end
6448
end
65-
end
66-
67-
parser.parse!
68-
6949

70-
class Kube_tool
71-
def build_hiera(hash)
72-
key_size = hash[:key_size].to_i
73-
OtherParams.create( hash[:os], hash[:version], hash[:container_runtime], hash[:cni_provider], hash[:cni_provider_version], hash[:etcd_initial_cluster], hash[:etcd_ip], hash[:kube_api_advertise_address], hash[:install_dashboard])
50+
def self.build_hiera(opts)
51+
OtherParams.create(opts)
7452
PreChecks.checks
75-
CreateCerts.etcd_ca(key_size)
76-
CreateCerts.etcd_clients(key_size)
77-
CreateCerts.etcd_certificates(hash[:etcd_initial_cluster], key_size)
78-
CreateCerts.kube_ca(key_size)
79-
CreateCerts.kube_front_proxy_ca(key_size)
80-
CreateCerts.sa(key_size)
53+
certs = CreateCerts.new(opts)
54+
certs.etcd_ca
55+
certs.etcd_clients
56+
certs.etcd_certificates
57+
certs.kube_ca
58+
certs.kube_front_proxy_ca
59+
certs.sa
8160
CleanUp.remove_files
82-
CleanUp.clean_yaml(hash[:os])
61+
CleanUp.clean_yaml(opts[:os])
8362
end
8463
end
85-
86-
generate = Kube_tool.new
87-
88-
generate.build_hiera(options)
64+
Kube_tool.build_hiera(Kube_tool.parse_args)

tooling/kube_tool/clean_up.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
require 'fileutils'
22

33
class CleanUp
4-
def CleanUp.remove_files
4+
def self.all(files)
5+
files.each do |x|
6+
if File.exist?(x)
7+
FileUtils.rm_f(x)
8+
end
9+
end
10+
end
11+
12+
def self.remove_files
513
puts "Cleaning up files"
614
FileUtils.rm Dir.glob('*.csr')
715
FileUtils.rm Dir.glob('*.json')
@@ -10,7 +18,7 @@ def CleanUp.remove_files
1018
FileUtils.rm('discovery_token_hash')
1119
end
1220

13-
def CleanUp.clean_yaml(os)
21+
def self.clean_yaml(os)
1422
os = os.capitalize
1523
puts "Cleaning up yaml"
1624
File.write("kubernetes.yaml",File.open("kubernetes.yaml",&:read).gsub(/^---$/,""))

tooling/kube_tool/create_certs.rb

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22
require 'openssl'
33
require 'json'
44
require 'base64'
5-
6-
#TODO fix repeatitive code after inital internal release
5+
require_relative 'clean_up.rb'
76

87
class CreateCerts
9-
def CreateCerts.etcd_ca(key_size)
8+
9+
def initialize(opts)
10+
@opts = opts
11+
end
12+
13+
def etcd_ca
1014
puts "Creating etcd ca"
11-
files = ['ca-conf.json', 'ca-csr.json', 'ca-key.pem', 'ca-key.pem']
12-
files.each do |x|
13-
if File.exist?(x)
14-
FileUtils.rm_f(x)
15-
end
16-
end
17-
csr = { "CN": "etcd", "key": {"algo": "rsa", "size": key_size }}
15+
CleanUp.all(['ca-conf.json', 'ca-csr.json', 'ca-key.pem', 'ca-key.pem'])
16+
csr = { "CN": "etcd", "key": {"algo": @opts[:ca_algo], "size": @opts[:key_size] }}
1817
conf = { "signing": { "default": { "expiry": "43800h" }, "profiles": { "server": { "expiry": "43800h", "usages": [ "signing", "key encipherment", "server auth", "client auth" ] }, "client": { "expiry": "43800h", "usages": [ "signing", "key encipherment", "client auth" ] }, "peer": { "expiry": "43800h", "usages": [ "signing", "key encipherment", "server auth", "client auth" ] } } } }
1918
File.open("ca-csr.json", "w+") { |file| file.write(csr.to_json) }
2019
File.open("ca-conf.json", "w+") { |file| file.write(conf.to_json) }
@@ -28,9 +27,9 @@ def CreateCerts.etcd_ca(key_size)
2827
File.open("kubernetes.yaml", "a") { |file| file.write(data.to_yaml) }
2928
end
3029

31-
def CreateCerts.etcd_clients(key_size)
30+
def etcd_clients
3231
puts "Creating etcd client certs"
33-
csr = { "CN": "client", "hosts": [""], "key": { "algo": "rsa", "size": key_size } }
32+
csr = { "CN": "client", "hosts": [""], "key": { "algo": @opts[:ca_algo], "size": @opts[:key_size] } }
3433
File.open("kube-etcd-csr.json", "w+") { |file| file.write(csr.to_json) }
3534
system("cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-conf.json -profile client kube-etcd-csr.json | cfssljson -bare client")
3635
FileUtils.rm_f('kube-etcd-csr.csr')
@@ -42,8 +41,8 @@ def CreateCerts.etcd_clients(key_size)
4241
File.open("kubernetes.yaml", "a") { |file| file.write(data.to_yaml) }
4342
end
4443

45-
def CreateCerts.etcd_certificates(etcd_initial_cluster, key_size)
46-
etcd_servers = etcd_initial_cluster.split(",")
44+
def etcd_certificates
45+
etcd_servers = @opts[:etcd_initial_cluster].split(",")
4746
etcd_server_ips = []
4847
etcd_servers.each do | servers |
4948
server = servers.split(":")
@@ -58,7 +57,7 @@ def CreateCerts.etcd_certificates(etcd_initial_cluster, key_size)
5857
FileUtils.rm_f("#{hostname}.yaml")
5958
end
6059
puts "Creating etcd peer and server certificates"
61-
csr = { "CN": "etcd-#{hostname}", "hosts": etcd_server_ips, "key": { "algo": "rsa", "size": key_size }}
60+
csr = { "CN": "etcd-#{hostname}", "hosts": etcd_server_ips, "key": { "algo": @opts[:ca_algo], "size": @opts[:key_size] }}
6261
File.open("config.json", "w+") { |file| file.write(csr.to_json) }
6362
system("cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-conf.json -profile server --hostname=#{etcd_server_ips * ","},#{hostname} config.json | cfssljson -bare #{hostname}-server")
6463
system("cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-conf.json -profile peer --hostname=#{ip},#{hostname} config.json | cfssljson -bare #{hostname}-peer")
@@ -81,15 +80,10 @@ def CreateCerts.etcd_certificates(etcd_initial_cluster, key_size)
8180
end
8281
end
8382

84-
def CreateCerts.kube_ca(key_size)
83+
def kube_ca
8584
puts "Creating kube ca"
86-
files = ['ca-conf.json', 'ca-csr.json', 'ca-key.pem', 'ca-key.pem']
87-
files.each do |x|
88-
if File.exist?(x)
89-
FileUtils.rm_f(x)
90-
end
91-
end
92-
csr = { "CN": "kubernetes", "key": {"algo": "rsa", "size": key_size }}
85+
CleanUp.all(['ca-conf.json', 'ca-csr.json', 'ca-key.pem', 'ca-key.pem'])
86+
csr = { "CN": "kubernetes", "key": {"algo": @opts[:ca_algo], "size": @opts[:key_size] }}
9387
conf = { "signing": { "default": { "expiry": "43800h" }, "profiles": { "server": { "expiry": "43800h", "usages": [ "signing", "key encipherment", "server auth", "client auth" ] }, "client": { "expiry": "43800h", "usages": [ "signing", "key encipherment", "client auth" ] }, "peer": { "expiry": "43800h", "usages": [ "signing", "key encipherment", "server auth", "client auth" ] } } } }
9488
File.open("ca-csr.json", "w+") { |file| file.write(csr.to_json) }
9589
File.open("ca-conf.json", "w+") { |file| file.write(conf.to_json) }
@@ -107,15 +101,10 @@ def CreateCerts.kube_ca(key_size)
107101
File.open("kubernetes.yaml", "a") { |file| file.write(data.to_yaml) }
108102
end
109103

110-
def CreateCerts.kube_front_proxy_ca(key_size)
104+
def kube_front_proxy_ca
111105
puts "Creating kube front-proxy ca"
112-
files = ['front-proxy-ca-conf.json', 'front-proxy-ca-csr.json', 'front-proxy-ca-key.pem', 'front-proxy-ca-key.pem']
113-
files.each do |x|
114-
if File.exist?(x)
115-
FileUtils.rm_f(x)
116-
end
117-
end
118-
csr = { "CN": "front-proxy-ca", "key": {"algo": "rsa", "size": key_size }}
106+
CleanUp.all(['front-proxy-ca-conf.json', 'front-proxy-ca-csr.json', 'front-proxy-ca-key.pem', 'front-proxy-ca-key.pem'])
107+
csr = { "CN": "front-proxy-ca", "key": {"algo": @opts[:ca_algo], "size": @opts[:key_size] }}
119108
conf = { "signing": { "default": { "expiry": "87600h" }}}
120109
File.open("front-proxy-ca-csr.json", "w+") { |file| file.write(csr.to_json) }
121110
File.open("front-proxy-ca-conf.json", "w+") { |file| file.write(conf.to_json) }
@@ -129,9 +118,9 @@ def CreateCerts.kube_front_proxy_ca(key_size)
129118
File.open("kubernetes.yaml", "a") { |file| file.write(data.to_yaml) }
130119
end
131120

132-
def CreateCerts.sa(key_size)
133-
puts "Creating service account certs"
134-
key = OpenSSL::PKey::RSA.new key_size
121+
def sa
122+
puts "Creating service account certs (key size: #{@opts[:sa_size]})"
123+
key = OpenSSL::PKey::RSA.new @opts[:sa_size]
135124
open 'sa-key.pem', 'w' do |io|
136125
io.write key.to_pem
137126
end
@@ -145,6 +134,4 @@ def CreateCerts.sa(key_size)
145134
data['kubernetes::sa_key'] = key
146135
File.open("kubernetes.yaml", "a") { |file| file.write(data.to_yaml) }
147136
end
148-
149-
150137
end

0 commit comments

Comments
 (0)