Class: Pfab::Templates::Web
Constant Summary collapse
- ANTI_AFFINITY_TYPES =
%w[disabled required preferred]- ANTI_AFFINITY_MODE =
'antiAffinityMode'- ANTI_AFFINITY_PREFERRED_MODE_WEIGHT =
'antiAffinityPreferredModeWeight'- ZONE_ANTI_AFFINITY_MODE =
'zoneAntiAffinityMode'- ZONE_ANTI_AFFINITY_PREFERRED_MODE_WEIGHT =
'zoneAntiAffinityPreferredModeWeight'
Instance Method Summary collapse
- #anti_affinity ⇒ Object
- #anti_affinity_builder(key, weight_key, topology_key) ⇒ Object
- #application_type ⇒ Object
- #default_probe ⇒ Object
- #deployment ⇒ Object
- #host_anti_affinity ⇒ Object
- #hosts ⇒ Object
- #ingres_enabled? ⇒ Boolean
- #ingress ⇒ Object
- #ingress_annotations ⇒ Object
- #labelSelector ⇒ Object
- #lifecycle ⇒ Object
- #livenessProbe ⇒ Object
- #pod_disruption_budget ⇒ Object
- #readinessProbe ⇒ Object
- #rules ⇒ Object
- #service ⇒ Object
- #service_annotations ⇒ Object
- #startupProbe ⇒ Object
- #tls_hosts ⇒ Object
- #topology_spread_constraints ⇒ Object
- #write_to(f) ⇒ Object
Methods inherited from Base
#app_vars, #container_ports, #cpu, #deploy_id, #deploy_unique_id, #env_from, #env_vars, #get, #get_command, #get_namespace, #image_name, #initialize, #load_env_vars, #load_secrets, #memory, #resources
Constructor Details
This class inherits a constructor from Pfab::Templates::Base
Instance Method Details
#anti_affinity ⇒ Object
198 199 200 |
# File 'lib/pfab/templates/web.rb', line 198 def anti_affinity return host_anti_affinity end |
#anti_affinity_builder(key, weight_key, topology_key) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/pfab/templates/web.rb', line 252 def anti_affinity_builder(key, weight_key, topology_key) antiAffinityMode = get(key) || "disabled" if antiAffinityMode affinitySelector = { topologyKey: topology_key, labelSelector: labelSelector, } return case antiAffinityMode when "disabled" puts "antiAffinityMode is set to disabled, skipping" {} when "required" { podAntiAffinity: { requiredDuringSchedulingIgnoredDuringExecution: [ affinitySelector ] } } when "preferred" { podAntiAffinity: { preferredDuringSchedulingIgnoredDuringExecution: [ { weight: app_vars[weight_key] || 100, podAffinityTerm: affinitySelector } ] } } else raise "Unexpected value #{antiAffinityMode} specified for `#{key}`. Valid selections are #{ANTI_AFFINITY_TYPES}" end end return {} end |
#application_type ⇒ Object
166 167 168 |
# File 'lib/pfab/templates/web.rb', line 166 def application_type "web" end |
#default_probe ⇒ Object
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/pfab/templates/web.rb', line 139 def default_probe { httpGet: { path: get("health_check_path") || "/", port: get("port"), }, initialDelaySeconds: 15, timeoutSeconds: 3 } end |
#deployment ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/pfab/templates/web.rb', line 287 def deployment secret_mounts = get("secretMounts") || [] volume_mounts = [] volumes = [] secret_mounts.each do |secret_mount| volumes.append({ name: secret_mount['name'], secret: { secretName: secret_mount['secretName'] } }) volume_mounts.append({ name: secret_mount['name'], mountPath: secret_mount['path'], readOnly: secret_mount['readOnly'] || true }) end if get("datadogVolumeMountEnabled") datadog_volume_name = "ddsocket" datadog_path = "/var/run/datadog" volumes.append({ name: datadog_volume_name, hostPath: { path: datadog_path } }) volume_mounts.append( name: datadog_volume_name, mountPath: datadog_path, readOnly: true ) end ports = container_ports() { kind: "Deployment", apiVersion: "apps/v1", metadata: { name: @data['deployed_name'], namespace: get_namespace, labels: { application: @data['application'], "deployed-name" => @data['deployed_name'], "application-type" => application_type, "deploy-id" => deploy_id, LABEL_DEPLOY_UNIQUE_ID: StyledYAML.double_quoted(deploy_unique_id), "tags.datadoghq.com/env": @data['env'], "tags.datadoghq.com/service": @data['deployed_name'], "tags.datadoghq.com/version": StyledYAML.double_quoted(@data['sha']) } }, spec: { replicas: get("replicas") || 1, selector: { matchLabels: { "deployed-name" => @data['deployed_name'], }, }, strategy: { type: "RollingUpdate", rollingUpdate: { maxSurge: 1, maxUnavailable: 0, } }, revisionHistoryLimit: 5, progressDeadlineSeconds: 120, template: { metadata: { labels: { application: @data['application'], "deployed-name" => @data['deployed_name'], "application-type" => "web", LABEL_DEPLOY_UNIQUE_ID => StyledYAML.double_quoted(deploy_unique_id), "tags.datadoghq.com/env": @data['env'], "tags.datadoghq.com/service": @data['deployed_name'], "tags.datadoghq.com/version": StyledYAML.double_quoted(@data['sha']) }, }, spec: { serviceAccountName: get('serviceAccountName'), terminationGracePeriodSeconds: get("terminationGracePeriodSeconds") || 30, containers: [ { image: image_name, name: @data['deployed_name'], command: get_command, env: env_vars, envFrom: env_from, resources: resources, ports: ports, livenessProbe: livenessProbe, readinessProbe: readinessProbe, startupProbe: startupProbe, lifecycle: lifecycle, volumeMounts: volume_mounts }.compact ], topologySpreadConstraints: topology_spread_constraints, volumes: volumes }.compact, }, }.compact, }.compact end |
#host_anti_affinity ⇒ Object
203 204 205 |
# File 'lib/pfab/templates/web.rb', line 203 def host_anti_affinity anti_affinity_builder(ANTI_AFFINITY_MODE, ANTI_AFFINITY_PREFERRED_MODE_WEIGHT, "kubernetes.io/hostname") end |
#hosts ⇒ Object
96 97 98 |
# File 'lib/pfab/templates/web.rb', line 96 def hosts get("host").split(",") end |
#ingres_enabled? ⇒ Boolean
25 26 27 28 29 30 |
# File 'lib/pfab/templates/web.rb', line 25 def ingres_enabled? if not app_vars.has_key?('generateIngressEnabled') || app_vars['generateIngressEnabled'] return true end return false end |
#ingress ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/pfab/templates/web.rb', line 67 def ingress { apiVersion: "networking.k8s.io/v1", kind: "Ingress", metadata: { name: "ingress-#{@data['deployed_name']}", namespace: get_namespace, labels: { application: @data['application'], "deployed-name" => @data['deployed_name'], }, annotations: ingress_annotations, }, spec: { rules: rules, tls: tls_hosts }, } end |
#ingress_annotations ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/pfab/templates/web.rb', line 124 def ingress_annotations h = { "kubernetes.io/ingress.class" => "traefik", "traefik.frontend.passHostHeader" => "false", "traefik.frontend.priority" => "1", "traefik.frontend.entryPoints" => "https", "traefik.protocol" => get("protocol") || "http", "traefik.frontend.headers.SSLRedirect" => "true", "traefik.docker.network" => "traefik", "traefik.ingress.kubernetes.io/router.entrypoints" => "websecure", "traefik.ingress.kubernetes.io/router.tls" => "true" } h end |
#labelSelector ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/pfab/templates/web.rb', line 231 def labelSelector { matchExpressions: [ { key: "deployed-name", operator: "In", values: [ @data['deployed_name'] ] }, { key: LABEL_DEPLOY_UNIQUE_ID, operator: "In", values: [ StyledYAML.double_quoted(deploy_unique_id) ] } ] } end |
#lifecycle ⇒ Object
162 163 164 |
# File 'lib/pfab/templates/web.rb', line 162 def lifecycle get("lifecycle") end |
#livenessProbe ⇒ Object
150 151 152 |
# File 'lib/pfab/templates/web.rb', line 150 def livenessProbe get("livenessProbe") || default_probe end |
#pod_disruption_budget ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/pfab/templates/web.rb', line 170 def pod_disruption_budget pdb = { apiVersion: "policy/v1", kind: "PodDisruptionBudget", metadata: { name: "#{@data['deployed_name']}-pdb", namespace: get_namespace() }, spec: { minAvailable: 1, selector: { matchLabels: { application: @data['application'], "deployed-name" => @data['deployed_name'], "application-type" => application_type } } } } return pdb end |
#readinessProbe ⇒ Object
154 155 156 |
# File 'lib/pfab/templates/web.rb', line 154 def readinessProbe get("readinessProbe") || default_probe end |
#rules ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/pfab/templates/web.rb', line 100 def rules hosts.map do |host| { host: host, http: { paths: [ { path: "/", pathType: "Prefix", backend: { service: { name: @data['deployed_name'], port: { name: "http" } } }, }, ], }, } end end |
#service ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/pfab/templates/web.rb', line 32 def service { apiVersion: "v1", kind: "Service", metadata: { name: @data['deployed_name'], namespace: get_namespace, labels: { application: @data['application'], "deployed-name" => @data['deployed_name'], }, annotations: service_annotations, }, spec: { selector: { "deployed-name" => @data['deployed_name'], }, ports: [ { name: "http", port: app_vars["service_listen_port"] || 80, targetPort: app_vars["port"], appProtocol: app_vars["appProtocol"] }.compact ] } } end |
#service_annotations ⇒ Object
61 62 63 64 65 |
# File 'lib/pfab/templates/web.rb', line 61 def service_annotations h = {} h["traefik.ingress.kubernetes.io/service.serversscheme"] = "h2c" if get("protocol") == "h2c" h end |
#startupProbe ⇒ Object
158 159 160 |
# File 'lib/pfab/templates/web.rb', line 158 def startupProbe get("startupProbe") || default_probe end |
#tls_hosts ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/pfab/templates/web.rb', line 87 def tls_hosts hosts.map do |host| { hosts: [host], secretName: get("tls_cert_secret") } end end |
#topology_spread_constraints ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/pfab/templates/web.rb', line 208 def topology_spread_constraints waiveTopologySpreadConstraints = get("waiveTopologySpreadConstraints") || false schedulingRule = waiveTopologySpreadConstraints ? "ScheduleAnyway" : "DoNotSchedule" zone_constraint = { maxSkew: 1, topologyKey: "topology.kubernetes.io/zone", whenUnsatisfiable: schedulingRule, labelSelector: labelSelector } host_constraint = { maxSkew: 1, topologyKey: "kubernetes.io/hostname", whenUnsatisfiable: schedulingRule, labelSelector: labelSelector } [zone_constraint, host_constraint] end |
#write_to(f) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/pfab/templates/web.rb', line 7 def write_to(f) if ingres_enabled? && get("host").nil? puts "No host to configure ingress for #{@data['deployed_name']}. Skipping deployment. add a host or generateIngressEnabled:false" else f << StyledYAML.dump(service.deep_stringify_keys) if ingres_enabled? f << StyledYAML.dump(ingress.deep_stringify_keys) else puts "skipping ingress because ingress_disabled = #{@data['generateIngressEnabled']}" end f << StyledYAML.dump(deployment.deep_stringify_keys) replica_count = get("replicas") ||1 if (replica_count > 1) f << StyledYAML.dump(pod_disruption_budget.deep_stringify_keys) end end end |