Skip to content

Commit 1bc266e

Browse files
committed
Use createOrUpdate pattern
1 parent 5fc4fdb commit 1bc266e

File tree

4 files changed

+160
-152
lines changed

4 files changed

+160
-152
lines changed

controllers/postgres_controller.go

Lines changed: 4 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ package controllers
88

99
import (
1010
"context"
11-
"encoding/json"
1211
"fmt"
13-
"net/url"
1412

1513
"github.com/go-logr/logr"
1614
zalando "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
@@ -74,6 +72,8 @@ func (r *PostgresReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
7472
return ctrl.Result{}, nil
7573
}
7674

75+
namespace := instance.ToPeripheralResourceNamespace()
76+
7777
// Delete
7878
if instance.IsBeingDeleted() {
7979
instance.Status.Description = "Terminating"
@@ -84,7 +84,6 @@ func (r *PostgresReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
8484
log.Info("instance being deleted")
8585

8686
matchingLabels := instance.ToZalandoPostgresqlMatchingLabels()
87-
namespace := instance.ToPeripheralResourceNamespace()
8887

8988
if err := r.deleteCWNP(ctx, instance); client.IgnoreNotFound(err) != nil { // todo: remove ignorenotfound
9089
r.recorder.Event(instance, "Warning", "Error", "failed to delete ClusterwideNetworkPolicy")
@@ -234,112 +233,8 @@ func (r *PostgresReconciler) createOrUpdateZalandoPostgresql(ctx context.Context
234233

235234
// ensureZalandoDependencies makes sure Zalando resources are installed in the service-cluster.
236235
func (r *PostgresReconciler) ensureZalandoDependencies(ctx context.Context, p *pg.Postgres) error {
237-
namespace := p.ToPeripheralResourceNamespace()
238-
isInstalled, err := r.IsOperatorInstalled(ctx, namespace)
239-
if err != nil {
240-
return fmt.Errorf("error while querying if zalando dependencies are installed: %w", err)
241-
}
242-
243-
if !isInstalled {
244-
if err := r.InstallOrUpdateOperator(ctx, namespace); err != nil {
245-
return fmt.Errorf("error while installing zalando dependencies: %w", err)
246-
}
247-
}
248-
249-
if err := r.updatePodEnvironmentConfigMap(ctx, p); err != nil {
250-
return fmt.Errorf("error while updating backup config: %w", err)
251-
}
252-
253-
return nil
254-
}
255-
256-
func (r *PostgresReconciler) updatePodEnvironmentConfigMap(ctx context.Context, p *pg.Postgres) error {
257-
log := r.Log.WithValues("postgres", p.UID)
258-
if p.Spec.BackupSecretRef == "" {
259-
log.Info("No configured backupSecretRef found, skipping configuration of postgres backup")
260-
return nil
261-
}
262-
263-
// fetch secret
264-
backupSecret := &v1.Secret{}
265-
backupNamespace := types.NamespacedName{
266-
Name: p.Spec.BackupSecretRef,
267-
Namespace: p.Namespace,
268-
}
269-
if err := r.CtrlClient.Get(ctx, backupNamespace, backupSecret); err != nil {
270-
return fmt.Errorf("error while getting the backup secret from control plane cluster: %w", err)
271-
}
272-
273-
backupConfigJSON, ok := backupSecret.Data[pg.BackupConfigKey]
274-
if !ok {
275-
return fmt.Errorf("no backupConfig stored in the secret")
276-
}
277-
var backupConfig pg.BackupConfig
278-
err := json.Unmarshal(backupConfigJSON, &backupConfig)
279-
if err != nil {
280-
return fmt.Errorf("unable to unmarshal backupconfig:%w", err)
281-
}
282-
283-
s3url, err := url.Parse(backupConfig.S3Endpoint)
284-
if err != nil {
285-
return fmt.Errorf("error while parsing the s3 endpoint url in the backup secret: %w", err)
286-
}
287-
// use the s3 endpoint as provided
288-
awsEndpoint := s3url.String()
289-
// modify the scheme to 'https+path'
290-
s3url.Scheme = "https+path"
291-
// use the modified s3 endpoint
292-
walES3Endpoint := s3url.String()
293-
// region
294-
region := backupConfig.S3Region
295-
296-
// use the rest as provided in the secret
297-
bucketName := backupConfig.S3BucketName
298-
awsAccessKeyID := backupConfig.S3AccessKey
299-
awsSecretAccessKey := backupConfig.S3SecretKey
300-
backupSchedule := backupConfig.Schedule
301-
backupNumToRetain := backupConfig.Retention
302-
303-
// s3 server side encryption SSE is enabled if the key is given
304-
// TODO our s3 needs a small change to make this work
305-
walgDisableSSE := "true"
306-
walgSSE := ""
307-
if backupConfig.S3EncryptionKey != nil {
308-
walgDisableSSE = "false"
309-
walgSSE = *backupConfig.S3EncryptionKey
310-
}
311-
312-
// create updated content for pod environment configmap
313-
data := map[string]string{
314-
"USE_WALG_BACKUP": "true",
315-
"USE_WALG_RESTORE": "true",
316-
"WALE_S3_PREFIX": "s3://" + bucketName + "/$(SCOPE)",
317-
"WALG_S3_PREFIX": "s3://" + bucketName + "/$(SCOPE)",
318-
"CLONE_WALG_S3_PREFIX": "s3://" + bucketName + "/$(CLONE_SCOPE)",
319-
"WALE_BACKUP_THRESHOLD_PERCENTAGE": "100",
320-
"AWS_ENDPOINT": awsEndpoint,
321-
"WALE_S3_ENDPOINT": walES3Endpoint, // same as above, but slightly modified
322-
"AWS_ACCESS_KEY_ID": awsAccessKeyID,
323-
"AWS_SECRET_ACCESS_KEY": awsSecretAccessKey,
324-
"AWS_S3_FORCE_PATH_STYLE": "true",
325-
"AWS_REGION": region, // now we can use AWS S3
326-
"WALG_DISABLE_S3_SSE": walgDisableSSE, // disable server side encryption if key is nil
327-
"WALG_S3_SSE": walgSSE, // server side encryption key
328-
"BACKUP_SCHEDULE": backupSchedule,
329-
"BACKUP_NUM_TO_RETAIN": backupNumToRetain,
330-
}
331-
332-
cm := &v1.ConfigMap{}
333-
ns := types.NamespacedName{
334-
Name: operatormanager.PodEnvCMName,
335-
Namespace: p.ToPeripheralResourceNamespace(),
336-
}
337-
if err := r.SvcClient.Get(ctx, ns, cm); err != nil {
338-
return fmt.Errorf("error while getting the pod environment configmap from service cluster: %w", err)
339-
}
340-
cm.Data = data
341-
if err := r.SvcClient.Update(ctx, cm); err != nil {
342-
return fmt.Errorf("error while updating the pod environment configmap in service cluster: %w", err)
236+
if err := r.InstallOrUpdateOperator(ctx, p); err != nil {
237+
return fmt.Errorf("error while installing zalando dependencies: %w", err)
343238
}
344239

345240
return nil

controllers/suite_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ var _ = BeforeSuite(func(done Done) {
100100

101101
// Todo: OperatorManager should be a reconciler
102102
opMgr, err := operatormanager.New(
103+
ctrlClusterMgr.GetClient(),
103104
svcClusterCfg,
104105
filepath.Join(externalYAMLDir, "svc-postgres-operator.yaml"),
105106
scheme,

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func main() {
130130
os.Exit(1)
131131
}
132132

133-
opMgr, err := operatormanager.New(svcClusterConf, "external/svc-postgres-operator.yaml", scheme, ctrl.Log.WithName("OperatorManager"), pspName)
133+
opMgr, err := operatormanager.New(ctrlPlaneClusterMgr.GetClient(), svcClusterConf, "external/svc-postgres-operator.yaml", scheme, ctrl.Log.WithName("OperatorManager"), pspName)
134134
if err != nil {
135135
setupLog.Error(err, "unable to create `OperatorManager`")
136136
os.Exit(1)
@@ -165,7 +165,7 @@ func main() {
165165
ctx := context.Background()
166166

167167
// update all existing operators to the current version
168-
if err := opMgr.UpdateAllOperators(ctx); err != nil {
168+
if err := opMgr.UpdateAllZalandoOperators(ctx); err != nil {
169169
setupLog.Error(err, "error updating the postgres operators")
170170
}
171171

0 commit comments

Comments
 (0)