Skip to content

Commit c63c5fe

Browse files
committed
Simplify logging
1 parent 4c908b6 commit c63c5fe

File tree

16 files changed

+211
-217
lines changed

16 files changed

+211
-217
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: 24 additions & 21 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))
@@ -57,22 +55,20 @@ func main() {
5755
"If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.")
5856
flag.BoolVar(&enableHTTP2, "enable-http2", true,
5957
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
60-
opts := zap.Options{
61-
Development: true,
62-
}
58+
opts := zap.Options{}
6359
opts.BindFlags(flag.CommandLine)
6460
flag.Parse()
6561

66-
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
67-
62+
logger := zap.New(zap.UseFlagOptions(&opts))
63+
logf.SetLogger(logger)
6864
// if the enable-http2 flag is false (the default), http/2 should be disabled
6965
// due to its vulnerabilities. More specifically, disabling http/2 will
7066
// prevent from being vulnerable to the HTTP/2 Stream Cancellation and
7167
// Rapid Reset CVEs. For more information see:
7268
// - https://github.com/advisories/GHSA-qppj-fm5r-hxr3
7369
// - https://github.com/advisories/GHSA-4374-p667-p6c8
7470
disableHTTP2 := func(c *tls.Config) {
75-
setupLog.Info("disabling http/2")
71+
logger.Info("disabling http/2")
7672
c.NextProtos = []string{"http/1.1"}
7773
}
7874

@@ -111,9 +107,9 @@ func main() {
111107
cfg := config.Get()
112108
lockName := "lock"
113109
if cfg.AnnotationFilter == "" {
114-
setupLog.Info("No POSTGRES_INSTANCE set, this instance will only process CRs without an annotation")
110+
logger.Info("No POSTGRES_INSTANCE set, this instance will only process CRs without an annotation")
115111
} else {
116-
setupLog.Info("POSTGRES_INSTANCE is set, this instance will only process CRs with the correct annotation", "annotation", cfg.AnnotationFilter)
112+
logger.Info("POSTGRES_INSTANCE is set, this instance will only process CRs with the correct annotation", "annotation", cfg.AnnotationFilter)
117113
lockName += "-" + cfg.AnnotationFilter
118114
}
119115
cacheOpts := cache.Options{}
@@ -145,38 +141,45 @@ func main() {
145141
// LeaderElectionReleaseOnCancel: true,
146142
})
147143
if err != nil {
148-
setupLog.Error(err, "unable to start manager")
144+
logger.Error(err, "unable to start manager")
149145
os.Exit(1)
150146
}
151147

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

158161
if err = (controller.NewPostgresReconciler(mgr, cfg, pg)).SetupWithManager(mgr); err != nil {
159-
setupLog.Error(err, "unable to create controller", "controller", "Postgres")
162+
logger.Error(err, "unable to create controller", "controller", "Postgres")
160163
os.Exit(1)
161164
}
162165
if err = (controller.NewPostgresUserReconciler(mgr, cfg, pg)).SetupWithManager(mgr); err != nil {
163-
setupLog.Error(err, "unable to create controller", "controller", "PostgresUser")
166+
logger.Error(err, "unable to create controller", "controller", "PostgresUser")
164167
os.Exit(1)
165168
}
166169
// +kubebuilder:scaffold:builder
167170

168171
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
169-
setupLog.Error(err, "unable to set up health check")
172+
logger.Error(err, "unable to set up health check")
170173
os.Exit(1)
171174
}
172175
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
173-
setupLog.Error(err, "unable to set up ready check")
176+
logger.Error(err, "unable to set up ready check")
174177
os.Exit(1)
175178
}
176179

177-
setupLog.Info("starting manager")
180+
logger.Info("starting manager")
178181
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
179-
setupLog.Error(err, "problem running manager")
182+
logger.Error(err, "problem running manager")
180183
os.Exit(1)
181184
}
182185
}

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
}
@@ -196,7 +196,7 @@ func (r *PostgresReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
196196
continue
197197
}
198198
// Execute create extension SQL statement
199-
err = r.pg.CreateExtension(instance.Spec.Database, extension, reqLogger)
199+
err = r.pg.CreateExtension(instance.Spec.Database, extension)
200200
if err != nil {
201201
reqLogger.Error(err, fmt.Sprintf("Could not add extensions %s", extension))
202202
continue
@@ -224,7 +224,7 @@ func (r *PostgresReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
224224
}
225225

226226
// Create schema
227-
err = r.pg.CreateSchema(database, owner, schema, reqLogger)
227+
err = r.pg.CreateSchema(database, owner, schema)
228228
if err != nil {
229229
reqLogger.Error(err, fmt.Sprintf("Could not create schema %s", schema))
230230
continue
@@ -243,7 +243,7 @@ func (r *PostgresReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
243243
Privs: readerPrivs,
244244
CreateSchema: false,
245245
}
246-
err = r.pg.SetSchemaPrivileges(schemaPrivilegesReader, reqLogger)
246+
err = r.pg.SetSchemaPrivileges(schemaPrivilegesReader)
247247
if err != nil {
248248
reqLogger.Error(err, fmt.Sprintf("Could not give %s permissions \"%s\"", reader, readerPrivs))
249249
continue
@@ -257,7 +257,7 @@ func (r *PostgresReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
257257
FunctionPrivs: writerFunctionPrivs,
258258
CreateSchema: true,
259259
}
260-
err = r.pg.SetSchemaPrivileges(schemaPrivilegesWriter, reqLogger)
260+
err = r.pg.SetSchemaPrivileges(schemaPrivilegesWriter)
261261
if err != nil {
262262
reqLogger.Error(err, fmt.Sprintf("Could not give %s permissions \"%s\", sequence privileges \"%s\", and function privileges \"%s\"", writer, writerPrivs, writerSequencePrivs, writerFunctionPrivs))
263263
continue
@@ -271,7 +271,7 @@ func (r *PostgresReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
271271
FunctionPrivs: ownerFunctionPrivs,
272272
CreateSchema: true,
273273
}
274-
err = r.pg.SetSchemaPrivileges(schemaPrivilegesOwner, reqLogger)
274+
err = r.pg.SetSchemaPrivileges(schemaPrivilegesOwner)
275275
if err != nil {
276276
reqLogger.Error(err, fmt.Sprintf("Could not give %s permissions \"%s\", sequence privileges \"%s\", and function privileges \"%s\"", owner, ownerPrivs, ownerSequencePrivs, ownerFunctionPrivs))
277277
continue
@@ -293,13 +293,15 @@ func (r *PostgresReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
293293
reqLogger.Info("Reconciling done")
294294
return ctrl.Result{}, nil
295295
}
296+
296297
func (r *PostgresReconciler) addFinalizer(reqLogger logr.Logger, m *dbv1alpha1.Postgres) error {
297298
if len(m.GetFinalizers()) < 1 && m.GetDeletionTimestamp() == nil {
298299
reqLogger.Info("adding Finalizer for Postgres")
299300
m.SetFinalizers([]string{"finalizer.db.movetokube.com"})
300301
}
301302
return nil
302303
}
304+
303305
func (r *PostgresReconciler) requeue(cr *dbv1alpha1.Postgres, reason error) (ctrl.Result, error) {
304306
cr.Status.Succeeded = false
305307
return ctrl.Result{}, reason

0 commit comments

Comments
 (0)