Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Limit --since and --after options to keys and VMs #684

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cmd/purge/keys/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package keys

import (
"fmt"
"time"

"github.com/spf13/cobra"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -83,3 +84,9 @@ pvsadm purge --help for information
return nil
},
}

func init() {
Cmd.PersistentFlags().DurationVar(&pkg.Options.Since, "since", 0*time.Second, "Remove resources since mentioned duration(format: 99h99m00s), mutually exclusive with --before")
Cmd.PersistentFlags().DurationVar(&pkg.Options.Before, "before", 0*time.Second, "Remove resources before mentioned duration(format: 99h99m00s), mutually exclusive with --since")
Cmd.MarkFlagsMutuallyExclusive("since", "before")
}
23 changes: 15 additions & 8 deletions cmd/purge/networks/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,27 @@ pvsadm purge --help for information
// Clean up instances and ports associated with the network instance
for _, port := range ports.Ports {
pvminstance := port.PvmInstance
portID := port.PortID
if deleteInstances && pvminstance != nil {
if deleteInstances && (pvminstance != nil) {
err = pvmclient.InstanceClient.Delete(pvminstance.PvmInstanceID)
if err != nil {
return fmt.Errorf("failed to delete the instance: %v", err)
if opt.IgnoreErrors {
klog.Errorf("error occurred while deleting PVMInstance: %s associated with network %s : %v", pvminstance.PvmInstanceID, *network.Name, err)
} else {
return err
}
}
klog.Infof("Successfully deleted a instance %s using network '%s'", pvminstance.PvmInstanceID, *network.Name)
}
if deletePorts {
err = pvmclient.NetworkClient.DeletePort(*network.NetworkID, *portID)
err = pvmclient.NetworkClient.DeletePort(*network.NetworkID, *port.PortID)
if err != nil {
return fmt.Errorf("failed to delete a port, err: %v", err)
if opt.IgnoreErrors {
klog.Errorf("error occurred while deleting port: %s associated with network %s : %v", *port.PortID, *network.Name, err)
} else {
return err
}
}
klog.Infof("Successfully deleted a port %s using network '%s'", *portID, *network.Name)
klog.Infof("Successfully deleted a port %s using network '%s'", *port.PortID, *network.Name)
}
}
}
Expand All @@ -108,6 +115,6 @@ pvsadm purge --help for information
}

func init() {
Cmd.PersistentFlags().BoolVar(&deletePorts, "ports", false, "Delete ports")
Cmd.PersistentFlags().BoolVar(&deleteInstances, "instances", false, "Delete instances")
Cmd.PersistentFlags().BoolVar(&deletePorts, "ports", false, "Delete ports that are associated with the network")
Cmd.PersistentFlags().BoolVar(&deleteInstances, "instances", false, "Delete instances that are associated with the network")
}
11 changes: 1 addition & 10 deletions cmd/purge/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
package purge

import (
"fmt"
"time"
"github.com/spf13/cobra"

"github.com/ppc64le-cloud/pvsadm/cmd/purge/images"
"github.com/ppc64le-cloud/pvsadm/cmd/purge/keys"
Expand All @@ -25,7 +24,6 @@ import (
"github.com/ppc64le-cloud/pvsadm/cmd/purge/volumes"
"github.com/ppc64le-cloud/pvsadm/pkg"
"github.com/ppc64le-cloud/pvsadm/pkg/utils"
"github.com/spf13/cobra"
)

var Cmd = &cobra.Command{
Expand Down Expand Up @@ -81,11 +79,6 @@ Examples:
if err := root.PersistentPreRunE(cmd, args); err != nil {
return err
}

if pkg.Options.Since != 0 && pkg.Options.Before != 0 {
return fmt.Errorf("--since and --before options can not be set at a time")
}

return utils.EnsurePrerequisitesAreSet(pkg.Options.APIKey, pkg.Options.WorkspaceID, pkg.Options.WorkspaceName)
},
}
Expand All @@ -103,8 +96,6 @@ func init() {
Cmd.PersistentFlags().StringVarP(&pkg.Options.WorkspaceID, "workspace-id", "", "", "Workspace ID of the PowerVS workspace")
Cmd.PersistentFlags().StringVarP(&pkg.Options.WorkspaceName, "workspace-name", "", "", "Workspace name of the PowerVS workspace")
Cmd.PersistentFlags().BoolVar(&pkg.Options.DryRun, "dry-run", false, "dry run the action and don't delete the actual resources")
Cmd.PersistentFlags().DurationVar(&pkg.Options.Since, "since", 0*time.Second, "Remove resources since mentioned duration(format: 99h99m00s), mutually exclusive with --before")
Cmd.PersistentFlags().DurationVar(&pkg.Options.Before, "before", 0*time.Second, "Remove resources before mentioned duration(format: 99h99m00s), mutually exclusive with --since")
Cmd.PersistentFlags().BoolVar(&pkg.Options.NoPrompt, "no-prompt", false, "Show prompt before doing any destructive operations")
Cmd.PersistentFlags().BoolVar(&pkg.Options.IgnoreErrors, "ignore-errors", false, "Ignore any errors during the operations")
Cmd.PersistentFlags().StringVar(&pkg.Options.Expr, "regexp", "", "Regular Expressions for filtering the selection")
Expand Down
12 changes: 10 additions & 2 deletions cmd/purge/vms/vms.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ package vms
import (
"fmt"
"strings"
"time"

"github.com/spf13/cobra"
"k8s.io/klog/v2"

"github.com/ppc64le-cloud/pvsadm/pkg"
"github.com/ppc64le-cloud/pvsadm/pkg/audit"
"github.com/ppc64le-cloud/pvsadm/pkg/client"
"github.com/ppc64le-cloud/pvsadm/pkg/utils"
"github.com/spf13/cobra"
"k8s.io/klog/v2"
)

var Cmd = &cobra.Command{
Expand Down Expand Up @@ -90,3 +92,9 @@ pvsadm purge --help for information
return nil
},
}

func init() {
Cmd.PersistentFlags().DurationVar(&pkg.Options.Since, "since", 0*time.Second, "Remove resources since mentioned duration(format: 99h99m00s), mutually exclusive with --before")
Cmd.PersistentFlags().DurationVar(&pkg.Options.Before, "before", 0*time.Second, "Remove resources before mentioned duration(format: 99h99m00s), mutually exclusive with --since")
Cmd.MarkFlagsMutuallyExclusive("since", "before")
}