-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
187 lines (178 loc) · 5.4 KB
/
index.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import * as pulumi from "@pulumi/pulumi";
import * as kubernetes from "@pulumi/kubernetes";
import {Secret} from "@pulumi/kubernetes/core/v1";
import {CustomResource} from "@pulumi/kubernetes/apiextensions";
const defaultCRDVersion = "v1.14.0";
const defaultOperatorVersion = "v1.14.0";
const image = "bjoern5urban/pulumi-kubernetes-operator:latest"
const config = new pulumi.Config();
const deployNamespace = config.get("namespace") || 'default';
const deployNamespaceList = config.getObject<string[]>("namespaces") || [deployNamespace];
const crdVersion = config.get("crd-version") || defaultCRDVersion;
const operatorVersion = config.get("operator-version") || defaultOperatorVersion;
// Get the Pulumi API token.
const pulumiAccessToken = config.requireSecret("pulumiAccessToken")
const stackCRD = new kubernetes.yaml.ConfigFile("stackcrd", {
file: `https://raw.githubusercontent.com/pulumi/pulumi-kubernetes-operator/${crdVersion}/deploy/crds/pulumi.com_stacks.yaml`
});
const programCRD = new kubernetes.yaml.ConfigFile("programcrd", {
file: `https://raw.githubusercontent.com/pulumi/pulumi-kubernetes-operator/${crdVersion}/deploy/crds/pulumi.com_programs.yaml`
});
const deploymentOptions = {dependsOn: [stackCRD, programCRD]};
const operatorClusterRole = new kubernetes.rbac.v1.ClusterRole(`operator-cluster-role`, {
rules: [
{
apiGroups: ["*"],
resources: ["*"],
verbs: ["*"],
},
// Add other rules as needed
],
});
const ns = deployNamespaceList[0]
const operatorServiceAccount = new kubernetes.core.v1.ServiceAccount(`operator-service-account-${ns}`, {
metadata: {
"namespace": ns,
},
});
// Bind the ClusterRole to the service account
const operatorClusterRoleBinding = new kubernetes.rbac.v1.ClusterRoleBinding(`operator-cluster-role-binding`, {
metadata: {
name: `operator-cluster-role-binding`,
},
subjects: [{
kind: "ServiceAccount",
name: operatorServiceAccount.metadata.name,
namespace: ns, // Specify the namespace of the ServiceAccount
}],
roleRef: {
kind: "ClusterRole",
name: operatorClusterRole.metadata.name,
apiGroup: "rbac.authorization.k8s.io",
},
});
const operatorDeployment = new kubernetes.apps.v1.Deployment(`pulumi-kubernetes-operator-${ns}`, {
metadata: {
"namespace": ns,
},
spec: {
replicas: 1,
selector: {
matchLabels: {
name: "pulumi-kubernetes-operator",
},
},
template: {
metadata: {
labels: {
name: "pulumi-kubernetes-operator",
},
},
spec: {
serviceAccountName: operatorServiceAccount.metadata.name,
containers: [{
name: "pulumi-kubernetes-operator",
image: image,
args: ["--zap-level=error", "--zap-time-encoding=iso8601"],
imagePullPolicy: "Always",
env: [
{
name: "WATCH_NAMESPACE",
valueFrom: {
fieldRef: {
fieldPath: "metadata.namespace",
},
},
},
{
name: "POD_NAME",
valueFrom: {
fieldRef: {
fieldPath: "metadata.name",
},
},
},
{
name: "OPERATOR_NAME",
value: "pulumi-kubernetes-operator",
},
{
name: "GRACEFUL_SHUTDOWN_TIMEOUT_DURATION",
value: "5m",
},
{
name: "MAX_CONCURRENT_RECONCILES",
value: "10",
},
],
}],
// Should be same or larger than GRACEFUL_SHUTDOWN_TIMEOUT_DURATION
terminationGracePeriodSeconds: 300,
},
},
},
}, deploymentOptions);
// Create the API token as a Kubernetes Secret.
const accessToken = new Secret("operator-accesstoken", {
metadata: {
name: "flux-secret",
namespace: ns
},
stringData: {accessToken: pulumiAccessToken},
});
// Create an NGINX deployment in-cluster.
const stackL1 = new CustomResource("l1", {
apiVersion: 'pulumi.com/v1',
kind: 'Stack',
metadata: {
name: "l1",
namespace: ns
},
spec: {
envRefs: {
PULUMI_ACCESS_TOKEN: {
type: "Secret",
secret: {
key: "accessToken",
name: accessToken.metadata.name,
namespace: ns,
},
}
},
stack: "hetzner",
projectRepo: "https://github.com/B-urb/infrastructure",
repoDir: "/l1",
branch: "master",
retryOnUpdateConflict: true,
destroyOnFinalize: true,
}
});
const stackL2 = new CustomResource("l2", {
apiVersion: 'pulumi.com/v1',
kind: 'Stack',
metadata: {
name: "l2",
namespace: ns
},
spec: {
envRefs: {
PULUMI_ACCESS_TOKEN: {
type: "Secret",
secret: {
key: "accessToken",
name: accessToken.metadata.name,
namespace: ns,
},
}
},
stack: "hetzner",
projectRepo: "https://github.com/B-Urb/infrastructure",
repoDir: "/l2",
branch: "master",
prerequisites: [
{name: "l1"}
],
retryOnUpdateConflict: true,
destroyOnFinalize: true,
}
});