Class: Pfab::Templates::Base
- Inherits:
-
Object
- Object
- Pfab::Templates::Base
- Defined in:
- lib/pfab/templates/base.rb
Instance Method Summary collapse
- #app_vars ⇒ Object
-
#application_type ⇒ Object
overridden in subtypes.
- #container_ports ⇒ Object
- #cpu(req_type) ⇒ Object
- #deploy_id ⇒ Object
- #deploy_unique_id ⇒ Object
- #env_from ⇒ Object
- #env_vars ⇒ Object
- #get(key) ⇒ Object
- #get_command ⇒ Object
- #get_namespace ⇒ Object
- #image_name ⇒ Object
-
#initialize(data) ⇒ Base
constructor
A new instance of Base.
- #load_env_vars(env_vars, hash) ⇒ Object
- #load_secrets(env_vars, hash) ⇒ Object
- #memory(req_type) ⇒ Object
- #resources ⇒ Object
Constructor Details
#initialize(data) ⇒ Base
Returns a new instance of Base.
6 7 8 |
# File 'lib/pfab/templates/base.rb', line 6 def initialize(data) @data = data end |
Instance Method Details
#app_vars ⇒ Object
14 15 16 |
# File 'lib/pfab/templates/base.rb', line 14 def app_vars @data["application_yaml"]["deployables"][@data["props"][:deployable]] end |
#application_type ⇒ Object
overridden in subtypes
60 61 62 |
# File 'lib/pfab/templates/base.rb', line 60 def application_type "base" end |
#container_ports ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/pfab/templates/base.rb', line 137 def container_ports ports = [] if app_vars["port"] ports.append ({ name: "main", containerPort: app_vars["port"] }) end %w[additionalPorts containerPorts].each do |key| if get(key) get(key).each do |name, number| ports.append( {name: name, containerPort: number} ) end end end return ports end |
#cpu(req_type) ⇒ Object
34 35 36 37 38 |
# File 'lib/pfab/templates/base.rb', line 34 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_id ⇒ Object
64 65 66 |
# File 'lib/pfab/templates/base.rb', line 64 def deploy_id @data['deployed_name'] end |
#deploy_unique_id ⇒ Object
68 69 70 |
# File 'lib/pfab/templates/base.rb', line 68 def deploy_unique_id @data['sha'] + "-" + @data['application_yaml_hash'][0,8] end |
#env_from ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/pfab/templates/base.rb', line 72 def env_from env_from = [] env_from << @data.dig("application_yaml", :env_from) env_from << @data.dig("application_yaml", @data["env"], :env_from) env_from.flatten! env_from.compact! env_from.empty? ? nil : env_from end |
#env_vars ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/pfab/templates/base.rb', line 81 def env_vars env_vars = { "DEPLOYED_NAME" => { value: @data['deployed_name'] }, "DEPLOY_ID" => { value: deploy_id }, # currently this is the same as deployed_name "DEPLOY_FAMILY" => { value: @data['application'] }, "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
18 19 20 |
# File 'lib/pfab/templates/base.rb', line 18 def get(key) app_vars.dig(@data["env"], key) || app_vars[key] end |
#get_command ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/pfab/templates/base.rb', line 26 def get_command() cmd = get("command") if cmd.kind_of?(Array) return cmd end return cmd.split(" ") end |
#get_namespace ⇒ Object
22 23 24 |
# File 'lib/pfab/templates/base.rb', line 22 def get_namespace @data['namespace'] end |
#image_name ⇒ Object
10 11 12 |
# File 'lib/pfab/templates/base.rb', line 10 def image_name "#{@data['container_repository']}/#{@data['image_name']}:#{@data['sha']}" end |
#load_env_vars(env_vars, hash) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/pfab/templates/base.rb', line 105 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
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/pfab/templates/base.rb', line 123 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
40 41 42 43 44 |
# File 'lib/pfab/templates/base.rb', line 40 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 |
#resources ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/pfab/templates/base.rb', line 46 def resources { requests: { cpu: cpu(:request), memory: memory(:request), }, limits: { cpu: cpu(:limit), memory: memory(:limit), } } end |