-
Notifications
You must be signed in to change notification settings - Fork 139
feat: warn on deprecated or unconfigured lb type #1313
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
35a0a89
feat: warn on deprecated or unconfigured lb type
lukasmetzner d2c92a7
refactor: use hcloud-go util
lukasmetzner 23aa6e6
fix: error on unavailable lb type
lukasmetzner 81ea7e5
feat: load balancer types cache metrics
lukasmetzner 7ce1c0b
fix: set subsystem for metric
lukasmetzner bfd4991
refactor: update internal/cache/lbtypecache.go
lukasmetzner c6f741a
fix: lint
lukasmetzner 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
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,44 @@ | ||
| package cache | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
|
|
||
| "github.com/hetznercloud/hcloud-cloud-controller-manager/internal/metrics" | ||
| "github.com/hetznercloud/hcloud-go/v2/hcloud" | ||
| ) | ||
|
|
||
| var lbTypeCacheRequests = prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
| Namespace: "cloud_controller_manager", | ||
| Subsystem: "load_balancer_type", | ||
| Name: "cache_requests_total", | ||
| Help: "Total cache requests to the Load Balancer Types API partitioned by subsystem, mode and result.", | ||
| }, []string{"subsystem", "mode", "result"}) | ||
|
lukasmetzner marked this conversation as resolved.
|
||
|
|
||
| func init() { | ||
| metrics.GetRegistry().MustRegister(lbTypeCacheRequests) | ||
| } | ||
|
|
||
| func NewLoadBalancerTypeCache(client *hcloud.Client, defaultMode Mode, defaultMaxAge time.Duration) *Cache[hcloud.LoadBalancerType] { | ||
| return newCache[hcloud.LoadBalancerType]( | ||
| func(ctx context.Context, id int64) (*hcloud.LoadBalancerType, error) { | ||
| value, _, err := client.LoadBalancerType.GetByID(ctx, id) | ||
| return value, err | ||
| }, | ||
| func(ctx context.Context, name string) (*hcloud.LoadBalancerType, error) { | ||
| value, _, err := client.LoadBalancerType.GetByName(ctx, name) | ||
| return value, err | ||
| }, | ||
| func(ctx context.Context) ([]*hcloud.LoadBalancerType, error) { | ||
| values, err := client.LoadBalancerType.All(ctx) | ||
| return values, err | ||
| }, | ||
| func(value *hcloud.LoadBalancerType) int64 { return value.ID }, | ||
| func(value *hcloud.LoadBalancerType) string { return value.Name }, | ||
| lbTypeCacheRequests, | ||
| defaultMode, | ||
| defaultMaxAge, | ||
| ) | ||
| } | ||
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,65 @@ | ||
| package cache | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/hetznercloud/hcloud-go/v2/hcloud" | ||
| "github.com/hetznercloud/hcloud-go/v2/hcloud/exp/mockutil" | ||
| ) | ||
|
|
||
| func TestNewLBTypeCache(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| mode Mode | ||
| requests []mockutil.Request | ||
| }{ | ||
| { | ||
| mode: ModeAll, | ||
| requests: []mockutil.Request{ | ||
| {Method: "GET", Path: "/load_balancer_types?page=1&per_page=50", Status: 200, JSONRaw: `{ "load_balancer_types": [{ "id": 1, "name": "lb11" }]}`}, | ||
| }, | ||
| }, | ||
| { | ||
| mode: ModeOne, | ||
| requests: []mockutil.Request{ | ||
| {Method: "GET", Path: "/load_balancer_types/1", Status: 200, JSONRaw: `{ "load_balancer_type": { "id": 1, "name": "lb11" }}`}, | ||
| }, | ||
| }, | ||
| { | ||
| mode: ModeOff, | ||
| requests: []mockutil.Request{ | ||
| {Method: "GET", Path: "/load_balancer_types/1", Status: 200, JSONRaw: `{ "load_balancer_type": { "id": 1, "name": "lb11" }}`}, | ||
| {Method: "GET", Path: "/load_balancer_types?name=lb11", Status: 200, JSONRaw: `{ "load_balancer_types": [{ "id": 1, "name": "lb11" }]}`}, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range testCases { | ||
| t.Run(string(tt.mode), func(t *testing.T) { | ||
| server := mockutil.NewServer(t, tt.requests) | ||
| client := hcloud.NewClient(hcloud.WithEndpoint(server.Server.URL)) | ||
|
|
||
| cache := NewLoadBalancerTypeCache(client, tt.mode, 10*time.Second) | ||
| require.NotNil(t, cache) | ||
| require.NotNil(t, cache.fetchOneByID) | ||
| require.NotNil(t, cache.fetchOneByName) | ||
| require.NotNil(t, cache.fetchAll) | ||
| require.NotNil(t, cache.getID) | ||
| require.NotNil(t, cache.getName) | ||
|
|
||
| ctx := t.Context() | ||
|
|
||
| srv, err := cache.ByID(ctx, int64(1)) | ||
| require.NoError(t, err) | ||
| assert.NotNil(t, srv) | ||
|
|
||
| srv, err = cache.ByName(ctx, "lb11") | ||
| require.NoError(t, err) | ||
| assert.NotNil(t, srv) | ||
| }) | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.