-
Notifications
You must be signed in to change notification settings - Fork 4
refactor(loadbalancer): target pool ownership #28
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
Open
Herbaert
wants to merge
4
commits into
main
Choose a base branch
from
refactor/loadbalancer-target-pool-ownership
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e68ffbc
refactor: move load balancer target pool management to the cluster co…
Herbaert 32f109e
test: cover target pool ownership in the cluster controller
Herbaert 20ef755
docs: move load balancer targets to the cluster reconciler responsibi…
Herbaert 316cb4d
fix: keep the target pool intact when no control plane machine is active
Herbaert 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
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 |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ import ( | |
| "fmt" | ||
| "net/http" | ||
| "os" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "github.com/stackitcloud/stackit-sdk-go/core/config" | ||
|
|
@@ -568,11 +569,31 @@ func (c *SDKClient) ListAPIServerLoadBalancersByTags( | |
| return matched, nil | ||
| } | ||
|
|
||
| func (c *SDKClient) EnsureAPIServerLoadBalancerTarget(ctx context.Context, input LoadBalancerTargetInput) error { | ||
| if input.LoadBalancerID == "" || input.Name == "" || input.IP == "" { | ||
| return fmt.Errorf("%w: load balancer ID, target name, and target IP are required", ErrInvalidInput) | ||
| func (c *SDKClient) SetAPIServerLoadBalancerTargets( | ||
| ctx context.Context, | ||
| loadBalancerID string, | ||
| port int32, | ||
| targets []LoadBalancerTargetInput, | ||
| ) error { | ||
| if loadBalancerID == "" || port <= 0 { | ||
| return fmt.Errorf("%w: load balancer ID and target port are required", ErrInvalidInput) | ||
| } | ||
| // STACKIT NLB target pools must contain at least one target. | ||
| if len(targets) == 0 { | ||
| return fmt.Errorf("%w: at least one target is required", ErrInvalidInput) | ||
| } | ||
| desired := make([]lb.Target, 0, len(targets)) | ||
| for _, targetInput := range targets { | ||
| if targetInput.Name == "" || targetInput.IP == "" { | ||
| return fmt.Errorf("%w: target name and target IP are required", ErrInvalidInput) | ||
| } | ||
| target := lb.NewTarget() | ||
| target.SetDisplayName(targetInput.Name) | ||
| target.SetIp(targetInput.IP) | ||
| desired = append(desired, *target) | ||
| } | ||
| loadBalancer, err := c.lbClient.DefaultAPI.GetLoadBalancer(ctx, c.projectID, c.region, input.LoadBalancerID).Execute() | ||
|
|
||
| loadBalancer, err := c.lbClient.DefaultAPI.GetLoadBalancer(ctx, c.projectID, c.region, loadBalancerID).Execute() | ||
| if err != nil { | ||
| return classifySDKError("get load balancer", err) | ||
| } | ||
|
|
@@ -581,68 +602,32 @@ func (c *SDKClient) EnsureAPIServerLoadBalancerTarget(ctx context.Context, input | |
| return fmt.Errorf( | ||
| "%w: load balancer %q has no %q target pool", | ||
| ErrNotFound, | ||
| input.LoadBalancerID, | ||
| loadBalancerID, | ||
| apiserverTargetPoolName, | ||
| ) | ||
| } | ||
|
|
||
| targets := withoutBootstrapTarget(targetPool.GetTargets()) | ||
| for i := range targets { | ||
| if targets[i].GetDisplayName() == input.Name || targets[i].GetIp() == input.IP { | ||
| targets[i].SetDisplayName(input.Name) | ||
| targets[i].SetIp(input.IP) | ||
| return c.updateAPIServerTargetPool(ctx, input.LoadBalancerID, targetPool, targets, input.Port) | ||
| } | ||
| // Control plane status updates are frequent, and each one reaches this path. | ||
| if targetPool.GetTargetPort() == port && sameTargets(targetPool.GetTargets(), desired) { | ||
| return nil | ||
| } | ||
|
|
||
| target := lb.NewTarget() | ||
| target.SetDisplayName(input.Name) | ||
| target.SetIp(input.IP) | ||
| targets = append(targets, *target) | ||
| return c.updateAPIServerTargetPool(ctx, input.LoadBalancerID, targetPool, targets, input.Port) | ||
| return c.updateAPIServerTargetPool(ctx, loadBalancerID, targetPool, desired, port) | ||
| } | ||
|
|
||
| func withoutBootstrapTarget(targets []lb.Target) []lb.Target { | ||
| out := targets[:0] | ||
| for _, target := range targets { | ||
| if target.GetDisplayName() == bootstrapTargetName { | ||
| continue | ||
| } | ||
| out = append(out, target) | ||
| func sameTargets(current, desired []lb.Target) bool { | ||
| if len(current) != len(desired) { | ||
| return false | ||
| } | ||
| return out | ||
| return slices.Equal(sortedTargetKeys(current), sortedTargetKeys(desired)) | ||
| } | ||
|
|
||
| func (c *SDKClient) DeleteAPIServerLoadBalancerTarget(ctx context.Context, input LoadBalancerTargetInput) error { | ||
| if input.LoadBalancerID == "" || input.Name == "" { | ||
| return fmt.Errorf("%w: load balancer ID and target name are required", ErrInvalidInput) | ||
| } | ||
| loadBalancer, err := c.lbClient.DefaultAPI.GetLoadBalancer(ctx, c.projectID, c.region, input.LoadBalancerID).Execute() | ||
| if err != nil { | ||
| return classifySDKError("get load balancer", err) | ||
| } | ||
| targetPool := apiServerTargetPool(loadBalancer) | ||
| if targetPool == nil { | ||
| return nil | ||
| } | ||
|
|
||
| targets := targetPool.GetTargets() | ||
| out := make([]lb.Target, 0, len(targets)) | ||
| func sortedTargetKeys(targets []lb.Target) []string { | ||
| keys := make([]string, 0, len(targets)) | ||
| for _, target := range targets { | ||
| if target.GetDisplayName() == input.Name { | ||
| continue | ||
| } | ||
| out = append(out, target) | ||
| } | ||
| if len(out) == len(targets) { | ||
| return nil | ||
| } | ||
| if len(out) == 0 { | ||
| // STACKIT NLB target pools must contain at least one target. Leave the | ||
| // last target in place; deleting the load balancer removes it. | ||
| return nil | ||
| keys = append(keys, target.GetDisplayName()+"\x00"+target.GetIp()) | ||
|
Collaborator
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. So whats the "magic" nullbyte for? This requires a comment or documentation link or something |
||
| } | ||
| return c.updateAPIServerTargetPool(ctx, input.LoadBalancerID, targetPool, out, input.Port) | ||
| slices.Sort(keys) | ||
| return keys | ||
| } | ||
|
|
||
| func (c *SDKClient) findLoadBalancerByTags(ctx context.Context, tags map[string]string) (*LoadBalancer, error) { | ||
|
|
||
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.
what is this comment supposed to tell me? so yes this happens frequently but why do i need to know this?