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
# 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,
                  :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, :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 :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



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

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

#formatted_containersObject



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

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

#formatted_image_pull_secretsObject



61
62
63
64
65
66
67
# File 'lib/metatron/templates/concerns/pod_producer.rb', line 61

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



69
70
71
72
73
74
75
# File 'lib/metatron/templates/concerns/pod_producer.rb', line 69

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

#formatted_node_selectorObject



77
78
79
# File 'lib/metatron/templates/concerns/pod_producer.rb', line 77

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

#formatted_pod_annotationsObject



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

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

#formatted_security_contextObject



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

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

#formatted_tolerationsObject



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

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

#formatted_volumesObject



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

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

#pod_metadataObject



104
105
106
107
108
109
110
# File 'lib/metatron/templates/concerns/pod_producer.rb', line 104

def 
  {
    metadata: {
      labels: { "#{label_namespace}/name": name }.merge(additional_pod_labels)
    }.merge(formatted_pod_annotations)
  }
end

#pod_producer_initializeObject



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

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

#pod_specObject

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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/metatron/templates/concerns/pod_producer.rb', line 114

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)
      .compact
  }.compact
end

#pod_templateObject

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



147
148
149
150
151
# File 'lib/metatron/templates/concerns/pod_producer.rb', line 147

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

#volume_claim_templatesObject



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/metatron/templates/concerns/pod_producer.rb', line 92

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