Skip to content

Commit 5203ec6

Browse files
authored
Merge pull request #73 from jgnagy/feat/allow-overriding-template-base-labels
Better handling of automated template pod labels
2 parents 1853aea + 82b59d0 commit 5203ec6

File tree

11 files changed

+49
-19
lines changed

11 files changed

+49
-19
lines changed

lib/metatron/template.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ module Metatron
44
# Base class for templating Kubernetes resources
55
class Template
66
attr_accessor :api_version, :name, :additional_labels
7-
attr_reader :kind, :label_namespace
7+
attr_reader :kind
8+
attr_writer :base_labels
89

910
class << self
1011
attr_writer :label_namespace
@@ -38,7 +39,6 @@ def metatron_template_class?
3839

3940
def initialize(name)
4041
@name = name
41-
@label_namespace = self.class.label_namespace
4242
@api_version = "v1"
4343
@kind = find_kind
4444
@additional_labels = {}
@@ -47,10 +47,15 @@ def initialize(name)
4747

4848
alias apiVersion api_version
4949

50-
def base_labels = { "#{label_namespace}/name": name }
50+
def base_labels
51+
@base_labels || { "#{label_namespace}/name": name }
52+
end
5153

5254
private
5355

56+
# defers to the nearest metatron ancestor to determine the label namespace
57+
def label_namespace = self.class.label_namespace
58+
5459
def run_initializers
5560
self.class.nearest_metatron_ancestor.initializers.each { send(_1.to_sym) }
5661
end

lib/metatron/templates/concerns/pod_producer.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ def self.included(base) # rubocop:disable Metrics/MethodLength
99
# base.extend ClassMethods
1010
base.class_eval do
1111
attr_accessor :active_deadline_seconds, :additional_pod_labels,
12-
:affinity, :automount_service_account_token, :containers,
13-
:dns_policy, :enable_service_links, :hostname, :host_ipc, :host_network,
14-
:host_pid, :image_pull_secrets, :init_containers, :node_selector,
15-
:node_name, :persistent_volume_claims, :pod_annotations,
12+
:additional_pod_match_labels, :affinity, :automount_service_account_token,
13+
:containers, :dns_policy, :enable_service_links, :hostname, :host_ipc,
14+
:host_network, :host_pid, :image_pull_secrets, :init_containers,
15+
:node_selector, :node_name, :persistent_volume_claims, :pod_annotations,
1616
:priority_class_name, :restart_policy, :scheduler_name, :security_context,
1717
:service_account, :service_account_name, :share_process_namespace,
1818
:subdomain, :termination_grace_period_seconds, :tolerations, :volumes
@@ -41,6 +41,7 @@ def self.included(base) # rubocop:disable Metrics/MethodLength
4141

4242
def pod_producer_initialize
4343
@additional_pod_labels = {}
44+
@additional_pod_match_labels = {}
4445
@affinity = {}
4546
@containers = []
4647
@enable_service_links = nil

lib/metatron/templates/daemon_set.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def render
2525
}.merge(formatted_annotations).merge(formatted_namespace),
2626
spec: {
2727
selector: {
28-
matchLabels: base_labels.merge(additional_pod_labels)
28+
matchLabels: base_labels.merge(additional_pod_match_labels)
2929
}
3030
}.merge(pod_template)
3131
}

lib/metatron/templates/deployment.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def render
2828
replicas:,
2929
strategy:,
3030
selector: {
31-
matchLabels: base_labels.merge(additional_pod_labels)
31+
matchLabels: base_labels.merge(additional_pod_match_labels)
3232
}
3333
}.merge(pod_template).compact
3434
}

lib/metatron/templates/replica_set.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def render
2727
spec: {
2828
replicas:,
2929
selector: {
30-
matchLabels: base_labels.merge(additional_pod_labels)
30+
matchLabels: base_labels.merge(additional_pod_match_labels)
3131
}
3232
}.merge(pod_template)
3333
}

lib/metatron/templates/stateful_set.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def render
3838
serviceName:,
3939
updateStrategy:,
4040
selector: {
41-
matchLabels: base_labels.merge(additional_pod_labels)
41+
matchLabels: base_labels.merge(additional_pod_match_labels)
4242
}
4343
}.merge(pod_template).merge(volume_claim_templates).compact
4444
}

spec/metatron/template_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,25 @@
2323
thing = FakeConfigMap.new("test", { "foo" => "bar" })
2424
expect(thing.annotations).to eq({}) # this is set by Concerns::Annotated#annotated_initialize
2525
end
26+
27+
it "uses the correct base label namespace" do
28+
actual_labels = FakeConfigMap.new("test", { "foo" => "bar" }).render[:metadata][:labels]
29+
expect(actual_labels.keys).to include(:"metatron.therubyist.org/name")
30+
end
31+
32+
it "allows using a different base label namespace" do
33+
FakeConfigMap.label_namespace = "example.com"
34+
actual_labels = FakeConfigMap.new("test", { "foo" => "bar" }).render[:metadata][:labels]
35+
expect(actual_labels.keys).to include(:"example.com/name")
36+
FakeConfigMap.label_namespace = "metatron.therubyist.org"
37+
end
38+
39+
it "allows setting the base labels directly on template instances" do
40+
cm = FakeConfigMap.new("test", { "foo" => "bar" })
41+
cm.base_labels = { "baz" => "qux" }
42+
actual_labels = cm.render[:metadata][:labels]
43+
expect(actual_labels).to eq({ "baz" => "qux" })
44+
end
2645
end
2746
end
2847

spec/metatron/templates/daemon_set_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@
8383
ds.annotations = { "a.test/foo": "bar" }
8484
ds.additional_labels = { "app.kubernetes.io/part-of": "test-app" }
8585
ds.namespace = "test-namespace"
86-
ds.additional_pod_labels = { thing: "swamp" }
86+
ds.additional_pod_labels = { them: "hills", thing: "swamp" }
87+
ds.additional_pod_match_labels = { thing: "swamp" }
8788
ds.security_context = { runAsUser: 1000, runAsGroup: 1000 }
8889
ds.volumes = [{ name: "tmpvol", emptyDir: {} }]
8990

@@ -109,7 +110,7 @@
109110
},
110111
template: {
111112
metadata: {
112-
labels: { "metatron.therubyist.org/name": "test", thing: "swamp" }
113+
labels: { "metatron.therubyist.org/name": "test", them: "hills", thing: "swamp" }
113114
},
114115
spec: {
115116
containers: [

spec/metatron/templates/deployment_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@
8686
dep.namespace = "test-namespace"
8787
dep.replicas = 10
8888
dep.strategy = { type: "RollingUpdate", rollingUpdate: { maxSurge: 1, maxUnavailable: 0 } }
89-
dep.additional_pod_labels = { thing: "swamp" }
89+
dep.additional_pod_labels = { them: "hills", thing: "swamp" }
90+
dep.additional_pod_match_labels = { thing: "swamp" }
9091
dep.security_context = { runAsUser: 1000, runAsGroup: 1000 }
9192
dep.volumes = [{ name: "tmpvol", emptyDir: {} }]
9293

@@ -114,7 +115,7 @@
114115
},
115116
template: {
116117
metadata: {
117-
labels: { "metatron.therubyist.org/name": "test", thing: "swamp" }
118+
labels: { "metatron.therubyist.org/name": "test", them: "hills", thing: "swamp" }
118119
},
119120
spec: {
120121
containers: [

spec/metatron/templates/replica_set_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@
7474
rs.annotations = { "a.test/foo": "bar" }
7575
rs.additional_labels = { "app.kubernetes.io/part-of": "test-app" }
7676
rs.replicas = 10
77-
rs.additional_pod_labels = { thing: "swamp" }
77+
rs.additional_pod_labels = { them: "hills", thing: "swamp" }
78+
rs.additional_pod_match_labels = { thing: "swamp" }
7879
rs.security_context = { runAsUser: 1000, runAsGroup: 1000 }
7980
rs.volumes = [{ name: "tmpvol", emptyDir: {} }]
8081

@@ -100,7 +101,7 @@
100101
},
101102
template: {
102103
metadata: {
103-
labels: { "metatron.therubyist.org/name": "test", thing: "swamp" }
104+
labels: { "metatron.therubyist.org/name": "test", them: "hills", thing: "swamp" }
104105
},
105106
spec: {
106107
containers: [

0 commit comments

Comments
 (0)