-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
131 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
.DS_Store | ||
.idea | ||
.vscode | ||
__debug_bin* | ||
bin/* | ||
kind-logs-* | ||
cover.out | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,94 @@ | ||
package scope | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"sync" | ||
|
||
"github.com/bxcodec/gotcha" | ||
"github.com/bxcodec/gotcha/cache" | ||
"github.com/bxcodec/httpcache" | ||
"github.com/bxcodec/httpcache/cache/inmem" | ||
"github.com/linode/cluster-api-provider-linode/util" | ||
"github.com/linode/linodego" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
var ( | ||
initClient sync.Once | ||
linodeClient linodego.Client | ||
) | ||
|
||
func createLinodeClient(apiKey string) *linodego.Client { | ||
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: apiKey}) | ||
initClient.Do(func() { | ||
//nolint:forcetypeassert // Always OK. | ||
baseTrans := http.DefaultTransport.(*http.Transport).Clone() | ||
baseTrans.MaxIdleConns = util.DefaultLinodeAPIMaxIdleConns | ||
// Otherwise we use just a few connections. | ||
baseTrans.MaxConnsPerHost = baseTrans.MaxIdleConns | ||
baseTrans.MaxIdleConnsPerHost = baseTrans.MaxIdleConns | ||
|
||
oauth2Client := &http.Client{ | ||
Transport: &oauth2.Transport{ | ||
Source: tokenSource, | ||
}, | ||
} | ||
linodeClient := linodego.NewClient(oauth2Client) | ||
oauthTrans := &oauth2.Transport{ | ||
Source: oauth2.StaticTokenSource(&oauth2.Token{AccessToken: apiKey}), | ||
Base: baseTrans, | ||
} | ||
|
||
oauth2Client := &http.Client{ | ||
Timeout: util.DefaultLinodeAPITimeout, | ||
Transport: oauthTrans, | ||
} | ||
|
||
cacheStore := gotcha.New(&cache.Option{ | ||
AlgorithmType: cache.LRUAlgorithm, | ||
ExpiryTime: util.DefaultLinodeAPICacheExpiration, | ||
MaxSizeItem: util.DefaultLinodeAPICacheMaxSizeItem, | ||
MaxMemory: util.DefaultLinodeAPICacheMaxMemory, | ||
}) | ||
cachedTrans := httpcache.NewCacheHandlerRoundtrip(&maxAgeFixRoundTripper{ | ||
oauthTrans, | ||
}, false, inmem.NewCache(cacheStore)) | ||
|
||
oauth2Client.Transport = &reqURIFixRoundTripper{ | ||
plain: oauthTrans, | ||
cached: cachedTrans, | ||
} | ||
|
||
linodeClient = linodego.NewClient(oauth2Client) | ||
}) | ||
|
||
return &linodeClient | ||
} | ||
|
||
type maxAgeFixRoundTripper struct { | ||
http.RoundTripper | ||
} | ||
|
||
func (r *maxAgeFixRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
resp, err := r.RoundTripper.RoundTrip(req) | ||
if err == nil && req.Method == http.MethodGet { | ||
// Workaround, because Linode API sends max-age=0 all the time. | ||
resp.Header.Set("Cache-Control", util.DefaultLinodeAPICacheControlOverride) | ||
} | ||
|
||
return resp, err | ||
} | ||
|
||
type reqURIFixRoundTripper struct { | ||
plain http.RoundTripper | ||
cached http.RoundTripper | ||
} | ||
|
||
func (r *reqURIFixRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
if req.Method != http.MethodGet { | ||
return r.plain.RoundTrip(req) | ||
} | ||
|
||
// Workaround because Linodego doesn't set it. | ||
req.RequestURI = req.URL.Path | ||
|
||
if filter := req.Header.Get("X-Filter"); filter != "" { | ||
req.RequestURI = fmt.Sprintf("%s?filter=%s", req.RequestURI, filter) | ||
} | ||
|
||
return r.cached.RoundTrip(req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
Copyright 2023 Akamai Technologies, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package util | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/bxcodec/gotcha/cache" | ||
) | ||
|
||
const ( | ||
// DefaultLinodeAPITimeout is the default timeout for Linode API calls. | ||
DefaultLinodeAPITimeout = 10 * time.Second | ||
// DefaultLinodeAPIMaxIdleConns is the default max idle connections for Linode API calls. | ||
DefaultLinodeAPIMaxIdleConns = 100 | ||
// DefaultLinodeAPICacheControlOverride is the default cache control override for Linode API calls. | ||
DefaultLinodeAPICacheControlOverride = "max-age=60" | ||
// DefaultLinodeAPICacheExpiration is the default cache expiration for Linode API calls. | ||
DefaultLinodeAPICacheExpiration = 5 * time.Second | ||
// DefaultLinodeAPICacheMaxSizeItem is the default cache max size item for Linode API calls. | ||
DefaultLinodeAPICacheMaxSizeItem = 500 | ||
// DefaultLinodeAPICacheMaxMemory is the default cache max memory for Linode API calls. | ||
DefaultLinodeAPICacheMaxMemory = 1 * cache.MB | ||
) |