/
install.go
420 lines (396 loc) · 11.1 KB
/
install.go
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package kube
import (
"fmt"
"math/rand"
"time"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes"
appsv1beta1 "k8s.io/api/apps/v1beta1"
apiv1 "k8s.io/api/core/v1"
rbacv1beta1 "k8s.io/api/rbac/v1beta1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// InstallOptions configures the installation of needle into a k8s cluster
type InstallOptions struct {
Namespace string
RBAC bool
ServiceAccountName string
DeoploymentName string
Replicas int32
Labels map[string]string
NamerdHost string
PGPVClaimName string
PGPassword string
PGPWSecretName string
PGPWSecretKey string
Version string
}
// Default install options - these can be modified using Option functions
var DefaultInstallOptions = InstallOptions{
Namespace: "kube-system",
Labels: map[string]string{
"name": "needle",
"app": "compass",
},
RBAC: false,
ServiceAccountName: "needle",
DeoploymentName: "needle",
Replicas: 1,
PGPVClaimName: "needle-postgres-pv",
PGPassword: "", // Balnk will auto generate one
PGPWSecretName: "needle-postgres-password",
PGPWSecretKey: "password",
}
// An Option can configure InstallOptions
type InstallOption func(*InstallOptions)
// WithNamespace sets the namespace needle should be installed into
func InstallWithNamespace(name string) InstallOption {
return func(opts *InstallOptions) {
opts.Namespace = name
}
}
// WithServiceAccount sets the service account name
func WithInastallServiceAccount(name string) InstallOption {
return func(opts *InstallOptions) {
opts.ServiceAccountName = name
}
}
// WithDeploymentName sets the deployment name
func InstallWithDeploymentName(name string) InstallOption {
return func(opts *InstallOptions) {
opts.ServiceAccountName = name
}
}
// WithLabels sets the labels used for the needle install
func InstallWithLabels(labels map[string]string) InstallOption {
return func(opts *InstallOptions) {
opts.Labels = labels
}
}
// WithRBAC enables RBAC roles to be created for needle on install
func InstallWithRBAC() InstallOption {
return func(opts *InstallOptions) {
opts.RBAC = true
}
}
// InstallWithNamerdHost sets the namerd configuration
func InstallWithNamerdHost(name string) InstallOption {
return func(opts *InstallOptions) {
opts.NamerdHost = name
}
}
// InstallWithVersion sets the version to install
func InstallWithVersion(version string) InstallOption {
return func(opts *InstallOptions) {
opts.Version = version
}
}
// Install will intall neeedle into a k8s cluster
func Install(client *kubernetes.Clientset, opts ...InstallOption) error {
var options = DefaultInstallOptions
for _, opt := range opts {
opt(&options)
}
// Ensure namespace exists
if err := createNamespace(client, options); err != nil {
return err
}
// install RBAC roles if RBAC is true
if options.RBAC {
// Create service account
if err := createServiceAccount(client, options); err != nil {
return err
}
// Create a role for needle
if err := createRole(client, options); err != nil {
return err
}
// Create role binding
if err := createRoleBinding(client, options); err != nil {
return err
}
}
// Create a persisntent volume claim for PostgreSQL
if err := createPVClaim(client, options); err != nil {
return err
}
// Create a postgres secret to store the password
if err := createSecret(client, options); err != nil {
return err
}
// Creeate deployment
if err := createDeployment(client, options); err != nil {
return err
}
return nil
}
// handleError handle a k8s error, if something already exists thats ok
// else we just bubble up the error
func handleError(err error) error {
switch e := err.(type) {
case *k8serrors.StatusError:
switch e.Status().Reason {
case metav1.StatusReasonAlreadyExists:
return nil
default:
return err
}
default:
return err
}
return nil
}
// createNamespace creates a namespace within a k8s cluster
func createNamespace(c *kubernetes.Clientset, opts InstallOptions) error {
_, err := c.CoreV1().Namespaces().Create(&apiv1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: opts.Namespace,
Labels: opts.Labels,
},
})
return handleError(err)
}
// createServiceAccount create a service account
func createServiceAccount(c *kubernetes.Clientset, opts InstallOptions) error {
_, err := c.CoreV1().ServiceAccounts(opts.Namespace).Create(&apiv1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: opts.ServiceAccountName,
Labels: opts.Labels,
},
})
return handleError(err)
}
// createServiceAccount create a service account
func createRole(c *kubernetes.Clientset, opts InstallOptions) error {
_, err := c.RbacV1beta1().ClusterRoles().Create(&rbacv1beta1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: "needle:services:reader",
Labels: opts.Labels,
},
Rules: []rbacv1beta1.PolicyRule{
{
APIGroups: []string{
"",
},
Resources: []string{
"services",
},
Verbs: []string{
"get",
"list",
},
},
},
})
return handleError(err)
}
// Creates a role binding between the service account and role
func createRoleBinding(c *kubernetes.Clientset, opts InstallOptions) error {
var name = "needle:services:reader"
_, err := c.RbacV1beta1().ClusterRoleBindings().Create(&rbacv1beta1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: opts.Labels,
},
Subjects: []rbacv1beta1.Subject{
{
Kind: rbacv1beta1.ServiceAccountKind,
Name: opts.ServiceAccountName,
Namespace: opts.Namespace,
},
},
RoleRef: rbacv1beta1.RoleRef{
Kind: "ClusterRole",
Name: name,
APIGroup: "",
},
})
return handleError(err)
}
// createPVClaim creates a persisntent volume claim for postgres
func createPVClaim(c *kubernetes.Clientset, opts InstallOptions) error {
_, err := c.CoreV1().PersistentVolumeClaims(opts.Namespace).Create(&apiv1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: opts.PGPVClaimName,
Labels: opts.Labels,
Annotations: map[string]string{
"volume.alpha.kubernetes.io/storage-class": "default",
},
},
Spec: apiv1.PersistentVolumeClaimSpec{
AccessModes: []apiv1.PersistentVolumeAccessMode{
apiv1.ReadWriteOnce,
},
Resources: apiv1.ResourceRequirements{
Requests: apiv1.ResourceList{
apiv1.ResourceStorage: *resource.NewScaledQuantity(5, resource.Giga),
},
},
},
})
return handleError(err)
}
// genPassword generates a random genPassword for postgres
func genPassword() string {
var charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var seed *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
b := make([]byte, 12)
for i := range b {
b[i] = charset[seed.Intn(len(charset))]
}
return string(b)
}
// cereateSecret creates a postgres secret with either a provided password
// or a auto generated password
func createSecret(c *kubernetes.Clientset, opts InstallOptions) error {
var password = opts.PGPassword
if password == "" {
password = genPassword()
}
_, err := c.CoreV1().Secrets(opts.Namespace).Create(&apiv1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: opts.PGPWSecretName,
Labels: opts.Labels,
},
Type: apiv1.SecretTypeOpaque,
Data: map[string][]byte{
opts.PGPWSecretKey: []byte(password),
},
})
return handleError(err)
}
// intOrStringPtr converts returns a pointer to a intstr.IntOrString
func intOrStringPtr(i intstr.IntOrString) *intstr.IntOrString { return &i }
// createDeployment creates a deployment for needle
func createDeployment(c *kubernetes.Clientset, opts InstallOptions) error {
var serviceAccount = ""
if opts.RBAC {
serviceAccount = opts.ServiceAccountName
}
_, err := c.AppsV1beta1().Deployments(opts.Namespace).Create(&appsv1beta1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: opts.DeoploymentName,
Labels: opts.Labels,
},
Spec: appsv1beta1.DeploymentSpec{
Replicas: &opts.Replicas,
Strategy: appsv1beta1.DeploymentStrategy{
RollingUpdate: &appsv1beta1.RollingUpdateDeployment{
MaxSurge: intOrStringPtr(intstr.FromInt(1)),
MaxUnavailable: intOrStringPtr(intstr.FromInt(1)),
},
},
Template: apiv1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: opts.Labels,
},
Spec: apiv1.PodSpec{
ServiceAccountName: serviceAccount,
DNSPolicy: apiv1.DNSClusterFirst,
Volumes: []apiv1.Volume{
{
Name: "pgdata",
VolumeSource: apiv1.VolumeSource{
PersistentVolumeClaim: &apiv1.PersistentVolumeClaimVolumeSource{
ClaimName: opts.PGPVClaimName,
},
},
},
},
Containers: []apiv1.Container{
{
Name: "needle",
Image: fmt.Sprintf("soon/needle:%s", opts.Version),
ImagePullPolicy: apiv1.PullIfNotPresent,
Ports: []apiv1.ContainerPort{
{
Name: "grpc",
ContainerPort: 5000,
},
},
Env: []apiv1.EnvVar{
{
Name: "NEEDLE_PSQL_PASSWORD",
ValueFrom: &apiv1.EnvVarSource{
SecretKeyRef: &apiv1.SecretKeySelector{
LocalObjectReference: apiv1.LocalObjectReference{
Name: opts.PGPWSecretName,
},
Key: opts.PGPWSecretKey,
},
},
},
{
Name: "NEEDLE_NAMERD_HOST",
Value: opts.NamerdHost,
},
},
Resources: apiv1.ResourceRequirements{
Limits: apiv1.ResourceList{
apiv1.ResourceCPU: resource.MustParse("0"),
apiv1.ResourceMemory: resource.MustParse("32Mi"),
},
Requests: apiv1.ResourceList{
apiv1.ResourceCPU: resource.MustParse("0"),
apiv1.ResourceMemory: resource.MustParse("16Mi"),
},
},
},
{
Name: "postgresql",
Image: "postgres:10.1-alpine",
ImagePullPolicy: apiv1.PullIfNotPresent,
VolumeMounts: []apiv1.VolumeMount{
{
Name: "pgdata",
MountPath: "/var/lib/postgresql/data",
SubPath: "data",
},
},
Env: []apiv1.EnvVar{
{
Name: "POSTGRES_DB",
Value: "needle",
},
{
Name: "POSTGRES_USER",
Value: "postgres",
},
{
Name: "POSTGRES_PASS",
ValueFrom: &apiv1.EnvVarSource{
SecretKeyRef: &apiv1.SecretKeySelector{
LocalObjectReference: apiv1.LocalObjectReference{
Name: opts.PGPWSecretName,
},
Key: opts.PGPWSecretKey,
},
},
},
{
Name: "POSTGRES_PGDATA",
Value: "/var/lib/postgresql/data",
},
},
Ports: []apiv1.ContainerPort{
{
Name: "grpc",
ContainerPort: 5000,
},
},
Resources: apiv1.ResourceRequirements{
Requests: apiv1.ResourceList{
apiv1.ResourceCPU: resource.MustParse("0"),
apiv1.ResourceMemory: resource.MustParse("128Mi"),
},
},
},
},
},
},
},
})
return handleError(err)
}