-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.go
144 lines (122 loc) · 3.63 KB
/
utils.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
package utils
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"github.com/easymile/postgresql-operator/api/postgresql/common"
postgresqlv1alpha1 "github.com/easymile/postgresql-operator/api/postgresql/v1alpha1"
"github.com/easymile/postgresql-operator/internal/controller/postgresql/postgres"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func CalculateHash(spec interface{}) (string, error) {
// Json marshal spec
bytes, err := json.Marshal(spec)
if err != nil {
return "", err
}
// Sha on bytes array
sha256Res := sha256.Sum256(bytes)
sha256Bytes := sha256Res[:]
// Transform it to string
return hex.EncodeToString(sha256Bytes), nil
}
func CreatePgInstance(
reqLogger logr.Logger,
secretData map[string][]byte,
pgec *postgresqlv1alpha1.PostgresqlEngineConfiguration,
) postgres.PG {
spec := pgec.Spec
user := string(secretData["user"])
password := string(secretData["password"])
return postgres.NewPG(
CreateNameKeyForSavedPools(pgec.Name, pgec.Namespace),
spec.Host,
user,
password,
spec.URIArgs,
spec.DefaultDatabase,
spec.Port,
spec.Provider,
reqLogger,
)
}
func GetSecret(ctx context.Context, cl client.Client, name, namespace string) (*corev1.Secret, error) {
secret := &corev1.Secret{}
err := cl.Get(ctx, types.NamespacedName{Name: name, Namespace: namespace}, secret)
return secret, err
}
func FindSecretPgEngineCfg(
ctx context.Context,
cl client.Client,
instance *postgresqlv1alpha1.PostgresqlEngineConfiguration,
) (*corev1.Secret, error) {
secret := &corev1.Secret{}
err := cl.Get(ctx, types.NamespacedName{Name: instance.Spec.SecretName, Namespace: instance.Namespace}, secret)
return secret, err
}
func CloseDatabaseSavedPoolsForName(instance *postgresqlv1alpha1.PostgresqlDatabase, database string) error {
// Try to get namespace from spec
namespace := instance.Spec.EngineConfiguration.Namespace
if namespace == "" {
// Namespace not found, take it from instance namespace
namespace = instance.Namespace
}
return postgres.CloseDatabaseSavedPoolsForName(
CreateNameKeyForSavedPools(instance.Spec.EngineConfiguration.Name, namespace),
database,
)
}
func CreateNameKey(name, namespace, instanceNamespace string) string {
res := ""
if namespace != "" {
res += namespace
} else {
res += instanceNamespace
}
res += "/" + name
return res
}
func CreateNameKeyForSavedPools(pgecName, pgecNamespace string) string {
return pgecNamespace + "/" + pgecName
}
func FindPgEngineCfg(
ctx context.Context,
cl client.Client,
instance *postgresqlv1alpha1.PostgresqlDatabase,
) (*postgresqlv1alpha1.PostgresqlEngineConfiguration, error) {
// Try to get namespace from spec
namespace := instance.Spec.EngineConfiguration.Namespace
if namespace == "" {
// Namespace not found, take it from instance namespace
namespace = instance.Namespace
}
pgEngineCfg := &postgresqlv1alpha1.PostgresqlEngineConfiguration{}
err := cl.Get(ctx, client.ObjectKey{
Name: instance.Spec.EngineConfiguration.Name,
Namespace: namespace,
}, pgEngineCfg)
return pgEngineCfg, err
}
func FindPgDatabaseFromLink(
ctx context.Context,
cl client.Client,
link *common.CRLink,
instanceNamespace string,
) (*postgresqlv1alpha1.PostgresqlDatabase, error) {
// Try to get namespace from spec
namespace := link.Namespace
if namespace == "" {
// Namespace not found, take it from instance namespace
namespace = instanceNamespace
}
pgDatabase := &postgresqlv1alpha1.PostgresqlDatabase{}
err := cl.Get(ctx, client.ObjectKey{
Name: link.Name,
Namespace: namespace,
}, pgDatabase)
return pgDatabase, err
}