From 9c36823559968b86bfc287c880ee719b3f276488 Mon Sep 17 00:00:00 2001 From: kgarg-px Date: Wed, 22 Nov 2023 07:48:00 +0000 Subject: [PATCH] PB-4844: kdmp can now use kopia debug mode Signed-off-by: kgarg-px --- pkg/drivers/kopiabackup/kopiabackup.go | 3 +++ pkg/drivers/kopiadelete/kopiadelete.go | 2 ++ .../kopiamaintenance/kopiamaintenance.go | 2 ++ pkg/drivers/kopiarestore/kopiarestore.go | 4 +++- pkg/drivers/options.go | 1 + pkg/drivers/utils/utils.go | 15 +++++++++++++ pkg/executor/kopia/kopia.go | 2 ++ pkg/executor/kopia/kopiabackup.go | 22 ++++++++++++++++++- pkg/executor/kopia/kopiadelete.go | 6 +++-- pkg/executor/kopia/maintenance.go | 7 +++++- pkg/executor/kopia/restore.go | 4 ++-- 11 files changed, 61 insertions(+), 7 deletions(-) diff --git a/pkg/drivers/kopiabackup/kopiabackup.go b/pkg/drivers/kopiabackup/kopiabackup.go index 1c29b6928..f1acb6ddf 100644 --- a/pkg/drivers/kopiabackup/kopiabackup.go +++ b/pkg/drivers/kopiabackup/kopiabackup.go @@ -287,6 +287,9 @@ func jobFor( "/data", }, " ") + // Read the config map from the job option and then add the debug level log if required + cmd = utils.CheckAndAddKopiaDebugModeEnabled(cmd, jobOption) + if jobOption.Compression != "" { splitCmd := strings.Split(cmd, " ") splitCmd = append(splitCmd, "--compression", jobOption.Compression) diff --git a/pkg/drivers/kopiadelete/kopiadelete.go b/pkg/drivers/kopiadelete/kopiadelete.go index 7ee70c20f..0837e4cfb 100644 --- a/pkg/drivers/kopiadelete/kopiadelete.go +++ b/pkg/drivers/kopiadelete/kopiadelete.go @@ -201,6 +201,8 @@ func jobFor( jobOption.VolumeBackupDeleteNamespace, }, " ") + cmd = utils.CheckAndAddKopiaDebugModeEnabled(cmd, jobOption) + kopiaExecutorImage, imageRegistrySecret, err := utils.GetExecutorImageAndSecret(drivers.KopiaExecutorImage, jobOption.KopiaImageExecutorSource, jobOption.KopiaImageExecutorSourceNs, diff --git a/pkg/drivers/kopiamaintenance/kopiamaintenance.go b/pkg/drivers/kopiamaintenance/kopiamaintenance.go index 8bb46e4d2..84dfec882 100644 --- a/pkg/drivers/kopiamaintenance/kopiamaintenance.go +++ b/pkg/drivers/kopiamaintenance/kopiamaintenance.go @@ -204,6 +204,8 @@ func jobFor( jobOption.MaintenanceType, }, " ") + cmd = utils.CheckAndAddKopiaDebugModeEnabled(cmd, jobOption) + kopiaExecutorImage, imageRegistrySecret, err := utils.GetExecutorImageAndSecret(drivers.KopiaExecutorImage, jobOption.KopiaImageExecutorSource, jobOption.KopiaImageExecutorSourceNs, diff --git a/pkg/drivers/kopiarestore/kopiarestore.go b/pkg/drivers/kopiarestore/kopiarestore.go index ba0b7c7e3..c5ef5929f 100644 --- a/pkg/drivers/kopiarestore/kopiarestore.go +++ b/pkg/drivers/kopiarestore/kopiarestore.go @@ -206,6 +206,8 @@ func jobFor( vb.Status.SnapshotID, }, " ") + cmd = utils.CheckAndAddKopiaDebugModeEnabled(cmd, jobOption) + kopiaExecutorImage, imageRegistrySecret, err := utils.GetExecutorImageAndSecret(drivers.KopiaExecutorImage, jobOption.KopiaImageExecutorSource, jobOption.KopiaImageExecutorSourceNs, @@ -369,7 +371,7 @@ func roleFor() *rbacv1.Role { }, { APIGroups: []string{"kdmp.portworx.com"}, - Resources: []string{"volumebackups"}, + Resources: []string{"volumebackups", "dataexports"}, Verbs: []string{rbacv1.VerbAll}, }, }, diff --git a/pkg/drivers/options.go b/pkg/drivers/options.go index 61d597c86..b888a661e 100644 --- a/pkg/drivers/options.go +++ b/pkg/drivers/options.go @@ -19,6 +19,7 @@ type JobOpts struct { VolumeBackupName string VolumeBackupNamespace string VolumeBackupDeleteName string + KopiaDebugMode bool VolumeBackupDeleteNamespace string DataExportName string SnapshotID string diff --git a/pkg/drivers/utils/utils.go b/pkg/drivers/utils/utils.go index 170ab9efd..e9627d9ff 100644 --- a/pkg/drivers/utils/utils.go +++ b/pkg/drivers/utils/utils.go @@ -63,6 +63,10 @@ const ( PvcBoundSuccessMsg = "pvc bounded successfully" // PvcBoundFailedMsg pvc not bounded msg PvcBoundFailedMsg = "pvc not bounded" + // KopiaDebugModeEnabled - debug level log messages are enabled for kopia + KopiaDebugModeEnabled = "kopia-debug-mode" + // KopiaExecutorDebugModeCMKey - key present in the config maps, which enables debug log level in kopia + KopiaExecutorDebugModeCMKey = "KDMP_KOPIAEXECUTOR_ENABLE_DEBUG_MODE" ) var ( @@ -876,3 +880,14 @@ func IsJobPodMountFailed(job *batchv1.Job, namespace string) bool { } return false } + +func CheckAndAddKopiaDebugModeEnabled(cmd string, jobOption drivers.JobOpts) string { + // check for the particular key-value pair + isKeyPresent := GetConfigValue(jobOption.JobConfigMap, jobOption.JobConfigMapNs, KopiaExecutorDebugModeCMKey) + if isKeyPresent == "true" { + splitCmd := strings.Split(cmd, " ") + splitCmd = append(splitCmd, "--log-level", "debug") + cmd = strings.Join(splitCmd, " ") + } + return cmd +} diff --git a/pkg/executor/kopia/kopia.go b/pkg/executor/kopia/kopia.go index 405446fc6..3c715b85b 100644 --- a/pkg/executor/kopia/kopia.go +++ b/pkg/executor/kopia/kopia.go @@ -14,6 +14,7 @@ var ( credentials string backupLocationName string backupLocationNamespace string + logLevelDebug string ) // NewCommand returns a kopia command wrapper @@ -29,6 +30,7 @@ func NewCommand() *cobra.Command { cmds.PersistentFlags().StringVar(&backupLocationName, "backup-location", "", "Name of the BackupLocation object, used for authentication") cmds.PersistentFlags().StringVar(&backupLocationNamespace, "backup-location-namespace", "", "Namespace of BackupLocation object, used for authentication") cmds.PersistentFlags().StringVar(&volumeBackupName, "volume-backup-name", "", "Provided VolumeBackup CRD will be updated with the latest backup progress details") + cmds.PersistentFlags().StringVar(&logLevelDebug, "log-level", "", "If debug mode in kopia is to be used") cmds.AddCommand( newBackupCommand(), diff --git a/pkg/executor/kopia/kopiabackup.go b/pkg/executor/kopia/kopiabackup.go index 4bbf425e4..ec4bf0783 100644 --- a/pkg/executor/kopia/kopiabackup.go +++ b/pkg/executor/kopia/kopiabackup.go @@ -69,7 +69,6 @@ func newBackupCommand() *cobra.Command { backupCommand.Flags().StringVar(&sourcePathGlob, "source-path-glob", "", "The regexp should match only one path that will be used for backup") backupCommand.Flags().StringVar(&compression, "compression", "", "Compression type to be used") backupCommand.Flags().StringVar(&excludeFileList, "exclude-file-list", "", " list of dir names that need to be exclude in the kopia snapshot") - return backupCommand } @@ -249,6 +248,9 @@ func runKopiaCreateRepo(repository *executor.Repository) error { if err != nil { return err } + // Now check if debug log level is to be added in the command which got prepared above. + // check if debug level exists + repoCreateCmd = addLogLevelDebugToCommand(repoCreateCmd, logLevelDebug) // NFS doesn't need any special treatment for repo create command // hence no case exist for it. switch repository.Type { @@ -323,6 +325,8 @@ func runKopiaBackup(repository *executor.Repository, sourcePath string) error { if err != nil { return err } + // Check and add debug level logs for kopia backup command + backupCmd = addLogLevelDebugToCommand(backupCmd, logLevelDebug) // This is needed to handle case where after kopia repo create was successful and // the pod got terminated. Now user triggers another backup, so we need to pass // credentials for "snapshot create". @@ -386,6 +390,8 @@ func runKopiaRepositoryConnect(repository *executor.Repository) error { if err != nil { return err } + // Check and add debug level logs for kopia connect command + connectCmd = addLogLevelDebugToCommand(connectCmd, logLevelDebug) switch repository.Type { case storkv1.BackupLocationS3: @@ -430,6 +436,7 @@ func setGlobalPolicy() error { if err != nil { return err } + policyCmd = addLogLevelDebugToCommand(policyCmd, logLevelDebug) // As we don't want kopia maintenance to kick in and trigger global policy on default values // for the repository, setting them to very high values policyCmd = addPolicySetting(policyCmd) @@ -483,6 +490,8 @@ func runKopiaExcludeFileList(repository *executor.Repository, sourcePath string) if err != nil { return err } + // Check and add debug level logs for kopia exclude file list command + excludeFileListCmd = addLogLevelDebugToCommand(excludeFileListCmd, logLevelDebug) excludeFileListExecutor := kopia.NewExcludeFileListExecutor(excludeFileListCmd) if err := excludeFileListExecutor.Run(); err != nil { err = fmt.Errorf("failed to run exclude file list command: %v", err) @@ -529,6 +538,8 @@ func runKopiaCompression(repository *executor.Repository, sourcePath string) err if err != nil { return err } + // Check and add debug level logs for kopia compression command + compressionCmd = addLogLevelDebugToCommand(compressionCmd, logLevelDebug) compressionExecutor := kopia.NewCompressionExecutor(compressionCmd) if err := compressionExecutor.Run(); err != nil { err = fmt.Errorf("failed to run compression command: %v", err) @@ -677,3 +688,12 @@ func addPolicySetting(policyCmd *kopia.Command) *kopia.Command { return policyCmd } + +func addLogLevelDebugToCommand(kopiaCommand *kopia.Command, logLevelDebug string) *kopia.Command { + if logLevelDebug != "" { + // Kopia command to be run with debug log levels now. + kopiaCommand.AddArg("--log-level") + kopiaCommand.AddArg("debug") + } + return kopiaCommand +} diff --git a/pkg/executor/kopia/kopiadelete.go b/pkg/executor/kopia/kopiadelete.go index ac4076cb8..63140a9b5 100644 --- a/pkg/executor/kopia/kopiadelete.go +++ b/pkg/executor/kopia/kopiadelete.go @@ -133,6 +133,8 @@ func runKopiaDelete(repository *executor.Repository, snapshotID string) error { logrus.Errorf("%s %v", fn, errMsg) return fmt.Errorf(errMsg) } + // Check and add debug level logs for kopia delete command + deleteCmd = addLogLevelDebugToCommand(deleteCmd, logLevelDebug) initExecutor := kopia.NewDeleteExecutor(deleteCmd) if err := initExecutor.Run(); err != nil { errMsg := fmt.Sprintf("running delete backup snapshot command for snapshotID [%v] failed: %v", snapshotID, err) @@ -163,11 +165,11 @@ func runKopiaSnapshotList(repository *executor.Repository) ([]string, error) { var listCmd *kopia.Command logrus.Infof("Executing kopia snapshot list command") listCmd, err = kopia.GetListCommand() - if err != nil { return nil, err } - + // Check and add bebug level logs for kopia snapshot list command + listCmd = addLogLevelDebugToCommand(listCmd, logLevelDebug) listExecutor := kopia.NewListExecutor(listCmd) if err := listExecutor.Run(); err != nil { err = fmt.Errorf("failed to run snapshot list command: %v", err) diff --git a/pkg/executor/kopia/maintenance.go b/pkg/executor/kopia/maintenance.go index b5e2ffd12..ede44b57f 100644 --- a/pkg/executor/kopia/maintenance.go +++ b/pkg/executor/kopia/maintenance.go @@ -280,7 +280,8 @@ func runKopiaQuickMaintenanceExecute(repository *executor.Repository) error { logrus.Errorf("%s %v", fn, errMsg) return fmt.Errorf(errMsg) } - + // Check and add debug log level for kopia maintenance command + maintenanceRunCmd = addLogLevelDebugToCommand(maintenanceRunCmd, logLevelDebug) initExecutor := kopia.NewMaintenanceRunExecutor(maintenanceRunCmd) if err := initExecutor.Run(); err != nil { errMsg := fmt.Sprintf("running maintenance run command for [%v] failed: %v", repository.Name, err) @@ -312,6 +313,8 @@ func runKopiaMaintenanceExecute(repository *executor.Repository) error { logrus.Errorf("%s %v", fn, errMsg) return fmt.Errorf(errMsg) } + // Check and add debug log level for kopia maintenance command + maintenanceRunCmd = addLogLevelDebugToCommand(maintenanceRunCmd, logLevelDebug) initExecutor := kopia.NewMaintenanceRunExecutor(maintenanceRunCmd) if err := initExecutor.Run(); err != nil { errMsg := fmt.Sprintf("running maintenance run command for [%v] failed: %v", repository.Name, err) @@ -343,6 +346,8 @@ func runKopiaMaintenanceSet(repository *executor.Repository) error { logrus.Errorf("%s %v", fn, errMsg) return fmt.Errorf(errMsg) } + // Check and add debug log level for kopia maintenance set command + maintenanceSetCmd = addLogLevelDebugToCommand(maintenanceSetCmd, logLevelDebug) initExecutor := kopia.NewMaintenanceSetExecutor(maintenanceSetCmd) if err := initExecutor.Run(); err != nil { errMsg := fmt.Sprintf("running maintenance set command for failed: %v", err) diff --git a/pkg/executor/kopia/restore.go b/pkg/executor/kopia/restore.go index f5be6e2eb..cd8c221e4 100644 --- a/pkg/executor/kopia/restore.go +++ b/pkg/executor/kopia/restore.go @@ -40,7 +40,6 @@ func newRestoreCommand() *cobra.Command { restoreCommand.Flags().StringVar(&targetPath, "target-path", "", "Destination path for kopia restore") restoreCommand.Flags().StringVar(&snapshotID, "snapshot-id", "", "Snapshot id of the restore") restoreCommand.Flags().StringVar(&appRestoreCR, "app-restore-cr", "", "ApplicationRestore CR name") - return restoreCommand } @@ -107,7 +106,8 @@ func runKopiaRestore(repository *executor.Repository, targetPath, snapshotID str if err != nil { return err } - + // Check and add debug level logs for kopia restore command + restoreCmd = addLogLevelDebugToCommand(restoreCmd, logLevelDebug) initExecutor := kopia.NewRestoreExecutor(restoreCmd) if err := initExecutor.Run(); err != nil { err = fmt.Errorf("failed to run restore command: %v", err)