Module: Metatron::Templates::Concerns::PodProducer

Included in:
Metatron::Templates::CronJob, DaemonSet, Deployment, Job, Pod, ReplicaSet, StatefulSet
Defined in:
lib/metatron/templates/concerns/pod_producer.rb

Overview

A mixin to assist with templating Kubernetes resources that create Pods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

rubocop:disable Metrics/MethodLength



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/metatron/templates/concerns/pod_producer.rb', line 8

def self.included(base) # rubocop:disable Metrics/MethodLength
  # base.extend ClassMethods
  base.class_eval do
    attr_accessor :active_deadline_seconds, :additional_pod_labels,
                  :additional_pod_match_labels, :affinity, :automount_service_account_token,
                  :containers, :dns_policy, :enable_service_links, :hostname, :host_ipc,
                  :host_network, :host_pid, :image_pull_secrets, :init_containers,
                  :node_selector, :node_name, :persistent_volume_claims, :pod_annotations,
                  :priority_class_name, :restart_policy, :scheduler_name, :security_context,
                  :service_account, :service_account_name, :share_process_namespace,
                  :subdomain, :termination_grace_period_seconds, :tolerations, :volumes

    initializer :pod_producer_initialize

    alias_method :activeDeadlineSeconds, :active_deadline_seconds
    alias_method :automountServiceAccountToken, :automount_service_account_token
    alias_method :dnsPolicy, :dns_policy
    alias_method :enableServiceLinks, :enable_service_links
    alias_method :hostIPC, :host_ipc
    alias_method :hostNetwork, :host_network
    alias_method :hostPID, :host_pid
    alias_method :priorityClassName, :priority_class_name
    alias_method :nodeSelector, :node_selector
    alias_method :nodeName, :node_name
    alias_method :restartPolicy, :restart_policy
    alias_method :schedulerName, :scheduler_name
    alias_method :securityContext, :security_context
    alias_method :serviceAccount, :service_account
    alias_method :serviceAccountName, :service_account_name
    alias_method :shareProcessNamespace, :share_process_namespace
    alias_method :terminationGracePeriodSeconds, :termination_grace_period_seconds
  end
end

Instance Method Details

#formatted_affinityObject



61
# File 'lib/metatron/templates/concerns/pod_producer.rb', line 61

def formatted_affinity = affinity && !affinity.empty? ? { affinity: } : {}

#formatted_containersObject



62
# File 'lib/metatron/templates/concerns/pod_producer.rb', line 62

def formatted_containers = { containers: containers.map(&:render) }

#formatted_image_pull_secretsObject



64
65
66
67
68
69
70
# File 'lib/metatron/templates/concerns/pod_producer.rb', line 64

def formatted_image_pull_secrets
  if image_pull_secrets&.any?
    { imagePullSecrets: image_pull_secrets.map { _1.is_a?(String) ? { name: _1 } : _1 } }
  else
    {}
  end
end

#formatted_init_containersObject



72
73
74
75
76
77
78
# File 'lib/metatron/templates/concerns/pod_producer.rb', line 72

def formatted_init_containers
  if init_containers&.any?
    { initContainers: init_containers.map(&:render) }
  else
    {}
  end
end

#formatted_node_selectorObject



80
81
82
# File 'lib/metatron/templates/concerns/pod_producer.rb', line 80

def formatted_node_selector
  node_selector && !node_selector.empty? ? { nodeSelector: } : {}
end

#formatted_pod_annotationsObject



84
85
86
# File 'lib/metatron/templates/concerns/pod_producer.rb', line 84

def formatted_pod_annotations
  pod_annotations && !pod_annotations.empty? ? { annotations: pod_annotations } : {}
end

#formatted_priority_class_nameObject



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/metatron/templates/concerns/pod_producer.rb', line 88

def formatted_priority_class_name
  return {} unless priority_class_name

  if priority_class_name.is_a?(String)
    { priorityClassName: priority_class_name }
  elsif priority_class_name.is_a?(PriorityClass)
    { priorityClassName: priority_class_name.name }
  else
    raise "priority_class_name must be a String or a PriorityClass"
  end
end

#formatted_security_contextObject



100
101
102
# File 'lib/metatron/templates/concerns/pod_producer.rb', line 100

def formatted_security_context
  security_context && !security_context.empty? ? { securityContext: } : {}
end

#formatted_tolerationsObject



104
# File 'lib/metatron/templates/concerns/pod_producer.rb', line 104

def formatted_tolerations = tolerations&.any? ? { tolerations: } : {}

#formatted_volumesObject



105
# File 'lib/metatron/templates/concerns/pod_producer.rb', line 105

def formatted_volumes = volumes&.any? ? { volumes: } : {}

#pod_metadataObject



119
120
121
122
123
124
125
# File 'lib/metatron/templates/concerns/pod_producer.rb', line 119

def 
  {
    metadata: {
      labels: base_labels.merge(additional_pod_labels)
    }.merge(formatted_pod_annotations)
  }
end

#pod_producer_initializeObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/metatron/templates/concerns/pod_producer.rb', line 42

def pod_producer_initialize
  @additional_pod_labels = {}
  @additional_pod_match_labels = {}
  @affinity = {}
  @containers = []
  @enable_service_links = nil
  @image_pull_secrets = []
  @init_containers = []
  @node_selector = {}
  @persistent_volume_claims = []
  @pod_annotations = {}
  @priority_class_name = nil
  @restart_policy = nil
  @security_context = {}
  @termination_grace_period_seconds = nil
  @tolerations = []
  @volumes = []
end

#pod_specObject

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/metatron/templates/concerns/pod_producer.rb', line 129

def pod_spec
  {
    spec: {
      activeDeadlineSeconds:,
      automountServiceAccountToken:,
      dnsPolicy:,
      enableServiceLinks:,
      hostIPC:,
      hostNetwork:,
      hostPID:,
      hostname:,
      nodeName:,
      restartPolicy:,
      schedulerName:,
      serviceAccount:,
      serviceAccountName:,
      shareProcessNamespace:,
      subdomain:,
      terminationGracePeriodSeconds:
    }.merge(formatted_volumes)
      .merge(formatted_affinity)
      .merge(formatted_tolerations)
      .merge(formatted_security_context)
      .merge(formatted_containers)
      .merge(formatted_init_containers)
      .merge(formatted_image_pull_secrets)
      .merge(formatted_node_selector)
      .merge(formatted_priority_class_name)
      .compact
  }.compact
end

#pod_templateObject

rubocop:enable Metrics/AbcSize rubocop:enable Metrics/MethodLength



163
164
165
166
167
# File 'lib/metatron/templates/concerns/pod_producer.rb', line 163

def pod_template
  {
    template: {}.merge().merge(pod_spec).compact
  }
end

#volume_claim_templatesObject



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/metatron/templates/concerns/pod_producer.rb', line 107

def volume_claim_templates
  if persistent_volume_claims&.any?
    {
      volumeClaimTemplates: persistent_volume_claims.map do |c|
        c.respond_to?(:render) ? c.render : c
      end
    }
  else
    {}
  end
end