Class: Metatron::Templates::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/metatron/templates/container.rb

Overview

Template for containers used by k8s resources (not an actual resource)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, image = "gcr.io/google_containers/pause") ⇒ Container

Returns a new instance of Container.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/metatron/templates/container.rb', line 19

def initialize(name, image = "gcr.io/google_containers/pause")
  @name = name
  @image = image
  @command = nil
  @args = []
  @env = []
  @resources = {}
  @volume_mounts = []
  @image_pull_policy = "IfNotPresent"
  @lifecycle = {}
  @probes = {}
  @stdin = true
  @tty = true
  @termination_message_path = nil
  @termination_message_policy = nil
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



7
8
9
# File 'lib/metatron/templates/container.rb', line 7

def args
  @args
end

#commandObject

Returns the value of attribute command.



7
8
9
# File 'lib/metatron/templates/container.rb', line 7

def command
  @command
end

#envObject Also known as: environment

Returns the value of attribute env.



7
8
9
# File 'lib/metatron/templates/container.rb', line 7

def env
  @env
end

#envfromObject Also known as: envFrom

Returns the value of attribute envfrom.



7
8
9
# File 'lib/metatron/templates/container.rb', line 7

def envfrom
  @envfrom
end

#imageObject

Returns the value of attribute image.



7
8
9
# File 'lib/metatron/templates/container.rb', line 7

def image
  @image
end

#image_pull_policyObject Also known as: imagePullPolicy

Returns the value of attribute image_pull_policy.



7
8
9
# File 'lib/metatron/templates/container.rb', line 7

def image_pull_policy
  @image_pull_policy
end

#lifecycleObject

Returns the value of attribute lifecycle.



7
8
9
# File 'lib/metatron/templates/container.rb', line 7

def lifecycle
  @lifecycle
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/metatron/templates/container.rb', line 7

def name
  @name
end

#portsObject

Returns the value of attribute ports.



7
8
9
# File 'lib/metatron/templates/container.rb', line 7

def ports
  @ports
end

#probesObject

Returns the value of attribute probes.



7
8
9
# File 'lib/metatron/templates/container.rb', line 7

def probes
  @probes
end

#resourcesObject

Returns the value of attribute resources.



7
8
9
# File 'lib/metatron/templates/container.rb', line 7

def resources
  @resources
end

#security_contextObject Also known as: securityContext

Returns the value of attribute security_context.



7
8
9
# File 'lib/metatron/templates/container.rb', line 7

def security_context
  @security_context
end

#stdinObject

Returns the value of attribute stdin.



7
8
9
# File 'lib/metatron/templates/container.rb', line 7

def stdin
  @stdin
end

#termination_message_pathObject Also known as: terminationMessagePath

Returns the value of attribute termination_message_path.



7
8
9
# File 'lib/metatron/templates/container.rb', line 7

def termination_message_path
  @termination_message_path
end

#termination_message_policyObject Also known as: terminationMessagePolicy

Returns the value of attribute termination_message_policy.



7
8
9
# File 'lib/metatron/templates/container.rb', line 7

def termination_message_policy
  @termination_message_policy
end

#ttyObject

Returns the value of attribute tty.



7
8
9
# File 'lib/metatron/templates/container.rb', line 7

def tty
  @tty
end

#volume_mountsObject Also known as: volumeMounts

Returns the value of attribute volume_mounts.



7
8
9
# File 'lib/metatron/templates/container.rb', line 7

def volume_mounts
  @volume_mounts
end

Instance Method Details

#formatted_argsObject



58
59
60
61
62
# File 'lib/metatron/templates/container.rb', line 58

def formatted_args
  return {} unless args && !args.empty?

  { args: }
end

#formatted_envfromObject



92
93
94
95
96
97
98
# File 'lib/metatron/templates/container.rb', line 92

def formatted_envfrom
  if envfrom && !envfrom.empty?
    { envFrom: envfrom.map { |secret| { secretRef: { name: secret } } } }
  else
    {}
  end
end

#formatted_environmentObject

rubocop:disable Metrics/PerceivedComplexity



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/metatron/templates/container.rb', line 70

def formatted_environment # rubocop:disable Metrics/PerceivedComplexity
  return {} unless env && !env.empty?

  if env.is_a?(Hash)
    mapped_values = env.map do |key, value|
      v = { name: key }
      if value.is_a?(Hash)
        v.merge!(value)
      else
        v[:value] = value
      end
      v
    end

    { env: mapped_values }
  elsif env.is_a?(Array)
    { env: }
  else
    raise "Environment must be a Hash or Array"
  end
end

#formatted_lifecycleObject



64
65
66
67
68
# File 'lib/metatron/templates/container.rb', line 64

def formatted_lifecycle
  return {} unless lifecycle && !lifecycle.empty?

  { lifecycle: }
end

#formatted_portsObject



102
# File 'lib/metatron/templates/container.rb', line 102

def formatted_ports = ports&.any? ? { ports: } : {}

#formatted_resourcesObject



100
# File 'lib/metatron/templates/container.rb', line 100

def formatted_resources = resources&.any? ? { resources: } : {}

#formatted_security_contextObject



104
105
106
# File 'lib/metatron/templates/container.rb', line 104

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

#formatted_volume_mountsObject



108
# File 'lib/metatron/templates/container.rb', line 108

def formatted_volume_mounts = volume_mounts&.any? ? { volumeMounts: } : {}

#renderObject

rubocop:disable Metrics/AbcSize



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/metatron/templates/container.rb', line 36

def render # rubocop:disable Metrics/AbcSize
  {
    name:,
    command:,
    image:,
    imagePullPolicy:,
    stdin:,
    tty:,
    terminationMessagePath:,
    terminationMessagePolicy:
  }.merge(probes)
    .merge(formatted_resources)
    .merge(formatted_environment)
    .merge(formatted_envfrom)
    .merge(formatted_ports)
    .merge(formatted_volume_mounts)
    .merge(formatted_security_context)
    .merge(formatted_args)
    .merge(formatted_lifecycle)
    .compact
end