Class: Pfab::Templates::Web

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

Instance Method Summary collapse

Methods inherited from Base

#app_vars, #cpu, #deploy_id, #env_vars, #get, #get_namespace, #image_name, #initialize, #load_env_vars, #load_secrets, #memory, #resources

Constructor Details

This class inherits a constructor from Pfab::Templates::Base

Instance Method Details

#application_typeObject



148
149
150
# File 'lib/pfab/templates/web.rb', line 148

def application_type
  "web"
end

#default_probeObject



125
126
127
128
129
130
131
132
133
134
# File 'lib/pfab/templates/web.rb', line 125

def default_probe
  {
    httpGet: {
      path: get("health_check_path") || "/",
      port: get("port"),
    },
    initialDelaySeconds: 15,
    timeoutSeconds: 3
  }
end

#deploymentObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/pfab/templates/web.rb', line 152

def deployment
  secret_mounts = get("secretMounts") || []
  volume_mounts = []
  volumes = []
  secret_mounts.each do |secret_mount|
    volumes.append({
                     name: secret_mount['name'],
                     secret: { secretName: secret_mount['secretName'] }
                   })
    volume_mounts.append({
                           name: secret_mount['name'],
                           mountPath: secret_mount['path'],
                           readOnly: secret_mount['readOnly'] || true
                         })
  end

  if get("datadogVolumeMountEnabled")
    datadog_volume_name = "ddsocket"
    datadog_path = "/var/run/datadog"
    volumes.append({
                     name: datadog_volume_name,
                     hostPath: {
                      path: datadog_path
                     }
                   })
    volume_mounts.append(
      name: datadog_volume_name,
      mountPath: datadog_path,
      readOnly: true
    )
  end

  {
    kind: "Deployment",
    apiVersion: "apps/v1",
    metadata: {
      name: @data['deployed_name'],
      namespace: get_namespace,
      labels: {
        application: @data['application'],
        "deployed-name" => @data['deployed_name'],
        "application-type" => application_type,
        "deploy-id" => deploy_id,
        "tags.datadoghq.com/env": @data['env'],
        "tags.datadoghq.com/service": @data['deployed_name'],
        "tags.datadoghq.com/version":"#{@data['sha']}"
      }
    },
    spec: {
      replicas: get("replicas") || 1,
      selector: {
        matchLabels: {
          "deployed-name" => @data['deployed_name'],
        },
      },
      strategy: {
        type: "RollingUpdate",
        rollingUpdate: {
          maxSurge: 1,
          maxUnavailable: 0,
        }
      },
      revisionHistoryLimit: 5,
      progressDeadlineSeconds: 120,
      template: {
        metadata: {
          labels: {
            application: @data['application'],
            "deployed-name" => @data['deployed_name'],
            "application-type" => "web",
            "tags.datadoghq.com/env": @data['env'],
            "tags.datadoghq.com/service": @data['deployed_name'],
            "tags.datadoghq.com/version": "#{@data['sha']}"
          },
        },
        spec: {
          serviceAccountName: get('serviceAccountName'),
          containers: [
            {
              image: image_name,
              name: @data['deployed_name'],
              command: get("command").split(" "),
              env: env_vars,
              resources: resources,
              ports: [
                {
                  name: "main",
                  containerPort: app_vars["port"]
                },
                {
                  name: "health-port",
                  containerPort: 8085 # the default micronaut endpoint port
                }
              ],
              livenessProbe: livenessProbe,
              readinessProbe: readinessProbe,
              startupProbe: startupProbe,
              volumeMounts: volume_mounts
            }
          ],
          volumes: volumes
        }.compact,
      },
    }.compact,
  }
end

#hostsObject



82
83
84
# File 'lib/pfab/templates/web.rb', line 82

def hosts
  get("host").split(",")
end

#ingressObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pfab/templates/web.rb', line 53

def ingress
  {
    apiVersion: "networking.k8s.io/v1",
    kind: "Ingress",
    metadata: {
      name: "ingress-#{@data['deployed_name']}",
      namespace: get_namespace,
      labels: {
        application: @data['application'],
        "deployed-name" => @data['deployed_name'],
      },
      annotations: ingress_annotations,
    },
    spec: {
      rules: rules,
      tls: tls_hosts
    },
  }
end

#ingress_annotationsObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/pfab/templates/web.rb', line 110

def ingress_annotations
  h = {
    "kubernetes.io/ingress.class" => "traefik",
    "traefik.frontend.passHostHeader" => "false",
    "traefik.frontend.priority" => "1",
    "traefik.frontend.entryPoints" => "https",
    "traefik.protocol" => get("protocol") || "http",
    "traefik.frontend.headers.SSLRedirect" => "true",
    "traefik.docker.network" => "traefik",
    "traefik.ingress.kubernetes.io/router.entrypoints" => "websecure",
    "traefik.ingress.kubernetes.io/router.tls" => "true"
  }
  h
end

#livenessProbeObject



136
137
138
# File 'lib/pfab/templates/web.rb', line 136

def livenessProbe
  get("livenessProbe") || default_probe
end

#readinessProbeObject



140
141
142
# File 'lib/pfab/templates/web.rb', line 140

def readinessProbe
  get("readinessProbe") || default_probe
end

#rulesObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/pfab/templates/web.rb', line 86

def rules
  hosts.map do |host|
    {
      host: host,
      http: {
        paths: [
          {
            path: "/",
            pathType: "Prefix",
            backend: {
              service: {
                name: @data['deployed_name'],
                port: {
                  name: "http"
                }
              }
            },
          },
        ],
      },
    }
  end
end

#serviceObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pfab/templates/web.rb', line 18

def service
  {
    apiVersion: "v1",
    kind: "Service",
    metadata: {
      name: @data['deployed_name'],
      namespace: get_namespace,
      labels: {
        application: @data['application'],
        "deployed-name" => @data['deployed_name'],
      },
      annotations: service_annotations,
    },
    spec: {
      selector: {
        "deployed-name" => @data['deployed_name'],
      },
      ports: [
        {
          name: "http",
          port: 80,
          targetPort: app_vars["port"],
          appProtocol: app_vars["appProtocol"]
        }.compact
      ]
    }
  }
end

#service_annotationsObject



47
48
49
50
51
# File 'lib/pfab/templates/web.rb', line 47

def service_annotations
  h = {}
  h["traefik.ingress.kubernetes.io/service.serversscheme"] = "h2c" if get("protocol") == "h2c"
  h
end

#startupProbeObject



144
145
146
# File 'lib/pfab/templates/web.rb', line 144

def startupProbe
  get("startupProbe") || default_probe
end

#tls_hostsObject



73
74
75
76
77
78
79
80
# File 'lib/pfab/templates/web.rb', line 73

def tls_hosts
  hosts.map do |host|
    {
      hosts: [host],
      secretName: get("tls_cert_secret")
    }
  end
end

#write_to(f) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/pfab/templates/web.rb', line 4

def write_to(f)
  if get("host").nil?
    puts "No host to deploy to for #{@data['deployed_name']}. Skipping."
  else
    f << YAML.dump(service.deep_stringify_keys)
    if not app_vars.has_key?('generateIngressEnabled') || app_vars['generateIngressEnabled']
      f << YAML.dump(ingress.deep_stringify_keys)
    else
      puts "skipping ingress because ingress_disabled = #{@data['generateIngressEnabled']}"
    end
    f << YAML.dump(deployment.deep_stringify_keys)
  end
end