-
Notifications
You must be signed in to change notification settings - Fork 20
/
postgresql_controller.go
162 lines (136 loc) · 5.1 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
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
// Copyright (c) 2024 Aiven, Helsinki, Finland. https://aiven.io/
package controllers
import (
"context"
"fmt"
"time"
"github.com/aiven/aiven-go-client/v2"
avngen "github.com/aiven/go-client-codegen"
"github.com/aiven/go-client-codegen/handler/service"
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"
pguserconfig "github.com/aiven/aiven-operator/api/v1alpha1/userconfig/service/pg"
)
// PostgreSQLReconciler reconciles a PostgreSQL object
type PostgreSQLReconciler struct {
Controller
}
func newPostgreSQLReconciler(c Controller) reconcilerType {
return &PostgreSQLReconciler{Controller: c}
}
const waitForTaskToCompleteInterval = time.Second * 10
//+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=get;create;update
func (r *PostgreSQLReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
return r.reconcileInstance(ctx, req, newGenericServiceHandler(newPostgreSQLAdapter), &v1alpha1.PostgreSQL{})
}
func (r *PostgreSQLReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&v1alpha1.PostgreSQL{}).
Owns(&corev1.Secret{}).
Complete(r)
}
func newPostgreSQLAdapter(_ *aiven.Client, object client.Object) (serviceAdapter, error) {
pg, ok := object.(*v1alpha1.PostgreSQL)
if !ok {
return nil, fmt.Errorf("object is not of type v1alpha1.PostgreSQL")
}
return &postgreSQLAdapter{pg}, nil
}
// postgreSQLAdapter handles an Aiven PostgreSQL service
type postgreSQLAdapter struct {
*v1alpha1.PostgreSQL
}
func (a *postgreSQLAdapter) getObjectMeta() *metav1.ObjectMeta {
return &a.ObjectMeta
}
func (a *postgreSQLAdapter) getServiceStatus() *v1alpha1.ServiceStatus {
return &a.Status
}
func (a *postgreSQLAdapter) getServiceCommonSpec() *v1alpha1.ServiceCommonSpec {
return &a.Spec.ServiceCommonSpec
}
func (a *postgreSQLAdapter) getUserConfig() any {
return a.Spec.UserConfig
}
func (a *postgreSQLAdapter) newSecret(ctx context.Context, s *service.ServiceGetOut) (*corev1.Secret, error) {
prefix := getSecretPrefix(a)
stringData := map[string]string{
prefix + "HOST": s.ServiceUriParams["host"],
prefix + "PORT": s.ServiceUriParams["port"],
prefix + "DATABASE": s.ServiceUriParams["dbname"],
prefix + "USER": s.ServiceUriParams["user"],
prefix + "PASSWORD": s.ServiceUriParams["password"],
prefix + "SSLMODE": s.ServiceUriParams["sslmode"],
prefix + "DATABASE_URI": s.ServiceUri,
// todo: remove in future releases
"PGHOST": s.ServiceUriParams["host"],
"PGPORT": s.ServiceUriParams["port"],
"PGDATABASE": s.ServiceUriParams["dbname"],
"PGUSER": s.ServiceUriParams["user"],
"PGPASSWORD": s.ServiceUriParams["password"],
"PGSSLMODE": s.ServiceUriParams["sslmode"],
"DATABASE_URI": s.ServiceUri,
}
return newSecret(a, stringData, false), nil
}
func (a *postgreSQLAdapter) getServiceType() string {
return "pg"
}
func (a *postgreSQLAdapter) getDiskSpace() string {
return a.Spec.DiskSpace
}
func (a *postgreSQLAdapter) performUpgradeTaskIfNeeded(ctx context.Context, avn avngen.Client, old *service.ServiceGetOut) error {
var currentVersion string = old.UserConfig["pg_version"].(string)
targetUserConfig := a.getUserConfig().(*pguserconfig.PgUserConfig)
if targetUserConfig == nil || targetUserConfig.PgVersion == nil {
return nil
}
var targetVersion string = *targetUserConfig.PgVersion
// No need to upgrade if pg_version hasn't changed
if targetVersion == currentVersion {
return nil
}
task, err := avn.ServiceTaskCreate(ctx, a.getServiceCommonSpec().Project, a.getObjectMeta().Name, &service.ServiceTaskCreateIn{
TargetVersion: service.TargetVersionType(targetVersion),
TaskType: service.TaskTypeUpgradeCheck,
})
if err != nil {
return fmt.Errorf("cannot create PG upgrade check task: %q", err)
}
finalTaskResult, err := waitForTaskToComplete(ctx, func() (bool, *service.ServiceTaskGetOut, error) {
t, getErr := avn.ServiceTaskGet(ctx, a.getServiceCommonSpec().Project, a.getObjectMeta().Name, task.TaskId)
if getErr != nil {
return true, nil, fmt.Errorf("error fetching service task %s: %q", t.TaskId, getErr)
}
if !t.Success {
return false, nil, nil
}
return true, t, nil
})
if err != nil {
return err
}
if !finalTaskResult.Success {
return fmt.Errorf("PG service upgrade check error, version upgrade from %s to %s, result: %s", currentVersion, targetVersion, finalTaskResult.Result)
}
return nil
}
func waitForTaskToComplete[T any](ctx context.Context, f func() (bool, *T, error)) (*T, error) {
var err error
for {
select {
case <-ctx.Done():
return nil, fmt.Errorf("context timeout while retrying operation, error=%q", err)
case <-time.After(waitForTaskToCompleteInterval):
finished, val, err := f()
if finished {
return val, err
}
}
}
}