|
| 1 | +local kube = import '../vendor/kube-prod-runtime/lib/kube.libsonnet'; |
| 2 | +local utils = import '../vendor/kube-prod-runtime/lib/utils.libsonnet'; |
| 3 | + |
| 4 | +local IMAGE = 'nginx:1.15.12'; |
| 5 | +local WWW_VOLUME_PATH = '/usr/share/nginx/html'; |
| 6 | +local CONFIG_TOP_LEVEL_PATH = '/etc/nginx/nginx.conf'; |
| 7 | +local CONFIG_DEFAULT_PATH = '/etc/nginx/conf.d'; |
| 8 | +local HTTP_PORT = 80; |
| 9 | + |
| 10 | +{ |
| 11 | + p:: '', |
| 12 | + |
| 13 | + app:: 'landingpage', |
| 14 | + |
| 15 | + index:: false, |
| 16 | + |
| 17 | + name:: $.p + $.app, |
| 18 | + |
| 19 | + domain:: 'example.net', |
| 20 | + |
| 21 | + sslRedirectDomains:: ['redirect1.example.net', 'redirect2.example.net'], |
| 22 | + |
| 23 | + namespace:: 'auth', |
| 24 | + |
| 25 | + labels:: { |
| 26 | + metadata+: { |
| 27 | + labels+: { |
| 28 | + app: $.app, |
| 29 | + }, |
| 30 | + }, |
| 31 | + }, |
| 32 | + |
| 33 | + metadata:: $.labels { |
| 34 | + metadata+: { |
| 35 | + namespace: $.namespace, |
| 36 | + }, |
| 37 | + }, |
| 38 | + |
| 39 | + redirectStatement:: if std.length($.sslRedirectDomains) > 0 then |
| 40 | + 'server {\n' + |
| 41 | + ' listen 80;\n' + |
| 42 | + ' server_name ' + std.join(' ', $.sslRedirectDomains) + ';\n' + |
| 43 | + ' return 301 https://$host$request_uri;\n' + |
| 44 | + '}\n' |
| 45 | + else |
| 46 | + '', |
| 47 | + |
| 48 | + configMap: kube.ConfigMap($.name) + $.metadata { |
| 49 | + data+: { |
| 50 | + 'nginx.conf': ||| |
| 51 | + user nginx; |
| 52 | + worker_processes 1; |
| 53 | +
|
| 54 | + error_log /var/log/nginx/error.log warn; |
| 55 | + pid /var/run/nginx.pid; |
| 56 | +
|
| 57 | +
|
| 58 | + events { |
| 59 | + worker_connections 1024; |
| 60 | + } |
| 61 | +
|
| 62 | +
|
| 63 | + http { |
| 64 | + include /etc/nginx/mime.types; |
| 65 | + default_type application/octet-stream; |
| 66 | +
|
| 67 | + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' |
| 68 | + '$status $body_bytes_sent "$http_referer" ' |
| 69 | + '"$http_user_agent" "$http_x_forwarded_for"'; |
| 70 | +
|
| 71 | + access_log /var/log/nginx/access.log main; |
| 72 | +
|
| 73 | + sendfile on; |
| 74 | +
|
| 75 | + keepalive_timeout 65; |
| 76 | +
|
| 77 | + server_names_hash_bucket_size 256; |
| 78 | +
|
| 79 | + include /etc/nginx/conf.d/*.conf; |
| 80 | + } |
| 81 | + |||, |
| 82 | + 'default.conf': ||| |
| 83 | + server { |
| 84 | + listen 80; |
| 85 | + server_name _; |
| 86 | +
|
| 87 | + location / { |
| 88 | + root /usr/share/nginx/html; |
| 89 | + index index.html index.htm; |
| 90 | + } |
| 91 | +
|
| 92 | + error_page 500 502 503 504 /50x.html; |
| 93 | + location = /50x.html { |
| 94 | + root /usr/share/nginx/html; |
| 95 | + } |
| 96 | + if ($http_x_forwarded_proto = "http") { |
| 97 | + return 301 https://$host$request_uri; |
| 98 | + } |
| 99 | + } |
| 100 | + ||| + $.redirectStatement, |
| 101 | + }, |
| 102 | + }, |
| 103 | + |
| 104 | + Link(cloud, link, text):: std.manifestXmlJsonml([ |
| 105 | + 'div', |
| 106 | + { class: 'col s12 m4' }, |
| 107 | + '\n ', |
| 108 | + [ |
| 109 | + 'div', |
| 110 | + {}, |
| 111 | + [ |
| 112 | + 'img', |
| 113 | + { |
| 114 | + width: 56, |
| 115 | + height: 56, |
| 116 | + alt: cloud, |
| 117 | + src: cloud + '.svg', |
| 118 | + }, |
| 119 | + ], |
| 120 | + ], |
| 121 | + [ |
| 122 | + 'div', |
| 123 | + {}, |
| 124 | + [ |
| 125 | + 'a', |
| 126 | + { |
| 127 | + class: 'btn-large waves-effect waves-light blue', |
| 128 | + href: link, |
| 129 | + }, |
| 130 | + text, |
| 131 | + ], |
| 132 | + ], |
| 133 | + '\n', |
| 134 | + ]), |
| 135 | + |
| 136 | + content:: '', |
| 137 | + |
| 138 | + wwwConfigMap: if $.index then kube.ConfigMap($.name + '-www') + $.metadata { |
| 139 | + data+: { |
| 140 | + 'index.html': std.strReplace( |
| 141 | + (importstr './landingpage/index.html'), |
| 142 | + '#CONTENT#', |
| 143 | + $.content, |
| 144 | + ), |
| 145 | + 'amazon.svg': importstr './landingpage/amazon.svg', |
| 146 | + 'google.svg': importstr './landingpage/google.svg', |
| 147 | + 'digitalocean.svg': importstr './landingpage/digitalocean.svg', |
| 148 | + }, |
| 149 | + }, |
| 150 | + |
| 151 | + deployment: kube.Deployment($.name) + $.metadata { |
| 152 | + local this = self, |
| 153 | + spec+: { |
| 154 | + replicas: 1, |
| 155 | + template+: { |
| 156 | + metadata+: { |
| 157 | + annotations+: { |
| 158 | + 'config/hash': std.md5(std.escapeStringJson($.configMap)), |
| 159 | + } + if $.index then { 'www/hash': std.md5(std.escapeStringJson($.wwwConfigMap)) } else {}, |
| 160 | + }, |
| 161 | + spec+: { |
| 162 | + affinity: kube.PodZoneAntiAffinityAnnotation(this.spec.template), |
| 163 | + default_container: $.app, |
| 164 | + volumes_+: { |
| 165 | + config: kube.ConfigMapVolume($.configMap), |
| 166 | + } + if $.index then { www: kube.ConfigMapVolume($.wwwConfigMap) } else {}, |
| 167 | + containers_+: { |
| 168 | + landingpage: kube.Container($.app) { |
| 169 | + local container = self, |
| 170 | + image: IMAGE, |
| 171 | + args: [ |
| 172 | + 'nginx', |
| 173 | + '-g', |
| 174 | + 'daemon off;', |
| 175 | + '-c', |
| 176 | + CONFIG_TOP_LEVEL_PATH, |
| 177 | + ], |
| 178 | + resources: { |
| 179 | + requests: { cpu: '10m', memory: '64Mi' }, |
| 180 | + }, |
| 181 | + ports_+: { |
| 182 | + http: { containerPort: HTTP_PORT }, |
| 183 | + }, |
| 184 | + volumeMounts_+: { |
| 185 | + config_top_level: { name: 'config', mountPath: CONFIG_TOP_LEVEL_PATH, subPath: 'nginx.conf' }, |
| 186 | + config_default: { name: 'config', mountPath: CONFIG_DEFAULT_PATH + '/default.conf', subPath: 'default.conf' }, |
| 187 | + } + if $.index then { www: { mountPath: WWW_VOLUME_PATH } } else {}, |
| 188 | + readinessProbe: { |
| 189 | + httpGet: { path: '/', port: HTTP_PORT }, |
| 190 | + }, |
| 191 | + livenessProbe: self.readinessProbe { |
| 192 | + // elasticsearch_logging_discovery has a 5min timeout on cluster bootstrap |
| 193 | + initialDelaySeconds: 2 * 60, |
| 194 | + failureThreshold: 4, |
| 195 | + }, |
| 196 | + }, |
| 197 | + }, |
| 198 | + }, |
| 199 | + }, |
| 200 | + }, |
| 201 | + }, |
| 202 | + |
| 203 | + |
| 204 | + ingress: if std.length($.sslRedirectDomains) > 0 || $.index then kube.Ingress($.name) + $.metadata { |
| 205 | + local hosts = $.sslRedirectDomains + if $.index then [$.domain] else [], |
| 206 | + metadata+: { |
| 207 | + annotations+: { |
| 208 | + 'kubernetes.io/ingress.class': 'contour', |
| 209 | + }, |
| 210 | + }, |
| 211 | + spec+: { |
| 212 | + rules+: std.map( |
| 213 | + (function(h) { |
| 214 | + host: h, |
| 215 | + http: { |
| 216 | + paths: [ |
| 217 | + { path: '/', backend: $.svc.name_port }, |
| 218 | + ], |
| 219 | + }, |
| 220 | + }), hosts |
| 221 | + ), |
| 222 | + } + if $.index && $.certificate != null then { |
| 223 | + tls: [{ |
| 224 | + hosts: [$.domain], |
| 225 | + secretName: $.certificate.spec.secretName, |
| 226 | + }], |
| 227 | + } else {}, |
| 228 | + }, |
| 229 | + |
| 230 | + |
| 231 | + svc: kube.Service($.name) + $.metadata { |
| 232 | + target_pod: $.deployment.spec.template, |
| 233 | + spec+: { |
| 234 | + sessionAffinity: 'None', |
| 235 | + }, |
| 236 | + }, |
| 237 | +} |
0 commit comments