Skip to content

Commit 1acace9

Browse files
committed
Simplify logging
1 parent c9b2ac5 commit 1acace9

16 files changed

+216
-223
lines changed

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/main.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
1616
ctrl "sigs.k8s.io/controller-runtime"
1717
"sigs.k8s.io/controller-runtime/pkg/healthz"
18+
logf "sigs.k8s.io/controller-runtime/pkg/log"
1819
"sigs.k8s.io/controller-runtime/pkg/log/zap"
1920
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
2021
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
@@ -28,10 +29,7 @@ import (
2829
// +kubebuilder:scaffold:imports
2930
)
3031

31-
var (
32-
scheme = runtime.NewScheme()
33-
setupLog = ctrl.Log.WithName("setup")
34-
)
32+
var scheme = runtime.NewScheme()
3533

3634
func init() {
3735
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
@@ -63,16 +61,16 @@ func main() {
6361
opts.BindFlags(flag.CommandLine)
6462
flag.Parse()
6563

66-
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
67-
64+
logger := zap.New(zap.UseFlagOptions(&opts))
65+
logf.SetLogger(logger)
6866
// if the enable-http2 flag is false (the default), http/2 should be disabled
6967
// due to its vulnerabilities. More specifically, disabling http/2 will
7068
// prevent from being vulnerable to the HTTP/2 Stream Cancellation and
7169
// Rapid Reset CVEs. For more information see:
7270
// - https://github.com/advisories/GHSA-qppj-fm5r-hxr3
7371
// - https://github.com/advisories/GHSA-4374-p667-p6c8
7472
disableHTTP2 := func(c *tls.Config) {
75-
setupLog.Info("disabling http/2")
73+
logger.Info("disabling http/2")
7674
c.NextProtos = []string{"http/1.1"}
7775
}
7876

@@ -111,9 +109,9 @@ func main() {
111109
cfg := config.Get()
112110
lockName := "lock"
113111
if cfg.AnnotationFilter == "" {
114-
setupLog.Info("No POSTGRES_INSTANCE set, this instance will only process CRs without an annotation")
112+
logger.Info("No POSTGRES_INSTANCE set, this instance will only process CRs without an annotation")
115113
} else {
116-
setupLog.Info("POSTGRES_INSTANCE is set, this instance will only process CRs with the correct annotation", "annotation", cfg.AnnotationFilter)
114+
logger.Info("POSTGRES_INSTANCE is set, this instance will only process CRs with the correct annotation", "annotation", cfg.AnnotationFilter)
117115
lockName += "-" + cfg.AnnotationFilter
118116
}
119117
cacheOpts := cache.Options{}
@@ -145,38 +143,45 @@ func main() {
145143
// LeaderElectionReleaseOnCancel: true,
146144
})
147145
if err != nil {
148-
setupLog.Error(err, "unable to start manager")
146+
logger.Error(err, "unable to start manager")
149147
os.Exit(1)
150148
}
151149

152-
pg, err := postgres.NewPG(cfg, ctrl.Log)
150+
pg, err := postgres.NewPG(cfg, logger)
153151
if err != nil {
154-
setupLog.Error(err, "DB-Connection failed", "cfg", cfg)
152+
// Avoid logging sensitive information like PostgresPass
153+
logger.Error(err, "DB-Connection failed", "cfg", map[string]any{
154+
"Host": cfg.PostgresHost,
155+
"User": cfg.PostgresUser,
156+
"UriArgs": cfg.PostgresUriArgs,
157+
"CloudPriver": cfg.CloudProvider,
158+
"DefaultDatabase": cfg.PostgresDefaultDb,
159+
})
155160
os.Exit(1)
156161
}
157162

158163
if err = (controller.NewPostgresReconciler(mgr, cfg, pg)).SetupWithManager(mgr); err != nil {
159-
setupLog.Error(err, "unable to create controller", "controller", "Postgres")
164+
logger.Error(err, "unable to create controller", "controller", "Postgres")
160165
os.Exit(1)
161166
}
162167
if err = (controller.NewPostgresUserReconciler(mgr, cfg, pg)).SetupWithManager(mgr); err != nil {
163-
setupLog.Error(err, "unable to create controller", "controller", "PostgresUser")
168+
logger.Error(err, "unable to create controller", "controller", "PostgresUser")
164169
os.Exit(1)
165170
}
166171
// +kubebuilder:scaffold:builder
167172

168173
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
169-
setupLog.Error(err, "unable to set up health check")
174+
logger.Error(err, "unable to set up health check")
170175
os.Exit(1)
171176
}
172177
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
173-
setupLog.Error(err, "unable to set up ready check")
178+
logger.Error(err, "unable to set up ready check")
174179
os.Exit(1)
175180
}
176181

177-
setupLog.Info("starting manager")
182+
logger.Info("starting manager")
178183
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
179-
setupLog.Error(err, "problem running manager")
184+
logger.Error(err, "problem running manager")
180185
os.Exit(1)
181186
}
182187
}

config/crd/bases/db.movetokube.com_postgres.yaml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@ spec:
2020
description: Postgres is the Schema for the postgres API
2121
properties:
2222
apiVersion:
23-
description: 'APIVersion defines the versioned schema of this representation
24-
of an object. Servers should convert recognized schemas to the latest
25-
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
23+
description: |-
24+
APIVersion defines the versioned schema of this representation of an object.
25+
Servers should convert recognized schemas to the latest internal value, and
26+
may reject unrecognized values.
27+
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
2628
type: string
2729
kind:
28-
description: 'Kind is a string value representing the REST resource this
29-
object represents. Servers may infer this from the endpoint the client
30-
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
30+
description: |-
31+
Kind is a string value representing the REST resource this object represents.
32+
Servers may infer this from the endpoint the client submits requests to.
33+
Cannot be updated.
34+
In CamelCase.
35+
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
3136
type: string
3237
metadata:
3338
type: object

config/crd/bases/db.movetokube.com_postgresusers.yaml

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@ spec:
2020
description: PostgresUser is the Schema for the postgresusers API
2121
properties:
2222
apiVersion:
23-
description: 'APIVersion defines the versioned schema of this representation
24-
of an object. Servers should convert recognized schemas to the latest
25-
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
23+
description: |-
24+
APIVersion defines the versioned schema of this representation of an object.
25+
Servers should convert recognized schemas to the latest internal value, and
26+
may reject unrecognized values.
27+
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
2628
type: string
2729
kind:
28-
description: 'Kind is a string value representing the REST resource this
29-
object represents. Servers may infer this from the endpoint the client
30-
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
30+
description: |-
31+
Kind is a string value representing the REST resource this object represents.
32+
Servers may infer this from the endpoint the client submits requests to.
33+
Cannot be updated.
34+
In CamelCase.
35+
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
3136
type: string
3237
metadata:
3338
type: object
@@ -39,28 +44,23 @@ spec:
3944
type: string
4045
type: object
4146
aws:
42-
description: AWS specific settings for this user.
47+
description: PostgresUserAWSSpec encapsulates AWS specific configuration
48+
toggles.
4349
properties:
4450
enableIamAuth:
45-
description: Enable IAM authentication for this user (PostgreSQL on AWS RDS only)
46-
default: false
4751
type: boolean
4852
type: object
4953
database:
50-
description: Name of the PostgresDatabase this user will be related to
5154
type: string
5255
labels:
5356
additionalProperties:
5457
type: string
5558
type: object
5659
privileges:
57-
description: List of privileges to grant to this user
5860
type: string
5961
role:
60-
description: Name of the PostgresRole this user will be associated with
6162
type: string
6263
secretName:
63-
description: Name of the secret to create with user credentials
6464
type: string
6565
secretTemplate:
6666
additionalProperties:
@@ -74,11 +74,10 @@ spec:
7474
status:
7575
description: PostgresUserStatus defines the observed state of PostgresUser
7676
properties:
77-
enableIamAuth:
78-
description: Reflects whether IAM authentication is enabled for this user.
79-
type: boolean
8077
databaseName:
8178
type: string
79+
enableIamAuth:
80+
type: boolean
8281
postgresGroup:
8382
type: string
8483
postgresLogin:
@@ -89,6 +88,7 @@ spec:
8988
type: boolean
9089
required:
9190
- databaseName
91+
- enableIamAuth
9292
- postgresGroup
9393
- postgresLogin
9494
- postgresRole

internal/controller/postgres_controller.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,27 +82,27 @@ func (r *PostgresReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
8282
if !instance.GetDeletionTimestamp().IsZero() {
8383
if r.shouldDropDB(ctx, instance, reqLogger) && instance.Status.Succeeded {
8484
if instance.Status.Roles.Owner != "" {
85-
err := r.pg.DropRole(instance.Status.Roles.Owner, r.pg.GetUser(), instance.Spec.Database, reqLogger)
85+
err := r.pg.DropRole(instance.Status.Roles.Owner, r.pg.GetUser(), instance.Spec.Database)
8686
if err != nil {
8787
return ctrl.Result{}, err
8888
}
8989
instance.Status.Roles.Owner = ""
9090
}
9191
if instance.Status.Roles.Reader != "" {
92-
err = r.pg.DropRole(instance.Status.Roles.Reader, r.pg.GetUser(), instance.Spec.Database, reqLogger)
92+
err = r.pg.DropRole(instance.Status.Roles.Reader, r.pg.GetUser(), instance.Spec.Database)
9393
if err != nil {
9494
return ctrl.Result{}, err
9595
}
9696
instance.Status.Roles.Reader = ""
9797
}
9898
if instance.Status.Roles.Writer != "" {
99-
err = r.pg.DropRole(instance.Status.Roles.Writer, r.pg.GetUser(), instance.Spec.Database, reqLogger)
99+
err = r.pg.DropRole(instance.Status.Roles.Writer, r.pg.GetUser(), instance.Spec.Database)
100100
if err != nil {
101101
return ctrl.Result{}, err
102102
}
103103
instance.Status.Roles.Writer = ""
104104
}
105-
err = r.pg.DropDatabase(instance.Spec.Database, reqLogger)
105+
err = r.pg.DropDatabase(instance.Spec.Database)
106106
if err != nil {
107107
return ctrl.Result{}, err
108108
}
@@ -175,7 +175,7 @@ func (r *PostgresReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
175175
continue
176176
}
177177
// Execute create extension SQL statement
178-
err = r.pg.CreateExtension(instance.Spec.Database, extension, reqLogger)
178+
err = r.pg.CreateExtension(instance.Spec.Database, extension)
179179
if err != nil {
180180
reqLogger.Error(err, fmt.Sprintf("Could not add extensions %s", extension))
181181
continue
@@ -198,7 +198,7 @@ func (r *PostgresReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
198198
}
199199

200200
// Create schema
201-
err = r.pg.CreateSchema(database, owner, schema, reqLogger)
201+
err = r.pg.CreateSchema(database, owner, schema)
202202
if err != nil {
203203
reqLogger.Error(err, fmt.Sprintf("Could not create schema %s", schema))
204204
continue
@@ -212,7 +212,7 @@ func (r *PostgresReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
212212
Privs: readerPrivs,
213213
CreateSchema: false,
214214
}
215-
err = r.pg.SetSchemaPrivileges(schemaPrivilegesReader, reqLogger)
215+
err = r.pg.SetSchemaPrivileges(schemaPrivilegesReader)
216216
if err != nil {
217217
reqLogger.Error(err, fmt.Sprintf("Could not give %s permissions \"%s\"", reader, readerPrivs))
218218
continue
@@ -224,7 +224,7 @@ func (r *PostgresReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
224224
Privs: writerPrivs,
225225
CreateSchema: true,
226226
}
227-
err = r.pg.SetSchemaPrivileges(schemaPrivilegesWriter, reqLogger)
227+
err = r.pg.SetSchemaPrivileges(schemaPrivilegesWriter)
228228
if err != nil {
229229
reqLogger.Error(err, fmt.Sprintf("Could not give %s permissions \"%s\"", writer, writerPrivs))
230230
continue
@@ -236,7 +236,7 @@ func (r *PostgresReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
236236
Privs: writerPrivs,
237237
CreateSchema: true,
238238
}
239-
err = r.pg.SetSchemaPrivileges(schemaPrivilegesOwner, reqLogger)
239+
err = r.pg.SetSchemaPrivileges(schemaPrivilegesOwner)
240240
if err != nil {
241241
reqLogger.Error(err, fmt.Sprintf("Could not give %s permissions \"%s\"", writer, writerPrivs))
242242
continue
@@ -259,13 +259,15 @@ func (r *PostgresReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
259259
reqLogger.Info("Reconciling done")
260260
return ctrl.Result{}, nil
261261
}
262+
262263
func (r *PostgresReconciler) addFinalizer(reqLogger logr.Logger, m *dbv1alpha1.Postgres) error {
263264
if len(m.GetFinalizers()) < 1 && m.GetDeletionTimestamp() == nil {
264265
reqLogger.Info("adding Finalizer for Postgres")
265266
m.SetFinalizers([]string{"finalizer.db.movetokube.com"})
266267
}
267268
return nil
268269
}
270+
269271
func (r *PostgresReconciler) requeue(cr *dbv1alpha1.Postgres, reason error) (ctrl.Result, error) {
270272
cr.Status.Succeeded = false
271273
return ctrl.Result{}, reason

0 commit comments

Comments
 (0)