Skip to content

Commit 6a4e482

Browse files
authored
add timeout for linodego http client (#219)
* add timeout for linodego http client * address review comments --------- Co-authored-by: Rahul Sharma <[email protected]>
1 parent 4cda99a commit 6a4e482

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

README.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,15 @@ sessionAffinityConfig:
286286
timeoutSeconds: 100
287287
```
288288

289+
## Additional environment variables
290+
To tweak CCM based on needs, one can overwrite the default values set for caches and requests by setting appropriate environment variables when applying the manifest or helm chart.
291+
292+
Environment Variable | Default | Description
293+
---|---|---
294+
`LINODE_INSTANCE_CACHE_TTL` | `15` | Default timeout of instance cache in seconds
295+
`LINODE_ROUTES_CACHE_TTL_SECONDS` | `60` | Default timeout of route cache in seconds
296+
`LINODE_REQUEST_TIMEOUT_SECONDS` | `120` | Default timeout in seconds for http requests to linode API
297+
289298
## Generating a Manifest for Deployment
290299
Use the script located at `./deploy/generate-manifest.sh` to generate a self-contained deployment manifest for the Linode CCM. Two arguments are required.
291300

@@ -320,7 +329,7 @@ helm repo update ccm-linode
320329
### To deploy ccm-linode. Run the following command:
321330

322331
```sh
323-
export VERSION=v0.3.22
332+
export VERSION=v0.4.8
324333
export LINODE_API_TOKEN=<linodeapitoken>
325334
export REGION=<linoderegion>
326335
helm install ccm-linode --set apiToken=$LINODE_API_TOKEN,region=$REGION ccm-linode/ccm-linode
@@ -335,7 +344,7 @@ _See [helm uninstall](https://helm.sh/docs/helm/helm_uninstall/) for command doc
335344

336345
### To upgrade when new changes are made to the helm chart. Run the following command:
337346
```sh
338-
export VERSION=v0.3.22
347+
export VERSION=v0.4.8
339348
export LINODE_API_TOKEN=<linodeapitoken>
340349
export REGION=<linoderegion>
341350

cloud/linode/client/client.go

+25-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,19 @@ package client
44

55
import (
66
"context"
7+
"fmt"
8+
"net/http"
9+
"os"
10+
"time"
711

812
"github.com/linode/linodego"
13+
"golang.org/x/oauth2"
14+
"k8s.io/klog/v2"
15+
)
16+
17+
const (
18+
// DefaultClientTimeout is the default timeout for a client Linode API call
19+
DefaultClientTimeout = 120 * time.Second
920
)
1021

1122
type Client interface {
@@ -47,15 +58,25 @@ type Client interface {
4758
// linodego.Client implements Client
4859
var _ Client = (*linodego.Client)(nil)
4960

50-
// New creates a new linode client with a given token, userAgent, and API URL
51-
func New(token, userAgent, apiURL string) (*linodego.Client, error) {
52-
linodeClient := linodego.NewClient(nil)
61+
// New creates a new linode client with a given token and default timeout
62+
func New(token string, timeout time.Duration) (*linodego.Client, error) {
63+
userAgent := fmt.Sprintf("linode-cloud-controller-manager %s", linodego.DefaultUserAgent)
64+
apiURL := os.Getenv("LINODE_URL")
65+
66+
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
67+
oauth2Client := &http.Client{
68+
Transport: &oauth2.Transport{
69+
Source: tokenSource,
70+
},
71+
Timeout: timeout,
72+
}
73+
linodeClient := linodego.NewClient(oauth2Client)
5374
client, err := linodeClient.UseURL(apiURL)
5475
if err != nil {
5576
return nil, err
5677
}
5778
client.SetUserAgent(userAgent)
58-
client.SetToken(token)
5979

80+
klog.V(3).Infof("Linode client created with default timeout of %v", timeout)
6081
return client, nil
6182
}

cloud/linode/cloud.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import (
44
"fmt"
55
"io"
66
"os"
7+
"strconv"
78
"sync"
9+
"time"
810

9-
"github.com/linode/linodego"
1011
"github.com/spf13/pflag"
1112
"golang.org/x/exp/slices"
1213
"k8s.io/client-go/informers"
@@ -20,7 +21,6 @@ const (
2021
ProviderName = "linode"
2122
accessTokenEnv = "LINODE_API_TOKEN"
2223
regionEnv = "LINODE_REGION"
23-
urlEnv = "LINODE_URL"
2424
ciliumLBType = "cilium-bgp"
2525
nodeBalancerLBType = "nodebalancer"
2626
)
@@ -95,10 +95,15 @@ func newCloud() (cloudprovider.Interface, error) {
9595
return nil, fmt.Errorf("%s must be set in the environment (use a k8s secret)", regionEnv)
9696
}
9797

98-
url := os.Getenv(urlEnv)
99-
ua := fmt.Sprintf("linode-cloud-controller-manager %s", linodego.DefaultUserAgent)
98+
// set timeout used by linodeclient for API calls
99+
timeout := client.DefaultClientTimeout
100+
if raw, ok := os.LookupEnv("LINODE_REQUEST_TIMEOUT_SECONDS"); ok {
101+
if t, err := strconv.Atoi(raw); err == nil && t > 0 {
102+
timeout = time.Duration(t) * time.Second
103+
}
104+
}
100105

101-
linodeClient, err := client.New(apiToken, ua, url)
106+
linodeClient, err := client.New(apiToken, timeout)
102107
if err != nil {
103108
return nil, fmt.Errorf("client was not created succesfully: %w", err)
104109
}

0 commit comments

Comments
 (0)