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

feat: add ability to use node ip as LB target #590

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions hcloud/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (i *instances) lookupServer(
return nil, errMissingRobotClient
}
server, err := getRobotServerByID(i.robotClient, int(serverID), node)

if err != nil {
return nil, fmt.Errorf("failed to get robot server \"%d\": %w", serverID, err)
}
Expand Down
2 changes: 2 additions & 0 deletions hcloud/instances_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package hcloud
import (
"context"
"fmt"
"k8s.io/klog/v2"
"strings"

hrobotmodels "github.com/syself/hrobot-go/models"
Expand Down Expand Up @@ -91,6 +92,7 @@ func getRobotServerByID(c robot.Client, id int, node *corev1.Node) (*hrobotmodel

// check whether name matches - otherwise this server does not belong to the respective node anymore
if server.Name != node.Name {
klog.Warningf("%s: server %d has name %q, but node %q has name %q. if you want node to be matched with node in Hetzner Robot you should rename it.", op, id, server.Name, node.Name, node.Name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems unrelated to this PR, but might be a good addition.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want this to be a separate PR or just a separate commit?

return nil, nil
}

Expand Down
6 changes: 6 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
hcloudLoadBalancersNetworkZone = "HCLOUD_LOAD_BALANCERS_NETWORK_ZONE"
hcloudLoadBalancersDisablePrivateIngress = "HCLOUD_LOAD_BALANCERS_DISABLE_PRIVATE_INGRESS"
hcloudLoadBalancersUsePrivateIP = "HCLOUD_LOAD_BALANCERS_USE_PRIVATE_IP"
hcloudLoadBalancersUseNodeIP = "HCLOUD_LOAD_BALANCERS_USE_NODE_IP"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about the naming or the option, this seems to target robot server only right?

Copy link
Author

@blitss blitss Jun 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't recall but I think it does yes. But we can make it work with cloud servers too, is this the way?

Update: there's an option which uses private IP and I believe it is used in conjunction with cloud servers. Do we need this option for the cloud servers in that case?

hcloudLoadBalancersDisableIPv6 = "HCLOUD_LOAD_BALANCERS_DISABLE_IPV6"

hcloudMetricsEnabled = "HCLOUD_METRICS_ENABLED"
Expand Down Expand Up @@ -74,6 +75,7 @@ type LoadBalancerConfiguration struct {
NetworkZone string
DisablePrivateIngress bool
UsePrivateIP bool
UseNodeIP bool
DisableIPv6 bool
}

Expand Down Expand Up @@ -157,6 +159,10 @@ func Read() (HCCMConfiguration, error) {
if err != nil {
errs = append(errs, err)
}
cfg.LoadBalancer.UseNodeIP, err = getEnvBool(hcloudLoadBalancersUseNodeIP, false)
if err != nil {
errs = append(errs, err)
}
cfg.LoadBalancer.DisableIPv6, err = getEnvBool(hcloudLoadBalancersDisableIPv6, false)
if err != nil {
errs = append(errs, err)
Expand Down
27 changes: 23 additions & 4 deletions internal/hcops/load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,9 +570,11 @@ func (l *LoadBalancerOps) ReconcileHCLBTargets(
// Set of all K8S server IDs currently assigned as nodes to this
// cluster.
k8sNodeIDsHCloud = make(map[int64]bool)
k8sNodeIDsRobot = make(map[int]bool)
k8sNodeIDsRobot = make(map[int]*corev1.Node)
k8sNodeNames = make(map[int64]string)

internalRobotIPsToNode = make(map[string]*corev1.Node)

robotIPsToIDs = make(map[string]int)
robotIDToIPv4 = make(map[int]string)
// Set of server IDs assigned as targets to the HC Load Balancer. Some
Expand Down Expand Up @@ -607,7 +609,10 @@ func (l *LoadBalancerOps) ReconcileHCLBTargets(
if isCloudServer {
k8sNodeIDsHCloud[id] = true
} else {
k8sNodeIDsRobot[int(id)] = true
if l.Cfg.LoadBalancer.UseNodeIP {
internalRobotIPsToNode[node.Status.Addresses[0].Address] = node
}
k8sNodeIDsRobot[int(id)] = node
}
k8sNodeNames[id] = node.Name
}
Expand All @@ -634,6 +639,8 @@ func (l *LoadBalancerOps) ReconcileHCLBTargets(
// not assigned as nodes to the K8S Load Balancer.
for _, target := range lb.Targets {
if target.Type == hcloud.LoadBalancerTargetTypeServer {
// We have to check if corresponding node exists at our cluster, if it does, we don't need to delete
// the target
id := target.Server.Server.ID
recreate := target.UsePrivateIP != usePrivateIP
hclbTargetIDs[id] = k8sNodeIDsHCloud[id] && !recreate
Expand All @@ -660,7 +667,15 @@ func (l *LoadBalancerOps) ReconcileHCLBTargets(
if target.Type == hcloud.LoadBalancerTargetTypeIP {
ip := target.IP.IP
id, foundServer := robotIPsToIDs[ip]
hclbTargetIPs[ip] = foundServer && k8sNodeIDsRobot[id]
_, exists := k8sNodeIDsRobot[id]
hclbTargetIPs[ip] = foundServer && exists

if l.Cfg.LoadBalancer.UseNodeIP {
_, exists = internalRobotIPsToNode[ip]

hclbTargetIPs[ip] = exists
}

if hclbTargetIPs[ip] {
continue
}
Expand Down Expand Up @@ -728,9 +743,13 @@ func (l *LoadBalancerOps) ReconcileHCLBTargets(
if l.Cfg.Robot.Enabled {
// Assign the dedicated servers which are currently assigned as nodes
// to the K8S Load Balancer as IP targets to the HC Load Balancer.
for id := range k8sNodeIDsRobot {
for id, node := range k8sNodeIDsRobot {
ip := robotIDToIPv4[id]

if l.Cfg.LoadBalancer.UseNodeIP {
ip = node.Status.Addresses[0].Address
}

// Don't assign the node again if it is already assigned to the HC load
// balancer.
if hclbTargetIPs[ip] {
Expand Down