-
Notifications
You must be signed in to change notification settings - Fork 90
/
postgres.go
84 lines (68 loc) · 2.7 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
package kotsadm
import (
"bytes"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/kotsadm/types"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
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)
var statefulset bytes.Buffer
if deployOptions.PostgresPassword == "" {
deployOptions.PostgresPassword = uuid.New().String()
}
if err := s.Encode(postgresStatefulset(deployOptions), &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(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 err := ensurePostgresStatefulset(deployOptions, clientset); 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) error {
_, err := clientset.AppsV1().StatefulSets(deployOptions.Namespace).Get("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(postgresStatefulset(deployOptions))
if err != nil {
return errors.Wrap(err, "failed to create postgres statefulset")
}
}
return nil
}
func ensurePostgresService(namespace string, clientset *kubernetes.Clientset) error {
_, err := clientset.CoreV1().Services(namespace).Get("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(postgresService(namespace))
if err != nil {
return errors.Wrap(err, "Failed to create service")
}
}
return nil
}