-
Notifications
You must be signed in to change notification settings - Fork 18
/
postgresql_controller.go
102 lines (82 loc) · 2.7 KB
/
postgresql_controller.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
// Copyright (c) 2022 Aiven, Helsinki, Finland. https://aiven.io/
package controllers
import (
"context"
"fmt"
"github.com/aiven/aiven-go-client"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/aiven/aiven-operator/api/v1alpha1"
)
// PostgreSQLReconciler reconciles a PostgreSQL object
type PostgreSQLReconciler struct {
Controller
}
//+kubebuilder:rbac:groups=aiven.io,resources=postgresqls,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=aiven.io,resources=postgresqls/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=aiven.io,resources=postgresqls/finalizers,verbs=update
func (r *PostgreSQLReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
return r.reconcileInstance(ctx, req, newGenericServiceHandler(newPostgresSQLAdapter), &v1alpha1.PostgreSQL{})
}
func (r *PostgreSQLReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&v1alpha1.PostgreSQL{}).
Owns(&corev1.Secret{}).
Complete(r)
}
func newPostgresSQLAdapter(_ *aiven.Client, object client.Object) (serviceAdapter, error) {
pg, ok := object.(*v1alpha1.PostgreSQL)
if !ok {
return nil, fmt.Errorf("object is not of type v1alpha1.PostgresSQL")
}
return &postgresSQLAdapter{pg}, nil
}
// postgresSQLAdapter handles an Aiven PostgresSQL service
type postgresSQLAdapter struct {
*v1alpha1.PostgreSQL
}
func (a *postgresSQLAdapter) getObjectMeta() *metav1.ObjectMeta {
return &a.ObjectMeta
}
func (a *postgresSQLAdapter) getServiceStatus() *v1alpha1.ServiceStatus {
return &a.Status
}
func (a *postgresSQLAdapter) getServiceCommonSpec() *v1alpha1.ServiceCommonSpec {
return &a.Spec.ServiceCommonSpec
}
func (a *postgresSQLAdapter) getUserConfig() any {
return &a.Spec.UserConfig
}
func (a *postgresSQLAdapter) newSecret(s *aiven.Service) (*corev1.Secret, error) {
name := a.Spec.ConnInfoSecretTarget.Name
if name == "" {
name = a.Name
}
stringData := map[string]string{
"PGHOST": s.URIParams["host"],
"PGPORT": s.URIParams["port"],
"PGDATABASE": s.URIParams["dbname"],
"PGUSER": s.URIParams["user"],
"PGPASSWORD": s.URIParams["password"],
"PGSSLMODE": s.URIParams["sslmode"],
"DATABASE_URI": s.URI,
}
// Removes empties
for k, v := range stringData {
if v == "" {
delete(stringData, k)
}
}
return &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: a.Namespace},
StringData: stringData,
}, nil
}
func (a *postgresSQLAdapter) getServiceType() string {
return "pg"
}
func (a *postgresSQLAdapter) getDiskSpace() string {
return a.Spec.DiskSpace
}