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

add HCLOUD_LOAD_BALANCERS_SERVICES_SELECTOR for selecting services #733

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
13 changes: 10 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
FROM alpine:3.20
FROM --platform=linux/amd64 golang:1.22.0-alpine AS builder

WORKDIR /builder
COPY ./ /builder

RUN go build -o hcloud-cloud-controller-manager main.go

FROM --platform=linux/amd64 alpine:3.20
RUN apk add --no-cache ca-certificates bash
COPY hcloud-cloud-controller-manager /bin/hcloud-cloud-controller-manager
ENTRYPOINT ["/bin/hcloud-cloud-controller-manager"]
COPY --from=builder /builder/hcloud-cloud-controller-manager /bin/

2 changes: 1 addition & 1 deletion hcloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (c *cloud) LoadBalancer() (cloudprovider.LoadBalancer, bool) {
Recorder: c.recorder,
}

return newLoadBalancers(lbOps, c.cfg.LoadBalancer.DisablePrivateIngress, c.cfg.LoadBalancer.DisableIPv6), true
return newLoadBalancers(lbOps, c.cfg.LoadBalancer.DisablePrivateIngress, c.cfg.LoadBalancer.DisableIPv6, c.cfg.LoadBalancer.ServiceSelector), true
}

func (c *cloud) Clusters() (cloudprovider.Clusters, bool) {
Expand Down
43 changes: 42 additions & 1 deletion hcloud/load_balancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,30 @@ type loadBalancers struct {
lbOps LoadBalancerOps
disablePrivateIngressDefault bool
disableIPv6Default bool
serviceSelector string
}

func newLoadBalancers(lbOps LoadBalancerOps, disablePrivateIngressDefault bool, disableIPv6Default bool) *loadBalancers {
func newLoadBalancers(lbOps LoadBalancerOps, disablePrivateIngressDefault bool, disableIPv6Default bool, serviceSelector string) *loadBalancers {
return &loadBalancers{
lbOps: lbOps,
disablePrivateIngressDefault: disablePrivateIngressDefault,
disableIPv6Default: disableIPv6Default,
serviceSelector: serviceSelector,
}
}

func matchServiceSelector(svc *corev1.Service, serviceSelector string) (bool, error) {
var (
err error
selector = labels.Everything()
)
selector, err = labels.Parse(serviceSelector)
if err != nil {
return false, fmt.Errorf("unable to parse the service-selector: %w", err)
}
return selector.Matches(labels.Set(svc.GetLabels())), nil
}

func matchNodeSelector(svc *corev1.Service, nodes []*corev1.Node) ([]*corev1.Node, error) {
var (
err error
Expand Down Expand Up @@ -114,6 +128,15 @@ func (l *loadBalancers) EnsureLoadBalancer(
selectedNodes []*corev1.Node
)

if matched, err := matchServiceSelector(svc, l.serviceSelector); err != nil {
return nil, fmt.Errorf("%s: %w", op, err)
} else if !matched {
klog.InfoS("service ignored by selector", "service", svc.Name, "selector", l.serviceSelector, "op", op)
return &svc.Status.LoadBalancer, nil
} else {
klog.InfoS("service accepted by selector", "service", svc.Name, "selector", l.serviceSelector, "op", op)
}

selectedNodes, err = matchNodeSelector(svc, nodes)
if err != nil {
return nil, fmt.Errorf("%s: %w", op, err)
Expand Down Expand Up @@ -265,6 +288,15 @@ func (l *loadBalancers) UpdateLoadBalancer(
const op = "hcloud/loadBalancers.UpdateLoadBalancer"
metrics.OperationCalled.WithLabelValues(op).Inc()

if matched, err := matchServiceSelector(svc, l.serviceSelector); err != nil {
return fmt.Errorf("%s: %w", op, err)
} else if !matched {
klog.InfoS("service ignored by selector", "service", svc.Name, "selector", l.serviceSelector, "op", op)
return nil
} else {
klog.InfoS("service accepted by selector", "service", svc.Name, "selector", l.serviceSelector, "op", op)
}

var (
lb *hcloud.LoadBalancer
err error
Expand Down Expand Up @@ -312,6 +344,15 @@ func (l *loadBalancers) EnsureLoadBalancerDeleted(ctx context.Context, _ string,
const op = "hcloud/loadBalancers.EnsureLoadBalancerDeleted"
metrics.OperationCalled.WithLabelValues(op).Inc()

if matched, err := matchServiceSelector(service, l.serviceSelector); err != nil {
return fmt.Errorf("%s: %w", op, err)
} else if !matched {
klog.InfoS("service ignored by selector", "service", service.Name, "selector", l.serviceSelector, "op", op)
return nil
} else {
klog.InfoS("service accepted by selector", "service", service.Name, "selector", l.serviceSelector, "op", op)
}

loadBalancer, err := l.lbOps.GetByK8SServiceUID(ctx, service)
if errors.Is(err, hcops.ErrNotFound) {
return nil
Expand Down
2 changes: 1 addition & 1 deletion hcloud/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (tt *LoadBalancerTestCase) run(t *testing.T) {
tt.Mock(t, tt)
}

tt.LoadBalancers = newLoadBalancers(tt.LBOps, tt.DisablePrivateIngressDefault, tt.DisableIPv6Default)
tt.LoadBalancers = newLoadBalancers(tt.LBOps, tt.DisablePrivateIngressDefault, tt.DisableIPv6Default, "")
tt.Perform(t, tt)

tt.LBOps.AssertExpectations(t)
Expand Down
4 changes: 4 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (

hcloudLoadBalancersEnabled = "HCLOUD_LOAD_BALANCERS_ENABLED"
hcloudLoadBalancersLocation = "HCLOUD_LOAD_BALANCERS_LOCATION"
hcloudLoadBalancersServicesSelector = "HCLOUD_LOAD_BALANCERS_SERVICES_SELECTOR"
hcloudLoadBalancersNetworkZone = "HCLOUD_LOAD_BALANCERS_NETWORK_ZONE"
hcloudLoadBalancersDisablePrivateIngress = "HCLOUD_LOAD_BALANCERS_DISABLE_PRIVATE_INGRESS"
hcloudLoadBalancersUsePrivateIP = "HCLOUD_LOAD_BALANCERS_USE_PRIVATE_IP"
Expand Down Expand Up @@ -74,6 +75,7 @@ type LoadBalancerConfiguration struct {
Enabled bool
Location string
NetworkZone string
ServiceSelector string
DisablePrivateIngress bool
UsePrivateIP bool
DisableIPv6 bool
Expand Down Expand Up @@ -160,6 +162,8 @@ func Read() (HCCMConfiguration, error) {
}
cfg.LoadBalancer.Location = os.Getenv(hcloudLoadBalancersLocation)
cfg.LoadBalancer.NetworkZone = os.Getenv(hcloudLoadBalancersNetworkZone)
cfg.LoadBalancer.ServiceSelector = os.Getenv(hcloudLoadBalancersServicesSelector)

cfg.LoadBalancer.DisablePrivateIngress, err = getEnvBool(hcloudLoadBalancersDisablePrivateIngress, false)
if err != nil {
errs = append(errs, err)
Expand Down