From 62f75ef47b525a749bcb1918fa9cedb1cdcc752c Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Fri, 2 Aug 2024 23:36:18 +0530 Subject: [PATCH 01/10] Introduce two new args to kanister functions that create new pods --- pkg/function/backup_data_stats.go | 31 ++++++++++- pkg/function/checkRepository.go | 28 +++++++++- pkg/function/copy_volume_data.go | 17 +++++- pkg/function/delete_data.go | 35 +++++++++++- pkg/function/delete_data_all.go | 30 ++++++++++- .../delete_data_using_kopia_server.go | 16 ++++++ pkg/function/export_rds_snapshot_location.go | 53 +++++++++++++++++-- pkg/function/kube_task.go | 37 ++++++++++--- pkg/function/prepare_data.go | 42 +++++++++++---- pkg/function/restore_data.go | 37 +++++++++++-- pkg/function/restore_data_all.go | 15 +++++- .../restore_data_using_kopia_server.go | 14 +++++ pkg/function/restore_rds_snapshot.go | 19 +++++-- 13 files changed, 337 insertions(+), 37 deletions(-) diff --git a/pkg/function/backup_data_stats.go b/pkg/function/backup_data_stats.go index f46aef3730..a0ba3851b3 100644 --- a/pkg/function/backup_data_stats.go +++ b/pkg/function/backup_data_stats.go @@ -47,6 +47,8 @@ const ( BackupDataStatsEncryptionKeyArg = "encryptionKey" // BackupDataStatsBackupIdentifierArg provides a unique ID added to the backed up artifacts BackupDataStatsBackupIdentifierArg = "backupID" + BackupDataStatsPodLabelsArg = "podLabels" + BackupDataStatsPodAnnotationsArg = "podAnnotations" // BackupDataStatsMode provides a mode for stats BackupDataStatsMode = "statsMode" BackupDataStatsOutputFileCount = "fileCount" @@ -69,13 +71,28 @@ func (*BackupDataStatsFunc) Name() string { return BackupDataStatsFuncName } -func backupDataStats(ctx context.Context, cli kubernetes.Interface, tp param.TemplateParams, namespace, encryptionKey, backupArtifactPrefix, backupID, mode, jobPrefix string, podOverride crv1alpha1.JSONMap) (map[string]interface{}, error) { +func backupDataStats( + ctx context.Context, + cli kubernetes.Interface, + tp param.TemplateParams, + namespace, + encryptionKey, + backupArtifactPrefix, + backupID, + mode, + jobPrefix string, + podOverride crv1alpha1.JSONMap, + annotations, + labels map[string]string, +) (map[string]interface{}, error) { options := &kube.PodOptions{ Namespace: namespace, GenerateName: jobPrefix, Image: consts.GetKanisterToolsImage(), Command: []string{"sh", "-c", "tail -f /dev/null"}, PodOverride: podOverride, + Annotations: annotations, + Labels: labels, } // Apply the registered ephemeral pod changes. @@ -148,6 +165,7 @@ func (b *BackupDataStatsFunc) Exec(ctx context.Context, tp param.TemplateParams, var namespace, backupArtifactPrefix, backupID, mode, encryptionKey string var err error + var annotations, labels map[string]string if err = Arg(args, BackupDataStatsNamespaceArg, &namespace); err != nil { return nil, err } @@ -163,6 +181,13 @@ func (b *BackupDataStatsFunc) Exec(ctx context.Context, tp param.TemplateParams, if err = OptArg(args, BackupDataStatsEncryptionKeyArg, &encryptionKey, restic.GeneratePassword()); err != nil { return nil, err } + if err = OptArg(args, BackupDataStatsPodAnnotationsArg, &annotations, nil); err != nil { + return nil, err + } + if err = OptArg(args, BackupDataStatsPodLabelsArg, &labels, nil); err != nil { + return nil, err + } + podOverride, err := GetPodSpecOverride(tp, args, CheckRepositoryPodOverrideArg) if err != nil { return nil, err @@ -178,7 +203,7 @@ func (b *BackupDataStatsFunc) Exec(ctx context.Context, tp param.TemplateParams, if err != nil { return nil, errors.Wrapf(err, "Failed to create Kubernetes client") } - return backupDataStats(ctx, cli, tp, namespace, encryptionKey, backupArtifactPrefix, backupID, mode, backupDataStatsJobPrefix, podOverride) + return backupDataStats(ctx, cli, tp, namespace, encryptionKey, backupArtifactPrefix, backupID, mode, backupDataStatsJobPrefix, podOverride, annotations, labels) } func (*BackupDataStatsFunc) RequiredArgs() []string { @@ -196,6 +221,8 @@ func (*BackupDataStatsFunc) Arguments() []string { BackupDataStatsBackupIdentifierArg, BackupDataStatsMode, BackupDataStatsEncryptionKeyArg, + BackupDataStatsPodAnnotationsArg, + BackupDataStatsPodLabelsArg, } } diff --git a/pkg/function/checkRepository.go b/pkg/function/checkRepository.go index 3f46c4def6..e81970d849 100644 --- a/pkg/function/checkRepository.go +++ b/pkg/function/checkRepository.go @@ -29,6 +29,8 @@ const ( CheckRepositoryEncryptionKeyArg = "encryptionKey" // CheckRepositoryPodOverrideArg contains pod specs to override default pod specs CheckRepositoryPodOverrideArg = "podOverride" + CheckRepositoryPodAnnotationsArg = "podAnnotations" + CheckRepositoryPodLabelsArg = "podLabels" CheckRepositoryJobPrefix = "check-repository-" CheckRepositoryPasswordIncorrect = "passwordIncorrect" CheckRepositoryRepoDoesNotExist = "repoUnavailable" @@ -48,7 +50,18 @@ func (*CheckRepositoryFunc) Name() string { return CheckRepositoryFuncName } -func CheckRepository(ctx context.Context, cli kubernetes.Interface, tp param.TemplateParams, encryptionKey, targetPaths, jobPrefix string, insecureTLS bool, podOverride crv1alpha1.JSONMap) (map[string]interface{}, error) { +func CheckRepository( + ctx context.Context, + cli kubernetes.Interface, + tp param.TemplateParams, + encryptionKey, + targetPaths, + jobPrefix string, + insecureTLS bool, + podOverride crv1alpha1.JSONMap, + annotations, + labels map[string]string, +) (map[string]interface{}, error) { namespace, err := kube.GetControllerNamespace() if err != nil { return nil, errors.Wrapf(err, "Failed to get controller namespace") @@ -59,6 +72,8 @@ func CheckRepository(ctx context.Context, cli kubernetes.Interface, tp param.Tem Image: consts.GetKanisterToolsImage(), Command: []string{"sh", "-c", "tail -f /dev/null"}, PodOverride: podOverride, + Annotations: annotations, + Labels: labels, } // Apply the registered ephemeral pod changes. @@ -136,6 +151,7 @@ func (c *CheckRepositoryFunc) Exec(ctx context.Context, tp param.TemplateParams, var checkRepositoryArtifactPrefix, encryptionKey string var insecureTLS bool + var annotations, labels map[string]string if err := Arg(args, CheckRepositoryArtifactPrefixArg, &checkRepositoryArtifactPrefix); err != nil { return nil, err } @@ -145,6 +161,12 @@ func (c *CheckRepositoryFunc) Exec(ctx context.Context, tp param.TemplateParams, if err := OptArg(args, InsecureTLS, &insecureTLS, false); err != nil { return nil, err } + if err := OptArg(args, CheckRepositoryPodAnnotationsArg, &annotations, nil); err != nil { + return nil, err + } + if err := OptArg(args, CheckRepositoryPodLabelsArg, &labels, nil); err != nil { + return nil, err + } podOverride, err := GetPodSpecOverride(tp, args, CheckRepositoryPodOverrideArg) if err != nil { @@ -161,7 +183,7 @@ func (c *CheckRepositoryFunc) Exec(ctx context.Context, tp param.TemplateParams, if err != nil { return nil, errors.Wrapf(err, "Failed to create Kubernetes client") } - return CheckRepository(ctx, cli, tp, encryptionKey, checkRepositoryArtifactPrefix, CheckRepositoryJobPrefix, insecureTLS, podOverride) + return CheckRepository(ctx, cli, tp, encryptionKey, checkRepositoryArtifactPrefix, CheckRepositoryJobPrefix, insecureTLS, podOverride, annotations, labels) } func (*CheckRepositoryFunc) RequiredArgs() []string { @@ -173,6 +195,8 @@ func (*CheckRepositoryFunc) Arguments() []string { CheckRepositoryArtifactPrefixArg, CheckRepositoryEncryptionKeyArg, InsecureTLS, + CheckRepositoryPodAnnotationsArg, + CheckRepositoryPodLabelsArg, } } diff --git a/pkg/function/copy_volume_data.go b/pkg/function/copy_volume_data.go index 96756b074c..c97635504a 100644 --- a/pkg/function/copy_volume_data.go +++ b/pkg/function/copy_volume_data.go @@ -46,6 +46,8 @@ const ( CopyVolumeDataNamespaceArg = "namespace" CopyVolumeDataVolumeArg = "volume" CopyVolumeDataArtifactPrefixArg = "dataArtifactPrefix" + CopyVolumeDataPodAnnotationsArg = "podAnnotations" + CopyVolumeDataPodLabelsArg = "podLabels" CopyVolumeDataOutputBackupID = "backupID" CopyVolumeDataOutputBackupRoot = "backupRoot" CopyVolumeDataOutputBackupArtifactLocation = "backupArtifactLocation" @@ -81,6 +83,8 @@ func copyVolumeData( encryptionKey string, insecureTLS bool, podOverride map[string]interface{}, + annotations, + labels map[string]string, ) (map[string]interface{}, error) { // Validate PVC exists pvc, err := cli.CoreV1().PersistentVolumeClaims(namespace).Get(ctx, pvcName, metav1.GetOptions{}) @@ -100,6 +104,8 @@ func copyVolumeData( ReadOnly: kube.PVCContainsReadOnlyAccessMode(pvc), }}, PodOverride: podOverride, + Annotations: annotations, + Labels: labels, } // Apply the registered ephemeral pod changes. @@ -194,6 +200,7 @@ func (c *copyVolumeDataFunc) Exec(ctx context.Context, tp param.TemplateParams, var namespace, vol, targetPath, encryptionKey string var err error + var annotations, labels map[string]string var insecureTLS bool if err = Arg(args, CopyVolumeDataNamespaceArg, &namespace); err != nil { return nil, err @@ -210,6 +217,12 @@ func (c *copyVolumeDataFunc) Exec(ctx context.Context, tp param.TemplateParams, if err = OptArg(args, InsecureTLS, &insecureTLS, false); err != nil { return nil, err } + if err = OptArg(args, CopyVolumeDataPodAnnotationsArg, &annotations, nil); err != nil { + return nil, err + } + if err = OptArg(args, CopyVolumeDataPodLabelsArg, &labels, nil); err != nil { + return nil, err + } podOverride, err := GetPodSpecOverride(tp, args, CopyVolumeDataPodOverrideArg) if err != nil { return nil, err @@ -225,7 +238,7 @@ func (c *copyVolumeDataFunc) Exec(ctx context.Context, tp param.TemplateParams, if err != nil { return nil, errors.Wrapf(err, "Failed to create Kubernetes client") } - return copyVolumeData(ctx, cli, tp, namespace, vol, targetPath, encryptionKey, insecureTLS, podOverride) + return copyVolumeData(ctx, cli, tp, namespace, vol, targetPath, encryptionKey, insecureTLS, podOverride, annotations, labels) } func (*copyVolumeDataFunc) RequiredArgs() []string { @@ -243,6 +256,8 @@ func (*copyVolumeDataFunc) Arguments() []string { CopyVolumeDataArtifactPrefixArg, CopyVolumeDataEncryptionKeyArg, InsecureTLS, + CopyVolumeDataPodAnnotationsArg, + CopyVolumeDataPodLabelsArg, } } diff --git a/pkg/function/delete_data.go b/pkg/function/delete_data.go index 6867666b4a..78b17f212d 100644 --- a/pkg/function/delete_data.go +++ b/pkg/function/delete_data.go @@ -48,7 +48,9 @@ const ( // DeleteDataBackupIdentifierArg provides a unique ID added to the backed up artifacts DeleteDataBackupIdentifierArg = "backupID" // DeleteDataBackupTagArg provides a unique tag added to the backed up artifacts - DeleteDataBackupTagArg = "backupTag" + DeleteDataBackupTagArg = "backupTag" + DeleteDataPodAnnotationsArg = "podAnnotations" + DeleteDataPodLabelsArg = "podLabels" // DeleteDataEncryptionKeyArg provides the encryption key to be used for deletes DeleteDataEncryptionKeyArg = "encryptionKey" // DeleteDataReclaimSpace provides a way to specify if space should be reclaimed @@ -87,6 +89,8 @@ func deleteData( deleteIdentifiers []string, jobPrefix string, podOverride crv1alpha1.JSONMap, + annotations, + labels map[string]string, ) (map[string]interface{}, error) { if (len(deleteIdentifiers) == 0) == (len(deleteTags) == 0) { return nil, errors.Errorf("Require one argument: %s or %s", DeleteDataBackupIdentifierArg, DeleteDataBackupTagArg) @@ -98,6 +102,8 @@ func deleteData( Image: consts.GetKanisterToolsImage(), Command: []string{"sh", "-c", "tail -f /dev/null"}, PodOverride: podOverride, + Annotations: annotations, + Labels: labels, } // Apply the registered ephemeral pod changes. @@ -219,6 +225,7 @@ func (d *deleteDataFunc) Exec(ctx context.Context, tp param.TemplateParams, args var reclaimSpace bool var err error var insecureTLS bool + var annotations, labels map[string]string if err = Arg(args, DeleteDataNamespaceArg, &namespace); err != nil { return nil, err } @@ -240,6 +247,13 @@ func (d *deleteDataFunc) Exec(ctx context.Context, tp param.TemplateParams, args if err = OptArg(args, InsecureTLS, &insecureTLS, false); err != nil { return nil, err } + if err = OptArg(args, DeleteDataPodAnnotationsArg, &annotations, nil); err != nil { + return nil, err + } + if err = OptArg(args, DeleteDataPodLabelsArg, &labels, nil); err != nil { + return nil, err + } + podOverride, err := GetPodSpecOverride(tp, args, DeleteDataPodOverrideArg) if err != nil { return nil, err @@ -255,7 +269,22 @@ func (d *deleteDataFunc) Exec(ctx context.Context, tp param.TemplateParams, args if err != nil { return nil, errors.Wrapf(err, "Failed to create Kubernetes client") } - return deleteData(ctx, cli, tp, reclaimSpace, namespace, encryptionKey, insecureTLS, strings.Fields(deleteArtifactPrefix), strings.Fields(deleteTag), strings.Fields(deleteIdentifier), deleteDataJobPrefix, podOverride) + return deleteData( + ctx, + cli, + tp, + reclaimSpace, + namespace, + encryptionKey, + insecureTLS, + strings.Fields(deleteArtifactPrefix), + strings.Fields(deleteTag), + strings.Fields(deleteIdentifier), + deleteDataJobPrefix, + podOverride, + annotations, + labels, + ) } func (*deleteDataFunc) RequiredArgs() []string { @@ -274,6 +303,8 @@ func (*deleteDataFunc) Arguments() []string { DeleteDataEncryptionKeyArg, DeleteDataReclaimSpace, InsecureTLS, + DeleteDataPodAnnotationsArg, + DeleteDataPodLabelsArg, } } diff --git a/pkg/function/delete_data_all.go b/pkg/function/delete_data_all.go index 9022b309a2..e16b630b82 100644 --- a/pkg/function/delete_data_all.go +++ b/pkg/function/delete_data_all.go @@ -40,7 +40,9 @@ const ( // DeleteDataAllBackupArtifactPrefixArg provides the path to restore backed up data DeleteDataAllBackupArtifactPrefixArg = "backupArtifactPrefix" // DeleteDataAllEncryptionKeyArg provides the encryption key to be used for deletes - DeleteDataAllEncryptionKeyArg = "encryptionKey" + DeleteDataAllEncryptionKeyArg = "encryptionKey" + DeleteDataAllPodAnnotationsArg = "podAnnotations" + DeleteDataAllPodLabelsArg = "podLabels" // DeleteDataAllReclaimSpace provides a way to specify if space should be reclaimed DeleteDataAllReclaimSpace = "reclaimSpace" // DeleteDataAllBackupInfo provides backup info required for delete @@ -73,6 +75,7 @@ func (d *deleteDataAllFunc) Exec(ctx context.Context, tp param.TemplateParams, a var reclaimSpace bool var err error var insecureTLS bool + var annotations, labels map[string]string if err = Arg(args, DeleteDataAllNamespaceArg, &namespace); err != nil { return nil, err } @@ -91,6 +94,12 @@ func (d *deleteDataAllFunc) Exec(ctx context.Context, tp param.TemplateParams, a if err = OptArg(args, InsecureTLS, &insecureTLS, false); err != nil { return nil, err } + if err = OptArg(args, DeleteDataAllPodAnnotationsArg, &annotations, nil); err != nil { + return nil, err + } + if err = OptArg(args, DeleteDataAllPodLabelsArg, &labels, nil); err != nil { + return nil, err + } podOverride, err := GetPodSpecOverride(tp, args, DeleteDataAllPodOverrideArg) if err != nil { return nil, err @@ -115,7 +124,22 @@ func (d *deleteDataAllFunc) Exec(ctx context.Context, tp param.TemplateParams, a deleteIdentifiers = append(deleteIdentifiers, info.BackupID) } - return deleteData(ctx, cli, tp, reclaimSpace, namespace, encryptionKey, insecureTLS, targetPaths, nil, deleteIdentifiers, deleteDataAllJobPrefix, podOverride) + return deleteData( + ctx, + cli, + tp, + reclaimSpace, + namespace, + encryptionKey, + insecureTLS, + targetPaths, + nil, + deleteIdentifiers, + deleteDataAllJobPrefix, + podOverride, + annotations, + labels, + ) } func (*deleteDataAllFunc) RequiredArgs() []string { @@ -134,6 +158,8 @@ func (*deleteDataAllFunc) Arguments() []string { DeleteDataAllEncryptionKeyArg, DeleteDataAllReclaimSpace, InsecureTLS, + DeleteDataAllPodAnnotationsArg, + DeleteDataAllPodLabelsArg, } } diff --git a/pkg/function/delete_data_using_kopia_server.go b/pkg/function/delete_data_using_kopia_server.go index 66ec41679d..3d99f54cc0 100644 --- a/pkg/function/delete_data_using_kopia_server.go +++ b/pkg/function/delete_data_using_kopia_server.go @@ -70,6 +70,8 @@ func (*deleteDataUsingKopiaServerFunc) Arguments() []string { DeleteDataNamespaceArg, RestoreDataImageArg, KopiaRepositoryServerUserHostname, + DeleteDataPodAnnotationsArg, + DeleteDataPodLabelsArg, } } @@ -92,6 +94,8 @@ func (d *deleteDataUsingKopiaServerFunc) Exec(ctx context.Context, tp param.Temp namespace string snapID string userHostname string + annotations map[string]string + labels map[string]string ) if err = Arg(args, DeleteDataBackupIdentifierArg, &snapID); err != nil { return nil, err @@ -105,6 +109,12 @@ func (d *deleteDataUsingKopiaServerFunc) Exec(ctx context.Context, tp param.Temp if err = OptArg(args, KopiaRepositoryServerUserHostname, &userHostname, ""); err != nil { return nil, err } + if err = OptArg(args, DeleteDataPodAnnotationsArg, &annotations, nil); err != nil { + return nil, err + } + if err = OptArg(args, DeleteDataPodLabelsArg, &labels, nil); err != nil { + return nil, err + } userPassphrase, cert, err := userCredentialsAndServerTLS(&tp) if err != nil { @@ -138,6 +148,8 @@ func (d *deleteDataUsingKopiaServerFunc) Exec(ctx context.Context, tp param.Temp snapID, tp.RepositoryServer.Username, userAccessPassphrase, + annotations, + labels, ) } @@ -161,12 +173,16 @@ func deleteDataFromServer( snapID, username, userPassphrase string, + annotations, + labels map[string]string, ) (map[string]any, error) { options := &kube.PodOptions{ Namespace: namespace, GenerateName: jobPrefix, Image: image, Command: []string{"bash", "-c", "tail -f /dev/null"}, + Annotations: annotations, + Labels: labels, } // Apply the registered ephemeral pod changes. diff --git a/pkg/function/export_rds_snapshot_location.go b/pkg/function/export_rds_snapshot_location.go index 31a6405082..e977cc1628 100644 --- a/pkg/function/export_rds_snapshot_location.go +++ b/pkg/function/export_rds_snapshot_location.go @@ -60,6 +60,8 @@ const ( ExportRDSSnapshotToLocBackupID = "backupID" ExportRDSSnapshotToLocDBSubnetGroupArg = "dbSubnetGroup" ExportRDSSnapshotToLocImageArg = "image" + ExportRDSSnapshotToLocPodAnnotationsArg = "podAnnotations" + ExportRDSSnapshotToLocPodLabelsArg = "podLabels" PostgrSQLEngine RDSDBEngine = "PostgreSQL" @@ -97,6 +99,8 @@ func exportRDSSnapshotToLoc( sgIDs []string, profile *param.Profile, postgresToolsImage string, + annotations, + labels map[string]string, ) (map[string]interface{}, error) { // Validate profilextractDumpFromDBe if err := ValidateProfile(profile); err != nil { @@ -152,7 +156,23 @@ func exportRDSSnapshotToLoc( } // Extract dump from DB - output, err := execDumpCommand(ctx, dbEngine, BackupAction, namespace, dbEndpoint, username, password, databases, backupPrefix, backupID, profile, dbEngineVersion, postgresToolsImage) + output, err := execDumpCommand( + ctx, + dbEngine, + BackupAction, + namespace, + dbEndpoint, + username, + password, + databases, + backupPrefix, + backupID, + profile, + dbEngineVersion, + postgresToolsImage, + annotations, + labels, + ) if err != nil { return nil, errors.Wrap(err, "Unable to extract and push db dump to location") } @@ -178,6 +198,7 @@ func (e *exportRDSSnapshotToLocationFunc) Exec(ctx context.Context, tp param.Tem var namespace, instanceID, snapshotID, username, password, dbSubnetGroup, backupArtifact, postgresToolsImage string var dbEngine RDSDBEngine + var annotations, labels map[string]string if err := Arg(args, ExportRDSSnapshotToLocNamespaceArg, &namespace); err != nil { return nil, err @@ -206,6 +227,12 @@ func (e *exportRDSSnapshotToLocationFunc) Exec(ctx context.Context, tp param.Tem if err := OptArg(args, ExportRDSSnapshotToLocImageArg, &postgresToolsImage, defaultPostgresToolsImage); err != nil { return nil, err } + if err := OptArg(args, ExportRDSSnapshotToLocPodAnnotationsArg, &annotations, nil); err != nil { + return nil, err + } + if err := OptArg(args, ExportRDSSnapshotToLocPodLabelsArg, &labels, nil); err != nil { + return nil, err + } // Find databases databases, err := GetYamlList(args, ExportRDSSnapshotToLocDatabasesArg) if err != nil { @@ -218,7 +245,23 @@ func (e *exportRDSSnapshotToLocationFunc) Exec(ctx context.Context, tp param.Tem return nil, err } - return exportRDSSnapshotToLoc(ctx, namespace, instanceID, snapshotID, username, password, databases, dbSubnetGroup, backupArtifact, dbEngine, sgIDs, tp.Profile, postgresToolsImage) + return exportRDSSnapshotToLoc( + ctx, + namespace, + instanceID, + snapshotID, + username, + password, + databases, + dbSubnetGroup, + backupArtifact, + dbEngine, + sgIDs, + tp.Profile, + postgresToolsImage, + annotations, + labels, + ) } func (*exportRDSSnapshotToLocationFunc) RequiredArgs() []string { @@ -242,6 +285,8 @@ func (*exportRDSSnapshotToLocationFunc) Arguments() []string { ExportRDSSnapshotToLocDatabasesArg, ExportRDSSnapshotToLocSecGrpIDArg, ExportRDSSnapshotToLocDBSubnetGroupArg, + ExportRDSSnapshotToLocPodAnnotationsArg, + ExportRDSSnapshotToLocPodLabelsArg, } } @@ -275,6 +320,8 @@ func execDumpCommand( profile *param.Profile, dbEngineVersion string, postgresToolsImage string, + annotations, + labels map[string]string, ) (map[string]interface{}, error) { // Trim "\n" from creds username = strings.TrimSpace(username) @@ -305,7 +352,7 @@ func execDumpCommand( } }() - return kubeTask(ctx, cli, namespace, postgresToolsImage, command, injectPostgresSecrets(secretName)) + return kubeTask(ctx, cli, namespace, postgresToolsImage, command, injectPostgresSecrets(secretName), annotations, labels) } func prepareCommand( diff --git a/pkg/function/kube_task.go b/pkg/function/kube_task.go index 509cdfa318..31c4cb9266 100644 --- a/pkg/function/kube_task.go +++ b/pkg/function/kube_task.go @@ -39,11 +39,13 @@ const ( jobPrefix = "kanister-job-" // KubeTaskFuncName gives the function name - KubeTaskFuncName = "KubeTask" - KubeTaskNamespaceArg = "namespace" - KubeTaskImageArg = "image" - KubeTaskCommandArg = "command" - KubeTaskPodOverrideArg = "podOverride" + KubeTaskFuncName = "KubeTask" + KubeTaskNamespaceArg = "namespace" + KubeTaskImageArg = "image" + KubeTaskCommandArg = "command" + KubeTaskPodOverrideArg = "podOverride" + KubeTaskPodAnnotationsArg = "podAnnotations" + KubeTaskPodLabelsArg = "podLabels" ) func init() { @@ -60,13 +62,24 @@ func (*kubeTaskFunc) Name() string { return KubeTaskFuncName } -func kubeTask(ctx context.Context, cli kubernetes.Interface, namespace, image string, command []string, podOverride crv1alpha1.JSONMap) (map[string]interface{}, error) { +func kubeTask( + ctx context.Context, + cli kubernetes.Interface, + namespace, + image string, + command []string, + podOverride crv1alpha1.JSONMap, + annotations, + labels map[string]string, +) (map[string]interface{}, error) { options := &kube.PodOptions{ Namespace: namespace, GenerateName: jobPrefix, Image: image, Command: command, PodOverride: podOverride, + Annotations: annotations, + Labels: labels, } // Apply the registered ephemeral pod changes. @@ -110,6 +123,7 @@ func (ktf *kubeTaskFunc) Exec(ctx context.Context, tp param.TemplateParams, args var namespace, image string var command []string var err error + var annotations, labels map[string]string if err = Arg(args, KubeTaskImageArg, &image); err != nil { return nil, err } @@ -119,6 +133,13 @@ func (ktf *kubeTaskFunc) Exec(ctx context.Context, tp param.TemplateParams, args if err = OptArg(args, KubeTaskNamespaceArg, &namespace, ""); err != nil { return nil, err } + if err = OptArg(args, KubeTaskPodAnnotationsArg, &annotations, nil); err != nil { + return nil, err + } + if err = OptArg(args, KubeTaskPodLabelsArg, &labels, nil); err != nil { + return nil, err + } + podOverride, err := GetPodSpecOverride(tp, args, KubeTaskPodOverrideArg) if err != nil { return nil, err @@ -128,7 +149,7 @@ func (ktf *kubeTaskFunc) Exec(ctx context.Context, tp param.TemplateParams, args if err != nil { return nil, errors.Wrapf(err, "Failed to create Kubernetes client") } - return kubeTask(ctx, cli, namespace, image, command, podOverride) + return kubeTask(ctx, cli, namespace, image, command, podOverride, annotations, labels) } func (*kubeTaskFunc) RequiredArgs() []string { @@ -144,6 +165,8 @@ func (*kubeTaskFunc) Arguments() []string { KubeTaskCommandArg, KubeTaskNamespaceArg, KubeTaskPodOverrideArg, + KubeTaskPodLabelsArg, + KubeTaskPodAnnotationsArg, } } diff --git a/pkg/function/prepare_data.go b/pkg/function/prepare_data.go index d733608255..1f185283f8 100644 --- a/pkg/function/prepare_data.go +++ b/pkg/function/prepare_data.go @@ -40,13 +40,15 @@ const ( defaultMountPoint = "/mnt/prepare_data/%s" prepareDataJobPrefix = "prepare-data-job-" // PrepareDataFuncName gives the function name - PrepareDataFuncName = "PrepareData" - PrepareDataNamespaceArg = "namespace" - PrepareDataImageArg = "image" - PrepareDataCommandArg = "command" - PrepareDataVolumes = "volumes" - PrepareDataServiceAccount = "serviceaccount" - PrepareDataPodOverrideArg = "podOverride" + PrepareDataFuncName = "PrepareData" + PrepareDataNamespaceArg = "namespace" + PrepareDataImageArg = "image" + PrepareDataCommandArg = "command" + PrepareDataVolumes = "volumes" + PrepareDataServiceAccount = "serviceaccount" + PrepareDataPodOverrideArg = "podOverride" + PrepareDataPodAnnotationsArg = "podAnnotations" + PrepareDataPodLabelsArg = "podLabels" ) func init() { @@ -85,7 +87,18 @@ func getVolumes(tp param.TemplateParams) (map[string]string, error) { return vols, nil } -func prepareData(ctx context.Context, cli kubernetes.Interface, namespace, serviceAccount, image string, vols map[string]string, podOverride crv1alpha1.JSONMap, command ...string) (map[string]interface{}, error) { +func prepareData( + ctx context.Context, + cli kubernetes.Interface, + namespace, + serviceAccount, + image string, + vols map[string]string, + podOverride crv1alpha1.JSONMap, + annotations, + labels map[string]string, + command ...string, +) (map[string]interface{}, error) { // Validate volumes validatedVols := make(map[string]kube.VolumeMountOptions) for pvcName, mountPoint := range vols { @@ -108,6 +121,8 @@ func prepareData(ctx context.Context, cli kubernetes.Interface, namespace, servi Volumes: validatedVols, ServiceAccountName: serviceAccount, PodOverride: podOverride, + Annotations: annotations, + Labels: labels, } // Apply the registered ephemeral pod changes. @@ -155,6 +170,7 @@ func (p *prepareDataFunc) Exec(ctx context.Context, tp param.TemplateParams, arg var namespace, image, serviceAccount string var command []string var vols map[string]string + var annotations, labels map[string]string var err error if err = Arg(args, PrepareDataNamespaceArg, &namespace); err != nil { return nil, err @@ -171,6 +187,12 @@ func (p *prepareDataFunc) Exec(ctx context.Context, tp param.TemplateParams, arg if err = OptArg(args, PrepareDataServiceAccount, &serviceAccount, ""); err != nil { return nil, err } + if err = OptArg(args, PrepareDataPodAnnotationsArg, &annotations, nil); err != nil { + return nil, err + } + if err = OptArg(args, PrepareDataPodLabelsArg, &labels, nil); err != nil { + return nil, err + } podOverride, err := GetPodSpecOverride(tp, args, PrepareDataPodOverrideArg) if err != nil { return nil, err @@ -185,7 +207,7 @@ func (p *prepareDataFunc) Exec(ctx context.Context, tp param.TemplateParams, arg return nil, err } } - return prepareData(ctx, cli, namespace, serviceAccount, image, vols, podOverride, command...) + return prepareData(ctx, cli, namespace, serviceAccount, image, vols, podOverride, annotations, labels, command...) } func (*prepareDataFunc) RequiredArgs() []string { @@ -204,6 +226,8 @@ func (*prepareDataFunc) Arguments() []string { PrepareDataVolumes, PrepareDataServiceAccount, PrepareDataPodOverrideArg, + PrepareDataPodAnnotationsArg, + PrepareDataPodLabelsArg, } } diff --git a/pkg/function/restore_data.go b/pkg/function/restore_data.go index ec5ac893c2..43e2755a87 100644 --- a/pkg/function/restore_data.go +++ b/pkg/function/restore_data.go @@ -57,7 +57,9 @@ const ( // RestoreDataBackupTagArg provides a unique tag added to the backup artifacts RestoreDataBackupTagArg = "backupTag" // RestoreDataPodOverrideArg contains pod specs which overrides default pod specs - RestoreDataPodOverrideArg = "podOverride" + RestoreDataPodOverrideArg = "podOverride" + RestoreDataPodAnnotationsArg = "podAnnotations" + RestoreDataPodLabelsArg = "podLabels" ) func init() { @@ -118,8 +120,24 @@ func validateAndGetOptArgs(args map[string]interface{}, tp param.TemplateParams) return restorePath, encryptionKey, pod, vols, tag, id, insecureTLS, podOverride, err } -func restoreData(ctx context.Context, cli kubernetes.Interface, tp param.TemplateParams, namespace, encryptionKey, backupArtifactPrefix, restorePath, backupTag, backupID, jobPrefix, image string, - insecureTLS bool, vols map[string]string, podOverride crv1alpha1.JSONMap) (map[string]interface{}, error) { +func restoreData( + ctx context.Context, + cli kubernetes.Interface, + tp param.TemplateParams, + namespace, + encryptionKey, + backupArtifactPrefix, + restorePath, + backupTag, + backupID, + jobPrefix, + image string, + insecureTLS bool, + vols map[string]string, + podOverride crv1alpha1.JSONMap, + annotations, + labels map[string]string, +) (map[string]interface{}, error) { // Validate volumes validatedVols := make(map[string]kube.VolumeMountOptions) for pvcName, mountPoint := range vols { @@ -141,6 +159,8 @@ func restoreData(ctx context.Context, cli kubernetes.Interface, tp param.Templat Command: []string{"sh", "-c", "tail -f /dev/null"}, Volumes: validatedVols, PodOverride: podOverride, + Annotations: annotations, + Labels: labels, } // Apply the registered ephemeral pod changes. @@ -211,6 +231,7 @@ func (r *restoreDataFunc) Exec(ctx context.Context, tp param.TemplateParams, arg var namespace, image, backupArtifactPrefix, backupTag, backupID string var podOverride crv1alpha1.JSONMap var err error + var annotations, labels map[string]string if err = Arg(args, RestoreDataNamespaceArg, &namespace); err != nil { return nil, err } @@ -220,6 +241,12 @@ func (r *restoreDataFunc) Exec(ctx context.Context, tp param.TemplateParams, arg if err = Arg(args, RestoreDataBackupArtifactPrefixArg, &backupArtifactPrefix); err != nil { return nil, err } + if err = OptArg(args, RestoreDataPodAnnotationsArg, &annotations, nil); err != nil { + return nil, err + } + if err = OptArg(args, RestoreDataPodLabelsArg, &labels, nil); err != nil { + return nil, err + } // Validate and get optional arguments restorePath, encryptionKey, pod, vols, backupTag, backupID, insecureTLS, podOverride, err := validateAndGetOptArgs(args, tp) @@ -256,7 +283,7 @@ func (r *restoreDataFunc) Exec(ctx context.Context, tp param.TemplateParams, arg if err != nil { return nil, errors.Wrapf(err, "Failed to create Kubernetes client") } - return restoreData(ctx, cli, tp, namespace, encryptionKey, backupArtifactPrefix, restorePath, backupTag, backupID, restoreDataJobPrefix, image, insecureTLS, vols, podOverride) + return restoreData(ctx, cli, tp, namespace, encryptionKey, backupArtifactPrefix, restorePath, backupTag, backupID, restoreDataJobPrefix, image, insecureTLS, vols, podOverride, annotations, labels) } func (*restoreDataFunc) RequiredArgs() []string { @@ -280,6 +307,8 @@ func (*restoreDataFunc) Arguments() []string { RestoreDataBackupIdentifierArg, RestoreDataPodOverrideArg, InsecureTLS, + RestoreDataPodAnnotationsArg, + RestoreDataPodLabelsArg, } } diff --git a/pkg/function/restore_data_all.go b/pkg/function/restore_data_all.go index f79889c701..37254cdb23 100644 --- a/pkg/function/restore_data_all.go +++ b/pkg/function/restore_data_all.go @@ -52,7 +52,9 @@ const ( // RestoreDataAllBackupInfo provides backup info required for restore RestoreDataAllBackupInfo = "backupInfo" // RestoreDataPodOverrideArg contains pod specs which overrides default pod specs - RestoreDataAllPodOverrideArg = "podOverride" + RestoreDataAllPodOverrideArg = "podOverride" + RestoreDataAllPodAnnotationsArg = "podAnnotations" + RestoreDataAllPodLabelsArg = "podLabels" ) func init() { @@ -116,6 +118,7 @@ func (r *restoreDataAllFunc) Exec(ctx context.Context, tp param.TemplateParams, var namespace, image, backupArtifactPrefix, backupInfo string var err error + var annotations, labels map[string]string if err = Arg(args, RestoreDataAllNamespaceArg, &namespace); err != nil { return nil, err @@ -129,6 +132,12 @@ func (r *restoreDataAllFunc) Exec(ctx context.Context, tp param.TemplateParams, if err = Arg(args, RestoreDataAllBackupInfo, &backupInfo); err != nil { return nil, err } + if err = OptArg(args, RestoreDataPodAnnotationsArg, &annotations, nil); err != nil { + return nil, err + } + if err = OptArg(args, RestoreDataAllPodLabelsArg, &labels, nil); err != nil { + return nil, err + } // Validate and get optional arguments restorePath, encryptionKey, pods, insecureTLS, podOverride, err := validateAndGetRestoreAllOptArgs(args, tp) @@ -161,7 +170,7 @@ func (r *restoreDataAllFunc) Exec(ctx context.Context, tp param.TemplateParams, outputChan <- out return } - out, err = restoreData(ctx, cli, tp, namespace, encryptionKey, fmt.Sprintf("%s/%s", backupArtifactPrefix, pod), restorePath, "", input[pod].BackupID, restoreDataAllJobPrefix, image, insecureTLS, vols, podOverride) + out, err = restoreData(ctx, cli, tp, namespace, encryptionKey, fmt.Sprintf("%s/%s", backupArtifactPrefix, pod), restorePath, "", input[pod].BackupID, restoreDataAllJobPrefix, image, insecureTLS, vols, podOverride, annotations, labels) errChan <- errors.Wrapf(err, "Failed to restore data for pod %s", pod) outputChan <- out }(pod) @@ -204,6 +213,8 @@ func (*restoreDataAllFunc) Arguments() []string { RestoreDataAllPodsArg, RestoreDataAllPodOverrideArg, InsecureTLS, + RestoreDataAllPodAnnotationsArg, + RestoreDataAllPodLabelsArg, } } diff --git a/pkg/function/restore_data_using_kopia_server.go b/pkg/function/restore_data_using_kopia_server.go index 08bb06a5a3..86c4a7b10a 100644 --- a/pkg/function/restore_data_using_kopia_server.go +++ b/pkg/function/restore_data_using_kopia_server.go @@ -98,6 +98,8 @@ func (r *restoreDataUsingKopiaServerFunc) Exec(ctx context.Context, tp param.Tem restorePath string snapID string userHostname string + annotations map[string]string + labels map[string]string ) if err = Arg(args, RestoreDataBackupIdentifierArg, &snapID); err != nil { return nil, err @@ -114,6 +116,12 @@ func (r *restoreDataUsingKopiaServerFunc) Exec(ctx context.Context, tp param.Tem if err = OptArg(args, KopiaRepositoryServerUserHostname, &userHostname, ""); err != nil { return nil, err } + if err = OptArg(args, RestoreDataPodAnnotationsArg, &annotations, nil); err != nil { + return nil, err + } + if err = OptArg(args, RestoreDataPodLabelsArg, &labels, nil); err != nil { + return nil, err + } userPassphrase, cert, err := userCredentialsAndServerTLS(&tp) if err != nil { @@ -166,6 +174,8 @@ func (r *restoreDataUsingKopiaServerFunc) Exec(ctx context.Context, tp param.Tem sparseRestore, vols, podOverride, + annotations, + labels, ) } @@ -193,6 +203,8 @@ func restoreDataFromServer( sparseRestore bool, vols map[string]string, podOverride crv1alpha1.JSONMap, + annotations, + labels map[string]string, ) (map[string]any, error) { validatedVols := make(map[string]kube.VolumeMountOptions) // Validate volumes @@ -215,6 +227,8 @@ func restoreDataFromServer( Command: []string{"bash", "-c", "tail -f /dev/null"}, Volumes: validatedVols, PodOverride: podOverride, + Annotations: annotations, + Labels: labels, } // Apply the registered ephemeral pod changes. diff --git a/pkg/function/restore_rds_snapshot.go b/pkg/function/restore_rds_snapshot.go index a9f3752d98..dcb0a153b8 100644 --- a/pkg/function/restore_rds_snapshot.go +++ b/pkg/function/restore_rds_snapshot.go @@ -71,7 +71,9 @@ const ( // RestoreRDSSnapshotPassword stores the password of the database RestoreRDSSnapshotPassword = "password" // RestoreRDSSnapshotImage provides the image of the container with required tools - RestoreRDSSnapshotImage = "image" + RestoreRDSSnapshotImage = "image" + RestoreRDSSnapshotPodAnnotations = "podAnnotations" + RestoreRDSSnapshotPodLabels = "podLabels" // PostgreSQLEngine stores the postgres appname PostgreSQLEngine RDSDBEngine = "PostgreSQL" @@ -105,6 +107,8 @@ func (*restoreRDSSnapshotFunc) Arguments() []string { RestoreRDSSnapshotNamespace, RestoreRDSSnapshotSecGrpID, RestoreRDSSnapshotDBSubnetGroup, + RestoreRDSSnapshotPodAnnotations, + RestoreRDSSnapshotPodLabels, } } @@ -123,6 +127,7 @@ func (r *restoreRDSSnapshotFunc) Exec(ctx context.Context, tp param.TemplatePara var namespace, instanceID, subnetGroup, snapshotID, backupArtifactPrefix, backupID, username, password, postgresToolsImage string var dbEngine RDSDBEngine + var annotations, labels map[string]string if err := Arg(args, RestoreRDSSnapshotInstanceID, &instanceID); err != nil { return nil, err @@ -141,6 +146,12 @@ func (r *restoreRDSSnapshotFunc) Exec(ctx context.Context, tp param.TemplatePara if err := OptArg(args, RestoreRDSSnapshotImage, &postgresToolsImage, defaultPostgresToolsImage); err != nil { return nil, err } + if err := OptArg(args, RestoreRDSSnapshotPodAnnotations, &annotations, nil); err != nil { + return nil, err + } + if err := OptArg(args, RestoreRDSSnapshotPodLabels, &labels, nil); err != nil { + return nil, err + } // Find security groups sgIDs, err := GetYamlList(args, RestoreRDSSnapshotSecGrpID) if err != nil { @@ -167,7 +178,7 @@ func (r *restoreRDSSnapshotFunc) Exec(ctx context.Context, tp param.TemplatePara } } - return restoreRDSSnapshot(ctx, namespace, instanceID, subnetGroup, snapshotID, backupArtifactPrefix, backupID, username, password, dbEngine, sgIDs, tp.Profile, postgresToolsImage) + return restoreRDSSnapshot(ctx, namespace, instanceID, subnetGroup, snapshotID, backupArtifactPrefix, backupID, username, password, dbEngine, sgIDs, tp.Profile, postgresToolsImage, annotations, labels) } func (r *restoreRDSSnapshotFunc) ExecutionProgress() (crv1alpha1.PhaseProgress, error) { @@ -192,6 +203,8 @@ func restoreRDSSnapshot( sgIDs []string, profile *param.Profile, postgresToolsImage string, + annotations, + labels map[string]string, ) (map[string]interface{}, error) { // Validate profile if err := ValidateProfile(profile); err != nil { @@ -239,7 +252,7 @@ func restoreRDSSnapshot( return nil, errors.Wrapf(err, "Couldn't find DBInstance Version") } - if _, err = execDumpCommand(ctx, dbEngine, RestoreAction, namespace, dbEndpoint, username, password, nil, backupArtifactPrefix, backupID, profile, dbEngineVersion, postgresToolsImage); err != nil { + if _, err = execDumpCommand(ctx, dbEngine, RestoreAction, namespace, dbEndpoint, username, password, nil, backupArtifactPrefix, backupID, profile, dbEngineVersion, postgresToolsImage, annotations, labels); err != nil { return nil, errors.Wrapf(err, "Failed to restore RDS from dump. InstanceID=%s", instanceID) } From 60feaaef5a64425ff2e5a1736cd64d944367ab04 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Mon, 5 Aug 2024 18:34:18 +0530 Subject: [PATCH 02/10] Use consts for labels and annotations functions args Since these args are going to be named the same for all the kanister functions that support these arguments, it's better to have them as a const and use that everywhere. It will help us when we try to get labels and annotations from blueprints. --- pkg/function/backup_data_stats.go | 10 ++++---- pkg/function/checkRepository.go | 10 ++++---- pkg/function/copy_volume_data.go | 10 ++++---- pkg/function/delete_data.go | 12 ++++------ pkg/function/delete_data_all.go | 12 ++++------ .../delete_data_using_kopia_server.go | 8 +++---- pkg/function/export_rds_snapshot_location.go | 8 +++---- pkg/function/kube_task.go | 20 +++++++--------- pkg/function/prepare_data.go | 24 +++++++++---------- pkg/function/restore_data.go | 12 ++++------ pkg/function/restore_data_all.go | 12 ++++------ .../restore_data_using_kopia_server.go | 6 +++-- pkg/function/restore_rds_snapshot.go | 8 +++---- pkg/function/utils.go | 6 +++++ 14 files changed, 74 insertions(+), 84 deletions(-) diff --git a/pkg/function/backup_data_stats.go b/pkg/function/backup_data_stats.go index a0ba3851b3..6cf59e2dd0 100644 --- a/pkg/function/backup_data_stats.go +++ b/pkg/function/backup_data_stats.go @@ -47,8 +47,6 @@ const ( BackupDataStatsEncryptionKeyArg = "encryptionKey" // BackupDataStatsBackupIdentifierArg provides a unique ID added to the backed up artifacts BackupDataStatsBackupIdentifierArg = "backupID" - BackupDataStatsPodLabelsArg = "podLabels" - BackupDataStatsPodAnnotationsArg = "podAnnotations" // BackupDataStatsMode provides a mode for stats BackupDataStatsMode = "statsMode" BackupDataStatsOutputFileCount = "fileCount" @@ -181,10 +179,10 @@ func (b *BackupDataStatsFunc) Exec(ctx context.Context, tp param.TemplateParams, if err = OptArg(args, BackupDataStatsEncryptionKeyArg, &encryptionKey, restic.GeneratePassword()); err != nil { return nil, err } - if err = OptArg(args, BackupDataStatsPodAnnotationsArg, &annotations, nil); err != nil { + if err = OptArg(args, PodAnnotationsArg, &annotations, nil); err != nil { return nil, err } - if err = OptArg(args, BackupDataStatsPodLabelsArg, &labels, nil); err != nil { + if err = OptArg(args, PodLabelsArg, &labels, nil); err != nil { return nil, err } @@ -221,8 +219,8 @@ func (*BackupDataStatsFunc) Arguments() []string { BackupDataStatsBackupIdentifierArg, BackupDataStatsMode, BackupDataStatsEncryptionKeyArg, - BackupDataStatsPodAnnotationsArg, - BackupDataStatsPodLabelsArg, + PodAnnotationsArg, + PodLabelsArg, } } diff --git a/pkg/function/checkRepository.go b/pkg/function/checkRepository.go index e81970d849..a302697707 100644 --- a/pkg/function/checkRepository.go +++ b/pkg/function/checkRepository.go @@ -29,8 +29,6 @@ const ( CheckRepositoryEncryptionKeyArg = "encryptionKey" // CheckRepositoryPodOverrideArg contains pod specs to override default pod specs CheckRepositoryPodOverrideArg = "podOverride" - CheckRepositoryPodAnnotationsArg = "podAnnotations" - CheckRepositoryPodLabelsArg = "podLabels" CheckRepositoryJobPrefix = "check-repository-" CheckRepositoryPasswordIncorrect = "passwordIncorrect" CheckRepositoryRepoDoesNotExist = "repoUnavailable" @@ -161,10 +159,10 @@ func (c *CheckRepositoryFunc) Exec(ctx context.Context, tp param.TemplateParams, if err := OptArg(args, InsecureTLS, &insecureTLS, false); err != nil { return nil, err } - if err := OptArg(args, CheckRepositoryPodAnnotationsArg, &annotations, nil); err != nil { + if err := OptArg(args, PodAnnotationsArg, &annotations, nil); err != nil { return nil, err } - if err := OptArg(args, CheckRepositoryPodLabelsArg, &labels, nil); err != nil { + if err := OptArg(args, PodLabelsArg, &labels, nil); err != nil { return nil, err } @@ -195,8 +193,8 @@ func (*CheckRepositoryFunc) Arguments() []string { CheckRepositoryArtifactPrefixArg, CheckRepositoryEncryptionKeyArg, InsecureTLS, - CheckRepositoryPodAnnotationsArg, - CheckRepositoryPodLabelsArg, + PodAnnotationsArg, + PodLabelsArg, } } diff --git a/pkg/function/copy_volume_data.go b/pkg/function/copy_volume_data.go index c97635504a..905d435fa5 100644 --- a/pkg/function/copy_volume_data.go +++ b/pkg/function/copy_volume_data.go @@ -46,8 +46,6 @@ const ( CopyVolumeDataNamespaceArg = "namespace" CopyVolumeDataVolumeArg = "volume" CopyVolumeDataArtifactPrefixArg = "dataArtifactPrefix" - CopyVolumeDataPodAnnotationsArg = "podAnnotations" - CopyVolumeDataPodLabelsArg = "podLabels" CopyVolumeDataOutputBackupID = "backupID" CopyVolumeDataOutputBackupRoot = "backupRoot" CopyVolumeDataOutputBackupArtifactLocation = "backupArtifactLocation" @@ -217,10 +215,10 @@ func (c *copyVolumeDataFunc) Exec(ctx context.Context, tp param.TemplateParams, if err = OptArg(args, InsecureTLS, &insecureTLS, false); err != nil { return nil, err } - if err = OptArg(args, CopyVolumeDataPodAnnotationsArg, &annotations, nil); err != nil { + if err = OptArg(args, PodAnnotationsArg, &annotations, nil); err != nil { return nil, err } - if err = OptArg(args, CopyVolumeDataPodLabelsArg, &labels, nil); err != nil { + if err = OptArg(args, PodLabelsArg, &labels, nil); err != nil { return nil, err } podOverride, err := GetPodSpecOverride(tp, args, CopyVolumeDataPodOverrideArg) @@ -256,8 +254,8 @@ func (*copyVolumeDataFunc) Arguments() []string { CopyVolumeDataArtifactPrefixArg, CopyVolumeDataEncryptionKeyArg, InsecureTLS, - CopyVolumeDataPodAnnotationsArg, - CopyVolumeDataPodLabelsArg, + PodAnnotationsArg, + PodLabelsArg, } } diff --git a/pkg/function/delete_data.go b/pkg/function/delete_data.go index 78b17f212d..d0bdde2b28 100644 --- a/pkg/function/delete_data.go +++ b/pkg/function/delete_data.go @@ -48,9 +48,7 @@ const ( // DeleteDataBackupIdentifierArg provides a unique ID added to the backed up artifacts DeleteDataBackupIdentifierArg = "backupID" // DeleteDataBackupTagArg provides a unique tag added to the backed up artifacts - DeleteDataBackupTagArg = "backupTag" - DeleteDataPodAnnotationsArg = "podAnnotations" - DeleteDataPodLabelsArg = "podLabels" + DeleteDataBackupTagArg = "backupTag" // DeleteDataEncryptionKeyArg provides the encryption key to be used for deletes DeleteDataEncryptionKeyArg = "encryptionKey" // DeleteDataReclaimSpace provides a way to specify if space should be reclaimed @@ -247,10 +245,10 @@ func (d *deleteDataFunc) Exec(ctx context.Context, tp param.TemplateParams, args if err = OptArg(args, InsecureTLS, &insecureTLS, false); err != nil { return nil, err } - if err = OptArg(args, DeleteDataPodAnnotationsArg, &annotations, nil); err != nil { + if err = OptArg(args, PodAnnotationsArg, &annotations, nil); err != nil { return nil, err } - if err = OptArg(args, DeleteDataPodLabelsArg, &labels, nil); err != nil { + if err = OptArg(args, PodLabelsArg, &labels, nil); err != nil { return nil, err } @@ -303,8 +301,8 @@ func (*deleteDataFunc) Arguments() []string { DeleteDataEncryptionKeyArg, DeleteDataReclaimSpace, InsecureTLS, - DeleteDataPodAnnotationsArg, - DeleteDataPodLabelsArg, + PodAnnotationsArg, + PodLabelsArg, } } diff --git a/pkg/function/delete_data_all.go b/pkg/function/delete_data_all.go index e16b630b82..7a5d3f9f53 100644 --- a/pkg/function/delete_data_all.go +++ b/pkg/function/delete_data_all.go @@ -40,9 +40,7 @@ const ( // DeleteDataAllBackupArtifactPrefixArg provides the path to restore backed up data DeleteDataAllBackupArtifactPrefixArg = "backupArtifactPrefix" // DeleteDataAllEncryptionKeyArg provides the encryption key to be used for deletes - DeleteDataAllEncryptionKeyArg = "encryptionKey" - DeleteDataAllPodAnnotationsArg = "podAnnotations" - DeleteDataAllPodLabelsArg = "podLabels" + DeleteDataAllEncryptionKeyArg = "encryptionKey" // DeleteDataAllReclaimSpace provides a way to specify if space should be reclaimed DeleteDataAllReclaimSpace = "reclaimSpace" // DeleteDataAllBackupInfo provides backup info required for delete @@ -94,10 +92,10 @@ func (d *deleteDataAllFunc) Exec(ctx context.Context, tp param.TemplateParams, a if err = OptArg(args, InsecureTLS, &insecureTLS, false); err != nil { return nil, err } - if err = OptArg(args, DeleteDataAllPodAnnotationsArg, &annotations, nil); err != nil { + if err = OptArg(args, PodAnnotationsArg, &annotations, nil); err != nil { return nil, err } - if err = OptArg(args, DeleteDataAllPodLabelsArg, &labels, nil); err != nil { + if err = OptArg(args, PodLabelsArg, &labels, nil); err != nil { return nil, err } podOverride, err := GetPodSpecOverride(tp, args, DeleteDataAllPodOverrideArg) @@ -158,8 +156,8 @@ func (*deleteDataAllFunc) Arguments() []string { DeleteDataAllEncryptionKeyArg, DeleteDataAllReclaimSpace, InsecureTLS, - DeleteDataAllPodAnnotationsArg, - DeleteDataAllPodLabelsArg, + PodAnnotationsArg, + PodLabelsArg, } } diff --git a/pkg/function/delete_data_using_kopia_server.go b/pkg/function/delete_data_using_kopia_server.go index 3d99f54cc0..1f74ca8588 100644 --- a/pkg/function/delete_data_using_kopia_server.go +++ b/pkg/function/delete_data_using_kopia_server.go @@ -70,8 +70,8 @@ func (*deleteDataUsingKopiaServerFunc) Arguments() []string { DeleteDataNamespaceArg, RestoreDataImageArg, KopiaRepositoryServerUserHostname, - DeleteDataPodAnnotationsArg, - DeleteDataPodLabelsArg, + PodAnnotationsArg, + PodLabelsArg, } } @@ -109,10 +109,10 @@ func (d *deleteDataUsingKopiaServerFunc) Exec(ctx context.Context, tp param.Temp if err = OptArg(args, KopiaRepositoryServerUserHostname, &userHostname, ""); err != nil { return nil, err } - if err = OptArg(args, DeleteDataPodAnnotationsArg, &annotations, nil); err != nil { + if err = OptArg(args, PodAnnotationsArg, &annotations, nil); err != nil { return nil, err } - if err = OptArg(args, DeleteDataPodLabelsArg, &labels, nil); err != nil { + if err = OptArg(args, PodLabelsArg, &labels, nil); err != nil { return nil, err } diff --git a/pkg/function/export_rds_snapshot_location.go b/pkg/function/export_rds_snapshot_location.go index e977cc1628..39be3df9c0 100644 --- a/pkg/function/export_rds_snapshot_location.go +++ b/pkg/function/export_rds_snapshot_location.go @@ -227,10 +227,10 @@ func (e *exportRDSSnapshotToLocationFunc) Exec(ctx context.Context, tp param.Tem if err := OptArg(args, ExportRDSSnapshotToLocImageArg, &postgresToolsImage, defaultPostgresToolsImage); err != nil { return nil, err } - if err := OptArg(args, ExportRDSSnapshotToLocPodAnnotationsArg, &annotations, nil); err != nil { + if err := OptArg(args, PodAnnotationsArg, &annotations, nil); err != nil { return nil, err } - if err := OptArg(args, ExportRDSSnapshotToLocPodLabelsArg, &labels, nil); err != nil { + if err := OptArg(args, PodLabelsArg, &labels, nil); err != nil { return nil, err } // Find databases @@ -285,8 +285,8 @@ func (*exportRDSSnapshotToLocationFunc) Arguments() []string { ExportRDSSnapshotToLocDatabasesArg, ExportRDSSnapshotToLocSecGrpIDArg, ExportRDSSnapshotToLocDBSubnetGroupArg, - ExportRDSSnapshotToLocPodAnnotationsArg, - ExportRDSSnapshotToLocPodLabelsArg, + PodAnnotationsArg, + PodLabelsArg, } } diff --git a/pkg/function/kube_task.go b/pkg/function/kube_task.go index 31c4cb9266..ba161205a1 100644 --- a/pkg/function/kube_task.go +++ b/pkg/function/kube_task.go @@ -39,13 +39,11 @@ const ( jobPrefix = "kanister-job-" // KubeTaskFuncName gives the function name - KubeTaskFuncName = "KubeTask" - KubeTaskNamespaceArg = "namespace" - KubeTaskImageArg = "image" - KubeTaskCommandArg = "command" - KubeTaskPodOverrideArg = "podOverride" - KubeTaskPodAnnotationsArg = "podAnnotations" - KubeTaskPodLabelsArg = "podLabels" + KubeTaskFuncName = "KubeTask" + KubeTaskNamespaceArg = "namespace" + KubeTaskImageArg = "image" + KubeTaskCommandArg = "command" + KubeTaskPodOverrideArg = "podOverride" ) func init() { @@ -133,10 +131,10 @@ func (ktf *kubeTaskFunc) Exec(ctx context.Context, tp param.TemplateParams, args if err = OptArg(args, KubeTaskNamespaceArg, &namespace, ""); err != nil { return nil, err } - if err = OptArg(args, KubeTaskPodAnnotationsArg, &annotations, nil); err != nil { + if err = OptArg(args, PodAnnotationsArg, &annotations, nil); err != nil { return nil, err } - if err = OptArg(args, KubeTaskPodLabelsArg, &labels, nil); err != nil { + if err = OptArg(args, PodLabelsArg, &labels, nil); err != nil { return nil, err } @@ -165,8 +163,8 @@ func (*kubeTaskFunc) Arguments() []string { KubeTaskCommandArg, KubeTaskNamespaceArg, KubeTaskPodOverrideArg, - KubeTaskPodLabelsArg, - KubeTaskPodAnnotationsArg, + PodAnnotationsArg, + PodLabelsArg, } } diff --git a/pkg/function/prepare_data.go b/pkg/function/prepare_data.go index 1f185283f8..efee87275f 100644 --- a/pkg/function/prepare_data.go +++ b/pkg/function/prepare_data.go @@ -40,15 +40,13 @@ const ( defaultMountPoint = "/mnt/prepare_data/%s" prepareDataJobPrefix = "prepare-data-job-" // PrepareDataFuncName gives the function name - PrepareDataFuncName = "PrepareData" - PrepareDataNamespaceArg = "namespace" - PrepareDataImageArg = "image" - PrepareDataCommandArg = "command" - PrepareDataVolumes = "volumes" - PrepareDataServiceAccount = "serviceaccount" - PrepareDataPodOverrideArg = "podOverride" - PrepareDataPodAnnotationsArg = "podAnnotations" - PrepareDataPodLabelsArg = "podLabels" + PrepareDataFuncName = "PrepareData" + PrepareDataNamespaceArg = "namespace" + PrepareDataImageArg = "image" + PrepareDataCommandArg = "command" + PrepareDataVolumes = "volumes" + PrepareDataServiceAccount = "serviceaccount" + PrepareDataPodOverrideArg = "podOverride" ) func init() { @@ -187,10 +185,10 @@ func (p *prepareDataFunc) Exec(ctx context.Context, tp param.TemplateParams, arg if err = OptArg(args, PrepareDataServiceAccount, &serviceAccount, ""); err != nil { return nil, err } - if err = OptArg(args, PrepareDataPodAnnotationsArg, &annotations, nil); err != nil { + if err = OptArg(args, PodAnnotationsArg, &annotations, nil); err != nil { return nil, err } - if err = OptArg(args, PrepareDataPodLabelsArg, &labels, nil); err != nil { + if err = OptArg(args, PodLabelsArg, &labels, nil); err != nil { return nil, err } podOverride, err := GetPodSpecOverride(tp, args, PrepareDataPodOverrideArg) @@ -226,8 +224,8 @@ func (*prepareDataFunc) Arguments() []string { PrepareDataVolumes, PrepareDataServiceAccount, PrepareDataPodOverrideArg, - PrepareDataPodAnnotationsArg, - PrepareDataPodLabelsArg, + PodAnnotationsArg, + PodLabelsArg, } } diff --git a/pkg/function/restore_data.go b/pkg/function/restore_data.go index 43e2755a87..6ed8c881b9 100644 --- a/pkg/function/restore_data.go +++ b/pkg/function/restore_data.go @@ -57,9 +57,7 @@ const ( // RestoreDataBackupTagArg provides a unique tag added to the backup artifacts RestoreDataBackupTagArg = "backupTag" // RestoreDataPodOverrideArg contains pod specs which overrides default pod specs - RestoreDataPodOverrideArg = "podOverride" - RestoreDataPodAnnotationsArg = "podAnnotations" - RestoreDataPodLabelsArg = "podLabels" + RestoreDataPodOverrideArg = "podOverride" ) func init() { @@ -241,10 +239,10 @@ func (r *restoreDataFunc) Exec(ctx context.Context, tp param.TemplateParams, arg if err = Arg(args, RestoreDataBackupArtifactPrefixArg, &backupArtifactPrefix); err != nil { return nil, err } - if err = OptArg(args, RestoreDataPodAnnotationsArg, &annotations, nil); err != nil { + if err = OptArg(args, PodAnnotationsArg, &annotations, nil); err != nil { return nil, err } - if err = OptArg(args, RestoreDataPodLabelsArg, &labels, nil); err != nil { + if err = OptArg(args, PodLabelsArg, &labels, nil); err != nil { return nil, err } @@ -307,8 +305,8 @@ func (*restoreDataFunc) Arguments() []string { RestoreDataBackupIdentifierArg, RestoreDataPodOverrideArg, InsecureTLS, - RestoreDataPodAnnotationsArg, - RestoreDataPodLabelsArg, + PodAnnotationsArg, + PodLabelsArg, } } diff --git a/pkg/function/restore_data_all.go b/pkg/function/restore_data_all.go index 37254cdb23..8e536e4681 100644 --- a/pkg/function/restore_data_all.go +++ b/pkg/function/restore_data_all.go @@ -52,9 +52,7 @@ const ( // RestoreDataAllBackupInfo provides backup info required for restore RestoreDataAllBackupInfo = "backupInfo" // RestoreDataPodOverrideArg contains pod specs which overrides default pod specs - RestoreDataAllPodOverrideArg = "podOverride" - RestoreDataAllPodAnnotationsArg = "podAnnotations" - RestoreDataAllPodLabelsArg = "podLabels" + RestoreDataAllPodOverrideArg = "podOverride" ) func init() { @@ -132,10 +130,10 @@ func (r *restoreDataAllFunc) Exec(ctx context.Context, tp param.TemplateParams, if err = Arg(args, RestoreDataAllBackupInfo, &backupInfo); err != nil { return nil, err } - if err = OptArg(args, RestoreDataPodAnnotationsArg, &annotations, nil); err != nil { + if err = OptArg(args, PodAnnotationsArg, &annotations, nil); err != nil { return nil, err } - if err = OptArg(args, RestoreDataAllPodLabelsArg, &labels, nil); err != nil { + if err = OptArg(args, PodLabelsArg, &labels, nil); err != nil { return nil, err } @@ -213,8 +211,8 @@ func (*restoreDataAllFunc) Arguments() []string { RestoreDataAllPodsArg, RestoreDataAllPodOverrideArg, InsecureTLS, - RestoreDataAllPodAnnotationsArg, - RestoreDataAllPodLabelsArg, + PodAnnotationsArg, + PodLabelsArg, } } diff --git a/pkg/function/restore_data_using_kopia_server.go b/pkg/function/restore_data_using_kopia_server.go index 86c4a7b10a..9e64e11e3f 100644 --- a/pkg/function/restore_data_using_kopia_server.go +++ b/pkg/function/restore_data_using_kopia_server.go @@ -75,6 +75,8 @@ func (*restoreDataUsingKopiaServerFunc) Arguments() []string { RestoreDataPodOverrideArg, RestoreDataImageArg, KopiaRepositoryServerUserHostname, + PodAnnotationsArg, + PodLabelsArg, } } @@ -116,10 +118,10 @@ func (r *restoreDataUsingKopiaServerFunc) Exec(ctx context.Context, tp param.Tem if err = OptArg(args, KopiaRepositoryServerUserHostname, &userHostname, ""); err != nil { return nil, err } - if err = OptArg(args, RestoreDataPodAnnotationsArg, &annotations, nil); err != nil { + if err = OptArg(args, PodAnnotationsArg, &annotations, nil); err != nil { return nil, err } - if err = OptArg(args, RestoreDataPodLabelsArg, &labels, nil); err != nil { + if err = OptArg(args, PodLabelsArg, &labels, nil); err != nil { return nil, err } diff --git a/pkg/function/restore_rds_snapshot.go b/pkg/function/restore_rds_snapshot.go index dcb0a153b8..b232d934e0 100644 --- a/pkg/function/restore_rds_snapshot.go +++ b/pkg/function/restore_rds_snapshot.go @@ -107,8 +107,8 @@ func (*restoreRDSSnapshotFunc) Arguments() []string { RestoreRDSSnapshotNamespace, RestoreRDSSnapshotSecGrpID, RestoreRDSSnapshotDBSubnetGroup, - RestoreRDSSnapshotPodAnnotations, - RestoreRDSSnapshotPodLabels, + PodAnnotationsArg, + PodLabelsArg, } } @@ -146,10 +146,10 @@ func (r *restoreRDSSnapshotFunc) Exec(ctx context.Context, tp param.TemplatePara if err := OptArg(args, RestoreRDSSnapshotImage, &postgresToolsImage, defaultPostgresToolsImage); err != nil { return nil, err } - if err := OptArg(args, RestoreRDSSnapshotPodAnnotations, &annotations, nil); err != nil { + if err := OptArg(args, PodAnnotationsArg, &annotations, nil); err != nil { return nil, err } - if err := OptArg(args, RestoreRDSSnapshotPodLabels, &labels, nil); err != nil { + if err := OptArg(args, PodLabelsArg, &labels, nil); err != nil { return nil, err } // Find security groups diff --git a/pkg/function/utils.go b/pkg/function/utils.go index f2d4340123..13b9642572 100644 --- a/pkg/function/utils.go +++ b/pkg/function/utils.go @@ -27,6 +27,12 @@ import ( const ( // FunctionOutputVersion returns version FunctionOutputVersion = "version" + + // since pod labels and annotations argument are going to be named the + // same for all the kanister functions that support these arguments, instead + // of creating these for the functions, it's better to have a const here. + PodLabelsArg = "podLabels" + PodAnnotationsArg = "podAnnotations" ) // ValidateCredentials verifies if the given credentials have appropriate values set From e2c982a250b71854a79963865c13f6d074d6e9f8 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Mon, 19 Aug 2024 12:31:29 +0200 Subject: [PATCH 03/10] Address review comment 1. Remove duplicate const declarations 2. Reformat function calls to have them in newline --- pkg/function/backup_data_stats.go | 15 +++++++- pkg/function/checkRepository.go | 13 ++++++- pkg/function/copy_volume_data.go | 14 ++++++- pkg/function/export_rds_snapshot_location.go | 2 - pkg/function/kube_task.go | 11 +++++- pkg/function/prepare_data.go | 13 ++++++- pkg/function/restore_data.go | 19 +++++++++- pkg/function/restore_data_all.go | 19 +++++++++- pkg/function/restore_rds_snapshot.go | 40 +++++++++++++++++--- 9 files changed, 132 insertions(+), 14 deletions(-) diff --git a/pkg/function/backup_data_stats.go b/pkg/function/backup_data_stats.go index 6cf59e2dd0..654d6a01ba 100644 --- a/pkg/function/backup_data_stats.go +++ b/pkg/function/backup_data_stats.go @@ -201,7 +201,20 @@ func (b *BackupDataStatsFunc) Exec(ctx context.Context, tp param.TemplateParams, if err != nil { return nil, errors.Wrapf(err, "Failed to create Kubernetes client") } - return backupDataStats(ctx, cli, tp, namespace, encryptionKey, backupArtifactPrefix, backupID, mode, backupDataStatsJobPrefix, podOverride, annotations, labels) + return backupDataStats( + ctx, + cli, + tp, + namespace, + encryptionKey, + backupArtifactPrefix, + backupID, + mode, + backupDataStatsJobPrefix, + podOverride, + annotations, + labels, + ) } func (*BackupDataStatsFunc) RequiredArgs() []string { diff --git a/pkg/function/checkRepository.go b/pkg/function/checkRepository.go index a302697707..cb7f2f6f7d 100644 --- a/pkg/function/checkRepository.go +++ b/pkg/function/checkRepository.go @@ -181,7 +181,18 @@ func (c *CheckRepositoryFunc) Exec(ctx context.Context, tp param.TemplateParams, if err != nil { return nil, errors.Wrapf(err, "Failed to create Kubernetes client") } - return CheckRepository(ctx, cli, tp, encryptionKey, checkRepositoryArtifactPrefix, CheckRepositoryJobPrefix, insecureTLS, podOverride, annotations, labels) + return CheckRepository( + ctx, + cli, + tp, + encryptionKey, + checkRepositoryArtifactPrefix, + CheckRepositoryJobPrefix, + insecureTLS, + podOverride, + annotations, + labels, + ) } func (*CheckRepositoryFunc) RequiredArgs() []string { diff --git a/pkg/function/copy_volume_data.go b/pkg/function/copy_volume_data.go index 905d435fa5..538f71317e 100644 --- a/pkg/function/copy_volume_data.go +++ b/pkg/function/copy_volume_data.go @@ -236,7 +236,19 @@ func (c *copyVolumeDataFunc) Exec(ctx context.Context, tp param.TemplateParams, if err != nil { return nil, errors.Wrapf(err, "Failed to create Kubernetes client") } - return copyVolumeData(ctx, cli, tp, namespace, vol, targetPath, encryptionKey, insecureTLS, podOverride, annotations, labels) + return copyVolumeData( + ctx, + cli, + tp, + namespace, + vol, + targetPath, + encryptionKey, + insecureTLS, + podOverride, + annotations, + labels, + ) } func (*copyVolumeDataFunc) RequiredArgs() []string { diff --git a/pkg/function/export_rds_snapshot_location.go b/pkg/function/export_rds_snapshot_location.go index 39be3df9c0..cf93486241 100644 --- a/pkg/function/export_rds_snapshot_location.go +++ b/pkg/function/export_rds_snapshot_location.go @@ -60,8 +60,6 @@ const ( ExportRDSSnapshotToLocBackupID = "backupID" ExportRDSSnapshotToLocDBSubnetGroupArg = "dbSubnetGroup" ExportRDSSnapshotToLocImageArg = "image" - ExportRDSSnapshotToLocPodAnnotationsArg = "podAnnotations" - ExportRDSSnapshotToLocPodLabelsArg = "podLabels" PostgrSQLEngine RDSDBEngine = "PostgreSQL" diff --git a/pkg/function/kube_task.go b/pkg/function/kube_task.go index ba161205a1..e7404cbc6f 100644 --- a/pkg/function/kube_task.go +++ b/pkg/function/kube_task.go @@ -147,7 +147,16 @@ func (ktf *kubeTaskFunc) Exec(ctx context.Context, tp param.TemplateParams, args if err != nil { return nil, errors.Wrapf(err, "Failed to create Kubernetes client") } - return kubeTask(ctx, cli, namespace, image, command, podOverride, annotations, labels) + return kubeTask( + ctx, + cli, + namespace, + image, + command, + podOverride, + annotations, + labels, + ) } func (*kubeTaskFunc) RequiredArgs() []string { diff --git a/pkg/function/prepare_data.go b/pkg/function/prepare_data.go index efee87275f..2a20450f36 100644 --- a/pkg/function/prepare_data.go +++ b/pkg/function/prepare_data.go @@ -205,7 +205,18 @@ func (p *prepareDataFunc) Exec(ctx context.Context, tp param.TemplateParams, arg return nil, err } } - return prepareData(ctx, cli, namespace, serviceAccount, image, vols, podOverride, annotations, labels, command...) + return prepareData( + ctx, + cli, + namespace, + serviceAccount, + image, + vols, + podOverride, + annotations, + labels, + command..., + ) } func (*prepareDataFunc) RequiredArgs() []string { diff --git a/pkg/function/restore_data.go b/pkg/function/restore_data.go index 6ed8c881b9..d5eda49404 100644 --- a/pkg/function/restore_data.go +++ b/pkg/function/restore_data.go @@ -281,7 +281,24 @@ func (r *restoreDataFunc) Exec(ctx context.Context, tp param.TemplateParams, arg if err != nil { return nil, errors.Wrapf(err, "Failed to create Kubernetes client") } - return restoreData(ctx, cli, tp, namespace, encryptionKey, backupArtifactPrefix, restorePath, backupTag, backupID, restoreDataJobPrefix, image, insecureTLS, vols, podOverride, annotations, labels) + return restoreData( + ctx, + cli, + tp, + namespace, + encryptionKey, + backupArtifactPrefix, + restorePath, + backupTag, + backupID, + restoreDataJobPrefix, + image, + insecureTLS, + vols, + podOverride, + annotations, + labels, + ) } func (*restoreDataFunc) RequiredArgs() []string { diff --git a/pkg/function/restore_data_all.go b/pkg/function/restore_data_all.go index 8e536e4681..017cca6663 100644 --- a/pkg/function/restore_data_all.go +++ b/pkg/function/restore_data_all.go @@ -168,7 +168,24 @@ func (r *restoreDataAllFunc) Exec(ctx context.Context, tp param.TemplateParams, outputChan <- out return } - out, err = restoreData(ctx, cli, tp, namespace, encryptionKey, fmt.Sprintf("%s/%s", backupArtifactPrefix, pod), restorePath, "", input[pod].BackupID, restoreDataAllJobPrefix, image, insecureTLS, vols, podOverride, annotations, labels) + out, err = restoreData( + ctx, + cli, + tp, + namespace, + encryptionKey, + fmt.Sprintf("%s/%s", backupArtifactPrefix, pod), + restorePath, + "", + input[pod].BackupID, + restoreDataAllJobPrefix, + image, + insecureTLS, + vols, + podOverride, + annotations, + labels, + ) errChan <- errors.Wrapf(err, "Failed to restore data for pod %s", pod) outputChan <- out }(pod) diff --git a/pkg/function/restore_rds_snapshot.go b/pkg/function/restore_rds_snapshot.go index b232d934e0..b679e084ce 100644 --- a/pkg/function/restore_rds_snapshot.go +++ b/pkg/function/restore_rds_snapshot.go @@ -71,9 +71,7 @@ const ( // RestoreRDSSnapshotPassword stores the password of the database RestoreRDSSnapshotPassword = "password" // RestoreRDSSnapshotImage provides the image of the container with required tools - RestoreRDSSnapshotImage = "image" - RestoreRDSSnapshotPodAnnotations = "podAnnotations" - RestoreRDSSnapshotPodLabels = "podLabels" + RestoreRDSSnapshotImage = "image" // PostgreSQLEngine stores the postgres appname PostgreSQLEngine RDSDBEngine = "PostgreSQL" @@ -178,7 +176,23 @@ func (r *restoreRDSSnapshotFunc) Exec(ctx context.Context, tp param.TemplatePara } } - return restoreRDSSnapshot(ctx, namespace, instanceID, subnetGroup, snapshotID, backupArtifactPrefix, backupID, username, password, dbEngine, sgIDs, tp.Profile, postgresToolsImage, annotations, labels) + return restoreRDSSnapshot( + ctx, + namespace, + instanceID, + subnetGroup, + snapshotID, + backupArtifactPrefix, + backupID, + username, + password, + dbEngine, + sgIDs, + tp.Profile, + postgresToolsImage, + annotations, + labels, + ) } func (r *restoreRDSSnapshotFunc) ExecutionProgress() (crv1alpha1.PhaseProgress, error) { @@ -252,7 +266,23 @@ func restoreRDSSnapshot( return nil, errors.Wrapf(err, "Couldn't find DBInstance Version") } - if _, err = execDumpCommand(ctx, dbEngine, RestoreAction, namespace, dbEndpoint, username, password, nil, backupArtifactPrefix, backupID, profile, dbEngineVersion, postgresToolsImage, annotations, labels); err != nil { + if _, err = execDumpCommand( + ctx, + dbEngine, + RestoreAction, + namespace, + dbEndpoint, + username, + password, + nil, + backupArtifactPrefix, + backupID, + profile, + dbEngineVersion, + postgresToolsImage, + annotations, + labels, + ); err != nil { return nil, errors.Wrapf(err, "Failed to restore RDS from dump. InstanceID=%s", instanceID) } From 821b7ebb19d68ace9c03db73e7c512ae48ac1a62 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Fri, 2 Aug 2024 23:36:18 +0530 Subject: [PATCH 04/10] Introduce two new args to kanister functions that create new pods --- pkg/function/backup_data_stats.go | 2 ++ pkg/function/checkRepository.go | 2 ++ pkg/function/copy_volume_data.go | 2 ++ pkg/function/delete_data.go | 4 +++- pkg/function/delete_data_all.go | 4 +++- pkg/function/kube_task.go | 12 +++++++----- pkg/function/prepare_data.go | 16 +++++++++------- pkg/function/restore_data.go | 4 +++- pkg/function/restore_data_all.go | 4 +++- 9 files changed, 34 insertions(+), 16 deletions(-) diff --git a/pkg/function/backup_data_stats.go b/pkg/function/backup_data_stats.go index 654d6a01ba..f38147435f 100644 --- a/pkg/function/backup_data_stats.go +++ b/pkg/function/backup_data_stats.go @@ -47,6 +47,8 @@ const ( BackupDataStatsEncryptionKeyArg = "encryptionKey" // BackupDataStatsBackupIdentifierArg provides a unique ID added to the backed up artifacts BackupDataStatsBackupIdentifierArg = "backupID" + BackupDataStatsPodLabelsArg = "podLabels" + BackupDataStatsPodAnnotationsArg = "podAnnotations" // BackupDataStatsMode provides a mode for stats BackupDataStatsMode = "statsMode" BackupDataStatsOutputFileCount = "fileCount" diff --git a/pkg/function/checkRepository.go b/pkg/function/checkRepository.go index cb7f2f6f7d..b3c32a7961 100644 --- a/pkg/function/checkRepository.go +++ b/pkg/function/checkRepository.go @@ -29,6 +29,8 @@ const ( CheckRepositoryEncryptionKeyArg = "encryptionKey" // CheckRepositoryPodOverrideArg contains pod specs to override default pod specs CheckRepositoryPodOverrideArg = "podOverride" + CheckRepositoryPodAnnotationsArg = "podAnnotations" + CheckRepositoryPodLabelsArg = "podLabels" CheckRepositoryJobPrefix = "check-repository-" CheckRepositoryPasswordIncorrect = "passwordIncorrect" CheckRepositoryRepoDoesNotExist = "repoUnavailable" diff --git a/pkg/function/copy_volume_data.go b/pkg/function/copy_volume_data.go index 538f71317e..ea1fdf0fb6 100644 --- a/pkg/function/copy_volume_data.go +++ b/pkg/function/copy_volume_data.go @@ -46,6 +46,8 @@ const ( CopyVolumeDataNamespaceArg = "namespace" CopyVolumeDataVolumeArg = "volume" CopyVolumeDataArtifactPrefixArg = "dataArtifactPrefix" + CopyVolumeDataPodAnnotationsArg = "podAnnotations" + CopyVolumeDataPodLabelsArg = "podLabels" CopyVolumeDataOutputBackupID = "backupID" CopyVolumeDataOutputBackupRoot = "backupRoot" CopyVolumeDataOutputBackupArtifactLocation = "backupArtifactLocation" diff --git a/pkg/function/delete_data.go b/pkg/function/delete_data.go index d0bdde2b28..6f4d1e7d10 100644 --- a/pkg/function/delete_data.go +++ b/pkg/function/delete_data.go @@ -48,7 +48,9 @@ const ( // DeleteDataBackupIdentifierArg provides a unique ID added to the backed up artifacts DeleteDataBackupIdentifierArg = "backupID" // DeleteDataBackupTagArg provides a unique tag added to the backed up artifacts - DeleteDataBackupTagArg = "backupTag" + DeleteDataBackupTagArg = "backupTag" + DeleteDataPodAnnotationsArg = "podAnnotations" + DeleteDataPodLabelsArg = "podLabels" // DeleteDataEncryptionKeyArg provides the encryption key to be used for deletes DeleteDataEncryptionKeyArg = "encryptionKey" // DeleteDataReclaimSpace provides a way to specify if space should be reclaimed diff --git a/pkg/function/delete_data_all.go b/pkg/function/delete_data_all.go index 7a5d3f9f53..6addee9f77 100644 --- a/pkg/function/delete_data_all.go +++ b/pkg/function/delete_data_all.go @@ -40,7 +40,9 @@ const ( // DeleteDataAllBackupArtifactPrefixArg provides the path to restore backed up data DeleteDataAllBackupArtifactPrefixArg = "backupArtifactPrefix" // DeleteDataAllEncryptionKeyArg provides the encryption key to be used for deletes - DeleteDataAllEncryptionKeyArg = "encryptionKey" + DeleteDataAllEncryptionKeyArg = "encryptionKey" + DeleteDataAllPodAnnotationsArg = "podAnnotations" + DeleteDataAllPodLabelsArg = "podLabels" // DeleteDataAllReclaimSpace provides a way to specify if space should be reclaimed DeleteDataAllReclaimSpace = "reclaimSpace" // DeleteDataAllBackupInfo provides backup info required for delete diff --git a/pkg/function/kube_task.go b/pkg/function/kube_task.go index e7404cbc6f..b474704a87 100644 --- a/pkg/function/kube_task.go +++ b/pkg/function/kube_task.go @@ -39,11 +39,13 @@ const ( jobPrefix = "kanister-job-" // KubeTaskFuncName gives the function name - KubeTaskFuncName = "KubeTask" - KubeTaskNamespaceArg = "namespace" - KubeTaskImageArg = "image" - KubeTaskCommandArg = "command" - KubeTaskPodOverrideArg = "podOverride" + KubeTaskFuncName = "KubeTask" + KubeTaskNamespaceArg = "namespace" + KubeTaskImageArg = "image" + KubeTaskCommandArg = "command" + KubeTaskPodOverrideArg = "podOverride" + KubeTaskPodAnnotationsArg = "podAnnotations" + KubeTaskPodLabelsArg = "podLabels" ) func init() { diff --git a/pkg/function/prepare_data.go b/pkg/function/prepare_data.go index 2a20450f36..f4e2ca2e61 100644 --- a/pkg/function/prepare_data.go +++ b/pkg/function/prepare_data.go @@ -40,13 +40,15 @@ const ( defaultMountPoint = "/mnt/prepare_data/%s" prepareDataJobPrefix = "prepare-data-job-" // PrepareDataFuncName gives the function name - PrepareDataFuncName = "PrepareData" - PrepareDataNamespaceArg = "namespace" - PrepareDataImageArg = "image" - PrepareDataCommandArg = "command" - PrepareDataVolumes = "volumes" - PrepareDataServiceAccount = "serviceaccount" - PrepareDataPodOverrideArg = "podOverride" + PrepareDataFuncName = "PrepareData" + PrepareDataNamespaceArg = "namespace" + PrepareDataImageArg = "image" + PrepareDataCommandArg = "command" + PrepareDataVolumes = "volumes" + PrepareDataServiceAccount = "serviceaccount" + PrepareDataPodOverrideArg = "podOverride" + PrepareDataPodAnnotationsArg = "podAnnotations" + PrepareDataPodLabelsArg = "podLabels" ) func init() { diff --git a/pkg/function/restore_data.go b/pkg/function/restore_data.go index d5eda49404..f5eabd5814 100644 --- a/pkg/function/restore_data.go +++ b/pkg/function/restore_data.go @@ -57,7 +57,9 @@ const ( // RestoreDataBackupTagArg provides a unique tag added to the backup artifacts RestoreDataBackupTagArg = "backupTag" // RestoreDataPodOverrideArg contains pod specs which overrides default pod specs - RestoreDataPodOverrideArg = "podOverride" + RestoreDataPodOverrideArg = "podOverride" + RestoreDataPodAnnotationsArg = "podAnnotations" + RestoreDataPodLabelsArg = "podLabels" ) func init() { diff --git a/pkg/function/restore_data_all.go b/pkg/function/restore_data_all.go index 017cca6663..65089680c1 100644 --- a/pkg/function/restore_data_all.go +++ b/pkg/function/restore_data_all.go @@ -52,7 +52,9 @@ const ( // RestoreDataAllBackupInfo provides backup info required for restore RestoreDataAllBackupInfo = "backupInfo" // RestoreDataPodOverrideArg contains pod specs which overrides default pod specs - RestoreDataAllPodOverrideArg = "podOverride" + RestoreDataAllPodOverrideArg = "podOverride" + RestoreDataAllPodAnnotationsArg = "podAnnotations" + RestoreDataAllPodLabelsArg = "podLabels" ) func init() { From ef0c7c69ac2e2d002c7e4d577eba515b24b4f037 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Mon, 5 Aug 2024 18:34:18 +0530 Subject: [PATCH 05/10] Use consts for labels and annotations functions args Since these args are going to be named the same for all the kanister functions that support these arguments, it's better to have them as a const and use that everywhere. It will help us when we try to get labels and annotations from blueprints. --- pkg/function/backup_data_stats.go | 2 -- pkg/function/checkRepository.go | 2 -- pkg/function/copy_volume_data.go | 2 -- pkg/function/delete_data.go | 4 +--- pkg/function/delete_data_all.go | 4 +--- pkg/function/kube_task.go | 12 +++++------- pkg/function/prepare_data.go | 16 +++++++--------- pkg/function/restore_data.go | 4 +--- pkg/function/restore_data_all.go | 4 +--- 9 files changed, 16 insertions(+), 34 deletions(-) diff --git a/pkg/function/backup_data_stats.go b/pkg/function/backup_data_stats.go index f38147435f..654d6a01ba 100644 --- a/pkg/function/backup_data_stats.go +++ b/pkg/function/backup_data_stats.go @@ -47,8 +47,6 @@ const ( BackupDataStatsEncryptionKeyArg = "encryptionKey" // BackupDataStatsBackupIdentifierArg provides a unique ID added to the backed up artifacts BackupDataStatsBackupIdentifierArg = "backupID" - BackupDataStatsPodLabelsArg = "podLabels" - BackupDataStatsPodAnnotationsArg = "podAnnotations" // BackupDataStatsMode provides a mode for stats BackupDataStatsMode = "statsMode" BackupDataStatsOutputFileCount = "fileCount" diff --git a/pkg/function/checkRepository.go b/pkg/function/checkRepository.go index b3c32a7961..cb7f2f6f7d 100644 --- a/pkg/function/checkRepository.go +++ b/pkg/function/checkRepository.go @@ -29,8 +29,6 @@ const ( CheckRepositoryEncryptionKeyArg = "encryptionKey" // CheckRepositoryPodOverrideArg contains pod specs to override default pod specs CheckRepositoryPodOverrideArg = "podOverride" - CheckRepositoryPodAnnotationsArg = "podAnnotations" - CheckRepositoryPodLabelsArg = "podLabels" CheckRepositoryJobPrefix = "check-repository-" CheckRepositoryPasswordIncorrect = "passwordIncorrect" CheckRepositoryRepoDoesNotExist = "repoUnavailable" diff --git a/pkg/function/copy_volume_data.go b/pkg/function/copy_volume_data.go index ea1fdf0fb6..538f71317e 100644 --- a/pkg/function/copy_volume_data.go +++ b/pkg/function/copy_volume_data.go @@ -46,8 +46,6 @@ const ( CopyVolumeDataNamespaceArg = "namespace" CopyVolumeDataVolumeArg = "volume" CopyVolumeDataArtifactPrefixArg = "dataArtifactPrefix" - CopyVolumeDataPodAnnotationsArg = "podAnnotations" - CopyVolumeDataPodLabelsArg = "podLabels" CopyVolumeDataOutputBackupID = "backupID" CopyVolumeDataOutputBackupRoot = "backupRoot" CopyVolumeDataOutputBackupArtifactLocation = "backupArtifactLocation" diff --git a/pkg/function/delete_data.go b/pkg/function/delete_data.go index 6f4d1e7d10..d0bdde2b28 100644 --- a/pkg/function/delete_data.go +++ b/pkg/function/delete_data.go @@ -48,9 +48,7 @@ const ( // DeleteDataBackupIdentifierArg provides a unique ID added to the backed up artifacts DeleteDataBackupIdentifierArg = "backupID" // DeleteDataBackupTagArg provides a unique tag added to the backed up artifacts - DeleteDataBackupTagArg = "backupTag" - DeleteDataPodAnnotationsArg = "podAnnotations" - DeleteDataPodLabelsArg = "podLabels" + DeleteDataBackupTagArg = "backupTag" // DeleteDataEncryptionKeyArg provides the encryption key to be used for deletes DeleteDataEncryptionKeyArg = "encryptionKey" // DeleteDataReclaimSpace provides a way to specify if space should be reclaimed diff --git a/pkg/function/delete_data_all.go b/pkg/function/delete_data_all.go index 6addee9f77..7a5d3f9f53 100644 --- a/pkg/function/delete_data_all.go +++ b/pkg/function/delete_data_all.go @@ -40,9 +40,7 @@ const ( // DeleteDataAllBackupArtifactPrefixArg provides the path to restore backed up data DeleteDataAllBackupArtifactPrefixArg = "backupArtifactPrefix" // DeleteDataAllEncryptionKeyArg provides the encryption key to be used for deletes - DeleteDataAllEncryptionKeyArg = "encryptionKey" - DeleteDataAllPodAnnotationsArg = "podAnnotations" - DeleteDataAllPodLabelsArg = "podLabels" + DeleteDataAllEncryptionKeyArg = "encryptionKey" // DeleteDataAllReclaimSpace provides a way to specify if space should be reclaimed DeleteDataAllReclaimSpace = "reclaimSpace" // DeleteDataAllBackupInfo provides backup info required for delete diff --git a/pkg/function/kube_task.go b/pkg/function/kube_task.go index b474704a87..e7404cbc6f 100644 --- a/pkg/function/kube_task.go +++ b/pkg/function/kube_task.go @@ -39,13 +39,11 @@ const ( jobPrefix = "kanister-job-" // KubeTaskFuncName gives the function name - KubeTaskFuncName = "KubeTask" - KubeTaskNamespaceArg = "namespace" - KubeTaskImageArg = "image" - KubeTaskCommandArg = "command" - KubeTaskPodOverrideArg = "podOverride" - KubeTaskPodAnnotationsArg = "podAnnotations" - KubeTaskPodLabelsArg = "podLabels" + KubeTaskFuncName = "KubeTask" + KubeTaskNamespaceArg = "namespace" + KubeTaskImageArg = "image" + KubeTaskCommandArg = "command" + KubeTaskPodOverrideArg = "podOverride" ) func init() { diff --git a/pkg/function/prepare_data.go b/pkg/function/prepare_data.go index f4e2ca2e61..2a20450f36 100644 --- a/pkg/function/prepare_data.go +++ b/pkg/function/prepare_data.go @@ -40,15 +40,13 @@ const ( defaultMountPoint = "/mnt/prepare_data/%s" prepareDataJobPrefix = "prepare-data-job-" // PrepareDataFuncName gives the function name - PrepareDataFuncName = "PrepareData" - PrepareDataNamespaceArg = "namespace" - PrepareDataImageArg = "image" - PrepareDataCommandArg = "command" - PrepareDataVolumes = "volumes" - PrepareDataServiceAccount = "serviceaccount" - PrepareDataPodOverrideArg = "podOverride" - PrepareDataPodAnnotationsArg = "podAnnotations" - PrepareDataPodLabelsArg = "podLabels" + PrepareDataFuncName = "PrepareData" + PrepareDataNamespaceArg = "namespace" + PrepareDataImageArg = "image" + PrepareDataCommandArg = "command" + PrepareDataVolumes = "volumes" + PrepareDataServiceAccount = "serviceaccount" + PrepareDataPodOverrideArg = "podOverride" ) func init() { diff --git a/pkg/function/restore_data.go b/pkg/function/restore_data.go index f5eabd5814..d5eda49404 100644 --- a/pkg/function/restore_data.go +++ b/pkg/function/restore_data.go @@ -57,9 +57,7 @@ const ( // RestoreDataBackupTagArg provides a unique tag added to the backup artifacts RestoreDataBackupTagArg = "backupTag" // RestoreDataPodOverrideArg contains pod specs which overrides default pod specs - RestoreDataPodOverrideArg = "podOverride" - RestoreDataPodAnnotationsArg = "podAnnotations" - RestoreDataPodLabelsArg = "podLabels" + RestoreDataPodOverrideArg = "podOverride" ) func init() { diff --git a/pkg/function/restore_data_all.go b/pkg/function/restore_data_all.go index 65089680c1..017cca6663 100644 --- a/pkg/function/restore_data_all.go +++ b/pkg/function/restore_data_all.go @@ -52,9 +52,7 @@ const ( // RestoreDataAllBackupInfo provides backup info required for restore RestoreDataAllBackupInfo = "backupInfo" // RestoreDataPodOverrideArg contains pod specs which overrides default pod specs - RestoreDataAllPodOverrideArg = "podOverride" - RestoreDataAllPodAnnotationsArg = "podAnnotations" - RestoreDataAllPodLabelsArg = "podLabels" + RestoreDataAllPodOverrideArg = "podOverride" ) func init() { From a7e8e7155c3b3e45aa04694b1e45d24e9ea5beb8 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Wed, 14 Aug 2024 15:45:11 +0530 Subject: [PATCH 06/10] Validate the label/annotation validity in validate method of kan functions --- pkg/function/backup_data_stats.go | 4 + pkg/function/checkRepository.go | 4 + pkg/function/copy_volume_data.go | 4 + pkg/function/delete_data.go | 4 + pkg/function/delete_data_all.go | 4 + .../delete_data_using_kopia_server.go | 4 + pkg/function/export_rds_snapshot_location.go | 4 + pkg/function/kube_task.go | 4 + pkg/function/prepare_data.go | 4 + pkg/function/restore_data.go | 4 + pkg/function/restore_data_all.go | 4 + .../restore_data_using_kopia_server.go | 4 + pkg/function/restore_rds_snapshot.go | 4 + pkg/function/utils.go | 82 +++++++++++++++++++ 14 files changed, 134 insertions(+) diff --git a/pkg/function/backup_data_stats.go b/pkg/function/backup_data_stats.go index 654d6a01ba..5a403344ba 100644 --- a/pkg/function/backup_data_stats.go +++ b/pkg/function/backup_data_stats.go @@ -238,6 +238,10 @@ func (*BackupDataStatsFunc) Arguments() []string { } func (b *BackupDataStatsFunc) Validate(args map[string]any) error { + if err := ValidatePodLabelsAndAnnotations(b.Name(), args); err != nil { + return nil + } + if err := utils.CheckSupportedArgs(b.Arguments(), args); err != nil { return err } diff --git a/pkg/function/checkRepository.go b/pkg/function/checkRepository.go index cb7f2f6f7d..560131576d 100644 --- a/pkg/function/checkRepository.go +++ b/pkg/function/checkRepository.go @@ -210,6 +210,10 @@ func (*CheckRepositoryFunc) Arguments() []string { } func (c *CheckRepositoryFunc) Validate(args map[string]any) error { + if err := ValidatePodLabelsAndAnnotations(c.Name(), args); err != nil { + return nil + } + if err := utils.CheckSupportedArgs(c.Arguments(), args); err != nil { return err } diff --git a/pkg/function/copy_volume_data.go b/pkg/function/copy_volume_data.go index 538f71317e..d31274c2bb 100644 --- a/pkg/function/copy_volume_data.go +++ b/pkg/function/copy_volume_data.go @@ -272,6 +272,10 @@ func (*copyVolumeDataFunc) Arguments() []string { } func (c *copyVolumeDataFunc) Validate(args map[string]any) error { + if err := ValidatePodLabelsAndAnnotations(c.Name(), args); err != nil { + return nil + } + if err := utils.CheckSupportedArgs(c.Arguments(), args); err != nil { return err } diff --git a/pkg/function/delete_data.go b/pkg/function/delete_data.go index d0bdde2b28..3cfefe7e92 100644 --- a/pkg/function/delete_data.go +++ b/pkg/function/delete_data.go @@ -307,6 +307,10 @@ func (*deleteDataFunc) Arguments() []string { } func (d *deleteDataFunc) Validate(args map[string]any) error { + if err := ValidatePodLabelsAndAnnotations(d.Name(), args); err != nil { + return nil + } + if err := utils.CheckSupportedArgs(d.Arguments(), args); err != nil { return err } diff --git a/pkg/function/delete_data_all.go b/pkg/function/delete_data_all.go index 7a5d3f9f53..77d06167d4 100644 --- a/pkg/function/delete_data_all.go +++ b/pkg/function/delete_data_all.go @@ -162,6 +162,10 @@ func (*deleteDataAllFunc) Arguments() []string { } func (d *deleteDataAllFunc) Validate(args map[string]any) error { + if err := ValidatePodLabelsAndAnnotations(d.Name(), args); err != nil { + return nil + } + if err := utils.CheckSupportedArgs(d.Arguments(), args); err != nil { return err } diff --git a/pkg/function/delete_data_using_kopia_server.go b/pkg/function/delete_data_using_kopia_server.go index 1f74ca8588..e38cb62a69 100644 --- a/pkg/function/delete_data_using_kopia_server.go +++ b/pkg/function/delete_data_using_kopia_server.go @@ -76,6 +76,10 @@ func (*deleteDataUsingKopiaServerFunc) Arguments() []string { } func (d *deleteDataUsingKopiaServerFunc) Validate(args map[string]any) error { + if err := ValidatePodLabelsAndAnnotations(d.Name(), args); err != nil { + return nil + } + if err := utils.CheckSupportedArgs(d.Arguments(), args); err != nil { return err } diff --git a/pkg/function/export_rds_snapshot_location.go b/pkg/function/export_rds_snapshot_location.go index cf93486241..ce6ed87443 100644 --- a/pkg/function/export_rds_snapshot_location.go +++ b/pkg/function/export_rds_snapshot_location.go @@ -289,6 +289,10 @@ func (*exportRDSSnapshotToLocationFunc) Arguments() []string { } func (e *exportRDSSnapshotToLocationFunc) Validate(args map[string]any) error { + if err := ValidatePodLabelsAndAnnotations(e.Name(), args); err != nil { + return nil + } + if err := utils.CheckSupportedArgs(e.Arguments(), args); err != nil { return err } diff --git a/pkg/function/kube_task.go b/pkg/function/kube_task.go index e7404cbc6f..4bd0becaa4 100644 --- a/pkg/function/kube_task.go +++ b/pkg/function/kube_task.go @@ -178,6 +178,10 @@ func (*kubeTaskFunc) Arguments() []string { } func (ktf *kubeTaskFunc) Validate(args map[string]any) error { + if err := ValidatePodLabelsAndAnnotations(ktf.Name(), args); err != nil { + return nil + } + if err := utils.CheckSupportedArgs(ktf.Arguments(), args); err != nil { return err } diff --git a/pkg/function/prepare_data.go b/pkg/function/prepare_data.go index 2a20450f36..b71a52750c 100644 --- a/pkg/function/prepare_data.go +++ b/pkg/function/prepare_data.go @@ -241,6 +241,10 @@ func (*prepareDataFunc) Arguments() []string { } func (p *prepareDataFunc) Validate(args map[string]any) error { + if err := ValidatePodLabelsAndAnnotations(p.Name(), args); err != nil { + return nil + } + if err := utils.CheckSupportedArgs(p.Arguments(), args); err != nil { return err } diff --git a/pkg/function/restore_data.go b/pkg/function/restore_data.go index d5eda49404..90c9d6e626 100644 --- a/pkg/function/restore_data.go +++ b/pkg/function/restore_data.go @@ -328,6 +328,10 @@ func (*restoreDataFunc) Arguments() []string { } func (r *restoreDataFunc) Validate(args map[string]any) error { + if err := ValidatePodLabelsAndAnnotations(r.Name(), args); err != nil { + return nil + } + if err := utils.CheckSupportedArgs(r.Arguments(), args); err != nil { return err } diff --git a/pkg/function/restore_data_all.go b/pkg/function/restore_data_all.go index 017cca6663..88ba7ec575 100644 --- a/pkg/function/restore_data_all.go +++ b/pkg/function/restore_data_all.go @@ -234,6 +234,10 @@ func (*restoreDataAllFunc) Arguments() []string { } func (r *restoreDataAllFunc) Validate(args map[string]any) error { + if err := ValidatePodLabelsAndAnnotations(r.Name(), args); err != nil { + return nil + } + if err := utils.CheckSupportedArgs(r.Arguments(), args); err != nil { return err } diff --git a/pkg/function/restore_data_using_kopia_server.go b/pkg/function/restore_data_using_kopia_server.go index 9e64e11e3f..565edb9d5d 100644 --- a/pkg/function/restore_data_using_kopia_server.go +++ b/pkg/function/restore_data_using_kopia_server.go @@ -81,6 +81,10 @@ func (*restoreDataUsingKopiaServerFunc) Arguments() []string { } func (r *restoreDataUsingKopiaServerFunc) Validate(args map[string]any) error { + if err := ValidatePodLabelsAndAnnotations(r.Name(), args); err != nil { + return nil + } + if err := utils.CheckSupportedArgs(r.Arguments(), args); err != nil { return err } diff --git a/pkg/function/restore_rds_snapshot.go b/pkg/function/restore_rds_snapshot.go index b679e084ce..fad52020d2 100644 --- a/pkg/function/restore_rds_snapshot.go +++ b/pkg/function/restore_rds_snapshot.go @@ -111,6 +111,10 @@ func (*restoreRDSSnapshotFunc) Arguments() []string { } func (r *restoreRDSSnapshotFunc) Validate(args map[string]any) error { + if err := ValidatePodLabelsAndAnnotations(r.Name(), args); err != nil { + return nil + } + if err := utils.CheckSupportedArgs(r.Arguments(), args); err != nil { return err } diff --git a/pkg/function/utils.go b/pkg/function/utils.go index 13b9642572..2dff003b8d 100644 --- a/pkg/function/utils.go +++ b/pkg/function/utils.go @@ -12,8 +12,10 @@ import ( "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/validation" "k8s.io/client-go/kubernetes" + "github.com/kanisterio/errkit" crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" "github.com/kanisterio/kanister/pkg/aws" "github.com/kanisterio/kanister/pkg/aws/rds" @@ -303,3 +305,83 @@ func GetRDSAuroraDBSubnetGroup(ctx context.Context, rdsCli *rds.RDS, instanceID } return desc.DBClusters[0].DBSubnetGroup, nil } + +func ValidatePodLabelsAndAnnotations(funcName string, args map[string]any) error { + labels, err := PodLabelsFromFunctionArgs(args) + if err != nil { + return errkit.Wrap(err, "Kanister function validation failed, while getting pod labels from function args", "funcName", funcName) + } + + if err = ValidateLabels(labels); err != nil { + return errkit.Wrap(err, "Kanister function validation failed, while validating labels", "funcName", funcName) + } + + annotations, err := PodAnnotationsFromFunctionArgs(args) + if err != nil { + return errkit.Wrap(err, "Kanister function validation failed, while getting pod annotations from function args", "funcName", funcName) + } + if err = ValidateAnnotations(annotations); err != nil { + return errkit.Wrap(err, "Kanister function validation failed, while validating annotations", "funcName", funcName) + } + return nil +} + +func PodLabelsFromFunctionArgs(args map[string]any) (map[string]string, error) { + for k, v := range args { + if k == PodLabelsArg { + labels, ok := v.(map[string]interface{}) + if !ok { + return nil, errkit.New("podLabels are not in correct format. Expected format is map[string]string.") + } + return mapStringInterfaceToString(labels), nil + } + } + return nil, nil +} + +func mapStringInterfaceToString(m map[string]interface{}) map[string]string { + res := map[string]string{} + for k, v := range m { + switch v := v.(type) { + case string: + res[k] = v + } + } + return res +} + +func PodAnnotationsFromFunctionArgs(args map[string]any) (map[string]string, error) { + for k, v := range args { + if k == PodAnnotationsArg { + annotations, ok := v.(map[string]interface{}) + if !ok { + return nil, errkit.New("podLabels are not in correct format. expected format is map[string]string.") + } + return mapStringInterfaceToString(annotations), nil + } + } + return nil, nil +} + +func ValidateLabels(labels map[string]string) error { + for k, v := range labels { + if errs := validation.IsQualifiedName(k); len(errs) > 0 { + return errkit.New("label key failed validation", "key", k, "errs", errs) + } + + if errs := validation.IsValidLabelValue(v); len(errs) > 0 { + return errkit.New("label value failed validation", "value", v, "errs", errs) + } + } + return nil +} + +func ValidateAnnotations(annotations map[string]string) error { + for k := range annotations { + if errs := validation.IsQualifiedName(k); len(errs) > 0 { + return errkit.New("annotation key failed validation", "key", k, "errs", errs) + } + } + // annotation values don't actually have a strict format + return nil +} From 6f80f622e3bb5200e747da60657924dead2877ef Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Mon, 19 Aug 2024 13:20:09 +0200 Subject: [PATCH 07/10] Fix bugs, and add comments to functions While validating the labels and annotations, when the validate resulted into error, we returned `nil` instead of `err`. This commit fixes that and adds some comments into some functions. --- pkg/function/backup_data_stats.go | 2 +- pkg/function/checkRepository.go | 2 +- pkg/function/copy_volume_data.go | 2 +- pkg/function/delete_data.go | 2 +- pkg/function/delete_data_all.go | 2 +- pkg/function/delete_data_using_kopia_server.go | 2 +- pkg/function/export_rds_snapshot_location.go | 2 +- pkg/function/kube_task.go | 2 +- pkg/function/prepare_data.go | 2 +- pkg/function/restore_data.go | 2 +- pkg/function/restore_data_all.go | 2 +- pkg/function/restore_data_using_kopia_server.go | 2 +- pkg/function/restore_rds_snapshot.go | 2 +- pkg/function/utils.go | 5 +++++ 14 files changed, 18 insertions(+), 13 deletions(-) diff --git a/pkg/function/backup_data_stats.go b/pkg/function/backup_data_stats.go index 5a403344ba..f23113d4ee 100644 --- a/pkg/function/backup_data_stats.go +++ b/pkg/function/backup_data_stats.go @@ -239,7 +239,7 @@ func (*BackupDataStatsFunc) Arguments() []string { func (b *BackupDataStatsFunc) Validate(args map[string]any) error { if err := ValidatePodLabelsAndAnnotations(b.Name(), args); err != nil { - return nil + return err } if err := utils.CheckSupportedArgs(b.Arguments(), args); err != nil { diff --git a/pkg/function/checkRepository.go b/pkg/function/checkRepository.go index 560131576d..9725cd0dc3 100644 --- a/pkg/function/checkRepository.go +++ b/pkg/function/checkRepository.go @@ -211,7 +211,7 @@ func (*CheckRepositoryFunc) Arguments() []string { func (c *CheckRepositoryFunc) Validate(args map[string]any) error { if err := ValidatePodLabelsAndAnnotations(c.Name(), args); err != nil { - return nil + return err } if err := utils.CheckSupportedArgs(c.Arguments(), args); err != nil { diff --git a/pkg/function/copy_volume_data.go b/pkg/function/copy_volume_data.go index d31274c2bb..4f9780cdea 100644 --- a/pkg/function/copy_volume_data.go +++ b/pkg/function/copy_volume_data.go @@ -273,7 +273,7 @@ func (*copyVolumeDataFunc) Arguments() []string { func (c *copyVolumeDataFunc) Validate(args map[string]any) error { if err := ValidatePodLabelsAndAnnotations(c.Name(), args); err != nil { - return nil + return err } if err := utils.CheckSupportedArgs(c.Arguments(), args); err != nil { diff --git a/pkg/function/delete_data.go b/pkg/function/delete_data.go index 3cfefe7e92..ab1c34ef89 100644 --- a/pkg/function/delete_data.go +++ b/pkg/function/delete_data.go @@ -308,7 +308,7 @@ func (*deleteDataFunc) Arguments() []string { func (d *deleteDataFunc) Validate(args map[string]any) error { if err := ValidatePodLabelsAndAnnotations(d.Name(), args); err != nil { - return nil + return err } if err := utils.CheckSupportedArgs(d.Arguments(), args); err != nil { diff --git a/pkg/function/delete_data_all.go b/pkg/function/delete_data_all.go index 77d06167d4..90339293d0 100644 --- a/pkg/function/delete_data_all.go +++ b/pkg/function/delete_data_all.go @@ -163,7 +163,7 @@ func (*deleteDataAllFunc) Arguments() []string { func (d *deleteDataAllFunc) Validate(args map[string]any) error { if err := ValidatePodLabelsAndAnnotations(d.Name(), args); err != nil { - return nil + return err } if err := utils.CheckSupportedArgs(d.Arguments(), args); err != nil { diff --git a/pkg/function/delete_data_using_kopia_server.go b/pkg/function/delete_data_using_kopia_server.go index e38cb62a69..ee6e9364d8 100644 --- a/pkg/function/delete_data_using_kopia_server.go +++ b/pkg/function/delete_data_using_kopia_server.go @@ -77,7 +77,7 @@ func (*deleteDataUsingKopiaServerFunc) Arguments() []string { func (d *deleteDataUsingKopiaServerFunc) Validate(args map[string]any) error { if err := ValidatePodLabelsAndAnnotations(d.Name(), args); err != nil { - return nil + return err } if err := utils.CheckSupportedArgs(d.Arguments(), args); err != nil { diff --git a/pkg/function/export_rds_snapshot_location.go b/pkg/function/export_rds_snapshot_location.go index ce6ed87443..09c25e59ec 100644 --- a/pkg/function/export_rds_snapshot_location.go +++ b/pkg/function/export_rds_snapshot_location.go @@ -290,7 +290,7 @@ func (*exportRDSSnapshotToLocationFunc) Arguments() []string { func (e *exportRDSSnapshotToLocationFunc) Validate(args map[string]any) error { if err := ValidatePodLabelsAndAnnotations(e.Name(), args); err != nil { - return nil + return err } if err := utils.CheckSupportedArgs(e.Arguments(), args); err != nil { diff --git a/pkg/function/kube_task.go b/pkg/function/kube_task.go index 4bd0becaa4..e3ab913a51 100644 --- a/pkg/function/kube_task.go +++ b/pkg/function/kube_task.go @@ -179,7 +179,7 @@ func (*kubeTaskFunc) Arguments() []string { func (ktf *kubeTaskFunc) Validate(args map[string]any) error { if err := ValidatePodLabelsAndAnnotations(ktf.Name(), args); err != nil { - return nil + return err } if err := utils.CheckSupportedArgs(ktf.Arguments(), args); err != nil { diff --git a/pkg/function/prepare_data.go b/pkg/function/prepare_data.go index b71a52750c..b7d88e52c0 100644 --- a/pkg/function/prepare_data.go +++ b/pkg/function/prepare_data.go @@ -242,7 +242,7 @@ func (*prepareDataFunc) Arguments() []string { func (p *prepareDataFunc) Validate(args map[string]any) error { if err := ValidatePodLabelsAndAnnotations(p.Name(), args); err != nil { - return nil + return err } if err := utils.CheckSupportedArgs(p.Arguments(), args); err != nil { diff --git a/pkg/function/restore_data.go b/pkg/function/restore_data.go index 90c9d6e626..b76e53eefc 100644 --- a/pkg/function/restore_data.go +++ b/pkg/function/restore_data.go @@ -329,7 +329,7 @@ func (*restoreDataFunc) Arguments() []string { func (r *restoreDataFunc) Validate(args map[string]any) error { if err := ValidatePodLabelsAndAnnotations(r.Name(), args); err != nil { - return nil + return err } if err := utils.CheckSupportedArgs(r.Arguments(), args); err != nil { diff --git a/pkg/function/restore_data_all.go b/pkg/function/restore_data_all.go index 88ba7ec575..30d8b5e673 100644 --- a/pkg/function/restore_data_all.go +++ b/pkg/function/restore_data_all.go @@ -235,7 +235,7 @@ func (*restoreDataAllFunc) Arguments() []string { func (r *restoreDataAllFunc) Validate(args map[string]any) error { if err := ValidatePodLabelsAndAnnotations(r.Name(), args); err != nil { - return nil + return err } if err := utils.CheckSupportedArgs(r.Arguments(), args); err != nil { diff --git a/pkg/function/restore_data_using_kopia_server.go b/pkg/function/restore_data_using_kopia_server.go index 565edb9d5d..a11b228419 100644 --- a/pkg/function/restore_data_using_kopia_server.go +++ b/pkg/function/restore_data_using_kopia_server.go @@ -82,7 +82,7 @@ func (*restoreDataUsingKopiaServerFunc) Arguments() []string { func (r *restoreDataUsingKopiaServerFunc) Validate(args map[string]any) error { if err := ValidatePodLabelsAndAnnotations(r.Name(), args); err != nil { - return nil + return err } if err := utils.CheckSupportedArgs(r.Arguments(), args); err != nil { diff --git a/pkg/function/restore_rds_snapshot.go b/pkg/function/restore_rds_snapshot.go index fad52020d2..b859857df8 100644 --- a/pkg/function/restore_rds_snapshot.go +++ b/pkg/function/restore_rds_snapshot.go @@ -112,7 +112,7 @@ func (*restoreRDSSnapshotFunc) Arguments() []string { func (r *restoreRDSSnapshotFunc) Validate(args map[string]any) error { if err := ValidatePodLabelsAndAnnotations(r.Name(), args); err != nil { - return nil + return err } if err := utils.CheckSupportedArgs(r.Arguments(), args); err != nil { diff --git a/pkg/function/utils.go b/pkg/function/utils.go index 2dff003b8d..b562121568 100644 --- a/pkg/function/utils.go +++ b/pkg/function/utils.go @@ -306,6 +306,8 @@ func GetRDSAuroraDBSubnetGroup(ctx context.Context, rdsCli *rds.RDS, instanceID return desc.DBClusters[0].DBSubnetGroup, nil } +// ValidatePodLabelsAndAnnotations validates the labels and annotations that are +// passed to a Kanister function (`funcName`) using `podLabels` and `podAnnotations` args. func ValidatePodLabelsAndAnnotations(funcName string, args map[string]any) error { labels, err := PodLabelsFromFunctionArgs(args) if err != nil { @@ -339,6 +341,9 @@ func PodLabelsFromFunctionArgs(args map[string]any) (map[string]string, error) { return nil, nil } +// mapStringInterfaceToString accepts a map of `string` and `interface{}` and creates +// a map of `string` and `string` from passed map and returns that. +// If a value in the passed map is not of type `string`, it will be skipped. func mapStringInterfaceToString(m map[string]interface{}) map[string]string { res := map[string]string{} for k, v := range m { From ba1ad1617bc00b14c15a6d23b517503af5fc0464 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Mon, 19 Aug 2024 13:47:36 +0200 Subject: [PATCH 08/10] Fix golint issues --- pkg/function/utils.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/function/utils.go b/pkg/function/utils.go index b562121568..fa5b013cda 100644 --- a/pkg/function/utils.go +++ b/pkg/function/utils.go @@ -9,13 +9,13 @@ import ( awssdk "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" rdserr "github.com/aws/aws-sdk-go/service/rds" + "github.com/kanisterio/errkit" "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/validation" "k8s.io/client-go/kubernetes" - "github.com/kanisterio/errkit" crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" "github.com/kanisterio/kanister/pkg/aws" "github.com/kanisterio/kanister/pkg/aws/rds" @@ -350,6 +350,8 @@ func mapStringInterfaceToString(m map[string]interface{}) map[string]string { switch v := v.(type) { case string: res[k] = v + default: + log.Info().Print("Map value is not of type string, while converting map[string]interface{} to map[string]string. Skipping.", map[string]interface{}{"value": v}) } } return res From 63e40ad326616052064c38d9aa45b801e4577a2e Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Mon, 19 Aug 2024 16:47:41 +0200 Subject: [PATCH 09/10] Add unit test to validate a kanister function specifying labels and annotations --- pkg/blueprint/validate/validate_test.go | 96 +++++++++++++++++++++++++ pkg/function/utils.go | 13 ++-- 2 files changed, 103 insertions(+), 6 deletions(-) diff --git a/pkg/blueprint/validate/validate_test.go b/pkg/blueprint/validate/validate_test.go index 7bda1d4990..1deea37275 100644 --- a/pkg/blueprint/validate/validate_test.go +++ b/pkg/blueprint/validate/validate_test.go @@ -23,6 +23,7 @@ import ( kanister "github.com/kanisterio/kanister/pkg" crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" + "github.com/kanisterio/kanister/pkg/function" "github.com/kanisterio/kanister/pkg/param" "github.com/kanisterio/kanister/pkg/utils" ) @@ -343,6 +344,101 @@ func (v *ValidateBlueprint) TestValidateNonDefaultVersion(c *C) { } } +func (v *ValidateBlueprint) TestValidateAnnLabelArgs(c *C) { + for _, tc := range []struct { + labels interface{} + annotations interface{} + error string + }{ + { + labels: map[string]interface{}{ + "key": "value", + }, + error: "", + }, + { + annotations: map[string]interface{}{ + "key": "value", + }, + error: "", + }, + { + labels: map[string]interface{}{ + "key": "value", + }, + annotations: map[string]interface{}{ + "key": "value", + }, + error: "", + }, + { + labels: map[string]interface{}{ + "key$": "value", + }, + annotations: map[string]interface{}{ + "key": "value", + }, + error: "label key 'key$' failed validation", + }, + { + labels: map[string]interface{}{ + "key*": "value", + }, + annotations: map[string]interface{}{ + "key": "value", + }, + error: "label key 'key*' failed validation", + }, + { + labels: map[string]interface{}{ + "key": "value$", + }, + annotations: map[string]interface{}{ + "key": "value", + }, + error: "label value 'value$' failed validation", + }, + { + labels: map[string]interface{}{ + "key": "value", + }, + annotations: map[string]interface{}{ + "key$": "value", + }, + error: "annotation key 'key$' failed validation", + }, + { + labels: map[string]interface{}{ + "key": "value", + }, + annotations: map[string]interface{}{ + "key": "value$", + }, + error: "", + }, + } { + bp := blueprint() + bp.Actions["backup"].Phases = []crv1alpha1.BlueprintPhase{ + { + Func: "KubeTask", + Name: "backup", + Args: map[string]interface{}{ + function.PodLabelsArg: tc.labels, + function.PodAnnotationsArg: tc.annotations, + "image": "", + "command": "", + }, + }, + } + err := Do(bp, kanister.DefaultVersion) + if tc.error != "" { + c.Assert(strings.Contains(err.Error(), tc.error), Equals, true) + } else { + c.Assert(err, Equals, nil) + } + } +} + func (v *ValidateBlueprint) TestValidatePhaseNames(c *C) { for _, tc := range []BlueprintTest{ { diff --git a/pkg/function/utils.go b/pkg/function/utils.go index fa5b013cda..f2161ff2f7 100644 --- a/pkg/function/utils.go +++ b/pkg/function/utils.go @@ -3,6 +3,7 @@ package function import ( "bytes" "context" + "fmt" "path" "strings" @@ -330,10 +331,10 @@ func ValidatePodLabelsAndAnnotations(funcName string, args map[string]any) error func PodLabelsFromFunctionArgs(args map[string]any) (map[string]string, error) { for k, v := range args { - if k == PodLabelsArg { + if k == PodLabelsArg && v != nil { labels, ok := v.(map[string]interface{}) if !ok { - return nil, errkit.New("podLabels are not in correct format. Expected format is map[string]string.") + return nil, errkit.New("podLabels are not in correct format. Expected format is map[string]interface{}.") } return mapStringInterfaceToString(labels), nil } @@ -359,7 +360,7 @@ func mapStringInterfaceToString(m map[string]interface{}) map[string]string { func PodAnnotationsFromFunctionArgs(args map[string]any) (map[string]string, error) { for k, v := range args { - if k == PodAnnotationsArg { + if k == PodAnnotationsArg && v != nil { annotations, ok := v.(map[string]interface{}) if !ok { return nil, errkit.New("podLabels are not in correct format. expected format is map[string]string.") @@ -373,11 +374,11 @@ func PodAnnotationsFromFunctionArgs(args map[string]any) (map[string]string, err func ValidateLabels(labels map[string]string) error { for k, v := range labels { if errs := validation.IsQualifiedName(k); len(errs) > 0 { - return errkit.New("label key failed validation", "key", k, "errs", errs) + return errors.New(fmt.Sprintf("label key '%s' failed validation. %s", k, errs)) } if errs := validation.IsValidLabelValue(v); len(errs) > 0 { - return errkit.New("label value failed validation", "value", v, "errs", errs) + return errors.New(fmt.Sprintf("label value '%s' failed validation. %s", v, errs)) } } return nil @@ -386,7 +387,7 @@ func ValidateLabels(labels map[string]string) error { func ValidateAnnotations(annotations map[string]string) error { for k := range annotations { if errs := validation.IsQualifiedName(k); len(errs) > 0 { - return errkit.New("annotation key failed validation", "key", k, "errs", errs) + return errors.New(fmt.Sprintf("annotation key '%s' failed validation. %s", k, errs)) } } // annotation values don't actually have a strict format From 26c65d15261429b641223093cf4e11ce3ed8fc0c Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Tue, 20 Aug 2024 15:11:18 +0200 Subject: [PATCH 10/10] Address review comments --- pkg/blueprint/validate/validate_test.go | 9 +++++++++ pkg/function/utils.go | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/pkg/blueprint/validate/validate_test.go b/pkg/blueprint/validate/validate_test.go index 1deea37275..8b8d782f74 100644 --- a/pkg/blueprint/validate/validate_test.go +++ b/pkg/blueprint/validate/validate_test.go @@ -416,6 +416,15 @@ func (v *ValidateBlueprint) TestValidateAnnLabelArgs(c *C) { }, error: "", }, + { + labels: map[string]interface{}{ + "key": "", + }, + annotations: map[string]interface{}{ + "key": "", + }, + error: "", + }, } { bp := blueprint() bp.Actions["backup"].Phases = []crv1alpha1.BlueprintPhase{ diff --git a/pkg/function/utils.go b/pkg/function/utils.go index f2161ff2f7..f47d196e7f 100644 --- a/pkg/function/utils.go +++ b/pkg/function/utils.go @@ -363,7 +363,7 @@ func PodAnnotationsFromFunctionArgs(args map[string]any) (map[string]string, err if k == PodAnnotationsArg && v != nil { annotations, ok := v.(map[string]interface{}) if !ok { - return nil, errkit.New("podLabels are not in correct format. expected format is map[string]string.") + return nil, errkit.New("podAnnotations are not in correct format. expected format is map[string]string.") } return mapStringInterfaceToString(annotations), nil }