Skip to content

Commit

Permalink
Merge pull request #179 from gridscale/feature/add-timeout-flag
Browse files Browse the repository at this point in the history
Allow to set timeout via --timeout flag
  • Loading branch information
nvthongswansea authored Sep 18, 2024
2 parents 936ccb8 + 9af88e6 commit 5336aee
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 7 deletions.
9 changes: 7 additions & 2 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,19 @@ Show summary for a given account:

var wg sync.WaitGroup
ch := make(chan objectCount)
ctxWithTimeout := context.Background()
if rootFlags.timeout > 0 {
var cancel context.CancelFunc
ctxWithTimeout, cancel = context.WithTimeout(ctxWithTimeout, rootFlags.timeout)
defer cancel()
}

for k, v := range funcs {
wg.Add(1)
cCopy := context.Background()
go func(obj string, f func(context.Context, *gsclient.Client) (map[string]interface{}, error)) {
defer wg.Done()

agg, err := f(cCopy, client)
agg, err := f(ctxWithTimeout, client)
if err != nil {
ch <- objectCount{obj, nil, NewError(cmd, fmt.Sprintf("Could not get %s", obj), err)}
}
Expand Down
30 changes: 30 additions & 0 deletions cmd/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ var ipLsCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
ipOp := rt.IPOperator()
ctx := context.Background()
if rootFlags.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, rootFlags.timeout)
defer cancel()
}
ipAddresses, err := ipOp.GetIPList(ctx)
if err != nil {
return NewError(cmd, "Could not get list of IP addresses", err)
Expand Down Expand Up @@ -123,6 +128,11 @@ Delete by address:
var id string
var err error
ctx := context.Background()
if rootFlags.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, rootFlags.timeout)
defer cancel()
}
ipOp := rt.IPOperator()
address := net.ParseIP(args[0])
if address != nil {
Expand Down Expand Up @@ -160,6 +170,11 @@ Set PTR entry and name on an existing IP:
var err error
address := net.ParseIP(args[0])
ctx := context.Background()
if rootFlags.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, rootFlags.timeout)
defer cancel()
}
ipOp := rt.IPOperator()
if address != nil {
id, err = idForAddress(ctx, address, ipOp)
Expand Down Expand Up @@ -223,6 +238,11 @@ Create a new IPv4 address:
}
ipOp := rt.IPOperator()
ctx := context.Background()
if rootFlags.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, rootFlags.timeout)
defer cancel()
}
ipAddress, err := ipOp.CreateIP(ctx, gsclient.IPCreateRequest{
Family: family,
Failover: ipFlags.failover,
Expand Down Expand Up @@ -264,6 +284,11 @@ Releasing an unassigned IP address will exit with status code 0:
var ipID string
var err error
ctx := context.Background()
if rootFlags.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, rootFlags.timeout)
defer cancel()
}
ipOp := rt.IPOperator()
address := net.ParseIP(args[0])
if address != nil {
Expand Down Expand Up @@ -319,6 +344,11 @@ Assign an IPv4 address to a server or load balancer:
var err error

ctx := context.Background()
if rootFlags.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, rootFlags.timeout)
defer cancel()
}
ipOp := rt.IPOperator()
addr := net.ParseIP(args[0])
if addr != nil {
Expand Down
15 changes: 15 additions & 0 deletions cmd/isoImage.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ var isoImageLsCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
imageOp := rt.ISOImageOperator()
ctx := context.Background()
if rootFlags.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, rootFlags.timeout)
defer cancel()
}
images, err := imageOp.GetISOImageList(ctx)
if err != nil {
return NewError(cmd, "Could not get list of images", err)
Expand Down Expand Up @@ -82,6 +87,11 @@ var isoImageRmCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
imageOp := rt.ISOImageOperator()
ctx := context.Background()
if rootFlags.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, rootFlags.timeout)
defer cancel()
}
err := imageOp.DeleteISOImage(ctx, args[0])
if err != nil {
return NewError(cmd, "Deleting image failed", err)
Expand Down Expand Up @@ -111,6 +121,11 @@ Create a Fedora CoreOS image:

imageOp := rt.ISOImageOperator()
ctx := context.Background()
if rootFlags.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, rootFlags.timeout)
defer cancel()
}
image, err := imageOp.CreateISOImage(ctx, gsclient.ISOImageCreateRequest{
Name: isoImageFlags.name,
SourceURL: isoImageFlags.sourceURL,
Expand Down
37 changes: 32 additions & 5 deletions cmd/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ var getKubernetesReleasesCmd = &cobra.Command{
Long: "Prints all available Kubernetes releases. The latest three releases are supported.",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
if rootFlags.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, rootFlags.timeout)
defer cancel()
}
out := new(bytes.Buffer)
op := rt.PaaSOperator()
paasTemplates, err := op.GetPaaSTemplateList(ctx)
Expand Down Expand Up @@ -97,6 +102,11 @@ var getKubernetesVersionsCmd = &cobra.Command{
Long: "Prints all available GS Kubernetes versions.",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
if rootFlags.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, rootFlags.timeout)
defer cancel()
}
out := new(bytes.Buffer)
op := rt.PaaSOperator()
paasTemplates, err := op.GetPaaSTemplateList(ctx)
Expand Down Expand Up @@ -136,6 +146,11 @@ var getKubernetesVersionsCmd = &cobra.Command{
func clusterLsCmdRun(cmd *cobra.Command, args []string) error {
paasOp := rt.PaaSOperator()
ctx := context.Background()
if rootFlags.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, rootFlags.timeout)
defer cancel()
}
out := new(bytes.Buffer)
paasList, err := paasOp.GetPaaSServiceList(ctx)
if err != nil {
Expand Down Expand Up @@ -201,6 +216,12 @@ KUBECONFIG
Specifies the path to the kubeconfig. Gets overriden by --kubeconfig
`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
if rootFlags.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, rootFlags.timeout)
defer cancel()
}
kubeConfigFile, _ := cmd.Flags().GetString("kubeconfig")
clusterID, _ := cmd.Flags().GetString("cluster")
credentialPlugin, _ := cmd.Flags().GetBool("credential-plugin")
Expand All @@ -225,7 +246,7 @@ KUBECONFIG
}

op := rt.KubernetesOperator()
newKubeConfig, _, err := fetchKubeConfigFromProvider(op, clusterID)
newKubeConfig, _, err := fetchKubeConfigFromProvider(ctx, op, clusterID)
if err != nil {
return NewError(cmd, "Invalid kubeconfig", err)
}
Expand Down Expand Up @@ -302,6 +323,12 @@ var execCredentialCmd = &cobra.Command{
Short: "Provides client credentials to kubectl command",
Long: "exec-credential provides client credentials to kubectl command.",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
if rootFlags.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, rootFlags.timeout)
defer cancel()
}
kubeConfigFile, _ := cmd.Flags().GetString("kubeconfig")
clusterID, _ := cmd.Flags().GetString("cluster")

Expand All @@ -323,7 +350,7 @@ var execCredentialCmd = &cobra.Command{
op := rt.KubernetesOperator()

if execCredential == nil {
newKubeConfig, expirationTime, err := fetchKubeConfigFromProvider(op, clusterID)
newKubeConfig, expirationTime, err := fetchKubeConfigFromProvider(ctx, op, clusterID)
if err != nil {
return NewError(cmd, "Could not fetch kubeconfig", err)
}
Expand Down Expand Up @@ -384,15 +411,15 @@ func init() {
rootCmd.AddCommand(kubernetesCmd)
}

func fetchKubeConfigFromProvider(op runtime.KubernetesOperator, id string) (kubeConfig, time.Time, error) {
func fetchKubeConfigFromProvider(ctx context.Context, op runtime.KubernetesOperator, id string) (kubeConfig, time.Time, error) {
var kc kubeConfig
var expirationTime time.Time

if err := op.RenewK8sCredentials(context.Background(), id); err != nil {
if err := op.RenewK8sCredentials(ctx, id); err != nil {
return kubeConfig{}, time.Time{}, err
}

platformService, err := op.GetPaaSService(context.Background(), id)
platformService, err := op.GetPaaSService(ctx, id)
if err != nil {
return kubeConfig{}, time.Time{}, err
}
Expand Down
15 changes: 15 additions & 0 deletions cmd/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ var networkLsCmd = &cobra.Command{
Long: `List networks.`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
if rootFlags.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, rootFlags.timeout)
defer cancel()
}
out := new(bytes.Buffer)
networkOps := rt.NetworkOperator()
networks, err := networkOps.GetNetworkList(ctx)
Expand Down Expand Up @@ -91,6 +96,11 @@ Create a network:

networkOp := rt.NetworkOperator()
ctx := context.Background()
if rootFlags.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, rootFlags.timeout)
defer cancel()
}
network, err := networkOp.CreateNetwork(ctx, gsclient.NetworkCreateRequest{
Name: networkFlags.networkName,
})
Expand All @@ -115,6 +125,11 @@ var networkRmCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
if rootFlags.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, rootFlags.timeout)
defer cancel()
}
networkOps := rt.NetworkOperator()
err := networkOps.DeleteNetwork(ctx, args[0])
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions cmd/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ var postgresReleasesCmd = &cobra.Command{
Long: "Returns the available PostgreSQL releases",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
if rootFlags.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, rootFlags.timeout)
defer cancel()
}
out := new(bytes.Buffer)
op := rt.PaaSOperator()
paasTemplates, err := op.GetPaaSTemplateList(ctx)
Expand Down
14 changes: 14 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/gridscale/gscloud/render"
"github.com/gridscale/gscloud/runtime"
Expand Down Expand Up @@ -35,6 +36,8 @@ type rootCmdFlags struct {
json bool
quiet bool
debug bool
timeoutStr string
timeout time.Duration
}

var (
Expand Down Expand Up @@ -182,6 +185,17 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&rootFlags.quiet, "quiet", "q", false, "Print only object IDs")
rootCmd.PersistentFlags().BoolVar(&rootFlags.debug, "debug", false, "Debug mode")
rootCmd.PersistentFlags().BoolP("help", "h", false, "Print usage")
rootCmd.PersistentFlags().StringVar(&rootFlags.timeoutStr, "timeout", "", "Timeout for API requests. Examples: 10s, 10m, 1h")
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
if rootFlags.timeoutStr != "" {
var err error
rootFlags.timeout, err = time.ParseDuration(rootFlags.timeoutStr)
if err != nil {
return fmt.Errorf("invalid timeout: %s", err)
}
}
return nil
}
}

func exists(path string) bool {
Expand Down
Loading

0 comments on commit 5336aee

Please sign in to comment.