Skip to content

Commit 895b6ed

Browse files
authored
Merge pull request #598 from deric/exec
Fix executing CNI addons commands (fixes #594)
2 parents 57eed28 + 76ae457 commit 895b6ed

File tree

2 files changed

+94
-79
lines changed

2 files changed

+94
-79
lines changed

manifests/kube_addons.pp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@
2424
try_sleep => 30,
2525
}
2626

27+
$exec_onlyif = 'kubectl get nodes'
28+
2729
if $cni_rbac_binding {
2830
$binding_command = ['kubectl', 'apply', '-f', $cni_rbac_binding]
29-
$binding_onlyif = ['kubectl', 'get', 'nodes']
30-
$binding_unless = ['kubectl get clusterrole | grep calico']
31+
$binding_unless = 'kubectl get clusterrole | grep calico'
3132

3233
exec { 'Install calico rbac bindings':
3334
environment => $env,
3435
command => $binding_command,
35-
onlyif => $binding_onlyif,
36+
onlyif => $exec_onlyif,
3637
unless => $binding_unless,
3738
}
3839
}
@@ -41,22 +42,20 @@
4142
if $cni_provider == 'calico-tigera' {
4243
if $cni_network_preinstall {
4344
$preinstall_command = ['kubectl', 'apply', '-f', $cni_network_preinstall]
44-
$preinstall_onlyif = ['kubectl', 'get', 'nodes']
45-
$preinstall_unless = "kubectl -n tigera-operator get deployments | egrep '^tigera-operator'"
45+
$preinstall_unless = 'kubectl -n tigera-operator get deployments | egrep "^tigera-operator"'
4646

4747
exec { 'Install cni network (preinstall)':
4848
command => $preinstall_command,
49-
onlyif => $preinstall_onlyif,
49+
onlyif => $exec_onlyif,
5050
unless => $preinstall_unless,
5151
environment => $env,
5252
before => Exec['Install cni network provider'],
5353
}
5454
}
5555
# Removing Calico_installation_path variable as it doesnt seem to apport any extra value here.
5656
$calico_installation_path = '/etc/kubernetes/calico-installation.yaml'
57-
$path_command = ['kubectl', 'apply', '-f', '/etc/kubernetes/calico-installation.yaml']
58-
$path_onlyif = ['kubectl', 'get', 'nodes']
59-
$path_unless = "kubectl -n calico-system get daemonset | egrep '^calico-node'"
57+
$path_command = 'kubectl apply -f /etc/kubernetes/calico-installation.yaml'
58+
$path_unless = 'kubectl -n calico-system get daemonset | egrep "^calico-node"'
6059

6160
file { $calico_installation_path:
6261
ensure => 'present',
@@ -74,18 +73,17 @@
7473
replace => true,
7574
} -> exec { 'Install cni network provider':
7675
command => $path_command,
77-
onlyif => $path_onlyif,
76+
onlyif => $exec_onlyif,
7877
unless => $path_unless,
7978
environment => $env,
8079
}
8180
} else {
8281
$provider_command = ['kubectl', 'apply', '-f', $cni_network_provider]
83-
$provider_onlyif = ['kubectl', 'get', 'nodes']
84-
$provider_unless = "kubectl -n kube-system get daemonset | egrep '(flannel|weave|calico-node|cilium)'"
82+
$provider_unless = 'kubectl -n kube-system get daemonset | egrep "(flannel|weave|calico-node|cilium)"'
8583

8684
exec { 'Install cni network provider':
8785
command => $provider_command,
88-
onlyif => $provider_onlyif,
86+
onlyif => $exec_onlyif,
8987
unless => $provider_unless,
9088
environment => $env,
9189
}
@@ -108,13 +106,14 @@
108106

109107
if $install_dashboard {
110108
$dashboard_command = ['kubectl', 'apply', '-f', $dashboard_url]
111-
$dashboard_onlyif = ['kubectl', 'get', 'nodes']
112-
$dashboard_unless = [['kubectl get pods --field-selector="status.phase=Running" -n kubernetes-dashboard | grep kubernetes-dashboard-'],
113-
['kubectl get pods --field-selector="status.phase=Running" -n kube-system | grep kubernetes-dashboard-']]
109+
$dashboard_unless = [
110+
'kubectl get pods --field-selector="status.phase=Running" -n kubernetes-dashboard | grep kubernetes-dashboard-',
111+
'kubectl get pods --field-selector="status.phase=Running" -n kube-system | grep kubernetes-dashboard-'
112+
]
114113

115114
exec { 'Install Kubernetes dashboard':
116115
command => $dashboard_command,
117-
onlyif => $dashboard_onlyif,
116+
onlyif => $exec_onlyif,
118117
unless => $dashboard_unless,
119118
environment => $env,
120119
}

spec/classes/kube_addons_spec.rb

Lines changed: 78 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,98 +4,114 @@
44
let(:facts) do
55
{
66
:os => {
7-
:family => "Debian",
7+
:family => 'Debian',
88
:name => 'Ubuntu',
99
:release => {
10-
:full => '16.04',
10+
:full => '22.04',
1111
},
1212
:distro => {
13-
:codename => "xenial",
13+
:codename => 'jammy',
1414
},
1515
},
1616
}
1717
end
1818
context 'with controller => true and schedule_on_controller => true' do
1919
let(:params) do {
20-
'controller' => true,
21-
'cni_rbac_binding' => 'foo',
22-
'cni_network_provider' => 'https://foo.test',
23-
'install_dashboard' => false,
24-
'kubernetes_version' => '1.10.2',
25-
'schedule_on_controller' => true,
26-
'node_name' => 'foo',
20+
controller: true,
21+
cni_rbac_binding: 'foo',
22+
cni_network_provider: 'https://foo.test',
23+
install_dashboard: false,
24+
kubernetes_version: '1.25.4',
25+
schedule_on_controller: true,
26+
node_name: 'foo',
2727
}
2828
end
2929

30-
it { should contain_exec('Install calico rbac bindings')}
31-
it { should contain_exec('Install cni network provider')}
32-
it { should contain_exec('schedule on controller')}
30+
it { is_expected.to contain_exec('Install calico rbac bindings').with({
31+
'command': ['kubectl', 'apply', '-f', 'foo'],
32+
'onlyif': ['kubectl get nodes'],
33+
})
34+
}
35+
it { is_expected.to contain_exec('Install cni network provider').with({
36+
'command': ['kubectl', 'apply', '-f', 'https://foo.test'],
37+
'onlyif': ['kubectl get nodes'],
38+
})
39+
}
40+
it { is_expected.to contain_exec('schedule on controller')}
3341

34-
it { should_not contain_exec('Install cni network (preinstall)')}
35-
it { should_not contain_file('/etc/kubernetes/calico-installation.yaml')}
36-
it { should_not contain_file_line('Configure calico ipPools.cidr')}
42+
it { is_expected.not_to contain_exec('Install cni network (preinstall)')}
43+
it { is_expected.not_to contain_file('/etc/kubernetes/calico-installation.yaml')}
44+
it { is_expected.not_to contain_file_line('Configure calico ipPools.cidr')}
3745
end
3846

39-
context 'with cni_provider => calico' do
40-
let(:params) do {
41-
'controller' => true,
42-
'cni_network_provider' => 'https://foo.test',
43-
'cni_provider' => 'calico',
44-
'install_dashboard' => false,
45-
'kubernetes_version' => '1.10.2',
46-
'node_name' => 'foo',
47-
}
48-
end
4947

50-
it { should contain_exec('Install cni network provider')}
48+
context 'CNI network provider' do
49+
['flannel', 'weave', 'calico', 'cilium', 'calico-tigera'].each do |provider|
5150

52-
it { should_not contain_exec('Install cni network (preinstall)')}
53-
it { should_not contain_file('/etc/kubernetes/calico-installation.yaml')}
54-
it { should_not contain_file_line('Configure calico ipPools.cidr')}
55-
end
51+
context "with #{provider}" do
52+
let(:params) do {
53+
controller: true,
54+
cni_network_provider: "https://#{provider}.test",
55+
cni_network_preinstall: 'https://foo.test/tigera-operator',
56+
cni_provider: provider,
57+
install_dashboard: false,
58+
kubernetes_version: '1.25.4',
59+
node_name: 'foo',
60+
}
61+
end
5662

57-
context 'with cni_provider => calico-tigera' do
58-
let(:params) do {
59-
'controller' => true,
60-
'cni_network_preinstall' => 'https://foo.test/tigera-operator',
61-
'cni_network_provider' => 'https://foo.test',
62-
'cni_provider' => 'calico-tigera',
63-
'install_dashboard' => false,
64-
'kubernetes_version' => '1.10.2',
65-
'node_name' => 'foo',
66-
}
67-
end
63+
case provider
64+
when 'calico-tigera'
65+
it { is_expected.to contain_exec('Install cni network (preinstall)').with({
66+
'command': ['kubectl', 'apply', '-f', 'https://foo.test/tigera-operator'],
67+
'onlyif': ['kubectl get nodes'],
68+
})
69+
}
70+
it { is_expected.to contain_file('/etc/kubernetes/calico-installation.yaml')}
71+
it { is_expected.to contain_file_line('Configure calico ipPools.cidr')}
72+
it { is_expected.to contain_exec('Install cni network provider')}
73+
else
74+
it {
75+
is_expected.to contain_exec('Install cni network provider').with({
76+
'onlyif': ['kubectl get nodes'],
77+
'command': ['kubectl', 'apply', '-f', "https://#{provider}.test"],
78+
'unless': ['kubectl -n kube-system get daemonset | egrep "(flannel|weave|calico-node|cilium)"'],
79+
})
80+
}
6881

69-
it { should contain_exec('Install cni network (preinstall)')}
70-
it { should contain_file('/etc/kubernetes/calico-installation.yaml')}
71-
it { should contain_file_line('Configure calico ipPools.cidr')}
72-
it { should contain_exec('Install cni network provider')}
82+
it { is_expected.not_to contain_exec('Install cni network (preinstall)').with({
83+
'onlyif': ['kubectl get nodes'],
84+
})
85+
}
86+
end
87+
end
88+
end
7389
end
7490

7591
context 'with install_dashboard => false' do
7692
let(:params) do {
77-
'controller' => true,
78-
'cni_rbac_binding' => nil,
79-
'cni_network_provider' => 'https://foo.test',
80-
'install_dashboard' => false,
81-
'kubernetes_version' => '1.10.2',
82-
'schedule_on_controller' => false,
83-
'node_name' => 'foo',
93+
controller: true,
94+
cni_rbac_binding: nil,
95+
cni_network_provider: 'https://foo.test',
96+
install_dashboard: false,
97+
kubernetes_version: '1.25.4',
98+
schedule_on_controller: false,
99+
node_name: 'foo',
84100
}
85101
end
86-
it { is_expected.to_not contain_exec('Install Kubernetes dashboard')}
102+
it { is_expected.not_to contain_exec('Install Kubernetes dashboard')}
87103
end
88104

89105
context 'with install_dashboard => true' do
90106
let(:params) do {
91-
'controller' => true,
92-
'cni_rbac_binding' => nil,
93-
'cni_network_provider' => 'https://foo.test',
94-
'install_dashboard' => true,
95-
'kubernetes_version' => '1.10.2',
96-
'dashboard_version' => '1.10.1',
97-
'schedule_on_controller' => false,
98-
'node_name' => 'foo',
107+
controller: true,
108+
cni_rbac_binding: nil,
109+
cni_network_provider: 'https://foo.test',
110+
install_dashboard: true,
111+
kubernetes_version: '1.25.4',
112+
dashboard_version: '1.10.1',
113+
schedule_on_controller: false,
114+
node_name: 'foo',
99115
}
100116
end
101117
it { is_expected.to contain_exec('Install Kubernetes dashboard').with_command(%r{dashboard/v1.10.1/src}) }

0 commit comments

Comments
 (0)