-
Notifications
You must be signed in to change notification settings - Fork 608
/
Copy pathpgadmin.go
494 lines (414 loc) · 17.9 KB
/
pgadmin.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
Copyright 2021 - 2022 Crunchy Data Solutions, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package postgrescluster
import (
"context"
"fmt"
"io"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/crunchydata/postgres-operator/internal/config"
"github.com/crunchydata/postgres-operator/internal/initialize"
"github.com/crunchydata/postgres-operator/internal/logging"
"github.com/crunchydata/postgres-operator/internal/naming"
"github.com/crunchydata/postgres-operator/internal/pgadmin"
"github.com/crunchydata/postgres-operator/internal/postgres"
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
)
// reconcilePGAdmin writes the objects necessary to run a pgAdmin Pod.
func (r *Reconciler) reconcilePGAdmin(
ctx context.Context, cluster *v1beta1.PostgresCluster,
) error {
// NOTE: [Reconciler.reconcilePGAdminUsers] is called in [Reconciler.reconcilePostgresUsers].
// TODO(tjmoore4): Currently, the returned service is only used in tests,
// but it may be useful during upcoming feature enhancements. If not, we
// may consider removing the service return altogether and refactoring
// this function to only return errors.
_, err := r.reconcilePGAdminService(ctx, cluster)
var configmap *corev1.ConfigMap
var dataVolume *corev1.PersistentVolumeClaim
if err == nil {
configmap, err = r.reconcilePGAdminConfigMap(ctx, cluster)
}
if err == nil {
dataVolume, err = r.reconcilePGAdminDataVolume(ctx, cluster)
}
if err == nil {
err = r.reconcilePGAdminStatefulSet(ctx, cluster, configmap, dataVolume)
}
return err
}
// generatePGAdminConfigMap returns a v1.ConfigMap for pgAdmin.
func (r *Reconciler) generatePGAdminConfigMap(
cluster *v1beta1.PostgresCluster) (*corev1.ConfigMap, bool, error,
) {
configmap := &corev1.ConfigMap{ObjectMeta: naming.ClusterPGAdmin(cluster)}
configmap.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("ConfigMap"))
if cluster.Spec.UserInterface == nil || cluster.Spec.UserInterface.PGAdmin == nil {
return configmap, false, nil
}
configmap.Annotations = naming.Merge(
cluster.Spec.Metadata.GetAnnotationsOrNil(),
cluster.Spec.UserInterface.PGAdmin.Metadata.GetAnnotationsOrNil())
configmap.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
cluster.Spec.UserInterface.PGAdmin.Metadata.GetLabelsOrNil(),
map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelRole: naming.RolePGAdmin,
})
err := errors.WithStack(pgadmin.ConfigMap(cluster, configmap))
if err == nil {
err = errors.WithStack(r.setControllerReference(cluster, configmap))
}
return configmap, true, err
}
// +kubebuilder:rbac:groups="",resources="configmaps",verbs={get}
// +kubebuilder:rbac:groups="",resources="configmaps",verbs={create,delete,patch}
// reconcilePGAdminConfigMap writes the ConfigMap for pgAdmin.
func (r *Reconciler) reconcilePGAdminConfigMap(
ctx context.Context, cluster *v1beta1.PostgresCluster,
) (*corev1.ConfigMap, error) {
configmap, specified, err := r.generatePGAdminConfigMap(cluster)
if err == nil && !specified {
// pgAdmin is disabled; delete the ConfigMap if it exists. Check the
// client cache first using Get.
key := client.ObjectKeyFromObject(configmap)
err := errors.WithStack(r.Client.Get(ctx, key, configmap))
if err == nil {
err = errors.WithStack(r.deleteControlled(ctx, cluster, configmap))
}
return nil, client.IgnoreNotFound(err)
}
if err == nil {
err = errors.WithStack(r.apply(ctx, configmap))
}
return configmap, err
}
// generatePGAdminService returns a v1.Service that exposes pgAdmin pods.
// The ServiceType comes from the cluster user interface spec.
func (r *Reconciler) generatePGAdminService(
cluster *v1beta1.PostgresCluster) (*corev1.Service, bool, error,
) {
service := &corev1.Service{ObjectMeta: naming.ClusterPGAdmin(cluster)}
service.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Service"))
if cluster.Spec.UserInterface == nil || cluster.Spec.UserInterface.PGAdmin == nil {
return service, false, nil
}
service.Annotations = naming.Merge(
cluster.Spec.Metadata.GetAnnotationsOrNil(),
cluster.Spec.UserInterface.PGAdmin.Metadata.GetAnnotationsOrNil())
service.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
cluster.Spec.UserInterface.PGAdmin.Metadata.GetLabelsOrNil())
if spec := cluster.Spec.UserInterface.PGAdmin.Service; spec != nil {
service.Annotations = naming.Merge(service.Annotations,
spec.Metadata.GetAnnotationsOrNil())
service.Labels = naming.Merge(service.Labels,
spec.Metadata.GetLabelsOrNil())
}
// add our labels last so they aren't overwritten
service.Labels = naming.Merge(service.Labels,
map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelRole: naming.RolePGAdmin,
})
// Allocate an IP address and/or node port and let Kubernetes manage the
// Endpoints by selecting Pods with the pgAdmin role.
// - https://docs.k8s.io/concepts/services-networking/service/#defining-a-service
service.Spec.Selector = map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelRole: naming.RolePGAdmin,
}
// The TargetPort must be the name (not the number) of the pgAdmin
// ContainerPort. This name allows the port number to differ between Pods,
// which can happen during a rolling update.
//
// TODO(tjmoore4): A custom service port is not currently supported as this
// requires updates to the pgAdmin service configuration.
servicePort := corev1.ServicePort{
Name: naming.PortPGAdmin,
Port: *initialize.Int32(5050),
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromString(naming.PortPGAdmin),
AppProtocol: initialize.String(naming.AppProtocolHTTP),
}
if spec := cluster.Spec.UserInterface.PGAdmin.Service; spec == nil {
service.Spec.Type = corev1.ServiceTypeClusterIP
} else {
service.Spec.Type = corev1.ServiceType(spec.Type)
if spec.NodePort != nil {
if service.Spec.Type == corev1.ServiceTypeClusterIP {
// The NodePort can only be set when the Service type is NodePort or
// LoadBalancer. However, due to a known issue prior to Kubernetes
// 1.20, we clear these errors during our apply. To preserve the
// appropriate behavior, we log an Event and return an error.
// TODO(tjmoore4): Once Validation Rules are available, this check
// and event could potentially be removed in favor of that validation
r.Recorder.Eventf(cluster, corev1.EventTypeWarning, "MisconfiguredClusterIP",
"NodePort cannot be set with type ClusterIP on Service %q", service.Name)
return nil, true, fmt.Errorf("NodePort cannot be set with type ClusterIP on Service %q", service.Name)
}
servicePort.NodePort = *spec.NodePort
}
}
service.Spec.Ports = []corev1.ServicePort{servicePort}
err := errors.WithStack(r.setControllerReference(cluster, service))
return service, true, err
}
// +kubebuilder:rbac:groups="",resources="services",verbs={get}
// +kubebuilder:rbac:groups="",resources="services",verbs={create,delete,patch}
// reconcilePGAdminService writes the Service that resolves to pgAdmin.
func (r *Reconciler) reconcilePGAdminService(
ctx context.Context, cluster *v1beta1.PostgresCluster,
) (*corev1.Service, error) {
service, specified, err := r.generatePGAdminService(cluster)
if err == nil && !specified {
// pgAdmin is disabled; delete the Service if it exists. Check the client
// cache first using Get.
key := client.ObjectKeyFromObject(service)
err := errors.WithStack(r.Client.Get(ctx, key, service))
if err == nil {
err = errors.WithStack(r.deleteControlled(ctx, cluster, service))
}
return nil, client.IgnoreNotFound(err)
}
if err == nil {
err = errors.WithStack(r.apply(ctx, service))
}
return service, err
}
// +kubebuilder:rbac:groups=apps,resources=statefulsets,verbs=get
// +kubebuilder:rbac:groups=apps,resources=statefulsets,verbs=create;delete;patch
// reconcilePGAdminStatefulSet writes the StatefulSet that runs pgAdmin.
func (r *Reconciler) reconcilePGAdminStatefulSet(
ctx context.Context, cluster *v1beta1.PostgresCluster,
configmap *corev1.ConfigMap, dataVolume *corev1.PersistentVolumeClaim,
) error {
sts := &appsv1.StatefulSet{ObjectMeta: naming.ClusterPGAdmin(cluster)}
sts.SetGroupVersionKind(appsv1.SchemeGroupVersion.WithKind("StatefulSet"))
if cluster.Spec.UserInterface == nil || cluster.Spec.UserInterface.PGAdmin == nil {
// pgAdmin is disabled; delete the Deployment if it exists. Check the
// client cache first using Get.
key := client.ObjectKeyFromObject(sts)
err := errors.WithStack(r.Client.Get(ctx, key, sts))
if err == nil {
err = errors.WithStack(r.deleteControlled(ctx, cluster, sts))
}
return client.IgnoreNotFound(err)
}
sts.Annotations = naming.Merge(
cluster.Spec.Metadata.GetAnnotationsOrNil(),
cluster.Spec.UserInterface.PGAdmin.Metadata.GetAnnotationsOrNil())
sts.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
cluster.Spec.UserInterface.PGAdmin.Metadata.GetLabelsOrNil(),
map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelRole: naming.RolePGAdmin,
naming.LabelData: naming.DataPGAdmin,
})
sts.Spec.Selector = &metav1.LabelSelector{
MatchLabels: map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelRole: naming.RolePGAdmin,
},
}
sts.Spec.Template.Annotations = naming.Merge(
cluster.Spec.Metadata.GetAnnotationsOrNil(),
cluster.Spec.UserInterface.PGAdmin.Metadata.GetAnnotationsOrNil())
sts.Spec.Template.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
cluster.Spec.UserInterface.PGAdmin.Metadata.GetLabelsOrNil(),
map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelRole: naming.RolePGAdmin,
naming.LabelData: naming.DataPGAdmin,
})
// if the shutdown flag is set, set pgAdmin replicas to 0
if cluster.Spec.Shutdown != nil && *cluster.Spec.Shutdown {
sts.Spec.Replicas = initialize.Int32(0)
} else {
sts.Spec.Replicas = cluster.Spec.UserInterface.PGAdmin.Replicas
}
// Don't clutter the namespace with extra ControllerRevisions.
sts.Spec.RevisionHistoryLimit = initialize.Int32(0)
// Give the Pod a stable DNS record based on its name.
// - https://docs.k8s.io/concepts/workloads/controllers/statefulset/#stable-network-id
// - https://docs.k8s.io/concepts/services-networking/dns-pod-service/#pods
sts.Spec.ServiceName = naming.ClusterPodService(cluster).Name
// Set the StatefulSet update strategy to "RollingUpdate", and the Partition size for the
// update strategy to 0 (note that these are the defaults for a StatefulSet). This means
// every pod of the StatefulSet will be deleted and recreated when the Pod template changes.
// - https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#rolling-updates
// - https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#forced-rollback
sts.Spec.UpdateStrategy.Type = appsv1.RollingUpdateStatefulSetStrategyType
sts.Spec.UpdateStrategy.RollingUpdate = &appsv1.RollingUpdateStatefulSetStrategy{
Partition: initialize.Int32(0),
}
// Use scheduling constraints from the cluster spec.
sts.Spec.Template.Spec.Affinity = cluster.Spec.UserInterface.PGAdmin.Affinity
sts.Spec.Template.Spec.Tolerations = cluster.Spec.UserInterface.PGAdmin.Tolerations
if cluster.Spec.UserInterface.PGAdmin.PriorityClassName != nil {
sts.Spec.Template.Spec.PriorityClassName = *cluster.Spec.UserInterface.PGAdmin.PriorityClassName
}
sts.Spec.Template.Spec.TopologySpreadConstraints =
cluster.Spec.UserInterface.PGAdmin.TopologySpreadConstraints
// Restart containers any time they stop, die, are killed, etc.
// - https://docs.k8s.io/concepts/workloads/pods/pod-lifecycle/#restart-policy
sts.Spec.Template.Spec.RestartPolicy = corev1.RestartPolicyAlways
// pgAdmin does not make any Kubernetes API calls. Use the default
// ServiceAccount and do not mount its credentials.
sts.Spec.Template.Spec.AutomountServiceAccountToken = initialize.Bool(false)
// Do not add environment variables describing services in this namespace.
sts.Spec.Template.Spec.EnableServiceLinks = initialize.Bool(false)
sts.Spec.Template.Spec.SecurityContext = postgres.PodSecurityContext(cluster)
// set the image pull secrets, if any exist
sts.Spec.Template.Spec.ImagePullSecrets = cluster.Spec.ImagePullSecrets
if err := errors.WithStack(r.setControllerReference(cluster, sts)); err != nil {
return err
}
pgadmin.Pod(cluster, configmap, &sts.Spec.Template.Spec, dataVolume)
// add nss_wrapper init container and add nss_wrapper env vars to the pgAdmin
// container
addNSSWrapper(
config.PGAdminContainerImage(cluster),
cluster.Spec.ImagePullPolicy,
&sts.Spec.Template)
// add an emptyDir volume to the PodTemplateSpec and an associated '/tmp'
// volume mount to all containers included within that spec
addTMPEmptyDir(&sts.Spec.Template)
return errors.WithStack(r.apply(ctx, sts))
}
// +kubebuilder:rbac:groups="",resources=persistentvolumeclaims,verbs=create;patch
// reconcilePGAdminDataVolume writes the PersistentVolumeClaim for instance's
// pgAdmin data volume.
func (r *Reconciler) reconcilePGAdminDataVolume(
ctx context.Context, cluster *v1beta1.PostgresCluster,
) (*corev1.PersistentVolumeClaim, error) {
labelMap := map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelRole: naming.RolePGAdmin,
naming.LabelData: naming.DataPGAdmin,
}
pvc := &corev1.PersistentVolumeClaim{ObjectMeta: naming.ClusterPGAdmin(cluster)}
pvc.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("PersistentVolumeClaim"))
if cluster.Spec.UserInterface == nil || cluster.Spec.UserInterface.PGAdmin == nil {
// pgAdmin is disabled; delete the PVC if it exists. Check the client
// cache first using Get.
key := client.ObjectKeyFromObject(pvc)
err := errors.WithStack(r.Client.Get(ctx, key, pvc))
if err == nil {
err = errors.WithStack(r.deleteControlled(ctx, cluster, pvc))
}
return nil, client.IgnoreNotFound(err)
}
pvc.Annotations = naming.Merge(
cluster.Spec.Metadata.GetAnnotationsOrNil(),
)
pvc.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
labelMap,
)
pvc.Spec = cluster.Spec.UserInterface.PGAdmin.DataVolumeClaimSpec
err := errors.WithStack(r.setControllerReference(cluster, pvc))
if err == nil {
err = r.handlePersistentVolumeClaimError(cluster,
errors.WithStack(r.apply(ctx, pvc)))
}
return pvc, err
}
// +kubebuilder:rbac:groups="",resources="pods",verbs={get}
// reconcilePGAdminUsers creates users inside of pgAdmin.
func (r *Reconciler) reconcilePGAdminUsers(
ctx context.Context, cluster *v1beta1.PostgresCluster,
specUsers []v1beta1.PostgresUserSpec, userSecrets map[string]*corev1.Secret,
) error {
const container = naming.ContainerPGAdmin
var podExecutor pgadmin.Executor
if cluster.Spec.UserInterface == nil || cluster.Spec.UserInterface.PGAdmin == nil {
// pgAdmin is disabled; clear its status.
// TODO(cbandy): Revisit this approach when there is more than one UI.
cluster.Status.UserInterface = nil
return nil
}
// Find the running pgAdmin container. When there is none, return early.
pod := &corev1.Pod{ObjectMeta: naming.ClusterPGAdmin(cluster)}
pod.Name += "-0"
err := errors.WithStack(r.Client.Get(ctx, client.ObjectKeyFromObject(pod), pod))
if err != nil {
return client.IgnoreNotFound(err)
}
var running bool
for _, status := range pod.Status.ContainerStatuses {
if status.Name == container {
running = status.State.Running != nil
}
}
if terminating := pod.DeletionTimestamp != nil; running && !terminating {
ctx = logging.NewContext(ctx, logging.FromContext(ctx).WithValues("pod", pod.Name))
podExecutor = func(
_ context.Context, stdin io.Reader, stdout, stderr io.Writer, command ...string,
) error {
return r.PodExec(pod.Namespace, pod.Name, container, stdin, stdout, stderr, command...)
}
}
if podExecutor == nil {
return nil
}
// Calculate a hash of the commands that should be executed in pgAdmin.
passwords := make(map[string]string, len(userSecrets))
for userName := range userSecrets {
passwords[userName] = string(userSecrets[userName].Data["password"])
}
write := func(ctx context.Context, exec pgadmin.Executor) error {
return pgadmin.WriteUsersInPGAdmin(ctx, cluster, exec, specUsers, passwords)
}
revision, err := safeHash32(func(hasher io.Writer) error {
// Discard log messages about executing.
return write(logging.NewContext(ctx, logging.Discard()), func(
_ context.Context, stdin io.Reader, _, _ io.Writer, command ...string,
) error {
_, err := fmt.Fprint(hasher, command)
if err == nil && stdin != nil {
_, err = io.Copy(hasher, stdin)
}
return err
})
})
if err == nil &&
cluster.Status.UserInterface != nil &&
cluster.Status.UserInterface.PGAdmin.UsersRevision == revision {
// The necessary commands have already been run; there's nothing more to do.
// TODO(cbandy): Give the user a way to trigger execution regardless.
// The value of an annotation could influence the hash, for example.
return nil
}
// Run the necessary commands and record their hash in cluster.Status.
// Include the hash in any log messages.
if err == nil {
log := logging.FromContext(ctx).WithValues("revision", revision)
err = errors.WithStack(write(logging.NewContext(ctx, log), podExecutor))
}
if err == nil {
if cluster.Status.UserInterface == nil {
cluster.Status.UserInterface = new(v1beta1.PostgresUserInterfaceStatus)
}
cluster.Status.UserInterface.PGAdmin.UsersRevision = revision
}
return err
}