Skip to content

Commit 923e271

Browse files
authored
Add secret deletion and tests (#192)
1 parent 6de14b2 commit 923e271

File tree

4 files changed

+48
-18
lines changed

4 files changed

+48
-18
lines changed

api/v1/postgres_types.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"k8s.io/apimachinery/pkg/util/intstr"
2525
"k8s.io/apimachinery/pkg/util/yaml"
2626
"sigs.k8s.io/controller-runtime/pkg/client"
27-
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2827
)
2928

3029
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
@@ -317,7 +316,6 @@ func (p *Postgres) ToPeripheralResourceName() string {
317316
func (p *Postgres) ToUserPasswordsSecret(src *corev1.SecretList, scheme *runtime.Scheme) (*corev1.Secret, error) {
318317
secret := &corev1.Secret{}
319318
secret.Namespace = p.Namespace
320-
// todo: Consider `p.Name + "-passwords", so the`
321319
secret.Name = p.ToUserPasswordsSecretName()
322320
secret.Type = corev1.SecretTypeOpaque
323321
secret.Data = map[string][]byte{}
@@ -327,11 +325,6 @@ func (p *Postgres) ToUserPasswordsSecret(src *corev1.SecretList, scheme *runtime
327325
secret.Data[string(v.Data["username"])] = v.Data["password"]
328326
}
329327

330-
// Set the owner of the secret
331-
if err := controllerutil.SetControllerReference(p, secret, scheme); err != nil {
332-
return nil, err
333-
}
334-
335328
return secret, nil
336329
}
337330

controllers/postgres_controller.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/go-logr/logr"
1616
zalando "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
17+
corev1 "k8s.io/api/core/v1"
1718
v1 "k8s.io/api/core/v1"
1819
"k8s.io/client-go/tools/record"
1920

@@ -119,6 +120,10 @@ func (r *PostgresReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
119120
return ctrl.Result{}, fmt.Errorf("error while uninstalling operator: %w", err)
120121
}
121122

123+
if err := r.deleteUserPasswordsSecret(ctx, instance); err != nil {
124+
return ctrl.Result{}, err
125+
}
126+
122127
instance.RemoveFinalizer(pg.PostgresFinalizerName)
123128
if err := r.CtrlClient.Update(ctx, instance); err != nil {
124129
r.recorder.Eventf(instance, "Warning", "Self-Reconcilation", "failed to remove finalizer: %v", err)
@@ -232,6 +237,19 @@ func (r *PostgresReconciler) createOrUpdateZalandoPostgresql(ctx context.Context
232237
return nil
233238
}
234239

240+
func (r *PostgresReconciler) deleteUserPasswordsSecret(ctx context.Context, instance *pg.Postgres) error {
241+
secret := &corev1.Secret{}
242+
secret.Namespace = instance.Namespace
243+
secret.Name = instance.ToUserPasswordsSecretName()
244+
if err := r.CtrlClient.Delete(ctx, secret); err != nil {
245+
msgWithFormat := "failed to delete user passwords secret: %w"
246+
r.recorder.Eventf(instance, "Warning", "Error", msgWithFormat, err)
247+
return fmt.Errorf(msgWithFormat, err)
248+
}
249+
250+
return nil
251+
}
252+
235253
// ensureZalandoDependencies makes sure Zalando resources are installed in the service-cluster.
236254
func (r *PostgresReconciler) ensureZalandoDependencies(ctx context.Context, p *pg.Postgres) error {
237255
namespace := p.ToPeripheralResourceNamespace()

controllers/postgres_controller_test.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
package controllers
88

99
import (
10-
"time"
11-
1210
pg "github.com/fi-ts/postgreslet/api/v1"
1311
firewall "github.com/metal-stack/firewall-controller/api/v1"
1412
. "github.com/onsi/ginkgo"
@@ -20,12 +18,6 @@ import (
2018
)
2119

2220
var _ = Describe("postgres controller", func() {
23-
const (
24-
// duration = time.Second * 10
25-
interval = time.Second * 2
26-
timeout = time.Second * 30
27-
)
28-
2921
BeforeEach(func() {})
3022
AfterEach(func() {})
3123

@@ -79,6 +71,15 @@ var _ = Describe("postgres controller", func() {
7971
}, &firewall.ClusterwideNetworkPolicy{}) == nil
8072
}, timeout, interval).Should(BeTrue())
8173
})
74+
75+
It("should create user-passwords-secret in control-plane-cluster", func() {
76+
Eventually(func() bool {
77+
return ctrlClusterClient.Get(newCtx(), types.NamespacedName{
78+
Namespace: instance.Namespace,
79+
Name: instance.ToUserPasswordsSecretName(),
80+
}, &corev1.Secret{}) == nil
81+
}, timeout, interval).Should(BeTrue())
82+
})
8283
})
8384

8485
Context("postgres instance being deleted", func() {
@@ -107,5 +108,14 @@ var _ = Describe("postgres controller", func() {
107108
return svcClusterClient.Get(newCtx(), instance.ToPeripheralResourceLookupKey(), z) == nil
108109
}, timeout, interval).ShouldNot(BeTrue())
109110
})
111+
112+
It("should delete user-passwords-secret in control-plane-cluster", func() {
113+
Eventually(func() bool {
114+
return ctrlClusterClient.Get(newCtx(), types.NamespacedName{
115+
Namespace: instance.Namespace,
116+
Name: instance.ToUserPasswordsSecretName(),
117+
}, &corev1.Secret{}) == nil
118+
}, timeout, interval).ShouldNot(BeTrue())
119+
})
110120
})
111121
})

controllers/suite_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212
"path/filepath"
1313
"testing"
14+
"time"
1415

1516
. "github.com/onsi/ginkgo"
1617
. "github.com/onsi/gomega"
@@ -38,6 +39,12 @@ import (
3839
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
3940
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
4041

42+
const (
43+
// duration = time.Second * 10
44+
interval = time.Second * 2
45+
timeout = time.Second * 30
46+
)
47+
4148
var (
4249
ctrlClusterCfg *rest.Config
4350
ctrlClusterClient client.Client
@@ -161,10 +168,12 @@ func createCredentialSecrets() {
161168
s := &core.Secret{}
162169
Expect(yaml.Unmarshal(bytes, s)).Should(Succeed())
163170

164-
s.Namespace = instance.Namespace
171+
s.Namespace = instance.ToPeripheralResourceNamespace()
165172
s.Name = users[i] + "." + instance.Name + ".credentials"
166-
s.Labels = instance.ToUserPasswordSecretMatchingLabels()
167-
Expect(ctrlClusterClient.Create(newCtx(), s)).Should(Succeed())
173+
s.Labels = instance.ToZalandoPostgresqlMatchingLabels()
174+
Eventually(func() bool {
175+
return svcClusterClient.Create(newCtx(), s) == nil
176+
}, timeout, interval).Should(BeTrue())
168177
}
169178
}
170179

0 commit comments

Comments
 (0)