Skip to content

Commit

Permalink
[tweak] - set timeout for default http clients, reduce webhook timeout (
Browse files Browse the repository at this point in the history
#324)

* set timeout for default http clients, reduce webhook timeout to default of 10 seconds

* make linode client timeout configurable per resource
  • Loading branch information
AshleyDumaine authored May 21, 2024
1 parent a3be836 commit 1a93e85
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 10 deletions.
6 changes: 4 additions & 2 deletions api/v1alpha1/webhook_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import (

const (
// defaultWebhookTimeout is the default timeout for an admission request
defaultWebhookTimeout = time.Minute
defaultWebhookTimeout = time.Second * 10
// defaultClientTimeout is the default timeout for a client Linode API call
defaultClientTimeout = time.Second * 10
)

var (
// defaultLinodeClient is an unauthenticated Linode client
defaultLinodeClient = linodego.NewClient(http.DefaultClient)
defaultLinodeClient = linodego.NewClient(&http.Client{Timeout: defaultClientTimeout})
)

func validateRegion(ctx context.Context, client LinodeClient, id string, path *field.Path) *field.Error {
Expand Down
2 changes: 1 addition & 1 deletion cloud/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func NewClusterScope(ctx context.Context, apiKey string, params ClusterScopePara
}
apiKey = string(data)
}
linodeClient, err := CreateLinodeClient(apiKey)
linodeClient, err := CreateLinodeClient(apiKey, defaultClientTimeout)
if err != nil {
return nil, fmt.Errorf("failed to create linode client: %w", err)
}
Expand Down
9 changes: 8 additions & 1 deletion cloud/scope/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"strings"
"time"

"github.com/linode/linodego"
"golang.org/x/oauth2"
Expand All @@ -18,7 +19,12 @@ import (
. "github.com/linode/cluster-api-provider-linode/clients"
)

func CreateLinodeClient(apiKey string) (*linodego.Client, error) {
const (
// defaultClientTimeout is the default timeout for a client Linode API call
defaultClientTimeout = time.Second * 10
)

func CreateLinodeClient(apiKey string, timeout time.Duration) (*linodego.Client, error) {
if apiKey == "" {
return nil, errors.New("missing Linode API key")
}
Expand All @@ -29,6 +35,7 @@ func CreateLinodeClient(apiKey string) (*linodego.Client, error) {
Transport: &oauth2.Transport{
Source: tokenSource,
},
Timeout: timeout,
}
linodeClient := linodego.NewClient(oauth2Client)

Expand Down
2 changes: 1 addition & 1 deletion cloud/scope/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestCreateLinodeClient(t *testing.T) {
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()

got, err := CreateLinodeClient(testCase.apiKey)
got, err := CreateLinodeClient(testCase.apiKey, defaultClientTimeout)

if testCase.expectedErr != nil {
assert.EqualError(t, err, testCase.expectedErr.Error())
Expand Down
2 changes: 1 addition & 1 deletion cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func NewMachineScope(ctx context.Context, apiKey string, params MachineScopePara
}
apiKey = string(data)
}
linodeClient, err := CreateLinodeClient(apiKey)
linodeClient, err := CreateLinodeClient(apiKey, defaultClientTimeout)
if err != nil {
return nil, fmt.Errorf("failed to create linode client: %w", err)
}
Expand Down
10 changes: 7 additions & 3 deletions cloud/scope/object_storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"time"

"github.com/go-logr/logr"
"github.com/linode/linodego"
Expand Down Expand Up @@ -47,8 +48,11 @@ type ObjectStorageBucketScope struct {
PatchHelper *patch.Helper
}

const AccessKeyNameTemplate = "%s-bucket-details"
const NumAccessKeys = 2
const (
AccessKeyNameTemplate = "%s-bucket-details"
NumAccessKeys = 2
clientTimeout = 20 * time.Second
)

func validateObjectStorageBucketScopeParams(params ObjectStorageBucketScopeParams) error {
if params.Bucket == nil {
Expand All @@ -74,7 +78,7 @@ func NewObjectStorageBucketScope(ctx context.Context, apiKey string, params Obje
}
apiKey = string(data)
}
linodeClient, err := CreateLinodeClient(apiKey)
linodeClient, err := CreateLinodeClient(apiKey, clientTimeout)
if err != nil {
return nil, fmt.Errorf("failed to create linode client: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cloud/scope/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func NewVPCScope(ctx context.Context, apiKey string, params VPCScopeParams) (*VP
}
apiKey = string(data)
}
linodeClient, err := CreateLinodeClient(apiKey)
linodeClient, err := CreateLinodeClient(apiKey, defaultClientTimeout)
if err != nil {
return nil, fmt.Errorf("failed to create linode client: %w", err)
}
Expand Down

0 comments on commit 1a93e85

Please sign in to comment.