Class: PodAntiAffinityMiddlewareTest

Inherits:
Minitest::Test
  • Object
show all
Defined in:
lib/kube/cluster/middleware/pod_anti_affinity.rb

Constant Summary collapse

Middleware =
Kube::Cluster::Middleware

Instance Method Summary collapse

Instance Method Details

#test_applies_to_statefulsetObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/kube/cluster/middleware/pod_anti_affinity.rb', line 174

def test_applies_to_statefulset
  m = manifest(Kube::Cluster["StatefulSet"].new {
    .name = "db"
    spec.selector.matchLabels = { app: "db" }
    spec.template..labels = { app: "db" }
    spec.template.spec.containers = [
      { name: "postgres", image: "postgres:16" },
    ]
  })

  Middleware::PodAntiAffinity.new.call(m)
  affinity = m.resources.first.to_h.dig(:spec, :template, :spec, :affinity)

  refute_nil affinity.dig(:podAntiAffinity)
end

#test_custom_topology_keyObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/kube/cluster/middleware/pod_anti_affinity.rb', line 96

def test_custom_topology_key
  m = manifest(Kube::Cluster["Deployment"].new {
    .name = "web"
    spec.selector.matchLabels = { app: "web" }
    spec.template..labels = { app: "web" }
    spec.template.spec.containers = [
      { name: "web", image: "nginx:latest" },
    ]
  })

  Middleware::PodAntiAffinity.new(
    topology_key: "topology.kubernetes.io/zone",
  ).call(m)

  affinity = m.resources.first.to_h.dig(:spec, :template, :spec, :affinity)
  term = affinity.dig(:podAntiAffinity, :preferredDuringSchedulingIgnoredDuringExecution, 0)

  assert_equal "topology.kubernetes.io/zone", term.dig(:podAffinityTerm, :topologyKey)
end

#test_custom_weightObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/kube/cluster/middleware/pod_anti_affinity.rb', line 116

def test_custom_weight
  m = manifest(Kube::Cluster["Deployment"].new {
    .name = "web"
    spec.selector.matchLabels = { app: "web" }
    spec.template..labels = { app: "web" }
    spec.template.spec.containers = [
      { name: "web", image: "nginx:latest" },
    ]
  })

  Middleware::PodAntiAffinity.new(weight: 100).call(m)
  term = m.resources.first.to_h.dig(:spec, :template, :spec, :affinity,
    :podAntiAffinity, :preferredDuringSchedulingIgnoredDuringExecution, 0)

  assert_equal 100, term[:weight]
end

#test_injects_soft_anti_affinity_on_deploymentObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/kube/cluster/middleware/pod_anti_affinity.rb', line 74

def test_injects_soft_anti_affinity_on_deployment
  m = manifest(Kube::Cluster["Deployment"].new {
    .name = "web"
    spec.selector.matchLabels = { app: "web", instance: "prod" }
    spec.template..labels = { app: "web" }
    spec.template.spec.containers = [
      { name: "web", image: "nginx:latest" },
    ]
  })

  Middleware::PodAntiAffinity.new.call(m)
  affinity = m.resources.first.to_h.dig(:spec, :template, :spec, :affinity)

  paa = affinity.dig(:podAntiAffinity, :preferredDuringSchedulingIgnoredDuringExecution)
  assert_equal 1, paa.size

  term = paa.first
  assert_equal 1, term[:weight]
  assert_equal "kubernetes.io/hostname", term.dig(:podAffinityTerm, :topologyKey)
  assert_equal({ app: "web", instance: "prod" }, term.dig(:podAffinityTerm, :labelSelector, :matchLabels))
end

#test_skips_non_pod_bearing_resourcesObject



150
151
152
153
154
155
156
157
# File 'lib/kube/cluster/middleware/pod_anti_affinity.rb', line 150

def test_skips_non_pod_bearing_resources
  resource = Kube::Cluster["ConfigMap"].new { .name = "config" }
  m = manifest(resource)

  Middleware::PodAntiAffinity.new.call(m)

  assert_equal resource.to_h, m.resources.first.to_h
end

#test_skips_resources_with_existing_affinityObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/kube/cluster/middleware/pod_anti_affinity.rb', line 133

def test_skips_resources_with_existing_affinity
  m = manifest(Kube::Cluster["Deployment"].new {
    .name = "web"
    spec.selector.matchLabels = { app: "web" }
    spec.template..labels = { app: "web" }
    spec.template.spec.affinity = { nodeAffinity: { custom: true } }
    spec.template.spec.containers = [
      { name: "web", image: "nginx:latest" },
    ]
  })

  Middleware::PodAntiAffinity.new.call(m)
  affinity = m.resources.first.to_h.dig(:spec, :template, :spec, :affinity)

  assert_equal({ nodeAffinity: { custom: true } }, affinity)
end

#test_skips_resources_without_match_labelsObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/kube/cluster/middleware/pod_anti_affinity.rb', line 159

def test_skips_resources_without_match_labels
  m = manifest(Kube::Cluster["Deployment"].new {
    .name = "web"
    spec.template..labels = { app: "web" }
    spec.template.spec.containers = [
      { name: "web", image: "nginx:latest" },
    ]
  })

  Middleware::PodAntiAffinity.new.call(m)
  affinity = m.resources.first.to_h.dig(:spec, :template, :spec, :affinity)

  assert_nil affinity
end