From 18ddb6d80a687ef4509448e11d3bfb3c40575820 Mon Sep 17 00:00:00 2001 From: Sujay Kumar Suman Date: Sun, 1 Oct 2023 13:50:46 +0530 Subject: [PATCH] adding v levels to debug logs --- cmd/manager/main.go | 12 ++++--- controllers/databaseclaim_controller.go | 34 ++++++++++++-------- controllers/dbroleclaim_controller.go | 4 +-- helm/db-controller/templates/deployment.yaml | 1 + helm/db-controller/values.yaml | 3 ++ 5 files changed, 34 insertions(+), 20 deletions(-) diff --git a/cmd/manager/main.go b/cmd/manager/main.go index ca3e2459..76761042 100644 --- a/cmd/manager/main.go +++ b/cmd/manager/main.go @@ -49,7 +49,7 @@ import ( var ( scheme = runtime.NewScheme() - setupLog = ctrl.Log.WithName("setup") + setupLog = ctrl.Log.WithName("setup").V(controllers.InfoLevel) ) func init() { @@ -92,6 +92,7 @@ func main() { var class string var metricsDepYamlPath string var metricsConfigYamlPath string + var verbosity int flag.StringVar(&class, "class", "default", "The class of claims this db-controller instance needs to address.") flag.StringVar(&dbIdentifierPrefix, "db-identifier-prefix", "", "The prefix to be added to the DbHost. Ideally this is the env name.") @@ -115,9 +116,12 @@ func main() { "Enable Dsnexec webhook. "+ "Enabling this option will cause the db-controller to inject dsnexec container into pods "+ "with the infoblox.com/remote-db-dsn-secret and infoblox.com/dsnexec-config-secret annotations set.") + flag.IntVar(&verbosity, "verbosity", 0, "Configures the verbosity of the logging. "+ + "By default it's set to 0, set to 1 to enable debug logs.") opts := zap.Options{ Development: false, TimeEncoder: zapcore.RFC3339NanoTimeEncoder, + Level: zapcore.Level(-1 * verbosity), } opts.BindFlags(flag.CommandLine) flag.Parse() @@ -144,7 +148,7 @@ func main() { Client: mgr.GetClient(), Config: ctlConfig, DbIdentifierPrefix: dbIdentifierPrefix, - Log: ctrl.Log.WithName("controllers").WithName("DatabaseClaim"), + Log: ctrl.Log.WithName("controllers").WithName("DatabaseClaim").V(controllers.InfoLevel), MasterAuth: rdsauth.NewMasterAuth(), MetricsDepYamlPath: metricsDepYamlPath, MetricsConfigYamlPath: metricsConfigYamlPath, @@ -185,7 +189,7 @@ func main() { setupLog.Error(err, "could not parse db proxy sidecar configuration") os.Exit(1) } - setupLog.Info("Parsed db proxy conig:", "dbproxysidecarconfig", cfg) + setupLog.V(controllers.DebugLevel).Info("Parsed db proxy conig:", "dbproxysidecarconfig", cfg) setupLog.Info("registering with webhook server for DbProxy") webHookServer.Register("/mutate", &webhook.Admission{ @@ -204,7 +208,7 @@ func main() { setupLog.Error(err, "could not parse dsnexec sidecar configuration") os.Exit(1) } - setupLog.Info("Parsed dsnexec conig:", "dsnexecsidecarconfig", cfg) + setupLog.V(controllers.DebugLevel).Info("Parsed dsnexec conig:", "dsnexecsidecarconfig", cfg) setupLog.Info("registering with webhook server for DsnExec") webHookServer.Register("/mutate-dsnexec", &webhook.Admission{ diff --git a/controllers/databaseclaim_controller.go b/controllers/databaseclaim_controller.go index 0bc9793e..89449419 100644 --- a/controllers/databaseclaim_controller.go +++ b/controllers/databaseclaim_controller.go @@ -74,6 +74,12 @@ const ( masterSecretSuffix = "-master" masterPasswordKey = "password" ErrMaxNameLen = Error("dbclaim name is too long. max length is 44 characters") + // InfoLevel is used to set V level to 0 as suggested by official docs + // https://github.com/kubernetes-sigs/controller-runtime/blob/main/TMP-LOGGING.md + InfoLevel = 0 + // DebugLevel is used to set V level to 1 as suggested by official docs + // https://github.com/kubernetes-sigs/controller-runtime/blob/main/TMP-LOGGING.md + DebugLevel = 1 ) type ModeEnum int @@ -147,7 +153,7 @@ func (r *DatabaseClaimReconciler) getMode(dbClaim *persistancev1.DatabaseClaim) logr.Info("upgrade requested for a shared host. shared host upgrades are not supported. ignoring upgrade request") } } - logr.Info("selected mode for shared db host", "dbclaim", dbClaim.Spec, "selected mode", "M_UseNewDB") + logr.V(DebugLevel).Info("selected mode for shared db host", "dbclaim", dbClaim.Spec, "selected mode", "M_UseNewDB") return M_UseNewDB } @@ -155,7 +161,7 @@ func (r *DatabaseClaimReconciler) getMode(dbClaim *persistancev1.DatabaseClaim) // use existing is true if *dbClaim.Spec.UseExistingSource { if dbClaim.Spec.SourceDataFrom != nil && dbClaim.Spec.SourceDataFrom.Type == "database" { - logr.Info("selected mode for", "dbclaim", dbClaim.Spec, "selected mode", "use existing db") + logr.V(DebugLevel).Info("selected mode for", "dbclaim", dbClaim.Spec, "selected mode", "use existing db") return M_UseExistingDB } else { return M_NotSupported @@ -166,10 +172,10 @@ func (r *DatabaseClaimReconciler) getMode(dbClaim *persistancev1.DatabaseClaim) if dbClaim.Spec.SourceDataFrom.Type == "database" { if dbClaim.Status.ActiveDB.DbState == persistancev1.UsingExistingDB { if dbClaim.Status.MigrationState == "" || dbClaim.Status.MigrationState == pgctl.S_Initial.String() { - logr.Info("selected mode for", "dbclaim", dbClaim.Spec, "selected mode", "M_MigrateExistingToNewDB") + logr.V(DebugLevel).Info("selected mode for", "dbclaim", dbClaim.Spec, "selected mode", "M_MigrateExistingToNewDB") return M_MigrateExistingToNewDB } else if dbClaim.Status.MigrationState != pgctl.S_Completed.String() { - logr.Info("selected mode for", "dbclaim", dbClaim.Spec, "selected mode", "M_MigrationInProgress") + logr.V(DebugLevel).Info("selected mode for", "dbclaim", dbClaim.Spec, "selected mode", "M_MigrationInProgress") return M_MigrationInProgress } } @@ -184,10 +190,10 @@ func (r *DatabaseClaimReconciler) getMode(dbClaim *persistancev1.DatabaseClaim) if dbClaim.Status.ActiveDB.SourceDataFrom != nil { dbClaim.Spec.SourceDataFrom = dbClaim.Status.ActiveDB.SourceDataFrom.DeepCopy() if dbClaim.Status.MigrationState == "" || dbClaim.Status.MigrationState == pgctl.S_Initial.String() { - logr.Info("selected mode for", "dbclaim", dbClaim.Spec, "selected mode", "M_MigrateExistingToNewDB") + logr.V(DebugLevel).Info("selected mode for", "dbclaim", dbClaim.Spec, "selected mode", "M_MigrateExistingToNewDB") return M_MigrateExistingToNewDB } else if dbClaim.Status.MigrationState != pgctl.S_Completed.String() { - logr.Info("selected mode for", "dbclaim", dbClaim.Spec, "selected mode", "M_MigrationInProgress") + logr.V(DebugLevel).Info("selected mode for", "dbclaim", dbClaim.Spec, "selected mode", "M_MigrationInProgress") return M_MigrationInProgress } } else { @@ -207,17 +213,17 @@ func (r *DatabaseClaimReconciler) getMode(dbClaim *persistancev1.DatabaseClaim) dbClaim.Status.MigrationState = "" } if dbClaim.Status.MigrationState == "" || dbClaim.Status.MigrationState == pgctl.S_Initial.String() { - logr.Info("selected mode for", "dbclaim", dbClaim.Spec, "selected mode", "M_InitiateDBUpgrade") + logr.V(DebugLevel).Info("selected mode for", "dbclaim", dbClaim.Spec, "selected mode", "M_InitiateDBUpgrade") return M_InitiateDBUpgrade } else if dbClaim.Status.MigrationState != pgctl.S_Completed.String() { - logr.Info("selected mode for", "dbclaim", dbClaim.Spec, "selected mode", "M_UpgradeDBInProgress") + logr.V(DebugLevel).Info("selected mode for", "dbclaim", dbClaim.Spec, "selected mode", "M_UpgradeDBInProgress") return M_UpgradeDBInProgress } } } - logr.Info("selected mode for", "dbclaim", dbClaim.Spec, "selected mode", "M_UseNewDB") + logr.V(DebugLevel).Info("selected mode for", "dbclaim", dbClaim.Spec, "selected mode", "M_UseNewDB") return M_UseNewDB } @@ -307,7 +313,7 @@ func (r *DatabaseClaimReconciler) setReqInfo(dbClaim *persistancev1.DatabaseClai r.Input.EnableReplicationRole = *dbClaim.Spec.EnableReplicationRole } - logr.Info("setup values of ", "DatabaseClaimReconciler", r) + logr.V(DebugLevel).Info("setup values of ", "DatabaseClaimReconciler", r) return nil } @@ -679,8 +685,8 @@ func (r *DatabaseClaimReconciler) reconcileMigrationInProgress(ctx context.Conte err := fmt.Errorf("unsupported mode %v", r.Mode) return r.manageError(ctx, dbClaim, err) } - logr.Info("DSN", "sourceAppDsn", sourceAppDsn) - logr.Info("DSN", "sourceMasterConn", sourceMasterConn) + logr.V(DebugLevel).Info("DSN", "sourceAppDsn", sourceAppDsn) + logr.V(DebugLevel).Info("DSN", "sourceMasterConn", sourceMasterConn) config := pgctl.Config{ Log: r.Log, @@ -691,7 +697,7 @@ func (r *DatabaseClaimReconciler) reconcileMigrationInProgress(ctx context.Conte ExportFilePath: r.Config.GetString("pgTemp"), } - logr.Info("DSN", "config", config) + logr.V(DebugLevel).Info("DSN", "config", config) s, err := pgctl.GetReplicatorState(migrationState, config) if err != nil { @@ -826,7 +832,7 @@ func (r *DatabaseClaimReconciler) getClientConn(dbClaim *persistancev1.DatabaseC func (r *DatabaseClaimReconciler) getDBClient(dbClaim *persistancev1.DatabaseClaim) (dbclient.Client, error) { logr := r.Log.WithValues("databaseclaim", dbClaim.Namespace+"/"+dbClaim.Name, "func", "getDBClient") - logr.Info("getting dbclient", "dsn", r.getMasterDefaultDsn()) + logr.V(DebugLevel).Info("getting dbclient", "dsn", r.getMasterDefaultDsn()) updateHostPortStatus(&dbClaim.Status.NewDB, r.Input.MasterConnInfo.Host, r.Input.MasterConnInfo.Port, r.Input.MasterConnInfo.SSLMode) return dbclient.New(dbclient.Config{Log: r.Log, DBType: "postgres", DSN: r.getMasterDefaultDsn()}) } diff --git a/controllers/dbroleclaim_controller.go b/controllers/dbroleclaim_controller.go index c34819db..abc2dec8 100644 --- a/controllers/dbroleclaim_controller.go +++ b/controllers/dbroleclaim_controller.go @@ -59,7 +59,7 @@ type DbRoleClaimReconciler struct { //+kubebuilder:rbac:groups="",resources=events,verbs=create;patch func (r *DbRoleClaimReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - log := log.FromContext(ctx).WithValues("databaserole", req.NamespacedName) + log := log.FromContext(ctx).WithValues("databaserole", req.NamespacedName).V(InfoLevel) var dbRoleClaim persistancev1.DbRoleClaim if err := r.Get(ctx, req.NamespacedName, &dbRoleClaim); err != nil { log.Error(err, "unable to fetch DatabaseClaim") @@ -106,7 +106,7 @@ func (r *DbRoleClaimReconciler) Reconcile(ctx context.Context, req ctrl.Request) dbRoleClaim.Status.SourceSecret = "" return r.manageError(ctx, &dbRoleClaim, fmt.Errorf("%s source secret not found", foundDbClaim.Spec.SecretName)) } - log.Info("found dbclaimsecret", "secretName", foundDbClaim.Spec.SecretName, "secret", foundSecret) + log.V(DebugLevel).Info("found dbclaimsecret", "secretName", foundDbClaim.Spec.SecretName, "secret", foundSecret) dbRoleClaim.Status.SourceSecret = foundSecret.Namespace + "/" + foundSecret.Name r.Recorder.Event(&dbRoleClaim, "Normal", "Found", fmt.Sprintf("Secret %s/%s", dbclaimNamespace, foundDbClaim.Spec.SecretName)) diff --git a/helm/db-controller/templates/deployment.yaml b/helm/db-controller/templates/deployment.yaml index 33147531..eb8643d4 100644 --- a/helm/db-controller/templates/deployment.yaml +++ b/helm/db-controller/templates/deployment.yaml @@ -69,6 +69,7 @@ spec: - --dsnexec-sidecar-config-path=config/dsnexec/dsnexecsidecar.json - --db-identifier-prefix={{ tpl .Values.db.identifier.prefix . }} - --class={{ .Values.dbController.class }} + - --verbosity={{ .Values.zapLogger.verbosity }} {{ if .Values.zapLogger.develMode }} - --zap-devel {{ end }} diff --git a/helm/db-controller/values.yaml b/helm/db-controller/values.yaml index 450d8431..b45a93f3 100644 --- a/helm/db-controller/values.yaml +++ b/helm/db-controller/values.yaml @@ -115,6 +115,9 @@ dsnexec: zapLogger: develMode: false level: info + # The level of verbosity required for logging. Possible values are 0 and 1. Default is 0 and to enable debug level logs + # set it to 1. Refer: https://github.com/kubernetes-sigs/controller-runtime/blob/main/TMP-LOGGING.md + verbosity: 0 controllerConfig: authSource: secret