Skip to content

Commit

Permalink
PB-5402: kdmp can now use koia debug mode
Browse files Browse the repository at this point in the history
Signed-off-by: kgarg-px <[email protected]>
  • Loading branch information
kgarg-px committed Dec 1, 2023
1 parent 68149a5 commit 8a2f2cc
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 5 deletions.
4 changes: 3 additions & 1 deletion pkg/drivers/kopiabackup/kopiabackup.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ func jobFor(
"/data",
}, " ")

cmd = utils.CheckAndAddKopiaDebugModeAnnotationsCommand(cmd, jobOption)

if jobOption.Compression != "" {
splitCmd := strings.Split(cmd, " ")
splitCmd = append(splitCmd, "--compression", jobOption.Compression)
Expand Down Expand Up @@ -507,7 +509,7 @@ func roleFor(live bool) *rbacv1.Role {
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{"kdmp.portworx.com"},
Resources: []string{"volumebackups"},
Resources: []string{"volumebackups", "dataexports"},
Verbs: []string{rbacv1.VerbAll},
},
},
Expand Down
2 changes: 2 additions & 0 deletions pkg/drivers/kopiadelete/kopiadelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ func jobFor(
jobOption.VolumeBackupDeleteNamespace,
}, " ")

cmd = utils.CheckAndAddKopiaDebugModeAnnotationsCommand(cmd, jobOption)

kopiaExecutorImage, imageRegistrySecret, err := utils.GetExecutorImageAndSecret(drivers.KopiaExecutorImage,
jobOption.KopiaImageExecutorSource,
jobOption.KopiaImageExecutorSourceNs,
Expand Down
2 changes: 2 additions & 0 deletions pkg/drivers/kopiamaintenance/kopiamaintenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ func jobFor(
jobOption.MaintenanceType,
}, " ")

cmd = utils.CheckAndAddKopiaDebugModeAnnotationsCommand(cmd, jobOption)

kopiaExecutorImage, imageRegistrySecret, err := utils.GetExecutorImageAndSecret(drivers.KopiaExecutorImage,
jobOption.KopiaImageExecutorSource,
jobOption.KopiaImageExecutorSourceNs,
Expand Down
4 changes: 3 additions & 1 deletion pkg/drivers/kopiarestore/kopiarestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ func jobFor(
vb.Status.SnapshotID,
}, " ")

cmd = utils.CheckAndAddKopiaDebugModeAnnotationsCommand(cmd, jobOption)

kopiaExecutorImage, imageRegistrySecret, err := utils.GetExecutorImageAndSecret(drivers.KopiaExecutorImage,
jobOption.KopiaImageExecutorSource,
jobOption.KopiaImageExecutorSourceNs,
Expand Down Expand Up @@ -369,7 +371,7 @@ func roleFor() *rbacv1.Role {
},
{
APIGroups: []string{"kdmp.portworx.com"},
Resources: []string{"volumebackups"},
Resources: []string{"volumebackups", "dataexports"},
Verbs: []string{rbacv1.VerbAll},
},
},
Expand Down
9 changes: 9 additions & 0 deletions pkg/drivers/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type JobOpts struct {
VolumeBackupName string
VolumeBackupNamespace string
VolumeBackupDeleteName string
KopiaDebugMode bool
VolumeBackupDeleteNamespace string
DataExportName string
SnapshotID string
Expand Down Expand Up @@ -395,6 +396,14 @@ func WithVolumeBackupDeleteName(name string) JobOption {
}
}

// WithKopiaDebugMode is job parameter
func WithKopiaDebugMode(debugMode bool) JobOption {
return func(opts *JobOpts) error {
opts.KopiaDebugMode = debugMode
return nil
}
}

// WithVolumeBackupDeleteNamespace is job parameter.
func WithVolumeBackupDeleteNamespace(ns string) JobOption {
return func(opts *JobOpts) error {
Expand Down
27 changes: 27 additions & 0 deletions pkg/drivers/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"context"
"errors"
"fmt"
"os"
Expand All @@ -12,6 +13,7 @@ import (
storkapi "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1"
"github.com/libopenstorage/stork/pkg/k8sutils"
"github.com/portworx/kdmp/pkg/drivers"
kdmpops "github.com/portworx/kdmp/pkg/util/ops"
"github.com/portworx/kdmp/pkg/version"
"github.com/portworx/sched-ops/k8s/apps"
"github.com/portworx/sched-ops/k8s/core"
Expand Down Expand Up @@ -63,6 +65,8 @@ 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"
)

var (
Expand Down Expand Up @@ -876,3 +880,26 @@ func IsJobPodMountFailed(job *batchv1.Job, namespace string) bool {
}
return false
}

func IsKopiaDebugModeAnnotationsEnabled(jobOption drivers.JobOpts) bool {
dataExportCR, err := kdmpops.Instance().GetDataExport(context.Background(), jobOption.DataExportName, jobOption.Namespace)
if err != nil {
logrus.Tracef("error reading data export job: %v", err)
return false
}

if _, ok := dataExportCR.Annotations[KopiaDebugModeEnabled]; ok {
logrus.Infof("annotation %v found in the data export CR.", KopiaDebugModeEnabled)
return true
}
return false
}

func CheckAndAddKopiaDebugModeAnnotationsCommand(cmd string, jobOption drivers.JobOpts) string {
if IsKopiaDebugModeAnnotationsEnabled(jobOption) {
splitCmd := strings.Split(cmd, " ")
splitCmd = append(splitCmd, "--log-level", "debug")
cmd = strings.Join(splitCmd, " ")
}
return cmd
}
36 changes: 36 additions & 0 deletions pkg/executor/kopia/kopiabackup.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
bkpNamespace string
compression string
excludeFileList string
logLevelDebug string
)

var (
Expand Down Expand Up @@ -69,6 +70,7 @@ 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")
backupCommand.Flags().StringVar(&logLevelDebug, "log-level", "", "If debug mode in kopia is to be used")

return backupCommand
}
Expand Down Expand Up @@ -249,6 +251,11 @@ 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 = isKopiaDebugModeEnabled(repoCreateCmd, logLevelDebug)

// NFS doesn't need any special treatment for repo create command
// hence no case exist for it.
switch repository.Type {
Expand Down Expand Up @@ -323,6 +330,9 @@ func runKopiaBackup(repository *executor.Repository, sourcePath string) error {
if err != nil {
return err
}

// Check and add debug level logs for kopia backup command
backupCmd = isKopiaDebugModeEnabled(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".
Expand Down Expand Up @@ -386,6 +396,8 @@ func runKopiaRepositoryConnect(repository *executor.Repository) error {
if err != nil {
return err
}
// Check and add debug level logs for kopia connect command
connectCmd = isKopiaDebugModeEnabled(connectCmd, logLevelDebug)

switch repository.Type {
case storkv1.BackupLocationS3:
Expand Down Expand Up @@ -427,6 +439,8 @@ func runKopiaRepositoryConnect(repository *executor.Repository) error {
func setGlobalPolicy() error {
logrus.Infof("Setting global policy")
policyCmd, err := kopia.SetGlobalPolicyCommand()
policyCmd = isKopiaDebugModeEnabled(policyCmd, logLevelDebug)

if err != nil {
return err
}
Expand Down Expand Up @@ -483,6 +497,10 @@ 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 = isKopiaDebugModeEnabled(excludeFileListCmd, logLevelDebug)

excludeFileListExecutor := kopia.NewExcludeFileListExecutor(excludeFileListCmd)
if err := excludeFileListExecutor.Run(); err != nil {
err = fmt.Errorf("failed to run exclude file list command: %v", err)
Expand Down Expand Up @@ -529,6 +547,10 @@ func runKopiaCompression(repository *executor.Repository, sourcePath string) err
if err != nil {
return err
}

// Check and add debug level logs for kopia compression command
compressionCmd = isKopiaDebugModeEnabled(compressionCmd, logLevelDebug)

compressionExecutor := kopia.NewCompressionExecutor(compressionCmd)
if err := compressionExecutor.Run(); err != nil {
err = fmt.Errorf("failed to run compression command: %v", err)
Expand Down Expand Up @@ -677,3 +699,17 @@ func addPolicySetting(policyCmd *kopia.Command) *kopia.Command {

return policyCmd
}

func isKopiaDebugModeEnabled(kopiaCommand *kopia.Command, logLevelDebug string) *kopia.Command {
if logLevelDebug != "" {
kopiaCommand = addLogLevelDebugToCommand(kopiaCommand)
}
return kopiaCommand
}

func addLogLevelDebugToCommand(initCmd *kopia.Command) *kopia.Command {
// Kopia command to be run with debug log levels now.
initCmd.AddArg("--log-level")
initCmd.AddArg("debug")
return initCmd
}
9 changes: 8 additions & 1 deletion pkg/executor/kopia/kopiadelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func newDeleteCommand() *cobra.Command {
credSecretNamespace string
volumeBackupDeleteName string
volumeBackupDeleteNamespace string
logLevelDebug string
)
deleteCommand := &cobra.Command{
Use: "delete",
Expand All @@ -34,6 +35,7 @@ func newDeleteCommand() *cobra.Command {
deleteCommand.Flags().StringVar(&credSecretNamespace, "cred-secret-namespace", "", "cred secret namespace for kopia backup snapshot that need to be deleted")
deleteCommand.Flags().StringVar(&volumeBackupDeleteName, "volume-backup-delete-name", "", "volumeBackupdelete CR name for kopia backup snapshot that need to be deleted")
deleteCommand.Flags().StringVar(&volumeBackupDeleteNamespace, "volume-backup-delete-namespace", "", "volumeBackupdelete CR namespace for kopia backup snapshot that need to be deleted")
deleteCommand.Flags().StringVar(&logLevelDebug, "log-level", "", "If debug mode in kopia is to be used")
return deleteCommand
}

Expand Down Expand Up @@ -133,6 +135,9 @@ 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 = isKopiaDebugModeEnabled(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)
Expand Down Expand Up @@ -163,11 +168,13 @@ 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 = isKopiaDebugModeEnabled(listCmd, logLevelDebug)

listExecutor := kopia.NewListExecutor(listCmd)
if err := listExecutor.Run(); err != nil {
err = fmt.Errorf("failed to run snapshot list command: %v", err)
Expand Down
10 changes: 10 additions & 0 deletions pkg/executor/kopia/maintenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func newMaintenanceCommand() *cobra.Command {
credSecretName string
credSecretNamespace string
maintenanceType string
logLevelDebug string
)
maintenanceCommand := &cobra.Command{
Use: "maintenance",
Expand All @@ -51,6 +52,7 @@ func newMaintenanceCommand() *cobra.Command {
maintenanceCommand.Flags().StringVar(&maintenanceStatusName, "maintenance-status-name", "", "backuplocation maintenance status CR name, where repo maintenance status will be stored")
maintenanceCommand.Flags().StringVar(&maintenanceStatusNamespace, "maintenance-status-namespace", "", "backuplocation maintenance status CR namespace, where repo maintenance status will be stored")
maintenanceCommand.Flags().StringVar(&maintenanceType, "maintenance-type", "", "full - will run full maintenance and quick - will run quick maintenance")
maintenanceCommand.Flags().StringVar(&logLevelDebug, "log-level", "", "If debug mode in kopia is to be used")
return maintenanceCommand
}

Expand Down Expand Up @@ -281,6 +283,8 @@ func runKopiaQuickMaintenanceExecute(repository *executor.Repository) error {
return fmt.Errorf(errMsg)
}

// Check and add debug log level for kopia maintenance command
maintenanceRunCmd = isKopiaDebugModeEnabled(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)
Expand Down Expand Up @@ -312,6 +316,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 = isKopiaDebugModeEnabled(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)
Expand Down Expand Up @@ -343,6 +349,10 @@ 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 = isKopiaDebugModeEnabled(maintenanceSetCmd, logLevelDebug)

initExecutor := kopia.NewMaintenanceSetExecutor(maintenanceSetCmd)
if err := initExecutor.Run(); err != nil {
errMsg := fmt.Sprintf("running maintenance set command for failed: %v", err)
Expand Down
9 changes: 7 additions & 2 deletions pkg/executor/kopia/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ var (

func newRestoreCommand() *cobra.Command {
var (
targetPath string
snapshotID string
targetPath string
snapshotID string
logLevelDebug string
)
restoreCommand := &cobra.Command{
Use: "restore",
Expand All @@ -40,6 +41,7 @@ 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")
restoreCommand.Flags().StringVar(&logLevelDebug, "log-level", "", "If debug mode in kopia is to be used")

return restoreCommand
}
Expand Down Expand Up @@ -108,6 +110,9 @@ func runKopiaRestore(repository *executor.Repository, targetPath, snapshotID str
return err
}

// Check and add debug level logs for kopia restore command
restoreCmd = isKopiaDebugModeEnabled(restoreCmd, logLevelDebug)

initExecutor := kopia.NewRestoreExecutor(restoreCmd)
if err := initExecutor.Run(); err != nil {
err = fmt.Errorf("failed to run restore command: %v", err)
Expand Down
Loading

0 comments on commit 8a2f2cc

Please sign in to comment.