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

add timeout for linodego http client #219

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,15 @@ sessionAffinityConfig:
timeoutSeconds: 100
```

## Additional environment variables
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.

Environment Variable | Default | Description
---|---|---
`LINODE_INSTANCE_CACHE_TTL` | `15` | Default timeout of instance cache in seconds
`LINODE_ROUTES_CACHE_TTL_SECONDS` | `60` | Default timeout of route cache in seconds
`LINODE_REQUEST_TIMEOUT_SECONDS` | `120` | Default timeout in seconds for http requests to linode API

## Generating a Manifest for Deployment
Use the script located at `./deploy/generate-manifest.sh` to generate a self-contained deployment manifest for the Linode CCM. Two arguments are required.

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

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

### To upgrade when new changes are made to the helm chart. Run the following command:
```sh
export VERSION=v0.3.22
export VERSION=v0.4.8
export LINODE_API_TOKEN=<linodeapitoken>
export REGION=<linoderegion>

Expand Down
22 changes: 19 additions & 3 deletions cloud/linode/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@ package client

import (
"context"
"net/http"
"time"

"github.com/linode/linodego"
"golang.org/x/oauth2"
"k8s.io/klog/v2"
)

const (
// DefaultClientTimeout is the default timeout for a client Linode API call
DefaultClientTimeout = 120 * time.Second
)

type Client interface {
Expand Down Expand Up @@ -48,14 +57,21 @@ type Client interface {
var _ Client = (*linodego.Client)(nil)

// New creates a new linode client with a given token, userAgent, and API URL
func New(token, userAgent, apiURL string) (*linodego.Client, error) {
linodeClient := linodego.NewClient(nil)
func New(token, userAgent, apiURL string, timeout time.Duration) (*linodego.Client, error) {
nesv marked this conversation as resolved.
Show resolved Hide resolved
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
oauth2Client := &http.Client{
Transport: &oauth2.Transport{
Source: tokenSource,
},
Timeout: timeout,
}
linodeClient := linodego.NewClient(oauth2Client)
client, err := linodeClient.UseURL(apiURL)
if err != nil {
return nil, err
}
client.SetUserAgent(userAgent)
client.SetToken(token)

klog.V(3).Infof("Linode client created with default timeout of %v seconds", timeout)
return client, nil
}
12 changes: 11 additions & 1 deletion cloud/linode/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"
"io"
"os"
"strconv"
"sync"
"time"

"github.com/linode/linodego"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -98,7 +100,15 @@ func newCloud() (cloudprovider.Interface, error) {
url := os.Getenv(urlEnv)
ua := fmt.Sprintf("linode-cloud-controller-manager %s", linodego.DefaultUserAgent)

linodeClient, err := client.New(apiToken, ua, url)
// set timeout used by linodeclient for API calls
timeout := client.DefaultClientTimeout
if raw, ok := os.LookupEnv("LINODE_REQUEST_TIMEOUT_SECONDS"); ok {
if t, _ := strconv.Atoi(raw); t > 0 {
timeout = time.Duration(t) * time.Second
}
nesv marked this conversation as resolved.
Show resolved Hide resolved
}

linodeClient, err := client.New(apiToken, ua, url, timeout)
if err != nil {
return nil, fmt.Errorf("client was not created succesfully: %w", err)
}
Expand Down
Loading