Skip to content

Commit f1f755e

Browse files
committed
Merge remote-tracking branch 'origin/main' into use-create-or-update-pattern
2 parents d92a8fd + 923e271 commit f1f755e

File tree

10 files changed

+211
-154
lines changed

10 files changed

+211
-154
lines changed

Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ FROM gcr.io/distroless/static:nonroot
2424
WORKDIR /
2525
COPY --from=builder /workspace/manager .
2626
COPY external/svc-postgres-operator.yaml external/svc-postgres-operator.yaml
27-
COPY external/crd-postgresql.yaml external/crd-postgresql.yaml
2827
USER nonroot:nonroot
2928

3029
ENTRYPOINT ["/manager"]

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
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/go-logr/logr"
1414
zalando "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
15+
corev1 "k8s.io/api/core/v1"
1516
v1 "k8s.io/api/core/v1"
1617
"k8s.io/client-go/tools/record"
1718

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

121+
if err := r.deleteUserPasswordsSecret(ctx, instance); err != nil {
122+
return ctrl.Result{}, err
123+
}
124+
120125
instance.RemoveFinalizer(pg.PostgresFinalizerName)
121126
if err := r.CtrlClient.Update(ctx, instance); err != nil {
122127
r.recorder.Eventf(instance, "Warning", "Self-Reconcilation", "failed to remove finalizer: %v", err)
@@ -229,6 +234,19 @@ func (r *PostgresReconciler) createOrUpdateZalandoPostgresql(ctx context.Context
229234
return nil
230235
}
231236

237+
func (r *PostgresReconciler) deleteUserPasswordsSecret(ctx context.Context, instance *pg.Postgres) error {
238+
secret := &corev1.Secret{}
239+
secret.Namespace = instance.Namespace
240+
secret.Name = instance.ToUserPasswordsSecretName()
241+
if err := r.CtrlClient.Delete(ctx, secret); err != nil {
242+
msgWithFormat := "failed to delete user passwords secret: %w"
243+
r.recorder.Eventf(instance, "Warning", "Error", msgWithFormat, err)
244+
return fmt.Errorf(msgWithFormat, err)
245+
}
246+
247+
return nil
248+
}
249+
232250
func (r *PostgresReconciler) isManagedByUs(obj *pg.Postgres) bool {
233251
if obj.Spec.PartitionID != r.PartitionID {
234252
return false

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
"github.com/fi-ts/postgreslet/pkg/operatormanager"
1412
firewall "github.com/metal-stack/firewall-controller/api/v1"
@@ -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

@@ -88,6 +80,15 @@ var _ = Describe("postgres controller", func() {
8880
}, &firewall.ClusterwideNetworkPolicy{}) == nil
8981
}, timeout, interval).Should(BeTrue())
9082
})
83+
84+
It("should create user-passwords-secret in control-plane-cluster", func() {
85+
Eventually(func() bool {
86+
return ctrlClusterClient.Get(newCtx(), types.NamespacedName{
87+
Namespace: instance.Namespace,
88+
Name: instance.ToUserPasswordsSecretName(),
89+
}, &corev1.Secret{}) == nil
90+
}, timeout, interval).Should(BeTrue())
91+
})
9192
})
9293

9394
Context("postgres instance being deleted", func() {
@@ -116,5 +117,14 @@ var _ = Describe("postgres controller", func() {
116117
return svcClusterClient.Get(newCtx(), instance.ToPeripheralResourceLookupKey(), z) == nil
117118
}, timeout, interval).ShouldNot(BeTrue())
118119
})
120+
121+
It("should delete user-passwords-secret in control-plane-cluster", func() {
122+
Eventually(func() bool {
123+
return ctrlClusterClient.Get(newCtx(), types.NamespacedName{
124+
Namespace: instance.Namespace,
125+
Name: instance.ToUserPasswordsSecretName(),
126+
}, &corev1.Secret{}) == nil
127+
}, timeout, interval).ShouldNot(BeTrue())
128+
})
119129
})
120130
})

controllers/suite_test.go

Lines changed: 15 additions & 4 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
@@ -50,6 +57,8 @@ var (
5057
externalYAMLDir = filepath.Join("..", "external")
5158
externalYAMLDirTest = filepath.Join(externalYAMLDir, "test")
5259

60+
HelmCRDDir = filepath.Join("..", "charts", "postgreslet", "crds")
61+
5362
instance = &pg.Postgres{}
5463
)
5564

@@ -77,7 +86,7 @@ var _ = BeforeSuite(func(done Done) {
7786
svcClusterTestEnv = &envtest.Environment{
7887
CRDInstallOptions: envtest.CRDInstallOptions{
7988
Paths: []string{
80-
filepath.Join(externalYAMLDir, "crd-postgresql.yaml"),
89+
filepath.Join(HelmCRDDir, "postgresql.yaml"),
8190
filepath.Join(externalYAMLDirTest, "crd-clusterwidenetworkpolicy.yaml"),
8291
},
8392
},
@@ -174,10 +183,12 @@ func createCredentialSecrets() {
174183
s := &core.Secret{}
175184
Expect(yaml.Unmarshal(bytes, s)).Should(Succeed())
176185

177-
s.Namespace = instance.Namespace
186+
s.Namespace = instance.ToPeripheralResourceNamespace()
178187
s.Name = users[i] + "." + instance.Name + ".credentials"
179-
s.Labels = instance.ToUserPasswordSecretMatchingLabels()
180-
Expect(ctrlClusterClient.Create(newCtx(), s)).Should(Succeed())
188+
s.Labels = instance.ToZalandoPostgresqlMatchingLabels()
189+
Eventually(func() bool {
190+
return svcClusterClient.Create(newCtx(), s) == nil
191+
}, timeout, interval).Should(BeTrue())
181192
}
182193
}
183194

main.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424

2525
databasev1 "github.com/fi-ts/postgreslet/api/v1"
2626
"github.com/fi-ts/postgreslet/controllers"
27-
"github.com/fi-ts/postgreslet/pkg/crdinstaller"
2827
"github.com/fi-ts/postgreslet/pkg/lbmanager"
2928
"github.com/fi-ts/postgreslet/pkg/operatormanager"
3029
firewall "github.com/metal-stack/firewall-controller/api/v1"
@@ -91,16 +90,6 @@ func main() {
9190
)
9291

9392
svcClusterConf := ctrl.GetConfigOrDie()
94-
i, err := crdinstaller.New(svcClusterConf, scheme, ctrl.Log.WithName("CRDInstaller"))
95-
if err != nil {
96-
setupLog.Error(err, "unable to create `CRDInstaller`")
97-
os.Exit(1)
98-
}
99-
if err := i.Install("external/crd-postgresql.yaml"); err != nil {
100-
setupLog.Error(err, "unable to install CRD Postgresql")
101-
os.Exit(1)
102-
}
103-
10493
svcClusterMgr, err := ctrl.NewManager(svcClusterConf, ctrl.Options{
10594
Scheme: scheme,
10695
MetricsBindAddress: metricsAddrSvcMgr,

pkg/crdinstaller/crdinstaller.go

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)