kube_cluster 0.3.8 → 0.3.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +9 -5
- data/Rakefile +1 -1
- data/bin/test +2 -15
- data/kube_cluster.gemspec +3 -2
- data/lib/kube/cluster/manifest.rb +160 -193
- data/lib/kube/cluster/middleware/annotations.rb +43 -51
- data/lib/kube/cluster/middleware/hpa_for_deployment.rb +136 -158
- data/lib/kube/cluster/middleware/ingress_for_service.rb +84 -110
- data/lib/kube/cluster/middleware/labels.rb +60 -73
- data/lib/kube/cluster/middleware/namespace.rb +49 -55
- data/lib/kube/cluster/middleware/pod_anti_affinity.rb +102 -113
- data/lib/kube/cluster/middleware/resource_preset.rb +145 -169
- data/lib/kube/cluster/middleware/security_context.rb +127 -154
- data/lib/kube/cluster/middleware/service_for_deployment.rb +123 -146
- data/lib/kube/cluster/resource/dirty_tracking.rb +328 -418
- data/lib/kube/cluster/version.rb +1 -1
- data/lib/kube/cluster.rb +4 -9
- data/lib/kube/helm/chart.rb +326 -325
- data/lib/kube/helm/endpoint.rb +59 -65
- data/lib/kube/helm/repo.rb +159 -166
- metadata +7 -7
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
require "kube/cluster"
|
|
6
|
-
end
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "kube/cluster"
|
|
7
5
|
|
|
8
6
|
module Kube
|
|
9
7
|
module Cluster
|
|
@@ -68,185 +66,163 @@ module Kube
|
|
|
68
66
|
end
|
|
69
67
|
end
|
|
70
68
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
class ResourcePresetMiddlewareTest < Minitest::Test
|
|
75
|
-
Middleware = Kube::Cluster::Middleware
|
|
76
|
-
|
|
77
|
-
def test_injects_small_preset_into_deployment
|
|
78
|
-
m = manifest(Kube::Cluster["Deployment"].new {
|
|
79
|
-
metadata.name = "web"
|
|
80
|
-
metadata.labels = { "app.kubernetes.io/size": "small" }
|
|
81
|
-
spec.selector.matchLabels = { app: "web" }
|
|
82
|
-
spec.template.metadata.labels = { app: "web" }
|
|
83
|
-
spec.template.spec.containers = [
|
|
84
|
-
{ name: "web", image: "nginx:latest" },
|
|
85
|
-
]
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
Middleware::ResourcePreset.new.call(m)
|
|
89
|
-
container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0)
|
|
90
|
-
|
|
91
|
-
assert_equal "500m", container.dig(:resources, :requests, :cpu)
|
|
92
|
-
assert_equal "512Mi", container.dig(:resources, :requests, :memory)
|
|
93
|
-
assert_equal "750m", container.dig(:resources, :limits, :cpu)
|
|
94
|
-
assert_equal "768Mi", container.dig(:resources, :limits, :memory)
|
|
95
|
-
end
|
|
69
|
+
test do
|
|
70
|
+
Middleware = Kube::Cluster::Middleware
|
|
96
71
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
Middleware::ResourcePreset.new.call(m)
|
|
109
|
-
container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0)
|
|
110
|
-
|
|
111
|
-
assert_equal "100m", container.dig(:resources, :requests, :cpu)
|
|
112
|
-
assert_equal "128Mi", container.dig(:resources, :requests, :memory)
|
|
113
|
-
end
|
|
72
|
+
it "injects_small_preset_into_deployment" do
|
|
73
|
+
m = manifest(Kube::Cluster["Deployment"].new {
|
|
74
|
+
metadata.name = "web"
|
|
75
|
+
metadata.labels = { "app.kubernetes.io/size": "small" }
|
|
76
|
+
spec.selector.matchLabels = { app: "web" }
|
|
77
|
+
spec.template.metadata.labels = { app: "web" }
|
|
78
|
+
spec.template.spec.containers = [
|
|
79
|
+
{ name: "web", image: "nginx:latest" },
|
|
80
|
+
]
|
|
81
|
+
})
|
|
114
82
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
metadata.name = "big"
|
|
118
|
-
metadata.labels = { "app.kubernetes.io/size": "xlarge" }
|
|
119
|
-
spec.selector.matchLabels = { app: "big" }
|
|
120
|
-
spec.template.metadata.labels = { app: "big" }
|
|
121
|
-
spec.template.spec.containers = [
|
|
122
|
-
{ name: "app", image: "app:latest" },
|
|
123
|
-
]
|
|
124
|
-
})
|
|
125
|
-
|
|
126
|
-
Middleware::ResourcePreset.new.call(m)
|
|
127
|
-
container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0)
|
|
128
|
-
|
|
129
|
-
assert_equal "1", container.dig(:resources, :requests, :cpu)
|
|
130
|
-
assert_equal "3072Mi", container.dig(:resources, :requests, :memory)
|
|
131
|
-
assert_equal "3", container.dig(:resources, :limits, :cpu)
|
|
132
|
-
assert_equal "6144Mi", container.dig(:resources, :limits, :memory)
|
|
133
|
-
end
|
|
83
|
+
Middleware::ResourcePreset.new.call(m)
|
|
84
|
+
container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0)
|
|
134
85
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
metadata.name = "multi"
|
|
138
|
-
metadata.labels = { "app.kubernetes.io/size": "micro" }
|
|
139
|
-
spec.selector.matchLabels = { app: "multi" }
|
|
140
|
-
spec.template.metadata.labels = { app: "multi" }
|
|
141
|
-
spec.template.spec.containers = [
|
|
142
|
-
{ name: "app", image: "app:latest" },
|
|
143
|
-
{ name: "sidecar", image: "sidecar:latest" },
|
|
144
|
-
]
|
|
145
|
-
})
|
|
146
|
-
|
|
147
|
-
Middleware::ResourcePreset.new.call(m)
|
|
148
|
-
containers = m.resources.first.to_h.dig(:spec, :template, :spec, :containers)
|
|
149
|
-
|
|
150
|
-
containers.each do |c|
|
|
151
|
-
assert_equal "250m", c.dig(:resources, :requests, :cpu)
|
|
152
|
-
assert_equal "256Mi", c.dig(:resources, :requests, :memory)
|
|
153
|
-
end
|
|
154
|
-
end
|
|
86
|
+
container.dig(:resources, :requests, :cpu).should == "500m"
|
|
87
|
+
end
|
|
155
88
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
|
|
89
|
+
it "injects_nano_preset" do
|
|
90
|
+
m = manifest(Kube::Cluster["Deployment"].new {
|
|
91
|
+
metadata.name = "tiny"
|
|
92
|
+
metadata.labels = { "app.kubernetes.io/size": "nano" }
|
|
93
|
+
spec.selector.matchLabels = { app: "tiny" }
|
|
94
|
+
spec.template.metadata.labels = { app: "tiny" }
|
|
95
|
+
spec.template.spec.containers = [
|
|
96
|
+
{ name: "app", image: "app:latest" },
|
|
97
|
+
]
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
Middleware::ResourcePreset.new.call(m)
|
|
101
|
+
container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0)
|
|
102
|
+
|
|
103
|
+
container.dig(:resources, :requests, :cpu).should == "100m"
|
|
104
|
+
end
|
|
162
105
|
|
|
163
|
-
|
|
106
|
+
it "injects_xlarge_preset" do
|
|
107
|
+
m = manifest(Kube::Cluster["Deployment"].new {
|
|
108
|
+
metadata.name = "big"
|
|
109
|
+
metadata.labels = { "app.kubernetes.io/size": "xlarge" }
|
|
110
|
+
spec.selector.matchLabels = { app: "big" }
|
|
111
|
+
spec.template.metadata.labels = { app: "big" }
|
|
112
|
+
spec.template.spec.containers = [
|
|
113
|
+
{ name: "app", image: "app:latest" },
|
|
114
|
+
]
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
Middleware::ResourcePreset.new.call(m)
|
|
118
|
+
container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0)
|
|
119
|
+
|
|
120
|
+
container.dig(:resources, :limits, :cpu).should == "3"
|
|
121
|
+
end
|
|
164
122
|
|
|
165
|
-
|
|
166
|
-
|
|
123
|
+
it "applies_to_all_containers" do
|
|
124
|
+
m = manifest(Kube::Cluster["Deployment"].new {
|
|
125
|
+
metadata.name = "multi"
|
|
126
|
+
metadata.labels = { "app.kubernetes.io/size": "micro" }
|
|
127
|
+
spec.selector.matchLabels = { app: "multi" }
|
|
128
|
+
spec.template.metadata.labels = { app: "multi" }
|
|
129
|
+
spec.template.spec.containers = [
|
|
130
|
+
{ name: "app", image: "app:latest" },
|
|
131
|
+
{ name: "sidecar", image: "sidecar:latest" },
|
|
132
|
+
]
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
Middleware::ResourcePreset.new.call(m)
|
|
136
|
+
containers = m.resources.first.to_h.dig(:spec, :template, :spec, :containers)
|
|
137
|
+
|
|
138
|
+
containers.last.dig(:resources, :requests, :cpu).should == "250m"
|
|
139
|
+
end
|
|
167
140
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
{ name: "web", image: "nginx:latest" },
|
|
175
|
-
]
|
|
176
|
-
})
|
|
141
|
+
it "skips_non_pod_bearing_resources" do
|
|
142
|
+
resource = Kube::Cluster["ConfigMap"].new {
|
|
143
|
+
metadata.name = "config"
|
|
144
|
+
metadata.labels = { "app.kubernetes.io/size": "small" }
|
|
145
|
+
}
|
|
146
|
+
m = manifest(resource)
|
|
177
147
|
|
|
178
|
-
|
|
179
|
-
container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0)
|
|
148
|
+
Middleware::ResourcePreset.new.call(m)
|
|
180
149
|
|
|
181
|
-
|
|
182
|
-
|
|
150
|
+
m.resources.first.to_h.should == resource.to_h
|
|
151
|
+
end
|
|
183
152
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
})
|
|
194
|
-
|
|
195
|
-
error = assert_raises(ArgumentError) do
|
|
196
|
-
Middleware::ResourcePreset.new.call(m)
|
|
197
|
-
end
|
|
153
|
+
it "skips_resources_without_size_label" do
|
|
154
|
+
m = manifest(Kube::Cluster["Deployment"].new {
|
|
155
|
+
metadata.name = "web"
|
|
156
|
+
spec.selector.matchLabels = { app: "web" }
|
|
157
|
+
spec.template.metadata.labels = { app: "web" }
|
|
158
|
+
spec.template.spec.containers = [
|
|
159
|
+
{ name: "web", image: "nginx:latest" },
|
|
160
|
+
]
|
|
161
|
+
})
|
|
198
162
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
end
|
|
163
|
+
Middleware::ResourcePreset.new.call(m)
|
|
164
|
+
container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0)
|
|
202
165
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
metadata.name = "db"
|
|
206
|
-
metadata.labels = { "app.kubernetes.io/size": "medium" }
|
|
207
|
-
spec.selector.matchLabels = { app: "db" }
|
|
208
|
-
spec.template.metadata.labels = { app: "db" }
|
|
209
|
-
spec.template.spec.containers = [
|
|
210
|
-
{ name: "postgres", image: "postgres:16" },
|
|
211
|
-
]
|
|
212
|
-
})
|
|
213
|
-
|
|
214
|
-
Middleware::ResourcePreset.new.call(m)
|
|
215
|
-
container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0)
|
|
216
|
-
|
|
217
|
-
assert_equal "500m", container.dig(:resources, :requests, :cpu)
|
|
218
|
-
assert_equal "1024Mi", container.dig(:resources, :requests, :memory)
|
|
219
|
-
end
|
|
166
|
+
container[:resources].should.be.nil
|
|
167
|
+
end
|
|
220
168
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
Middleware::ResourcePreset.new.call(m)
|
|
236
|
-
container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0)
|
|
237
|
-
|
|
238
|
-
# The container's explicit value wins over the preset
|
|
239
|
-
assert_equal "999m", container.dig(:resources, :requests, :cpu)
|
|
240
|
-
# The preset fills in missing values
|
|
241
|
-
assert_equal "512Mi", container.dig(:resources, :requests, :memory)
|
|
242
|
-
end
|
|
169
|
+
it "raises_on_unknown_size" do
|
|
170
|
+
m = manifest(Kube::Cluster["Deployment"].new {
|
|
171
|
+
metadata.name = "web"
|
|
172
|
+
metadata.labels = { "app.kubernetes.io/size": "potato" }
|
|
173
|
+
spec.selector.matchLabels = { app: "web" }
|
|
174
|
+
spec.template.metadata.labels = { app: "web" }
|
|
175
|
+
spec.template.spec.containers = [
|
|
176
|
+
{ name: "web", image: "nginx:latest" },
|
|
177
|
+
]
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
lambda { Middleware::ResourcePreset.new.call(m) }.should.raise ArgumentError
|
|
181
|
+
end
|
|
243
182
|
|
|
244
|
-
|
|
183
|
+
it "applies_to_statefulset" do
|
|
184
|
+
m = manifest(Kube::Cluster["StatefulSet"].new {
|
|
185
|
+
metadata.name = "db"
|
|
186
|
+
metadata.labels = { "app.kubernetes.io/size": "medium" }
|
|
187
|
+
spec.selector.matchLabels = { app: "db" }
|
|
188
|
+
spec.template.metadata.labels = { app: "db" }
|
|
189
|
+
spec.template.spec.containers = [
|
|
190
|
+
{ name: "postgres", image: "postgres:16" },
|
|
191
|
+
]
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
Middleware::ResourcePreset.new.call(m)
|
|
195
|
+
container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0)
|
|
196
|
+
|
|
197
|
+
container.dig(:resources, :requests, :cpu).should == "500m"
|
|
198
|
+
end
|
|
245
199
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
200
|
+
it "preserves_existing_container_resources_via_deep_merge" do
|
|
201
|
+
m = manifest(Kube::Cluster["Deployment"].new {
|
|
202
|
+
metadata.name = "web"
|
|
203
|
+
metadata.labels = { "app.kubernetes.io/size": "small" }
|
|
204
|
+
spec.selector.matchLabels = { app: "web" }
|
|
205
|
+
spec.template.metadata.labels = { app: "web" }
|
|
206
|
+
spec.template.spec.containers = [
|
|
207
|
+
{
|
|
208
|
+
name: "web", image: "nginx:latest",
|
|
209
|
+
resources: { requests: { cpu: "999m" } },
|
|
210
|
+
},
|
|
211
|
+
]
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
Middleware::ResourcePreset.new.call(m)
|
|
215
|
+
container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0)
|
|
216
|
+
|
|
217
|
+
# The container's explicit value wins over the preset
|
|
218
|
+
container.dig(:resources, :requests, :cpu).should == "999m"
|
|
251
219
|
end
|
|
220
|
+
|
|
221
|
+
private
|
|
222
|
+
|
|
223
|
+
def manifest(*resources)
|
|
224
|
+
m = Kube::Cluster::Manifest.new
|
|
225
|
+
resources.each { |r| m << r }
|
|
226
|
+
m
|
|
227
|
+
end
|
|
252
228
|
end
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
require "kube/cluster"
|
|
6
|
-
end
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "kube/cluster"
|
|
7
5
|
|
|
8
6
|
module Kube
|
|
9
7
|
module Cluster
|
|
@@ -88,167 +86,142 @@ module Kube
|
|
|
88
86
|
end
|
|
89
87
|
end
|
|
90
88
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
class SecurityContextMiddlewareTest < Minitest::Test
|
|
95
|
-
Middleware = Kube::Cluster::Middleware
|
|
96
|
-
|
|
97
|
-
def test_applies_restricted_profile_by_default
|
|
98
|
-
m = manifest(Kube::Cluster["Deployment"].new {
|
|
99
|
-
metadata.name = "web"
|
|
100
|
-
spec.selector.matchLabels = { app: "web" }
|
|
101
|
-
spec.template.metadata.labels = { app: "web" }
|
|
102
|
-
spec.template.spec.containers = [
|
|
103
|
-
{ name: "web", image: "nginx:latest" },
|
|
104
|
-
]
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
Middleware::SecurityContext.new.call(m)
|
|
108
|
-
h = m.resources.first.to_h
|
|
109
|
-
pod_sc = h.dig(:spec, :template, :spec, :securityContext)
|
|
110
|
-
container_sc = h.dig(:spec, :template, :spec, :containers, 0, :securityContext)
|
|
111
|
-
|
|
112
|
-
assert_equal true, pod_sc[:runAsNonRoot]
|
|
113
|
-
assert_equal 1000, pod_sc[:runAsUser]
|
|
114
|
-
assert_equal 1000, pod_sc[:fsGroup]
|
|
115
|
-
assert_equal({ type: "RuntimeDefault" }, pod_sc[:seccompProfile])
|
|
116
|
-
|
|
117
|
-
assert_equal false, container_sc[:allowPrivilegeEscalation]
|
|
118
|
-
assert_equal true, container_sc[:readOnlyRootFilesystem]
|
|
119
|
-
assert_equal({ drop: ["ALL"] }, container_sc[:capabilities])
|
|
120
|
-
end
|
|
89
|
+
test do
|
|
90
|
+
Middleware = Kube::Cluster::Middleware
|
|
121
91
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
})
|
|
132
|
-
|
|
133
|
-
Middleware::SecurityContext.new.call(m)
|
|
134
|
-
h = m.resources.first.to_h
|
|
135
|
-
pod_sc = h.dig(:spec, :template, :spec, :securityContext)
|
|
136
|
-
container_sc = h.dig(:spec, :template, :spec, :containers, 0, :securityContext)
|
|
137
|
-
|
|
138
|
-
assert_equal true, pod_sc[:runAsNonRoot]
|
|
139
|
-
assert_nil pod_sc[:seccompProfile]
|
|
140
|
-
|
|
141
|
-
assert_equal false, container_sc[:allowPrivilegeEscalation]
|
|
142
|
-
assert_nil container_sc[:readOnlyRootFilesystem]
|
|
143
|
-
end
|
|
92
|
+
it "applies_restricted_profile_by_default" do
|
|
93
|
+
m = manifest(Kube::Cluster["Deployment"].new {
|
|
94
|
+
metadata.name = "web"
|
|
95
|
+
spec.selector.matchLabels = { app: "web" }
|
|
96
|
+
spec.template.metadata.labels = { app: "web" }
|
|
97
|
+
spec.template.spec.containers = [
|
|
98
|
+
{ name: "web", image: "nginx:latest" },
|
|
99
|
+
]
|
|
100
|
+
})
|
|
144
101
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
spec.selector.matchLabels = { app: "web" }
|
|
149
|
-
spec.template.metadata.labels = { app: "web" }
|
|
150
|
-
spec.template.spec.containers = [
|
|
151
|
-
{ name: "web", image: "nginx:latest" },
|
|
152
|
-
]
|
|
153
|
-
})
|
|
154
|
-
|
|
155
|
-
Middleware::SecurityContext.new(default: :baseline).call(m)
|
|
156
|
-
h = m.resources.first.to_h
|
|
157
|
-
pod_sc = h.dig(:spec, :template, :spec, :securityContext)
|
|
158
|
-
|
|
159
|
-
assert_nil pod_sc[:seccompProfile]
|
|
160
|
-
end
|
|
102
|
+
Middleware::SecurityContext.new.call(m)
|
|
103
|
+
h = m.resources.first.to_h
|
|
104
|
+
pod_sc = h.dig(:spec, :template, :spec, :securityContext)
|
|
161
105
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
metadata.name = "web"
|
|
165
|
-
metadata.labels = { "app.kubernetes.io/security": "restricted" }
|
|
166
|
-
spec.selector.matchLabels = { app: "web" }
|
|
167
|
-
spec.template.metadata.labels = { app: "web" }
|
|
168
|
-
spec.template.spec.containers = [
|
|
169
|
-
{ name: "web", image: "nginx:latest" },
|
|
170
|
-
]
|
|
171
|
-
})
|
|
172
|
-
|
|
173
|
-
Middleware::SecurityContext.new(default: :baseline).call(m)
|
|
174
|
-
h = m.resources.first.to_h
|
|
175
|
-
pod_sc = h.dig(:spec, :template, :spec, :securityContext)
|
|
176
|
-
|
|
177
|
-
assert_equal({ type: "RuntimeDefault" }, pod_sc[:seccompProfile])
|
|
178
|
-
end
|
|
106
|
+
pod_sc[:runAsNonRoot].should == true
|
|
107
|
+
end
|
|
179
108
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
end
|
|
109
|
+
it "applies_baseline_profile_via_label" do
|
|
110
|
+
m = manifest(Kube::Cluster["Deployment"].new {
|
|
111
|
+
metadata.name = "web"
|
|
112
|
+
metadata.labels = { "app.kubernetes.io/security": "baseline" }
|
|
113
|
+
spec.selector.matchLabels = { app: "web" }
|
|
114
|
+
spec.template.metadata.labels = { app: "web" }
|
|
115
|
+
spec.template.spec.containers = [
|
|
116
|
+
{ name: "web", image: "nginx:latest" },
|
|
117
|
+
]
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
Middleware::SecurityContext.new.call(m)
|
|
121
|
+
h = m.resources.first.to_h
|
|
122
|
+
pod_sc = h.dig(:spec, :template, :spec, :securityContext)
|
|
123
|
+
|
|
124
|
+
pod_sc[:runAsNonRoot].should == true
|
|
125
|
+
end
|
|
198
126
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
127
|
+
it "applies_baseline_profile_via_constructor_default" do
|
|
128
|
+
m = manifest(Kube::Cluster["Deployment"].new {
|
|
129
|
+
metadata.name = "web"
|
|
130
|
+
spec.selector.matchLabels = { app: "web" }
|
|
131
|
+
spec.template.metadata.labels = { app: "web" }
|
|
132
|
+
spec.template.spec.containers = [
|
|
133
|
+
{ name: "web", image: "nginx:latest" },
|
|
134
|
+
]
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
Middleware::SecurityContext.new(default: :baseline).call(m)
|
|
138
|
+
h = m.resources.first.to_h
|
|
139
|
+
pod_sc = h.dig(:spec, :template, :spec, :securityContext)
|
|
140
|
+
|
|
141
|
+
pod_sc[:seccompProfile].should.be.nil
|
|
142
|
+
end
|
|
202
143
|
|
|
203
|
-
|
|
144
|
+
it "label_overrides_constructor_default" do
|
|
145
|
+
m = manifest(Kube::Cluster["Deployment"].new {
|
|
146
|
+
metadata.name = "web"
|
|
147
|
+
metadata.labels = { "app.kubernetes.io/security": "restricted" }
|
|
148
|
+
spec.selector.matchLabels = { app: "web" }
|
|
149
|
+
spec.template.metadata.labels = { app: "web" }
|
|
150
|
+
spec.template.spec.containers = [
|
|
151
|
+
{ name: "web", image: "nginx:latest" },
|
|
152
|
+
]
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
Middleware::SecurityContext.new(default: :baseline).call(m)
|
|
156
|
+
h = m.resources.first.to_h
|
|
157
|
+
pod_sc = h.dig(:spec, :template, :spec, :securityContext)
|
|
158
|
+
|
|
159
|
+
pod_sc[:seccompProfile].should == { type: "RuntimeDefault" }
|
|
160
|
+
end
|
|
204
161
|
|
|
205
|
-
|
|
206
|
-
|
|
162
|
+
it "applies_to_all_containers" do
|
|
163
|
+
m = manifest(Kube::Cluster["Deployment"].new {
|
|
164
|
+
metadata.name = "web"
|
|
165
|
+
spec.selector.matchLabels = { app: "web" }
|
|
166
|
+
spec.template.metadata.labels = { app: "web" }
|
|
167
|
+
spec.template.spec.containers = [
|
|
168
|
+
{ name: "app", image: "app:latest" },
|
|
169
|
+
{ name: "sidecar", image: "sidecar:latest" },
|
|
170
|
+
]
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
Middleware::SecurityContext.new.call(m)
|
|
174
|
+
containers = m.resources.first.to_h.dig(:spec, :template, :spec, :containers)
|
|
175
|
+
|
|
176
|
+
containers.last.dig(:securityContext, :allowPrivilegeEscalation).should == false
|
|
177
|
+
end
|
|
207
178
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
metadata.labels = { "app.kubernetes.io/security": "yolo" }
|
|
212
|
-
spec.selector.matchLabels = { app: "web" }
|
|
213
|
-
spec.template.metadata.labels = { app: "web" }
|
|
214
|
-
spec.template.spec.containers = [
|
|
215
|
-
{ name: "web", image: "nginx:latest" },
|
|
216
|
-
]
|
|
217
|
-
})
|
|
218
|
-
|
|
219
|
-
error = assert_raises(ArgumentError) do
|
|
220
|
-
Middleware::SecurityContext.new.call(m)
|
|
221
|
-
end
|
|
179
|
+
it "skips_non_pod_bearing_resources" do
|
|
180
|
+
resource = Kube::Cluster["ConfigMap"].new { metadata.name = "config" }
|
|
181
|
+
m = manifest(resource)
|
|
222
182
|
|
|
223
|
-
|
|
224
|
-
end
|
|
183
|
+
Middleware::SecurityContext.new.call(m)
|
|
225
184
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
metadata.name = "web"
|
|
229
|
-
spec.selector.matchLabels = { app: "web" }
|
|
230
|
-
spec.template.metadata.labels = { app: "web" }
|
|
231
|
-
spec.template.spec.securityContext = { runAsUser: 9999 }
|
|
232
|
-
spec.template.spec.containers = [
|
|
233
|
-
{ name: "web", image: "nginx:latest" },
|
|
234
|
-
]
|
|
235
|
-
})
|
|
236
|
-
|
|
237
|
-
Middleware::SecurityContext.new.call(m)
|
|
238
|
-
pod_sc = m.resources.first.to_h.dig(:spec, :template, :spec, :securityContext)
|
|
239
|
-
|
|
240
|
-
# Existing value wins
|
|
241
|
-
assert_equal 9999, pod_sc[:runAsUser]
|
|
242
|
-
# Middleware fills in missing values
|
|
243
|
-
assert_equal true, pod_sc[:runAsNonRoot]
|
|
244
|
-
end
|
|
185
|
+
m.resources.first.to_h.should == resource.to_h
|
|
186
|
+
end
|
|
245
187
|
|
|
246
|
-
|
|
188
|
+
it "raises_on_unknown_profile" do
|
|
189
|
+
m = manifest(Kube::Cluster["Deployment"].new {
|
|
190
|
+
metadata.name = "web"
|
|
191
|
+
metadata.labels = { "app.kubernetes.io/security": "yolo" }
|
|
192
|
+
spec.selector.matchLabels = { app: "web" }
|
|
193
|
+
spec.template.metadata.labels = { app: "web" }
|
|
194
|
+
spec.template.spec.containers = [
|
|
195
|
+
{ name: "web", image: "nginx:latest" },
|
|
196
|
+
]
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
lambda { Middleware::SecurityContext.new.call(m) }.should.raise ArgumentError
|
|
200
|
+
end
|
|
247
201
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
202
|
+
it "preserves_existing_pod_security_context" do
|
|
203
|
+
m = manifest(Kube::Cluster["Deployment"].new {
|
|
204
|
+
metadata.name = "web"
|
|
205
|
+
spec.selector.matchLabels = { app: "web" }
|
|
206
|
+
spec.template.metadata.labels = { app: "web" }
|
|
207
|
+
spec.template.spec.securityContext = { runAsUser: 9999 }
|
|
208
|
+
spec.template.spec.containers = [
|
|
209
|
+
{ name: "web", image: "nginx:latest" },
|
|
210
|
+
]
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
Middleware::SecurityContext.new.call(m)
|
|
214
|
+
pod_sc = m.resources.first.to_h.dig(:spec, :template, :spec, :securityContext)
|
|
215
|
+
|
|
216
|
+
# Existing value wins
|
|
217
|
+
pod_sc[:runAsUser].should == 9999
|
|
253
218
|
end
|
|
219
|
+
|
|
220
|
+
private
|
|
221
|
+
|
|
222
|
+
def manifest(*resources)
|
|
223
|
+
m = Kube::Cluster::Manifest.new
|
|
224
|
+
resources.each { |r| m << r }
|
|
225
|
+
m
|
|
226
|
+
end
|
|
254
227
|
end
|