Improve Ingress generation from #29

Add TLS to ingress as an optional feature and also be able to provide
own certificates.
This commit is contained in:
Carlos de Paula 2020-03-02 16:46:38 -03:00
parent 11b4b0f11b
commit bed566c8c5
8 changed files with 76 additions and 9 deletions

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
vendor vendor
auth auth
server.crt
server.key

View File

@ -33,6 +33,8 @@ The additional modules are:
There are also options to set the ingress domain suffix and enable persistence for Grafana and Prometheus. There are also options to set the ingress domain suffix and enable persistence for Grafana and Prometheus.
The ingresses can use TLS with the default self-signed certificate from your Ingress controller by setting `TLSingress` to `true` and use a custom certificate by creating the files `server.crt` and `server.key` and enabling the `UseProvidedCerts` parameter at `vars.jsonnet`.
After changing these parameters, rebuild the manifests with `make`. After changing these parameters, rebuild the manifests with `make`.
## Quickstart ## Quickstart

View File

@ -123,13 +123,34 @@ local utils = import 'utils.libsonnet';
// Create ingress objects per application // Create ingress objects per application
ingress+:: { ingress+:: {
alertmanager: alertmanager:
utils.newIngress('alertmanager-main', $._config.namespace, $._config.urls.alert_ingress, '/', 'alertmanager-main', 'web'), local I = utils.newIngress('alertmanager-main', $._config.namespace, $._config.urls.alert_ingress, '/', 'alertmanager-main', 'web');
if vars.TLSingress then
if vars.UseProvidedCerts then
utils.addIngressTLS(I, 'ingress-TLS-secret')
else
utils.addIngressTLS(I)
else
I,
grafana: grafana:
utils.newIngress('grafana', $._config.namespace, $._config.urls.grafana_ingress, '/', 'grafana', 'http'), local I = utils.newIngress('grafana', $._config.namespace, $._config.urls.grafana_ingress, '/', 'grafana', 'http');
if vars.TLSingress then
if vars.UseProvidedCerts then
utils.addIngressTLS(I, 'ingress-TLS-secret')
else
utils.addIngressTLS(I)
else
I,
prometheus: prometheus:
utils.newIngress('prometheus-k8s', $._config.namespace, $._config.urls.prom_ingress, '/', 'prometheus-k8s', 'web'), local I = utils.newIngress('prometheus-k8s', $._config.namespace, $._config.urls.prom_ingress, '/', 'prometheus-k8s', 'web');
if vars.TLSingress then
if vars.UseProvidedCerts then
utils.addIngressTLS(I, 'ingress-TLS-secret')
else
utils.addIngressTLS(I)
else
I,
// // Example external ingress with authentication // // Example external ingress with authentication
// 'grafana-external': // 'grafana-external':
@ -155,5 +176,8 @@ local utils = import 'utils.libsonnet';
// // First generate the auth secret with gen_auth.sh script // // First generate the auth secret with gen_auth.sh script
// secret.new('basic-auth', { auth: std.base64(importstr 'auth') }) + // secret.new('basic-auth', { auth: std.base64(importstr 'auth') }) +
// secret.mixin.metadata.withNamespace($._config.namespace), // secret.mixin.metadata.withNamespace($._config.namespace),
}, } + if vars.UseProvidedCerts then {
secret:
utils.newTLSSecret('ingress-TLS-secret', $._config.namespace, vars.TLSCertificate, vars.TLSKey)
} else {},
} }

View File

@ -12,3 +12,6 @@ spec:
serviceName: alertmanager-main serviceName: alertmanager-main
servicePort: web servicePort: web
path: / path: /
tls:
- hosts:
- alertmanager.192.168.99.100.nip.io

View File

@ -12,3 +12,6 @@ spec:
serviceName: grafana serviceName: grafana
servicePort: http servicePort: http
path: / path: /
tls:
- hosts:
- grafana.192.168.99.100.nip.io

View File

@ -12,3 +12,6 @@ spec:
serviceName: prometheus-k8s serviceName: prometheus-k8s
servicePort: web servicePort: web
path: / path: /
tls:
- hosts:
- prometheus.192.168.99.100.nip.io

View File

@ -91,7 +91,6 @@ local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
// Creates ingress objects // Creates ingress objects
newIngress(name, namespace, host, path, serviceName, servicePort):: ( newIngress(name, namespace, host, path, serviceName, servicePort):: (
local secret = k.core.v1.secret;
local ingress = k.extensions.v1beta1.ingress; local ingress = k.extensions.v1beta1.ingress;
local ingressTls = ingress.mixin.spec.tlsType; local ingressTls = ingress.mixin.spec.tlsType;
local ingressRule = ingress.mixin.spec.rulesType; local ingressRule = ingress.mixin.spec.rulesType;
@ -100,10 +99,6 @@ local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
ingress.new() ingress.new()
+ ingress.mixin.metadata.withName(name) + ingress.mixin.metadata.withName(name)
+ ingress.mixin.metadata.withNamespace(namespace) + ingress.mixin.metadata.withNamespace(namespace)
+ ingress.mixin.spec.withTls(
ingressTls.new()
+ ingressTls.withHosts(host)
)
+ ingress.mixin.spec.withRules( + ingress.mixin.spec.withRules(
ingressRule.new() ingressRule.new()
+ ingressRule.withHost(host) + ingressRule.withHost(host)
@ -116,6 +111,34 @@ local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
) )
), ),
// Add TLS to Ingress resource with secret containing the certificates if exists
addIngressTLS(I, S=''):: (
local ingress = k.extensions.v1beta1.ingress;
local ingressTls = ingress.mixin.spec.tlsType;
local host = I.spec.rules[0].host;
local namespace = I.metadata.namespace;
I + ingress.mixin.spec.withTls(
ingressTls.new() +
ingressTls.withHosts(host) +
(if S != '' then {'secretName': S} else {})
)
),
// Creates a new TLS Secred with Certificate and Key
newTLSSecret(name, namespace, crt, key):: (
local secret = k.core.v1.secret;
secret.new('ingress-secret') +
secret.mixin.metadata.withNamespace(namespace) +
secret.withType('kubernetes.io/tls') +
secret.withData(
{
'tls.crt': std.base64(crt),
'tls.key': std.base64(key),
})
),
// Creates new basic deployments // Creates new basic deployments
newDeployment(name, namespace, image, cmd, port):: ( newDeployment(name, namespace, image, cmd, port):: (
local deployment = k.apps.v1.deployment; local deployment = k.apps.v1.deployment;

View File

@ -38,6 +38,13 @@
// Domain suffix for the ingresses // Domain suffix for the ingresses
suffixDomain: '192.168.99.100.nip.io', suffixDomain: '192.168.99.100.nip.io',
// If TLSingress is true, a self-signed HTTPS ingress with redirect will be created
TLSingress: true,
# If UseProvidedCerts is true, provided files will be used on created HTTPS ingresses.
# Use a wildcard certificate for the domain like ex. "*.192.168.99.100.nip.io"
UseProvidedCerts: false,
TLSCertificate: importstr 'server.crt',
TLSKey: importstr 'server.key',
// Setting these to false, defaults to emptyDirs // Setting these to false, defaults to emptyDirs
enablePersistence: { enablePersistence: {