-
Notifications
You must be signed in to change notification settings - Fork 90
/
postgres.go
161 lines (131 loc) · 5.61 KB
/
postgres.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
package kotsadm
import (
"bytes"
"context"
"time"
"github.com/google/uuid"
"github.com/pkg/errors"
kotsadmobjects "github.com/replicatedhq/kots/pkg/kotsadm/objects"
"github.com/replicatedhq/kots/pkg/kotsadm/types"
"github.com/replicatedhq/kots/pkg/logger"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
)
func getPostgresYAML(deployOptions types.DeployOptions) (map[string][]byte, error) {
docs := map[string][]byte{}
s := json.NewYAMLSerializer(json.DefaultMetaFactory, scheme.Scheme, scheme.Scheme)
if deployOptions.PostgresPassword == "" {
deployOptions.PostgresPassword = uuid.New().String()
}
if !deployOptions.IsOpenShift {
var configmap bytes.Buffer
if err := s.Encode(kotsadmobjects.PostgresConfigMap(deployOptions), &configmap); err != nil {
return nil, errors.Wrap(err, "failed to marshal postgres configmap")
}
docs["postgres-configmap.yaml"] = configmap.Bytes()
}
size, err := getSize(deployOptions, "postgres", resource.MustParse("1Gi"))
if err != nil {
return nil, errors.Wrap(err, "failed to get size")
}
postgresSts, err := kotsadmobjects.PostgresStatefulset(deployOptions, size)
if err != nil {
return nil, errors.Wrap(err, "failed to get postgres statefulset definition")
}
var statefulset bytes.Buffer
if err := s.Encode(postgresSts, &statefulset); err != nil {
return nil, errors.Wrap(err, "failed to marshal postgres statefulset")
}
docs["postgres-statefulset.yaml"] = statefulset.Bytes()
var service bytes.Buffer
if err := s.Encode(kotsadmobjects.PostgresService(deployOptions.Namespace), &service); err != nil {
return nil, errors.Wrap(err, "failed to marshal postgres service")
}
docs["postgres-service.yaml"] = service.Bytes()
return docs, nil
}
func ensurePostgres(deployOptions types.DeployOptions, clientset *kubernetes.Clientset) error {
if err := ensurePostgresSecret(deployOptions, clientset); err != nil {
return errors.Wrap(err, "failed to ensure postgres secret")
}
if !deployOptions.IsOpenShift {
if err := ensurePostgresConfigMap(deployOptions, clientset); err != nil {
return errors.Wrap(err, "failed to ensure postgres configmap")
}
}
size, err := getSize(deployOptions, "postgres", resource.MustParse("1Gi"))
if err != nil {
return errors.Wrap(err, "failed to get size")
}
if err := ensurePostgresStatefulset(deployOptions, clientset, size); err != nil {
return errors.Wrap(err, "failed to ensure postgres statefulset")
}
if err := ensurePostgresService(deployOptions.Namespace, clientset); err != nil {
return errors.Wrap(err, "failed to ensure postgres service")
}
return nil
}
func ensurePostgresStatefulset(deployOptions types.DeployOptions, clientset *kubernetes.Clientset, size resource.Quantity) error {
desiredPostgres, err := kotsadmobjects.PostgresStatefulset(deployOptions, size)
if err != nil {
return errors.Wrap(err, "failed to get desired postgres statefulset definition")
}
ctx := context.TODO()
existingPostgres, err := clientset.AppsV1().StatefulSets(deployOptions.Namespace).Get(ctx, "kotsadm-postgres", metav1.GetOptions{})
if err != nil {
if !kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "failed to get existing statefulset")
}
_, err := clientset.AppsV1().StatefulSets(deployOptions.Namespace).Create(ctx, desiredPostgres, metav1.CreateOptions{})
if err != nil {
return errors.Wrap(err, "failed to create postgres statefulset")
}
return nil
}
if len(existingPostgres.Spec.Template.Spec.Containers) != 1 || len(desiredPostgres.Spec.Template.Spec.Containers) != 1 {
return errors.New("postgres stateful set cannot be upgraded")
}
existingPostgres.Spec.Template.Spec.Volumes = desiredPostgres.Spec.Template.Spec.DeepCopy().Volumes
existingPostgres.Spec.Template.Spec.Containers[0].Image = desiredPostgres.Spec.Template.Spec.Containers[0].Image
existingPostgres.Spec.Template.Spec.Containers[0].VolumeMounts = desiredPostgres.Spec.Template.Spec.Containers[0].DeepCopy().VolumeMounts
_, err = clientset.AppsV1().StatefulSets(deployOptions.Namespace).Update(ctx, existingPostgres, metav1.UpdateOptions{})
if err != nil {
return errors.Wrap(err, "failed to update postgres statefulset")
}
return nil
}
func ensurePostgresService(namespace string, clientset *kubernetes.Clientset) error {
_, err := clientset.CoreV1().Services(namespace).Get(context.TODO(), "kotsadm-postgres", metav1.GetOptions{})
if err != nil {
if !kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "failed to get existing service")
}
_, err := clientset.CoreV1().Services(namespace).Create(context.TODO(), kotsadmobjects.PostgresService(namespace), metav1.CreateOptions{})
if err != nil {
return errors.Wrap(err, "Failed to create service")
}
}
return nil
}
func waitForHealthyStatefulSet(name string, deployOptions types.DeployOptions, clientset *kubernetes.Clientset, log *logger.CLILogger) error {
log.ChildActionWithSpinner("Waiting for datastore to be ready")
defer log.FinishChildSpinner()
start := time.Now()
for {
s, err := clientset.AppsV1().StatefulSets(deployOptions.Namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
return errors.Wrap(err, "failed to list pods")
}
if s.Status.ReadyReplicas == *s.Spec.Replicas && s.Status.UpdateRevision == s.Status.CurrentRevision {
return nil
}
time.Sleep(time.Second)
if time.Now().Sub(start) > time.Duration(deployOptions.Timeout) {
return &types.ErrorTimeout{Message: "timeout waiting for postgres pod"}
}
}
}