Skip to content

Commit

Permalink
Improve func interface to have Arguments method and add other utils (
Browse files Browse the repository at this point in the history
…#1251)

* Add function to list all the registered kanister functions

* Add another behaviour in kanister Fun, Arguments

* Fix CI, mock kanister functions should implement new method

* Address review comments, make Arguments return string slice in multiple lines

* Update docs for new Arguments method in Func interface

* Add supported arguments check in phase.Exec and Validate

* Refactor kanister functions RequiredArgs method

* Fix CI, arguments was wrong for kubetask

* Fix CI golint

* Address review comment, rename function

* Fix controller test that failed becaue of unsupported arg

The mock function that we were using in the controller test didn't
return any argument, and because of that the execution of actionset
failed.
This fixes that.

* Add utility method to get Func type from fun name

* Address review comments

* Re-trigger CI

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
viveksinghggits and mergify[bot] committed Mar 9, 2022
1 parent dcbf055 commit 67ecd2a
Show file tree
Hide file tree
Showing 35 changed files with 438 additions and 23 deletions.
5 changes: 4 additions & 1 deletion docs/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ implements the following go interface:
Name() string
Exec(ctx context.Context, args ...string) (map[string]interface{}, error)
RequiredArgs() []string
Arguments() []string
}
Kanister Functions are registered by the return value of ``Name()``, which must be
Expand All @@ -24,7 +25,9 @@ a ``BlueprintPhase`` is used to lookup a Kanister Function. After
``BlueprintPhase.Args`` are rendered, they are passed into the Kanister Function's
``Exec()`` method.

The ``RequiredArgs`` method returns the list of argument names that are required.
The ``RequiredArgs`` method returns the list of argument names that are required. And
``Arguments`` method returns the list of all the argument names that are supported
by the function.

Existing Functions
==================
Expand Down
20 changes: 18 additions & 2 deletions pkg/function/backup_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,24 @@ func (*backupDataFunc) Exec(ctx context.Context, tp param.TemplateParams, args m
}

func (*backupDataFunc) RequiredArgs() []string {
return []string{BackupDataNamespaceArg, BackupDataPodArg, BackupDataContainerArg,
BackupDataIncludePathArg, BackupDataBackupArtifactPrefixArg}
return []string{
BackupDataNamespaceArg,
BackupDataPodArg,
BackupDataContainerArg,
BackupDataIncludePathArg,
BackupDataBackupArtifactPrefixArg,
}
}

func (*backupDataFunc) Arguments() []string {
return []string{
BackupDataNamespaceArg,
BackupDataPodArg,
BackupDataContainerArg,
BackupDataIncludePathArg,
BackupDataBackupArtifactPrefixArg,
BackupDataEncryptionKeyArg,
}
}

type backupDataParsedOutput struct {
Expand Down
19 changes: 17 additions & 2 deletions pkg/function/backup_data_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,23 @@ func (*backupDataAllFunc) Exec(ctx context.Context, tp param.TemplateParams, arg
}

func (*backupDataAllFunc) RequiredArgs() []string {
return []string{BackupDataAllNamespaceArg, BackupDataAllContainerArg,
BackupDataAllIncludePathArg, BackupDataAllBackupArtifactPrefixArg}
return []string{
BackupDataAllNamespaceArg,
BackupDataAllContainerArg,
BackupDataAllIncludePathArg,
BackupDataAllBackupArtifactPrefixArg,
}
}

func (*backupDataAllFunc) Arguments() []string {
return []string{
BackupDataAllNamespaceArg,
BackupDataAllContainerArg,
BackupDataAllIncludePathArg,
BackupDataAllBackupArtifactPrefixArg,
BackupDataAllPodsArg,
BackupDataAllEncryptionKeyArg,
}
}

func backupDataAll(ctx context.Context, cli kubernetes.Interface, namespace string, ps []string, container string, backupArtifactPrefix, includePath, encryptionKey string, tp param.TemplateParams) (map[string]interface{}, error) {
Expand Down
16 changes: 15 additions & 1 deletion pkg/function/backup_data_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,19 @@ func (*BackupDataStatsFunc) Exec(ctx context.Context, tp param.TemplateParams, a
}

func (*BackupDataStatsFunc) RequiredArgs() []string {
return []string{BackupDataStatsNamespaceArg, BackupDataStatsBackupArtifactPrefixArg}
return []string{
BackupDataStatsNamespaceArg,
BackupDataStatsBackupArtifactPrefixArg,
BackupDataStatsBackupIdentifierArg,
}
}

func (*BackupDataStatsFunc) Arguments() []string {
return []string{
BackupDataStatsNamespaceArg,
BackupDataStatsBackupArtifactPrefixArg,
BackupDataStatsBackupIdentifierArg,
BackupDataStatsMode,
BackupDataStatsEncryptionKeyArg,
}
}
7 changes: 7 additions & 0 deletions pkg/function/checkRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,10 @@ func (*CheckRepositoryFunc) Exec(ctx context.Context, tp param.TemplateParams, a
func (*CheckRepositoryFunc) RequiredArgs() []string {
return []string{CheckRepositoryArtifactPrefixArg}
}

func (*CheckRepositoryFunc) Arguments() []string {
return []string{
CheckRepositoryArtifactPrefixArg,
CheckRepositoryEncryptionKeyArg,
}
}
15 changes: 14 additions & 1 deletion pkg/function/copy_volume_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,18 @@ func (*copyVolumeDataFunc) Exec(ctx context.Context, tp param.TemplateParams, ar
}

func (*copyVolumeDataFunc) RequiredArgs() []string {
return []string{CopyVolumeDataNamespaceArg, CopyVolumeDataVolumeArg, CopyVolumeDataArtifactPrefixArg}
return []string{
CopyVolumeDataNamespaceArg,
CopyVolumeDataVolumeArg,
CopyVolumeDataArtifactPrefixArg,
}
}

func (*copyVolumeDataFunc) Arguments() []string {
return []string{
CopyVolumeDataNamespaceArg,
CopyVolumeDataVolumeArg,
CopyVolumeDataArtifactPrefixArg,
CopyVolumeDataEncryptionKeyArg,
}
}
10 changes: 10 additions & 0 deletions pkg/function/create_csi_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ func (*createCSISnapshotFunc) RequiredArgs() []string {
}
}

func (*createCSISnapshotFunc) Arguments() []string {
return []string{
CreateCSISnapshotPVCNameArg,
CreateCSISnapshotNamespaceArg,
CreateCSISnapshotSnapshotClassArg,
CreateCSISnapshotNameArg,
CreateCSISnapshotLabelsArg,
}
}

func createCSISnapshot(ctx context.Context, snapshotter snapshot.Snapshotter, name, namespace, pvc, snapshotClass string, wait bool, labels map[string]string) (*v1.VolumeSnapshot, error) {
if err := snapshotter.Create(ctx, name, namespace, pvc, &snapshotClass, wait, labels); err != nil {
return nil, err
Expand Down
7 changes: 7 additions & 0 deletions pkg/function/create_rds_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,10 @@ func (*createRDSSnapshotFunc) RequiredArgs() []string {
CreateRDSSnapshotInstanceIDArg,
}
}

func (crs *createRDSSnapshotFunc) Arguments() []string {
return []string{
CreateRDSSnapshotInstanceIDArg,
CreateRDSSnapshotDBEngine,
}
}
13 changes: 12 additions & 1 deletion pkg/function/create_volume_from_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,16 @@ func (kef *createVolumeFromSnapshotFunc) Exec(ctx context.Context, tp param.Temp
}

func (*createVolumeFromSnapshotFunc) RequiredArgs() []string {
return []string{CreateVolumeFromSnapshotNamespaceArg, CreateVolumeFromSnapshotManifestArg}
return []string{
CreateVolumeFromSnapshotNamespaceArg,
CreateVolumeFromSnapshotManifestArg,
}
}

func (*createVolumeFromSnapshotFunc) Arguments() []string {
return []string{
CreateVolumeFromSnapshotNamespaceArg,
CreateVolumeFromSnapshotManifestArg,
CreateVolumeFromSnapshotPVCNamesArg,
}
}
8 changes: 8 additions & 0 deletions pkg/function/create_volume_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,11 @@ func getConfig(profile *param.Profile, sType blockstorage.Type) map[string]strin
func (*createVolumeSnapshotFunc) RequiredArgs() []string {
return []string{CreateVolumeSnapshotNamespaceArg}
}

func (*createVolumeSnapshotFunc) Arguments() []string {
return []string{
CreateVolumeSnapshotNamespaceArg,
CreateVolumeSnapshotPVCsArg,
CreateVolumeSnapshotSkipWaitArg,
}
}
7 changes: 7 additions & 0 deletions pkg/function/delete_csi_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ func (*deleteCSISnapshotFunc) RequiredArgs() []string {
}
}

func (*deleteCSISnapshotFunc) Arguments() []string {
return []string{
DeleteCSISnapshotNameArg,
DeleteCSISnapshotNamespaceArg,
}
}

func deleteCSISnapshot(ctx context.Context, snapshotter snapshot.Snapshotter, name, namespace string) (*v1.VolumeSnapshot, error) {
return snapshotter.Delete(ctx, name, namespace)
}
Expand Down
16 changes: 15 additions & 1 deletion pkg/function/delete_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,19 @@ func (*deleteDataFunc) Exec(ctx context.Context, tp param.TemplateParams, args m
}

func (*deleteDataFunc) RequiredArgs() []string {
return []string{DeleteDataNamespaceArg, DeleteDataBackupArtifactPrefixArg}
return []string{
DeleteDataNamespaceArg,
DeleteDataBackupArtifactPrefixArg,
}
}

func (*deleteDataFunc) Arguments() []string {
return []string{
DeleteDataNamespaceArg,
DeleteDataBackupArtifactPrefixArg,
DeleteDataBackupIdentifierArg,
DeleteDataBackupTagArg,
DeleteDataEncryptionKeyArg,
DeleteDataReclaimSpace,
}
}
16 changes: 15 additions & 1 deletion pkg/function/delete_data_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,19 @@ func (*deleteDataAllFunc) Exec(ctx context.Context, tp param.TemplateParams, arg
}

func (*deleteDataAllFunc) RequiredArgs() []string {
return []string{DeleteDataAllNamespaceArg, DeleteDataAllBackupArtifactPrefixArg, DeleteDataAllBackupInfo}
return []string{
DeleteDataAllNamespaceArg,
DeleteDataAllBackupArtifactPrefixArg,
DeleteDataAllBackupInfo,
}
}

func (*deleteDataAllFunc) Arguments() []string {
return []string{
DeleteDataAllNamespaceArg,
DeleteDataAllBackupArtifactPrefixArg,
DeleteDataAllBackupInfo,
DeleteDataAllEncryptionKeyArg,
DeleteDataAllReclaimSpace,
}
}
7 changes: 7 additions & 0 deletions pkg/function/delete_rds_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,10 @@ func (crs *deleteRDSSnapshotFunc) Exec(ctx context.Context, tp param.TemplatePar
func (*deleteRDSSnapshotFunc) RequiredArgs() []string {
return []string{DeleteRDSSnapshotSnapshotIDArg}
}

func (*deleteRDSSnapshotFunc) Arguments() []string {
return []string{
DeleteRDSSnapshotSnapshotIDArg,
CreateRDSSnapshotDBEngine,
}
}
12 changes: 11 additions & 1 deletion pkg/function/delete_volume_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,15 @@ func (kef *deleteVolumeSnapshotFunc) Exec(ctx context.Context, tp param.Template
}

func (*deleteVolumeSnapshotFunc) RequiredArgs() []string {
return []string{DeleteVolumeSnapshotNamespaceArg, DeleteVolumeSnapshotManifestArg}
return []string{
DeleteVolumeSnapshotNamespaceArg,
DeleteVolumeSnapshotManifestArg,
}
}

func (*deleteVolumeSnapshotFunc) Arguments() []string {
return []string{
DeleteVolumeSnapshotNamespaceArg,
DeleteVolumeSnapshotManifestArg,
}
}
7 changes: 7 additions & 0 deletions pkg/function/describe_backups.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,10 @@ func (*DescribeBackupsFunc) Exec(ctx context.Context, tp param.TemplateParams, a
func (*DescribeBackupsFunc) RequiredArgs() []string {
return []string{DescribeBackupsArtifactPrefixArg}
}

func (*DescribeBackupsFunc) Arguments() []string {
return []string{
DescribeBackupsArtifactPrefixArg,
DescribeBackupsEncryptionKeyArg,
}
}
19 changes: 18 additions & 1 deletion pkg/function/export_rds_snapshot_location.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,24 @@ func (crs *exportRDSSnapshotToLocationFunc) Exec(ctx context.Context, tp param.T
}

func (*exportRDSSnapshotToLocationFunc) RequiredArgs() []string {
return []string{ExportRDSSnapshotToLocNamespaceArg, ExportRDSSnapshotToLocInstanceIDArg, ExportRDSSnapshotToLocSnapshotIDArg, ExportRDSSnapshotToLocDBEngineArg}
return []string{
ExportRDSSnapshotToLocNamespaceArg,
ExportRDSSnapshotToLocInstanceIDArg,
ExportRDSSnapshotToLocSnapshotIDArg,
ExportRDSSnapshotToLocDBEngineArg,
}
}

func (*exportRDSSnapshotToLocationFunc) Arguments() []string {
return []string{
ExportRDSSnapshotToLocNamespaceArg,
ExportRDSSnapshotToLocInstanceIDArg,
ExportRDSSnapshotToLocSnapshotIDArg,
ExportRDSSnapshotToLocDBEngineArg,
ExportRDSSnapshotToLocDBUsernameArg,
ExportRDSSnapshotToLocDBPasswordArg,
ExportRDSSnapshotToLocBackupArtPrefixArg,
}
}

func execDumpCommand(ctx context.Context, dbEngine RDSDBEngine, action RDSAction, namespace, dbEndpoint, username, password string, databases []string, backupPrefix, backupID string, profile *param.Profile, dbEngineVersion string) (map[string]interface{}, error) {
Expand Down
15 changes: 14 additions & 1 deletion pkg/function/kube_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,18 @@ func (kef *kubeExecFunc) Exec(ctx context.Context, tp param.TemplateParams, args
}

func (*kubeExecFunc) RequiredArgs() []string {
return []string{KubeExecNamespaceArg, KubeExecPodNameArg, KubeExecCommandArg}
return []string{
KubeExecNamespaceArg,
KubeExecPodNameArg,
KubeExecCommandArg,
}
}

func (*kubeExecFunc) Arguments() []string {
return []string{
KubeExecNamespaceArg,
KubeExecPodNameArg,
KubeExecCommandArg,
KubeExecContainerNameArg,
}
}
16 changes: 15 additions & 1 deletion pkg/function/kube_exec_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,21 @@ func (*kubeExecAllFunc) Exec(ctx context.Context, tp param.TemplateParams, args
}

func (*kubeExecAllFunc) RequiredArgs() []string {
return []string{KubeExecAllNamespaceArg, KubeExecAllPodsNameArg, KubeExecAllContainersNameArg, KubeExecAllCommandArg}
return []string{
KubeExecAllNamespaceArg,
KubeExecAllPodsNameArg,
KubeExecAllContainersNameArg,
KubeExecAllCommandArg,
}
}

func (*kubeExecAllFunc) Arguments() []string {
return []string{
KubeExecAllNamespaceArg,
KubeExecAllPodsNameArg,
KubeExecAllContainersNameArg,
KubeExecAllCommandArg,
}
}

func execAll(ctx context.Context, cli kubernetes.Interface, namespace string, ps []string, cs []string, cmd []string) (map[string]interface{}, error) {
Expand Down
14 changes: 13 additions & 1 deletion pkg/function/kube_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,17 @@ func (ktf *kubeTaskFunc) Exec(ctx context.Context, tp param.TemplateParams, args
}

func (*kubeTaskFunc) RequiredArgs() []string {
return []string{KubeTaskImageArg, KubeTaskCommandArg}
return []string{
KubeTaskImageArg,
KubeTaskCommandArg,
}
}

func (*kubeTaskFunc) Arguments() []string {
return []string{
KubeTaskImageArg,
KubeTaskCommandArg,
KubeTaskNamespaceArg,
KubeTaskPodOverrideArg,
}
}
9 changes: 9 additions & 0 deletions pkg/function/kubeops.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,12 @@ func (*kubeops) RequiredArgs() []string {
KubeOpsOperationArg,
}
}

func (*kubeops) Arguments() []string {
return []string{
KubeOpsSpecArg,
KubeOpsOperationArg,
KubeOpsNamespaceArg,
KubeOpsObjectReferenceArg,
}
}
4 changes: 4 additions & 0 deletions pkg/function/location_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ func (*locationDeleteFunc) Exec(ctx context.Context, tp param.TemplateParams, ar
func (*locationDeleteFunc) RequiredArgs() []string {
return []string{LocationDeleteArtifactArg}
}

func (*locationDeleteFunc) Arguments() []string {
return []string{LocationDeleteArtifactArg}
}
Loading

0 comments on commit 67ecd2a

Please sign in to comment.