Last active
October 9, 2021 06:09
-
-
Save vitobotta/219e9bcdd09b9b841167c33b493b65d5 to your computer and use it in GitHub Desktop.
Installing Nginx ingress controller with Pulumi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as pulumi from '@pulumi/pulumi' | |
const config: pulumi.Config = new pulumi.Config('nginx-ingress') | |
export const version: string = config.get('version') || "1.34.2" | |
export const namespace: string = config.get('namespace') || "nginx-ingress" | |
export const serviceType: string = config.get('serviceType') || "ClusterIP" | |
export const ingressClass: string = config.get('ingressClass') || "nginx" | |
export const nodePortHTTP: number = Number(config.get('nodePortHTTP')) || 30080 | |
export const nodePortHTTPS: number = Number(config.get('nodePortHTTPS')) || 30443 | |
export const replicaCount: number = Number(config.get('replicaCount')) || 1 | |
export const useProxyProtocol: string = config.get('useProxyProtocol') || "false" | |
export const useForwardedHeaders: string = config.get('useForwardedHeaders') || "true" | |
export const clientMaxBodySize: string = config.get('clientMaxBodySize') || "0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NginxIngress } from "./NginxIngress"; | |
// ClusterIP | |
const nginxIngress = new NginxIngress("nginx-ingress", { | |
namespace: "nginx-ingress", | |
ingressClass: "nginx", | |
}); | |
// NodePort | |
const nginxIngress = new NginxIngress("nginx-ingress", { | |
namespace: "nginx-ingress", | |
serviceType: "NodePort", | |
ingressClass: "nginx", | |
nodePortHTTP: 30080, | |
nodePortHTTPS: 30443 | |
}); | |
// LoadBalancer | |
const nginxIngress = new NginxIngress("nginx-ingress", { | |
namespace: "nginx-ingress", | |
serviceType: "LoadBalancer", | |
ingressClass: "nginx", | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as k8s from '@pulumi/kubernetes' | |
import * as pulumi from '@pulumi/pulumi' | |
import * as config from './config' | |
export interface NginxIngressArgs { | |
version?: pulumi.Input<string>, | |
namespace?: pulumi.Input<string>, | |
serviceType?: pulumi.Input<string>, | |
ingressClass?: pulumi.Input<string>, | |
nodePortHTTP?: pulumi.Input<number>, | |
nodePortHTTPS?: pulumi.Input<number>, | |
replicaCount?: pulumi.Input<number>, | |
useProxyProtocol?: pulumi.Input<string>, | |
useForwardedHeaders?: pulumi.Input<string>, | |
clientMaxBodySize?: pulumi.Input<string>, | |
} | |
export class NginxIngress extends pulumi.ComponentResource { | |
constructor( | |
appName: string, | |
args: NginxIngressArgs, | |
opts?: pulumi.ComponentResourceOptions, | |
) { | |
super(appName, appName, {}, opts) | |
const version = args.version || config.version | |
const namespace = args.namespace || config.namespace | |
const serviceType = args.serviceType || config.serviceType | |
const ingressClass = args.ingressClass || config.ingressClass | |
const nodePortHTTP = args.nodePortHTTP || config.nodePortHTTP | |
const nodePortHTTPS = args.nodePortHTTPS || config.nodePortHTTPS | |
const replicaCount = args.replicaCount || config.replicaCount | |
const useProxyProtocol = args.useProxyProtocol || config.useProxyProtocol | |
const useForwardedHeaders = args.useForwardedHeaders || config.useForwardedHeaders | |
const clientMaxBodySize = args.clientMaxBodySize || config.clientMaxBodySize | |
let kind: string = "DaemonSet" | |
let useHostPort: boolean = true | |
let hostNetwork: boolean = true | |
let externalTrafficPolicy: string = "" | |
let nodePorts; | |
const ns = new k8s.core.v1.Namespace( | |
`${appName}-ns`, | |
{ | |
metadata: { | |
name: namespace, | |
}, | |
}, | |
{ parent: this }, | |
) | |
switch (serviceType) { | |
case "ClusterIP": | |
break; | |
case "NodePort": | |
useHostPort = false | |
hostNetwork = false | |
externalTrafficPolicy = "Local" | |
nodePorts = { | |
http: nodePortHTTP, | |
https: nodePortHTTPS | |
} | |
break; | |
case "LoadBalancer": | |
kind = "Deployment" | |
useHostPort = false | |
hostNetwork = false | |
externalTrafficPolicy = "Local" | |
break; | |
default: | |
break; | |
} | |
const nginxIngress = new k8s.helm.v3.Chart( | |
appName, | |
{ | |
chart: "nginx-ingress", | |
version: version, | |
fetchOpts: { | |
repo: 'https://kubernetes-charts.storage.googleapis.com', | |
}, | |
namespace: namespace, | |
values: { | |
controller: { | |
kind: kind, | |
replicaCount: replicaCount, | |
service: { | |
type: serviceType, | |
externalTrafficPolicy: externalTrafficPolicy, | |
nodePorts: nodePorts | |
}, | |
ingressClass: ingressClass, | |
daemonset: { | |
useHostPort: useHostPort, | |
}, | |
hostNetwork: hostNetwork, | |
metrics: { | |
enabled: true, | |
service: { | |
type: "ClusterIP" | |
} | |
} | |
} | |
} | |
}, | |
{ | |
parent: this, | |
dependsOn: [ | |
ns, | |
], | |
}, | |
) | |
const configMap = new k8s.core.v1.ConfigMap(`${appName}-config-map`, { | |
metadata: { | |
namespace: namespace, | |
name: `${appName}-controller` | |
}, | |
data: { | |
"use-proxy-protocol": useProxyProtocol, | |
"use-forwarded-headers": useForwardedHeaders, | |
"client-max-body-size": clientMaxBodySize | |
} | |
}, | |
{ | |
parent: this, | |
dependsOn: [ | |
nginxIngress, | |
], | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment