Class: Pfab::Templates::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/pfab/templates/base.rb

Direct Known Subclasses

Cron, Daemon, Job, Web

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Base

Returns a new instance of Base.



4
5
6
# File 'lib/pfab/templates/base.rb', line 4

def initialize(data)
  @data = data
end

Instance Method Details

#app_varsObject



12
13
14
# File 'lib/pfab/templates/base.rb', line 12

def app_vars
  @data["application_yaml"]["deployables"][@data["props"][:deployable]]
end

#application_typeObject

overridden in subtypes



54
55
56
# File 'lib/pfab/templates/base.rb', line 54

def application_type
  "base"
end

#cpu(req_type) ⇒ Object



28
29
30
31
32
# File 'lib/pfab/templates/base.rb', line 28

def cpu(req_type)
  default_cpu_string = @data["config"]["default_cpu_string"] || "50m/250m"
  (request, limit) = (get("cpu") || default_cpu_string).split("/")
  req_type == :limit ? limit : request
end

#deploy_idObject



58
59
60
# File 'lib/pfab/templates/base.rb', line 58

def deploy_id
  "#{@data['application']}.#{application_type}.#{@data['deployed_name']}"
end

#env_varsObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pfab/templates/base.rb', line 62

def env_vars
  env_vars = { "DEPLOYED_NAME" => { value: @data['deployed_name'] },
               "DEPLOY_ID" => { value: deploy_id },
               "POD_ID" => { valueFrom: { fieldRef: { fieldPath: 'metadata.name' } } },
               "SPEC_NODENAME" => { valueFrom: { fieldRef: { fieldPath: 'spec.nodeName' } } },
               "DD_ENV" => { valueFrom: { fieldRef: { fieldPath: "metadata.labels['tags.datadoghq.com/env']" } } },
               "DD_SERVICE" => { valueFrom: { fieldRef: { fieldPath: "metadata.labels['tags.datadoghq.com/service']" } } },
               "DD_VERSION" => { valueFrom: { fieldRef: { fieldPath: "metadata.labels['tags.datadoghq.com/version']" } } }
  }

  # load defaults
  load_env_vars(env_vars, @data.dig("application_yaml", :environment))
  load_secrets(env_vars, @data.dig("application_yaml", :env_secrets))

  # load env overrides
  load_env_vars(env_vars, @data.dig("application_yaml", @data["env"], :environment))
  load_secrets(env_vars, @data.dig("application_yaml", @data["env"], :env_secrets))

  env_vars.map do |k, v|
    { name: k }.merge(v)
  end
end

#get(key) ⇒ Object



16
17
18
# File 'lib/pfab/templates/base.rb', line 16

def get(key)
  app_vars.dig(@data["env"], key) || app_vars[key]
end

#get_namespaceObject



24
25
26
# File 'lib/pfab/templates/base.rb', line 24

def get_namespace()
  get("namespace") || get_top_level("namespace") || @data['env']
end

#get_top_level(key) ⇒ Object



20
21
22
# File 'lib/pfab/templates/base.rb', line 20

def get_top_level(key)
  @data["application_yaml"].dig(@data["env"], key) || @data["application_yaml"][key]
end

#image_nameObject



8
9
10
# File 'lib/pfab/templates/base.rb', line 8

def image_name
  "#{@data['container_repository']}/#{@data['image_name']}:#{@data['sha']}"
end

#load_env_vars(env_vars, hash) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/pfab/templates/base.rb', line 85

def load_env_vars(env_vars, hash)
  (hash || {}).each do |env_var_name, v|
    if v.to_s.start_with? "field/"
      (_, field_name) = v.split("/")
      env_vars[env_var_name] = { valueFrom: {
        fieldRef: { fieldPath: field_name }
      } }
    elsif v.to_s.start_with? "configmap/"
       (_, configmap_name, key_name) = v.split("/")
       env_vars[env_var_name] = { valueFrom: {
         configMapKeyRef: { name: configmap_name, key: key_name }
       } }
    else
      env_vars[env_var_name] = { value: v }
    end
  end
end

#load_secrets(env_vars, hash) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/pfab/templates/base.rb', line 103

def load_secrets(env_vars, hash)
  (hash || {}).each do |env_var_name, v|
    (ref, key) = v.split("/")
    env_vars[env_var_name] = {
      valueFrom: {
        secretKeyRef: {
          name: ref,
          key: key
        }
      } }
  end
end

#memory(req_type) ⇒ Object



34
35
36
37
38
# File 'lib/pfab/templates/base.rb', line 34

def memory(req_type)
  default_memory_string = @data["config"]["default_memory_string"] || "256Mi/500Mi"
  (request, limit) = (get("memory") || default_memory_string).split("/")
  req_type == :limit ? limit : request
end

#resourcesObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pfab/templates/base.rb', line 40

def resources
  {
    requests: {
      cpu: cpu(:request),
      memory: memory(:request),
    },
    limits: {
      cpu: cpu(:limit),
      memory: memory(:limit),
    }
  }
end