Skip to content

Commit 837ae41

Browse files
thewolfpacktaylor
authored andcommitted
#lfn-cnti/certification/issues/56 sample_setup and sample_setup_args now setup a generic cnf
1 parent 5d345d9 commit 837ae41

File tree

6 files changed

+151
-46
lines changed

6 files changed

+151
-46
lines changed

sample-cnfs/sample-coredns-cnf/cnf-conformance.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
helm_directory: cnfs/coredns/helm_chart/coredns
3+
# helm_directory: helm_chart
34
git_clone_url: https://github.com/coredns/coredns.git
45
install_script: cnfs/coredns/Makefile
56
release_name: coredns
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Set up Sample CoreDNS CNF
2+
./sample-cnfs/sample-coredns-cnf/readme.md
3+
# Prerequistes
4+
### Install helm
5+
```
6+
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3
7+
chmod 700 get_helm.sh
8+
./get_helm.sh
9+
```
10+
### Optional: Use a helm version manager
11+
https://github.com/yuya-takeyama/helmenv
12+
Check out helmenv into any path (here is ${HOME}/.helmenv)
13+
```
14+
${HOME}/.helmenv)
15+
$ git clone https://github.com/yuya-takeyama/helmenv.git ~/.helmenv
16+
```
17+
Add ~/.helmenv/bin to your $PATH any way you like
18+
```
19+
$ echo 'export PATH="$HOME/.helmenv/bin:$PATH"' >> ~/.bash_profile
20+
```
21+
```
22+
helmenv versions
23+
helmenv install <version 3.1?>
24+
```
25+
26+
### core-dns installation
27+
```
28+
helm install coredns stable/coredns
29+
```
30+
### Pull down the helm chart code, untar it, and put it in the cnfs/coredns directory
31+
```
32+
helm pull stable/coredns
33+
```
34+
### Example cnf-conformance config file for sample-core-dns-cnf
35+
In ./cnfs/sample-core-dns-cnf/cnf-conformance.yml
36+
```
37+
---
38+
container_names: [coredns-coredns]
39+
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
helm_directory: helm_chart
3+
git_clone_url: https://github.com/coredns/coredns.git
4+
install_script: cnfs/coredns/Makefile
5+
release_name: coredns
6+
deployment_name: coredns-coredns
7+
application_deployment_names: [coredns-coredns]
8+
helm_chart: stable/coredns
9+
helm_chart_container_name: coredns
10+
white_list_helm_chart_container_names: [falco, nginx, coredns, calico-node, kube-proxy, nginx-proxy]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
# helm_directory: cnfs/sample_privileged_cnf_setup_coredns/chart
3+
helm_directory: chart
4+
git_clone_url:
5+
install_script: cnfs/sample_privileged_cnf_setup_coredns/chart
6+
release_name: privileged-coredns
7+
deployment_name: privileged-coredns-coredns
8+
application_deployment_names: [privileged-coredns-coredns]
9+
helm_chart: stable/coredns
10+
helm_chart_container_name: privileged-coredns-coredns
11+
white_list_helm_chart_container_names: []

spec/sample_coredns_setup_spec.cr

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ describe "Utils" do
2727
after_each do
2828
`crystal src/cnf-conformance.cr cleanup`
2929
$?.success?.should be_true
30+
sample_cleanup(sample_dir: "sample-cnfs/sample-generic-cnf", verbose: true)
3031
end
31-
32+
3233
it "'wait_for_install' should wait for a cnf to be installed" do
3334
`crystal src/cnf-conformance.cr sample_coredns_setup`
3435
$?.success?.should be_true
@@ -44,34 +45,44 @@ describe "Utils" do
4445
(current_replicas.to_i > 0).should be_true
4546
end
4647

47-
it "'sample_setup' should set up a sample cnf" do
48+
it "'sample_setup' should set up a sample cnf", tags: "WIP" do
4849
args = Sam::Args.new
49-
sample_setup(sample_dir: "sample-cnfs/sample-coredns-cnf", release_name: "coredns", deployment_name: "coredns-coredns", helm_chart: "stable/coredns", helm_directory: "cnfs/coredns/helm_chart/coredns", git_clone_url: "https://github.com/coredns/coredns.git" )
50+
sample_setup(sample_dir: "sample-cnfs/sample-generic-cnf", release_name: "coredns", deployment_name: "coredns-coredns", helm_chart: "stable/coredns", helm_directory: "helm_chart", git_clone_url: "https://github.com/coredns/coredns.git" )
5051
# check if directory exists
51-
(Dir.exists? "sample-cnfs/sample-coredns-cnf").should be_true
52-
(File.exists?("cnfs/coredns/cnf-conformance.yml")).should be_true
53-
(File.exists?("cnfs/coredns/cnf-conformance.yml")).should be_true
54-
(File.exists?("cnfs/coredns/helm_chart/coredns/Chart.yaml")).should be_true
52+
(Dir.exists? "sample-cnfs/sample-generic-cnf").should be_true
53+
(File.exists?("cnfs/sample-generic-cnf/cnf-conformance.yml")).should be_true
54+
(File.exists?("cnfs/sample-generic-cnf/helm_chart/Chart.yaml")).should be_true
55+
sample_cleanup(sample_dir: "sample-cnfs/sample-generic-cnf", verbose: true)
5556
end
56-
57+
#
5758
it "'sample_setup_args' should set up a sample cnf from a argument", tags: "WIP" do
5859
args = Sam::Args.new
59-
sample_setup_args(sample_dir: "sample-cnfs/sample-coredns-cnf", args: args, verbose: true )
60+
sample_setup_args(sample_dir: "sample-cnfs/sample-generic-cnf", args: args, verbose: true )
6061
# check if directory exists
61-
(Dir.exists? "sample-cnfs/sample-coredns-cnf").should be_true
62-
(File.exists?("cnfs/coredns/cnf-conformance.yml")).should be_true
63-
(File.exists?("cnfs/coredns/cnf-conformance.yml")).should be_true
64-
(File.exists?("cnfs/coredns/helm_chart/coredns/Chart.yaml")).should be_true
62+
(Dir.exists? "sample-cnfs/sample-generic-cnf").should be_true
63+
(File.exists?("cnfs/sample-generic-cnf/cnf-conformance.yml")).should be_true
64+
(File.exists?("cnfs/sample-generic-cnf/helm_chart/Chart.yaml")).should be_true
65+
sample_cleanup(sample_dir: "sample-cnfs/sample-generic-cnf", verbose: true)
6566
end
6667

6768
it "'sample_cleanup' should clean up a sample cnf from a argument", tags: "WIP" do
6869
args = Sam::Args.new
69-
sample_setup_args(sample_dir: "sample-cnfs/sample-coredns-cnf", args: args, verbose: true )
70-
sample_cleanup(sample_dir: "sample-cnfs/sample-coredns-cnf", verbose: true)
70+
sample_setup_args(sample_dir: "sample-cnfs/sample-generic-cnf", args: args, verbose: true )
71+
sample_cleanup(sample_dir: "sample-cnfs/sample-generic-cnf", verbose: true)
72+
# check if directory exists
73+
(Dir.exists? "cnfs/sample-generic-cnf").should be_false
74+
(File.exists?("cnfs/sample-generic-cnf/cnf-conformance.yml")).should be_false
75+
(File.exists?("cnfs/sample-generic-cnf/helm_chart/Chart.yaml")).should be_false
76+
end
77+
78+
it "'sample_setup_args' should be able to deploy using a helm_directory" do
79+
args = Sam::Args.new
80+
sample_setup_args(sample_dir: "sample-cnfs/sample_privileged_cnf_setup_coredns", deploy_with_chart: false, args: args, verbose: true )
7181
# check if directory exists
72-
(Dir.exists? "cnfs/coredns").should be_false
73-
(File.exists?("cnfs/coredns/cnf-conformance.yml")).should be_false
74-
(File.exists?("cnfs/coredns/cnf-conformance.yml")).should be_false
75-
(File.exists?("cnfs/coredns/helm_chart/coredns/Chart.yaml")).should be_false
82+
(Dir.exists? "cnfs/sample_privileged_cnf_setup_coredns").should be_true
83+
# should not clone
84+
(Dir.exists? "cnfs/sample_privileged_cnf_setup_coredns/privileged-coredns").should be_false
85+
(File.exists? "cnfs/sample_privileged_cnf_setup_coredns/cnf-conformance.yml").should be_true
86+
(File.exists? "cnfs/sample_privileged_cnf_setup_coredns/chart/Chart.yaml").should be_true
7687
end
7788
end

src/tasks/utils.cr

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,76 +52,100 @@ def sample_setup_args(sample_dir, args, deploy_with_chart=true, verbose=false)
5252
config = sample_conformance_yml(sample_dir)
5353

5454
if args.named.keys.includes? "release_name"
55-
release_name = args.named["release_name"]
55+
release_name = "#{args.named["release_name"]}"
5656
else
57-
release_name = config.get("release_name").as_s
57+
release_name = "#{config.get("release_name").as_s?}"
5858
end
5959
puts "release_name: #{release_name}" if verbose
6060

6161
if args.named.keys.includes? "deployment_name"
62-
deployment_name = args.named["deployment_name"]
62+
deployment_name = "#{args.named["deployment_name"]}"
6363
else
64-
deployment_name = config.get("deployment_name").as_s
64+
deployment_name = "#{config.get("deployment_name").as_s?}"
6565
end
6666
puts "deployment_name: #{deployment_name}" if verbose
6767

6868
if args.named.keys.includes? "helm_chart"
69-
helm_chart = args.named["helm_chart"]
69+
helm_chart = "#{args.named["helm_chart"]}"
7070
else
71-
helm_chart = config.get("helm_chart").as_s
71+
helm_chart = "#{config.get("helm_chart").as_s?}"
7272
end
7373
puts "helm_chart: #{helm_chart}" if verbose
7474

7575
if args.named.keys.includes? "helm_directory"
76-
helm_directory = args.named["helm_directory"]
76+
helm_directory = "#{args.named["helm_directory"]}"
7777
else
78-
helm_directory = config.get("helm_directory").as_s
78+
helm_directory = "#{config.get("helm_directory").as_s?}"
7979
end
8080
puts "helm_directory: #{helm_directory}" if verbose
8181

8282
if args.named.keys.includes? "git_clone_url"
83-
git_clone_url = args.named["git_clone_url"]
83+
git_clone_url = "#{args.named["git_clone_url"]}"
8484
else
85-
git_clone_url = config.get("git_clone_url").as_s
85+
# TODO check type (any) before doing .as_s
86+
git_clone_url = "#{config.get("git_clone_url").as_s?}"
8687
end
8788
puts "git_clone_url: #{git_clone_url}" if verbose
8889

8990
sample_setup(sample_dir: sample_dir, release_name: release_name, deployment_name: deployment_name, helm_chart: helm_chart, helm_directory: helm_directory, git_clone_url: git_clone_url, deploy_with_chart: deploy_with_chart, verbose: verbose )
9091

9192
end
9293

93-
def sample_setup(sample_dir, release_name, deployment_name, helm_chart, helm_directory, git_clone_url, deploy_with_chart=true, verbose=false)
94+
def sample_setup(sample_dir, release_name, deployment_name, helm_chart, helm_directory, git_clone_url="", deploy_with_chart=true, verbose=false)
9495

9596
current_dir = FileUtils.pwd
9697
puts current_dir if verbose
9798

99+
destination_cnf_dir = "#{current_dir}/#{CNF_DIR}/#{short_sample_dir(sample_dir)}"
100+
puts "destination_cnf_dir: #{destination_cnf_dir}" if verbose
101+
FileUtils.mkdir_p(destination_cnf_dir)
98102
# TODO enable recloning/fetching etc
99103
# TODO pass in block
100-
git_clone = `git clone #{git_clone_url} #{current_dir}/#{CNF_DIR}/#{release_name}`
104+
git_clone = `git clone #{git_clone_url} #{destination_cnf_dir}/#{release_name}` if git_clone_url.empty? == false
101105
puts git_clone if verbose
102106

103107
# Copy the cnf-conformance.yml
104-
yml_cp = `cp #{sample_dir}/cnf-conformance.yml #{current_dir}/#{CNF_DIR}/#{release_name}`
108+
# yml_cp = `cp #{sample_dir}/cnf-conformance.yml #{destination_cnf_dir}`
109+
# Copy the sample
110+
yml_cp = `cp -a #{sample_dir} #{CNF_DIR}`
105111
puts yml_cp if verbose
106112

113+
raise "Copy of #{sample_dir}/cnf-conformance.yml to #{destination_cnf_dir} failed!" unless $?.success?
114+
107115
begin
108116

109117
helm = "#{current_dir}/#{TOOLS_DIR}/helm/linux-amd64/helm"
110118
puts helm if verbose
111-
helm_install = `#{helm} install #{release_name} #{helm_chart}`
112-
puts helm_install if verbose
113-
114-
115-
# Retrieve the helm chart source
116-
FileUtils.mkdir_p("#{current_dir}/#{CNF_DIR}/#{release_name}/helm_chart")
117-
helm_pull = `#{helm} pull #{helm_chart}`
118-
puts helm_pull if verbose
119-
core_mv = `mv #{release_name}-*.tgz #{current_dir}/#{CNF_DIR}/#{release_name}/helm_chart`
120-
puts core_mv if verbose
121-
tar = `cd #{current_dir}/#{CNF_DIR}/#{release_name}/helm_chart; tar -xvf #{current_dir}/#{CNF_DIR}/#{release_name}/helm_chart/#{release_name}-*.tgz`
122-
puts tar if verbose
119+
if deploy_with_chart
120+
puts "deploying with chart" if verbose
121+
helm_install = `#{helm} install #{release_name} #{helm_chart}`
122+
puts helm_install if verbose
123+
124+
# Retrieve the helm chart source
125+
FileUtils.mkdir_p("#{destination_cnf_dir}/#{helm_directory}")
126+
helm_pull = `#{helm} pull #{helm_chart}`
127+
puts helm_pull if verbose
128+
# core_mv = `mv #{release_name}-*.tgz #{destination_cnf_dir}/#{helm_directory}`
129+
# TODO helm_chart should be helm_chart_repo
130+
puts "mv #{chart_name(helm_chart)}-*.tgz #{destination_cnf_dir}/#{helm_directory}" if verbose
131+
core_mv = `mv #{chart_name(helm_chart)}-*.tgz #{destination_cnf_dir}/#{helm_directory}`
132+
puts core_mv if verbose
133+
134+
puts "cd #{destination_cnf_dir}/#{helm_directory}; tar -xvf #{destination_cnf_dir}/#{helm_directory}/#{chart_name(helm_chart)}-*.tgz" if verbose
135+
tar = `cd #{destination_cnf_dir}/#{helm_directory}; tar -xvf #{destination_cnf_dir}/#{helm_directory}/#{chart_name(helm_chart)}-*.tgz`
136+
puts tar if verbose
137+
138+
puts "mv #{destination_cnf_dir}/#{helm_directory}/#{chart_name(helm_chart)}/* #{destination_cnf_dir}/#{helm_directory}" if verbose
139+
move_chart = `mv #{destination_cnf_dir}/#{helm_directory}/#{chart_name(helm_chart)}/* #{destination_cnf_dir}/#{helm_directory}`
140+
puts move_chart if verbose
141+
else
142+
puts "deploying with helm directory" if verbose
143+
helm_install = `#{helm} install #{release_name} #{destination_cnf_dir}/#{helm_directory}`
144+
puts helm_install if verbose
145+
end
146+
123147
wait_for_install(deployment_name)
124-
if helm_install.to_s.size > 0 && helm_pull.to_s.size > 0
148+
if helm_install.to_s.size > 0 # && helm_pull.to_s.size > 0
125149
puts "Successfully setup #{release_name}".colorize(:green)
126150
end
127151
ensure
@@ -139,6 +163,15 @@ def sample_cleanup(sample_dir, verbose=true)
139163
puts helm if verbose
140164
helm_uninstall = `#{helm} uninstall #{release_name}`
141165
puts helm_uninstall if verbose
142-
rm = `rm -rf #{current_dir}/#{CNF_DIR}/#{release_name}`
166+
destination_cnf_dir = "#{current_dir}/#{CNF_DIR}/#{short_sample_dir(sample_dir)}"
167+
rm = `rm -rf #{destination_cnf_dir}`
143168
puts rm if verbose
144169
end
170+
171+
def chart_name(helm_chart_repo)
172+
helm_chart_repo.split("/").last
173+
end
174+
175+
def short_sample_dir(full_sample_dir)
176+
full_sample_dir.split("/").last
177+
end

0 commit comments

Comments
 (0)