-
Notifications
You must be signed in to change notification settings - Fork 137
feat(instances): cache server lookups to reduce API calls #1252
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
Draft
lukasmetzner
wants to merge
10
commits into
main
Choose a base branch
from
fix-improve-api-usage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1d6ef5f
feat(instances): cache server lookups to reduce API calls
lukasmetzner d308874
docs: server cache interface
lukasmetzner 8c694bc
test: improve coverage
lukasmetzner 5245ef7
fix: prometheus metric label typo
lukasmetzner 8f3fc0a
test: rename test after implementation changed
lukasmetzner 817e35f
feat: wip impl
lukasmetzner 47fd6a0
refactor: wip cleanup
lukasmetzner 035c73f
refactor: gobal config option
lukasmetzner e5ca590
docs: update cache settings
lukasmetzner a2c56c8
test: simplify call counting and client mocking
jooola File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,4 +1,4 @@ | ||
| FROM golang:1.26 as builder | ||
| FROM golang:1.26 AS builder | ||
|
|
||
| WORKDIR /build | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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,26 @@ | ||
| # Instance Cache | ||
|
|
||
| > **Experimental:** Instance caching is experimental, breaking changes may occur within minor releases. We believe the implementation is safe in practice — that is why it ships enabled by default (`all-server`). Set `HCLOUD_CACHE_MODE=off` to opt out. | ||
|
|
||
| The instance cache reduces calls to the Hetzner Cloud API made by the `InstancesV2` controller, which looks up Servers by ID or name to reconcile Node state. The cache sits between the controller and the Hetzner Cloud API; behavior is controlled by the environment variables below. | ||
|
|
||
| ## Environment Variables | ||
|
|
||
| | Name | Type | Default | Description | | ||
| | ------------------- | ------------------- | ------- | ------------------------------------------------------------------------------------- | | ||
| | `HCLOUD_CACHE_MODE` | `all \| one \| off` | `all` | Selects the caching strategy. See [Modes](#modes) below. | | ||
| | `HCLOUD_CACHE_TTL` | `duration` | `10s` | Lifetime of cached entries. Accepts any Go `time.Duration` string (e.g. `30s`, `2m`). | | ||
|
|
||
| ## Modes | ||
|
|
||
| ### `all` | ||
|
|
||
| Fetches every Server in the project with a single `GET /servers` call and serves all subsequent `ByID` / `ByName` lookups from the resulting snapshot until the TTL expires. The snapshot is refreshed on the next lookup after expiry. On a cache miss within the TTL (e.g. a freshly created Server), one rate-limited refresh per TTL window is allowed to pick up the new Server; further misses in the same window return without an API call. | ||
|
|
||
| ### `one` | ||
|
|
||
| Caches each Server individually with its own expiration. A `ByID` / `ByName` lookup either returns a non-expired entry or issues a `GET /servers/{id}` (or `GET /servers?name=`) call and stores the result. Expired entries are evicted lazily when other entries are inserted. | ||
|
|
||
| ### `off` | ||
|
|
||
| Disables caching entirely. Every lookup goes directly to the API. | ||
This file contains hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -37,6 +37,7 @@ import ( | |||||
| "github.com/hetznercloud/hcloud-cloud-controller-manager/internal/hcops" | ||||||
| "github.com/hetznercloud/hcloud-cloud-controller-manager/internal/metrics" | ||||||
| "github.com/hetznercloud/hcloud-cloud-controller-manager/internal/robot" | ||||||
| "github.com/hetznercloud/hcloud-cloud-controller-manager/internal/servercache" | ||||||
| "github.com/hetznercloud/hcloud-go/v2/hcloud" | ||||||
| "github.com/hetznercloud/hcloud-go/v2/hcloud/metadata" | ||||||
| ) | ||||||
|
|
@@ -50,13 +51,14 @@ const ( | |||||
| var providerVersion = "unknown" | ||||||
|
|
||||||
| type cloud struct { | ||||||
| client *hcloud.Client | ||||||
| robotClient hrobot.RobotClient | ||||||
| cfg config.HCCMConfiguration | ||||||
| recorder record.EventRecorder | ||||||
| networkID int64 | ||||||
| cidr string | ||||||
| nodeLister corelisters.NodeLister | ||||||
| client *hcloud.Client | ||||||
| robotClient hrobot.RobotClient | ||||||
| instanceCache *servercache.Cache[hcloud.Server] | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| cfg config.HCCMConfiguration | ||||||
| recorder record.EventRecorder | ||||||
| networkID int64 | ||||||
| cidr string | ||||||
| nodeLister corelisters.NodeLister | ||||||
| } | ||||||
|
|
||||||
| func NewCloud(cidr string, nodeLister corelisters.NodeLister) (cloudprovider.Interface, error) { | ||||||
|
|
@@ -144,13 +146,16 @@ func NewCloud(cidr string, nodeLister corelisters.NodeLister) (cloudprovider.Int | |||||
|
|
||||||
| klog.Infof("Hetzner Cloud k8s cloud controller %s started\n", providerVersion) | ||||||
|
|
||||||
| instanceCache := servercache.NewServerCache(client, cfg.Cache.Mode, cfg.Cache.TTL) | ||||||
|
|
||||||
| return &cloud{ | ||||||
| client: client, | ||||||
| robotClient: robotClient, | ||||||
| cfg: cfg, | ||||||
| networkID: networkID, | ||||||
| cidr: cidr, | ||||||
| nodeLister: nodeLister, | ||||||
| client: client, | ||||||
| robotClient: robotClient, | ||||||
| instanceCache: instanceCache, | ||||||
| cfg: cfg, | ||||||
| networkID: networkID, | ||||||
| cidr: cidr, | ||||||
| nodeLister: nodeLister, | ||||||
| }, nil | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -175,7 +180,7 @@ func (c *cloud) Instances() (cloudprovider.Instances, bool) { | |||||
| } | ||||||
|
|
||||||
| func (c *cloud) InstancesV2() (cloudprovider.InstancesV2, bool) { | ||||||
| return newInstances(c.client, c.robotClient, c.recorder, c.networkID, c.cfg), true | ||||||
| return newInstances(c.client, c.robotClient, c.instanceCache, c.recorder, c.networkID, c.cfg), true | ||||||
| } | ||||||
|
|
||||||
| func (c *cloud) Zones() (cloudprovider.Zones, bool) { | ||||||
|
|
||||||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.