/** * @module "@blitzm/kubernetes-nginx-ingress" * @packageDocumentation * @example * ```typescript * import { KubernetesNginxIngress } from '@blitzm/kubernetes-nginx-ingress'; * * new KubernetesNginxIngress('my-thing', { * // ... * }); * ``` */ import * as pulumi from '@pulumi/pulumi'; import * as k8s from '@pulumi/kubernetes'; import * as path from 'path'; import { merge } from 'lodash'; export interface KubernetesNginxIngressInputs { provider?: k8s.Provider; namespace?: pulumi.Input; /** * the helm chart version */ version?: string; /** * mode configures nginx as either a kubernetes * deployment or daemonset. * * In Deployment mode a kubernetes Deployment will be * created behind a kubernetes Service of type LoadBalancer. * The Deployment mode is intended to be used in Cloud Environments. * * In DaemonSet mode a kubernetes DaemonSet will be created * without a kubernetes Service, instead, hostPorts will be * used by the DaemonSet containers. The DaemonSet mode is intended * to be used in onpremise environments where a LoadBalancer services * may not available. * * defaults to Deployment mode. */ mode?: NginxIngressDeploymentMode | NginxIngressDaemonSetMode; /** * config sets nginx config map entries. * * see: https://github.com/kubernetes/ingress-nginx/blob/master/docs/user-guide/nginx-configuration/configmap.md */ config?: Record; /** * configure any L4 TCP services */ tcpServices?: Record; /** * configure any L4 UDP services */ udpServices?: Record; /** * maxmind license key to download GeoLite2 Databases * https://blog.maxmind.com/2019/12/18/significant-changes-to-accessing-and-using-geolite2-databases */ maxmindLicenseKey?: string; /** * Enables the SSL Passthrough feature in nginx-ingress. * This is required to use the `nginx.ingress.kubernetes.io/ssl-passthrough` * annotation for SNI based virtual domain (host) routing. * * https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#ssl-passthrough * * https://kubernetes.github.io/ingress-nginx/user-guide/tls/#ssl-passthrough */ enableSSLPassthrough?: boolean; } export interface NginxIngressDeploymentMode { kind: 'Deployment'; /** * how many nginx ingress controller * replicas should be deployed. * * defaults to 1 */ replicas?: pulumi.Input; /** * loadBalancerIP configures the cloud loadbalancer * IP address on supported clouds. * This IP will need to be provisioned using the * appropriate cloud resource. * * If this in left blank then kubernetes will assign * an IP address to the nginx-ingress LoadBalancer Service * for you (if supported by your cloud provider). */ loadBalancerIP?: pulumi.Input; /** * serviceAnnotations adds the given annotations to the * nginx-ingress loadbalancer service */ serviceAnnotations?: pulumi.Input>>; } export interface NginxIngressDaemonSetMode { kind: 'DaemonSet'; } export interface L4ServiceBackend { namespace: pulumi.Input; serviceName: pulumi.Input; servicePort: pulumi.Input; } type NginxL4Backends = Record>; export interface KubernetesNginxIngressOutputs { /** * the 'kubernetes.io/ingress.class' annotation that this * ingress controller will consume */ ingressClass: pulumi.Output; /** * The kubernetes service resource of the ingress controller */ ingressService: pulumi.Output; } /** * @noInheritDoc */ export class KubernetesNginxIngress extends pulumi.ComponentResource implements KubernetesNginxIngressOutputs { readonly ingressClass: pulumi.Output; readonly ingressService: pulumi.Output; constructor(name: string, props?: KubernetesNginxIngressInputs, opts?: pulumi.CustomResourceOptions) { super('cloud:kubernetes:KubernetesNginxIngress', name, props, opts); this.ingressClass = pulumi.output('nginx'); const ingressChart = new k8s.helm.v3.Chart( name, { namespace: props?.namespace, path: path.join(__dirname, 'charts/ingress-nginx'), values: { fullnameOverride: name, rbac: { create: true, }, defaultBackend: { enabled: false, }, controller: merge({}, this.controllerValues(props), { config: props?.config, labels: { app: 'nginx-ingress', }, podLabels: { app: 'nginx-ingress', }, admissionWebhooks: { enabled: false, }, metrics: { enabled: true, service: { type: 'ClusterIP', servicePort: 10254, annotations: { 'prometheus.io/scrape': 'true', 'prometheus.io/port': '10254', }, }, }, extraArgs: this.extraArgs(props), maxmindLicenseKey: props?.maxmindLicenseKey, }), tcp: this.l4Services(props?.tcpServices ?? {}), udp: this.l4Services(props?.udpServices ?? {}), }, }, { parent: this, provider: props?.provider, } ); const serviceName = `${name}-controller`; this.ingressService = props?.namespace ? ingressChart.getResource('v1/Service', props.namespace.toString(), serviceName) : ingressChart.getResource('v1/Service', serviceName); } private controllerValues(props?: KubernetesNginxIngressInputs) { switch (props?.mode?.kind) { case 'DaemonSet': return this.daemonSetValues(props?.mode); case 'Deployment': return this.deploymentValues(props?.mode); default: return this.deploymentValues({ kind: 'Deployment', replicas: 1, }); } } private daemonSetValues(props: NginxIngressDaemonSetMode) { return { kind: 'DaemonSet', reportNodeInternalIp: true, service: { enabled: false, }, publishService: { enabled: false, }, hostNetwork: true, hostPort: { enabled: true, }, }; } private deploymentValues(props: NginxIngressDeploymentMode) { const replicas = props?.replicas ?? 1; return { kind: 'Deployment', replicaCount: replicas, service: { type: 'LoadBalancer', loadBalancerIP: props?.loadBalancerIP, externalTrafficPolicy: 'Local', annotations: props.serviceAnnotations, }, /** * we'll only add the antiaffinity policy if * there are more than 1 replicas */ affinity: replicas <= 1 ? undefined : { podAntiAffinity: { preferredDuringSchedulingIgnoredDuringExecution: [ { weight: 1, podAffinityTerm: { topologyKey: 'kubernetes.io/hostname', labelSelector: { matchExpressions: [ { key: 'app', operator: 'In', values: ['nginx-ingress'], }, { key: 'component', operator: 'In', values: ['controller'], }, ], }, }, }, ], }, }, }; } private l4Services(props: Record): NginxL4Backends { const backends: NginxL4Backends = {}; for (const [key, value] of Object.entries(props)) { backends[Number(key)] = pulumi.interpolate`${value.namespace}/${value.serviceName}:${value.servicePort}`; } return backends; } private extraArgs(props?: KubernetesNginxIngressInputs) { const args: Record = {}; if (props?.enableSSLPassthrough) { // the --enable-ssl-passthrough flag doesn't take a value args['enable-ssl-passthrough'] = ''; } return args; } }