|
| 1 | +import * as vpcmod from '@pulumi/vpcmod'; |
| 2 | +import * as k8s from '@pulumi/kubernetes'; |
| 3 | +import * as pulumi from '@pulumi/pulumi'; |
| 4 | +import * as eksmod from '@pulumi/eksmod'; |
| 5 | +import * as aws from '@pulumi/aws'; |
| 6 | +import * as std from '@pulumi/std'; |
| 7 | +import { getKubeConfig } from './kube-config'; |
| 8 | + |
| 9 | +const azs = aws.getAvailabilityZonesOutput({ |
| 10 | + filters: [{ |
| 11 | + name: "opt-in-status", |
| 12 | + values: ["opt-in-not-required"], |
| 13 | + }] |
| 14 | +}).names.apply(names => names.slice(0, 3)); |
| 15 | + |
| 16 | +const cidr = "10.0.0.0/16"; |
| 17 | + |
| 18 | +const cfg = new pulumi.Config(); |
| 19 | +const prefix = cfg.get("prefix") ?? pulumi.getStack(); |
| 20 | + |
| 21 | +const vpc = new vpcmod.Module("test-vpc", { |
| 22 | + azs: azs, |
| 23 | + name: `test-vpc-${prefix}`, |
| 24 | + cidr, |
| 25 | + private_subnets: azs.apply(azs => azs.map((_, i) => { |
| 26 | + return getCidrSubnet(cidr, 8, i+1); |
| 27 | + })), |
| 28 | + public_subnets: azs.apply(azs => azs.map((_, i) => { |
| 29 | + return getCidrSubnet(cidr, 8, i+1+4); |
| 30 | + })), |
| 31 | + intra_subnets: azs.apply(azs => azs.map((_, i) => { |
| 32 | + return getCidrSubnet(cidr, 8, i+1 + 8); |
| 33 | + })), |
| 34 | + |
| 35 | + |
| 36 | + enable_nat_gateway: true, |
| 37 | + single_nat_gateway: true, |
| 38 | + |
| 39 | + public_subnet_tags: { |
| 40 | + 'kubernetes.io/role/elb': '1', |
| 41 | + }, |
| 42 | + private_subnet_tags: { |
| 43 | + 'kubernetes.io/role/internal-elb': '1', |
| 44 | + }, |
| 45 | +}); |
| 46 | + |
| 47 | +const cluster = new eksmod.Module('test-cluster', { |
| 48 | + cluster_name: `test-cluster-${prefix}`, |
| 49 | + cluster_endpoint_public_access: true, |
| 50 | + cluster_compute_config: { |
| 51 | + enabled: true, |
| 52 | + node_pools: ["general-purpose"], |
| 53 | + }, |
| 54 | + vpc_id: vpc.vpc_id.apply(id => id!), |
| 55 | + // TODO [pulumi/pulumi-terraform-module#228] have to use a list of unknowns instead of unknown list |
| 56 | + subnet_ids: [ |
| 57 | + vpc.private_subnets.apply(subnets => subnets![0]), |
| 58 | + vpc.private_subnets.apply(subnets => subnets![1]), |
| 59 | + vpc.private_subnets.apply(subnets => subnets![2]), |
| 60 | + ], |
| 61 | + enable_cluster_creator_admin_permissions: true, |
| 62 | +}, { dependsOn: vpc }); |
| 63 | + |
| 64 | +// Make the cluster name an output so the downstream resources depend on the cluster creation |
| 65 | +const clusterName = pulumi.all([cluster.cluster_arn, cluster.cluster_name]).apply(([_clusterArn, clusterName]) => { |
| 66 | + return clusterName!; |
| 67 | +}); |
| 68 | + |
| 69 | +const kubeconfig = getKubeConfig(clusterName, { |
| 70 | + dependsOn: cluster, |
| 71 | +}); |
| 72 | + |
| 73 | +const k8sProvider = new k8s.Provider("k8sProvider", { |
| 74 | + kubeconfig, |
| 75 | +}, { dependsOn: cluster }); |
| 76 | + |
| 77 | + |
| 78 | +const appName = "nginx"; |
| 79 | +const ns = new k8s.core.v1.Namespace(appName, { |
| 80 | + metadata: { name: appName }, |
| 81 | +}, { provider: k8sProvider }); |
| 82 | + |
| 83 | +const configMap = new k8s.core.v1.ConfigMap(appName, { |
| 84 | + metadata: { |
| 85 | + namespace: ns.metadata.name, |
| 86 | + }, |
| 87 | + data: { |
| 88 | + "index.html": "<html><body><h1>Hello, Pulumi!</h1></body></html>", |
| 89 | + }, |
| 90 | +}, { provider: k8sProvider }); |
| 91 | + |
| 92 | +const deployment = new k8s.apps.v1.Deployment(appName, { |
| 93 | + metadata: { |
| 94 | + namespace: ns.metadata.name |
| 95 | + }, |
| 96 | + spec: { |
| 97 | + selector: { matchLabels: { app: appName } }, |
| 98 | + replicas: 3, |
| 99 | + template: { |
| 100 | + metadata: { labels: { app: appName } }, |
| 101 | + spec: { |
| 102 | + containers: [{ |
| 103 | + name: appName, |
| 104 | + image: appName, |
| 105 | + ports: [{ containerPort: 80 }], |
| 106 | + volumeMounts: [{ name: "nginx-index", mountPath: "/usr/share/nginx/html" }], |
| 107 | + }], |
| 108 | + volumes: [{ |
| 109 | + name: "nginx-index", |
| 110 | + configMap: { name: configMap.metadata.name }, |
| 111 | + }], |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | +}, { provider: k8sProvider }); |
| 116 | + |
| 117 | +const service = new k8s.core.v1.Service(appName, { |
| 118 | + metadata: { |
| 119 | + name: appName, |
| 120 | + namespace: ns.metadata.name |
| 121 | + }, |
| 122 | + spec: { |
| 123 | + selector: { app: appName }, |
| 124 | + ports: [{ port: 80, targetPort: 80 }], |
| 125 | + }, |
| 126 | +}, { provider: k8sProvider, dependsOn: [deployment] }); |
| 127 | + |
| 128 | +const ingressClass = new k8s.networking.v1.IngressClass("alb", { |
| 129 | + metadata: { |
| 130 | + namespace: ns.metadata.name, |
| 131 | + labels: { |
| 132 | + "app.kubernetes.io/name": "LoadBalancerController", |
| 133 | + }, |
| 134 | + name: "alb", |
| 135 | + }, |
| 136 | + spec: { |
| 137 | + controller: "eks.amazonaws.com/alb", |
| 138 | + } |
| 139 | +}, { provider: k8sProvider }); |
| 140 | + |
| 141 | +const ingress = new k8s.networking.v1.Ingress(appName, { |
| 142 | + metadata: { |
| 143 | + namespace: ns.metadata.name, |
| 144 | + // Annotations for EKS Auto Mode to identify the Ingress as internet-facing and target-type as IP. |
| 145 | + annotations: { |
| 146 | + "alb.ingress.kubernetes.io/scheme": "internet-facing", |
| 147 | + "alb.ingress.kubernetes.io/target-type": "ip", |
| 148 | + } |
| 149 | + }, |
| 150 | + spec: { |
| 151 | + ingressClassName: ingressClass.metadata.name, |
| 152 | + rules: [{ |
| 153 | + http: { |
| 154 | + paths: [{ |
| 155 | + path: "/", |
| 156 | + pathType: "Prefix", |
| 157 | + backend: { |
| 158 | + service: { |
| 159 | + name: service.metadata.name, |
| 160 | + port: { |
| 161 | + number: 80, |
| 162 | + }, |
| 163 | + }, |
| 164 | + }, |
| 165 | + }], |
| 166 | + }, |
| 167 | + }], |
| 168 | + } |
| 169 | +}, { provider: k8sProvider }); |
| 170 | + |
| 171 | +export const url = ingress.status.apply(status => status?.loadBalancer?.ingress?.[0]?.hostname); |
| 172 | + |
| 173 | +function getCidrSubnet(cidr: string, newbits: number, netnum: number): pulumi.Output<string> { |
| 174 | + return std.cidrsubnetOutput({ |
| 175 | + input: cidr, |
| 176 | + newbits, |
| 177 | + netnum, |
| 178 | + }).result |
| 179 | +} |
0 commit comments