Skip to content

Commit

Permalink
Nginx directory separate html (#481)
Browse files Browse the repository at this point in the history
* Separate out html from config

* Move snippets to files
  • Loading branch information
malcolmholmes authored Mar 1, 2021
1 parent 815de90 commit c3e89e7
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 46 deletions.
47 changes: 1 addition & 46 deletions nginx-directory/configfile.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -53,55 +53,10 @@ local kausal = import 'ksonnet-util/kausal.libsonnet';
for service in std.set($._config.admin_services, function(s) s.url)
],
locations: std.join('\n', self.location_stanzas),
link_stanzas: [
|||
<li><a href="/%(path)s%(params)s">%(title)s</a></li>
||| % ({ params: '' } + service)
for service in $._config.admin_services
],
links: std.join('\n', self.link_stanzas),
};
configMap.new('nginx-config') +
configMap.withData({
'nginx.conf': |||
worker_processes 5; ## Default: 1
error_log /dev/stderr;
pid /tmp/nginx.pid;
worker_rlimit_nofile 8192;
events {
worker_connections 4096; ## Default: 1024
}
http {
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /dev/stderr main;
sendfile on;
tcp_nopush on;
resolver kube-dns.kube-system.svc.%(cluster_dns_suffix)s;
server {
listen 80;
%(locations)s
location ~ /(index.html)? {
root /etc/nginx;
}
}
}
||| % ($._config + vars),
'index.html': |||
<html>
<head><title>Admin</title></head>
<body>
<h1>Admin</h1>
<ul>
%(links)s
</ul>
</body>
</html>
||| % vars,
'nginx.conf': (importstr 'files/nginx.conf') % ($._config + vars),
}),
}
1 change: 1 addition & 0 deletions nginx-directory/deployment.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ local kausal = import 'ksonnet-util/kausal.libsonnet';
nginx_deployment:
deployment.new('nginx', 1, [$.nginx_container]) +
k.util.configMapVolumeMount($.nginx_config_map, '/etc/nginx') +
k.util.configMapVolumeMount($.nginx_html_config_map, '/var/www/html') +
k.util.podPriority('critical'),

nginx_service:
Expand Down
1 change: 1 addition & 0 deletions nginx-directory/directory.libsonnet
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
(import 'config.libsonnet')
+ (import 'configfile.libsonnet')
+ (import 'html.libsonnet')
+ (import 'deployment.libsonnet')
9 changes: 9 additions & 0 deletions nginx-directory/files/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head><title>Admin</title></head>
<body>
<h1>Admin</h1>
<ul>
%(links)s
</ul>
</body>
</html>
1 change: 1 addition & 0 deletions nginx-directory/files/link.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<li><a href="/%(path)s%(params)s">%(title)s</a></li>
26 changes: 26 additions & 0 deletions nginx-directory/files/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
worker_processes 5; ## Default: 1
error_log /dev/stderr;
pid /tmp/nginx.pid;
worker_rlimit_nofile 8192;

events {
worker_connections 4096; ## Default: 1024
}

http {
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /dev/stderr main;
sendfile on;
tcp_nopush on;
resolver kube-dns.kube-system.svc.%(cluster_dns_suffix)s;
server {
listen 80;
%(locations)s
location ~ /(index.html)? {
root /var/www/html;
}
}
}
18 changes: 18 additions & 0 deletions nginx-directory/html.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
local k = import 'k.libsonnet';
local configMap = k.core.v1.configMap;
{

nginx_html_config_map:
local vars = {
link_stanzas: [
(importstr 'files/link.html') % ({ params: '' } + service)
for service in $._config.admin_services
],
links: std.join('\n', self.link_stanzas),
};

configMap.new('nginx-config-html') +
configMap.withData({
'index.html': (importstr 'files/index.html') % vars,
}),
}

0 comments on commit c3e89e7

Please sign in to comment.