Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding v levels to debug logs #189

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import (

var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
setupLog = ctrl.Log.WithName("setup").V(controllers.InfoLevel)
)

func init() {
Expand Down Expand Up @@ -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.")
Expand All @@ -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()
Expand All @@ -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,
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand Down
34 changes: 20 additions & 14 deletions controllers/databaseclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -147,15 +153,15 @@ 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
}

// 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
Expand All @@ -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
}
}
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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()})
}
Expand Down
4 changes: 2 additions & 2 deletions controllers/dbroleclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions helm/db-controller/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
3 changes: 3 additions & 0 deletions helm/db-controller/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading