-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcluster.ts
163 lines (157 loc) · 4.67 KB
/
cluster.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import * as gcp from '@pulumi/gcp';
import * as k8s from '@pulumi/kubernetes';
import * as pulumi from '@pulumi/pulumi';
import { CidrBlock } from './config/network';
interface ClusterArgs {
location: string;
serviceAccountIamRoles: gcp.projects.IAMMember[];
serviceAccountCustomIamRoles: gcp.projects.IAMMember[];
projectServices: gcp.projects.Service[];
bucketIamRoles: gcp.storage.BucketIAMMember[];
vpc: gcp.compute.Network;
subnet: gcp.compute.Subnetwork;
serviceAccount: gcp.serviceAccount.Account;
cryptoKeyIamRole: gcp.kms.CryptoKeyIAMMember;
routerNat: gcp.compute.RouterNat;
k8sSecret: gcp.kms.CryptoKey;
k8sSecretGke: gcp.kms.CryptoKeyIAMMember;
loadBalancerIp: gcp.compute.Address;
kubernetesMastersIpv4Cidr: string;
kubernetesMasterAuthorizedNetworks: CidrBlock[];
kubernetesNodesPerZone: number;
kubernetesInstanceType: string;
kubernetesDailyMaintenanceWindow: string;
kubernetesLoggingService: string;
kubernetesMonitoringService: string;
kubernetesSecretsCryptoKey: string;
}
/**
* Create Vault cluster.
*/
export default function createCluster(
args: ClusterArgs
): {
k8sConfig: pulumi.Output<string>;
provider: k8s.Provider;
} {
const cluster = new gcp.container.Cluster(
'vault-cluster',
{
location: args.location,
network: args.vpc.selfLink,
subnetwork: args.subnet.selfLink,
initialNodeCount: args.kubernetesNodesPerZone,
minMasterVersion: gcp.container.getEngineVersions({
location: args.location,
}).latestMasterVersion,
nodeVersion: gcp.container.getEngineVersions({ location: args.location })
.latestMasterVersion,
loggingService: args.kubernetesLoggingService,
monitoringService: args.kubernetesMonitoringService,
enableLegacyAbac: false,
databaseEncryption: {
state: 'ENCRYPTED',
keyName: args.k8sSecret.selfLink,
},
nodeConfig: {
machineType: args.kubernetesInstanceType,
serviceAccount: args.serviceAccount.email,
oauthScopes: ['https://www.googleapis.com/auth/cloud-platform'],
metadata: {
'google-compute-enable-virtio-rng': 'true',
'disable-legacy-endpoints': 'true',
},
labels: {
service: 'vault',
},
tags: ['vault'],
workloadMetadataConfig: {
nodeMetadata: 'SECURE',
},
},
addonsConfig: {
networkPolicyConfig: {
disabled: false,
},
},
masterAuth: {
username: '',
password: '',
clientCertificateConfig: {
issueClientCertificate: false,
},
},
networkPolicy: {
enabled: true,
},
maintenancePolicy: {
dailyMaintenanceWindow: {
startTime: args.kubernetesDailyMaintenanceWindow,
},
},
ipAllocationPolicy: {
clusterSecondaryRangeName: args.subnet.secondaryIpRanges[0].rangeName,
servicesSecondaryRangeName: args.subnet.secondaryIpRanges[1].rangeName,
},
masterAuthorizedNetworksConfig: {
cidrBlocks: args.kubernetesMasterAuthorizedNetworks,
},
privateClusterConfig: {
enablePrivateEndpoint: false,
enablePrivateNodes: true,
masterIpv4CidrBlock: args.kubernetesMastersIpv4Cidr,
},
},
{
dependsOn: [
...args.serviceAccountIamRoles,
...args.serviceAccountCustomIamRoles,
...args.projectServices,
...args.bucketIamRoles,
args.cryptoKeyIamRole,
args.k8sSecretGke,
args.routerNat,
],
}
);
/**
* Manufacture a GKE-style Kubeconfig. Note that this is slightly
* "different" because of the way GKE requires gcloud to be in the
* picture for cluster authentication (rather than using the client
* cert/key directly).
*/
const k8sConfig = pulumi
.all([cluster.name, cluster.endpoint, cluster.masterAuth])
.apply(([name, endpoint, auth]) => {
const context = `${gcp.config.project}_${gcp.config.zone}_${name}`;
return `apiVersion: v1
clusters:
- cluster:
certificate-authority-data: ${auth.clusterCaCertificate}
server: https://${endpoint}
name: ${context}
contexts:
- context:
cluster: ${context}
user: ${context}
name: ${context}
current-context: ${context}
kind: Config
preferences: {}
users:
- name: ${context}
user:
auth-provider:
config:
cmd-args: config config-helper --format=json
cmd-path: gcloud
expiry-key: '{.credential.token_expiry}'
token-key: '{.credential.access_token}'
name: gcp
`;
});
const provider = new k8s.Provider('vault-cluster-provider', {
kubeconfig: k8sConfig,
});
return { k8sConfig, provider };
}