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, #env_vars, #get, #image_name, #initialize, #load_env_vars, #load_secrets, #memory, #resources

Constructor Details

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

Instance Method Details

#default_probeObject



85
86
87
88
89
90
91
92
93
# File 'lib/pfab/templates/web.rb', line 85

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

#deploymentObject



103
104
105
106
107
108
109
110
111
112
113
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
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/pfab/templates/web.rb', line 103

def deployment
  {
    kind: "Deployment",
    apiVersion: "extensions/v1beta1",
    metadata: {
      name: @data['deployed_name'],
      namespace: @data['env'],
      labels: {
        application: @data['application'],
        "deployed-name" => @data['deployed_name'],
        "application-type" => "web",
      }
    },
    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",
          },
        },
        spec: {
          containers: [
            {
              image: image_name,
              name: @data['deployed_name'],
              command: get("command").split(" "),
              env: env_vars,
              resources: resources,
              livenessProbe: livenessProbe,
              readinessProbe: readinessProbe,
            }
          ]
        },
      },
    },
  }
end

#ingressObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pfab/templates/web.rb', line 37

def ingress
  {
    apiVersion: "extensions/v1beta1",
    kind: "Ingress",
    metadata: {
      name: "ingress-#{@data['deployed_name']}",
      namespace: @data['env'],
      labels: {
        application: @data['application'],
        "deployed-name" => @data['deployed_name'],
      },
      annotations: ingress_annotations,
    },
    spec: {
      rules: [
        {
          host: get("host"),
          http: {
            paths: [
              {
                path: "/",
                backend: {
                  serviceName: @data['deployed_name'],
                  servicePort: "http",
                },
              },
            ],
          },
        },
      ],
    },
  }
end

#ingress_annotationsObject



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

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",
  }
  h["ingress.kubernetes.io/protocol"] = "h2c" if get("protocol") == "h2c"
  h
end

#livenessProbeObject



95
96
97
# File 'lib/pfab/templates/web.rb', line 95

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

#readinessProbeObject



99
100
101
# File 'lib/pfab/templates/web.rb', line 99

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

#serviceObject



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
# File 'lib/pfab/templates/web.rb', line 10

def service
  {
    apiVersion: "v1",
    kind: "Service",
    metadata: {
      name: @data['deployed_name'],
      namespace: @data['env'],
      labels: {
        application: @data['application'],
        "deployed-name" => @data['deployed_name'],
      }
    },
    spec: {
      selector: {
        "deployed-name" => @data['deployed_name'],
      },
      ports: [
        {
          name: "http",
          port: 80,
          targetPort: get("port"),
        }
      ]
    }
  }
end

#write_to(f) ⇒ Object



4
5
6
7
8
# File 'lib/pfab/templates/web.rb', line 4

def write_to(f)
  f << YAML.dump(service.deep_stringify_keys)
  f << YAML.dump(ingress.deep_stringify_keys)
  f << YAML.dump(deployment.deep_stringify_keys)
end