Skip to content

Commit

Permalink
refactor: replace LoadBalancerDefaults with cfg
Browse files Browse the repository at this point in the history
The struct `LoadBalancerDefaults` was a subset of
`config.LoadBalancerConfiguration`.
  • Loading branch information
apricote committed Oct 26, 2023
1 parent ff27dd3 commit 9858b50
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 30 deletions.
6 changes: 1 addition & 5 deletions hcloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,7 @@ func (c *cloud) LoadBalancer() (cloudprovider.LoadBalancer, bool) {
ActionClient: &c.client.Action,
NetworkClient: &c.client.Network,
NetworkID: c.networkID,
Defaults: hcops.LoadBalancerDefaults{
Location: c.cfg.LoadBalancer.Location,
NetworkZone: c.cfg.LoadBalancer.NetworkZone,
UsePrivateIP: c.cfg.LoadBalancer.UsePrivateIP,
},
Cfg: c.cfg.LoadBalancer,
}

return newLoadBalancers(lbOps, c.cfg.LoadBalancer.DisablePrivateIngress, c.cfg.LoadBalancer.DisableIPv6), true
Expand Down
18 changes: 6 additions & 12 deletions internal/hcops/load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"k8s.io/klog/v2"

"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/annotation"
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/config"
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/metrics"
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/providerid"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
Expand Down Expand Up @@ -70,14 +71,7 @@ type LoadBalancerOps struct {
CertOps *CertificateOps
RetryDelay time.Duration
NetworkID int64
Defaults LoadBalancerDefaults
}

// LoadBalancerDefaults stores cluster-wide default values for load balancers.
type LoadBalancerDefaults struct {
Location string
NetworkZone string
UsePrivateIP bool
Cfg config.LoadBalancerConfiguration
}

// GetByK8SServiceUID tries to find a Load Balancer by its Kubernetes service
Expand Down Expand Up @@ -170,8 +164,8 @@ func (l *LoadBalancerOps) Create(
if v, ok := annotation.LBType.StringFromService(svc); ok {
opts.LoadBalancerType.Name = v
}
if l.Defaults.Location != "" {
opts.Location = &hcloud.Location{Name: l.Defaults.Location}
if l.Cfg.Location != "" {
opts.Location = &hcloud.Location{Name: l.Cfg.Location}
}
if v, ok := annotation.LBLocation.StringFromService(svc); ok {
if v == "" {
Expand All @@ -182,7 +176,7 @@ func (l *LoadBalancerOps) Create(
opts.Location = &hcloud.Location{Name: v}
}
}
opts.NetworkZone = hcloud.NetworkZone(l.Defaults.NetworkZone)
opts.NetworkZone = hcloud.NetworkZone(l.Cfg.NetworkZone)
if v, ok := annotation.LBNetworkZone.StringFromService(svc); ok {
opts.NetworkZone = hcloud.NetworkZone(v)
}
Expand Down Expand Up @@ -664,7 +658,7 @@ func (l *LoadBalancerOps) getUsePrivateIP(svc *corev1.Service) (bool, error) {
usePrivateIP, err := annotation.LBUsePrivateIP.BoolFromService(svc)
if err != nil {
if errors.Is(err, annotation.ErrNotSet) {
return l.Defaults.UsePrivateIP, nil
return l.Cfg.UsePrivateIP, nil
}
return false, err
}
Expand Down
27 changes: 14 additions & 13 deletions internal/hcops/load_balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"k8s.io/apimachinery/pkg/types"

"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/annotation"
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/config"
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/hcops"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)
Expand Down Expand Up @@ -243,7 +244,7 @@ func TestGetByK8SServiceUID(t *testing.T) {
func TestLoadBalancerOps_Create(t *testing.T) {
type testCase struct {
name string
defaults hcops.LoadBalancerDefaults
cfg config.LoadBalancerConfiguration
serviceAnnotations map[annotation.Name]interface{}
createOpts hcloud.LoadBalancerCreateOpts
mock func(t *testing.T, tt *testCase, fx *hcops.LoadBalancerOpsFixture)
Expand All @@ -253,7 +254,7 @@ func TestLoadBalancerOps_Create(t *testing.T) {
tests := []testCase{
{
name: "create with with location name (and default set)",
defaults: hcops.LoadBalancerDefaults{
cfg: config.LoadBalancerConfiguration{
Location: "hel1",
},
serviceAnnotations: map[annotation.Name]interface{}{
Expand All @@ -273,7 +274,7 @@ func TestLoadBalancerOps_Create(t *testing.T) {
},
{
name: "create with network zone name only (and default set)",
defaults: hcops.LoadBalancerDefaults{
cfg: config.LoadBalancerConfiguration{
NetworkZone: "eu-central",
},
serviceAnnotations: map[annotation.Name]interface{}{
Expand All @@ -291,7 +292,7 @@ func TestLoadBalancerOps_Create(t *testing.T) {
},
{
name: "create with location as default only",
defaults: hcops.LoadBalancerDefaults{
cfg: config.LoadBalancerConfiguration{
Location: "fsn1",
},
createOpts: hcloud.LoadBalancerCreateOpts{
Expand All @@ -308,7 +309,7 @@ func TestLoadBalancerOps_Create(t *testing.T) {
},
{
name: "create with network zone as default only",
defaults: hcops.LoadBalancerDefaults{
cfg: config.LoadBalancerConfiguration{
NetworkZone: "eu-central",
},
createOpts: hcloud.LoadBalancerCreateOpts{
Expand All @@ -323,7 +324,7 @@ func TestLoadBalancerOps_Create(t *testing.T) {
},
{
name: "create with network zone and reset default location",
defaults: hcops.LoadBalancerDefaults{
cfg: config.LoadBalancerConfiguration{
Location: "hel1",
},
serviceAnnotations: map[annotation.Name]interface{}{
Expand All @@ -342,7 +343,7 @@ func TestLoadBalancerOps_Create(t *testing.T) {
},
{
name: "create with location and reset default network zone",
defaults: hcops.LoadBalancerDefaults{
cfg: config.LoadBalancerConfiguration{
NetworkZone: "eu-central",
},
serviceAnnotations: map[annotation.Name]interface{}{
Expand Down Expand Up @@ -511,7 +512,7 @@ func TestLoadBalancerOps_Create(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
fx := hcops.NewLoadBalancerOpsFixture(t)

fx.LBOps.Defaults = tt.defaults
fx.LBOps.Cfg = tt.cfg

if tt.mock == nil {
tt.mock = func(t *testing.T, tt *testCase, fx *hcops.LoadBalancerOpsFixture) {
Expand Down Expand Up @@ -589,7 +590,7 @@ func TestLoadBalancerOps_Delete(t *testing.T) {

type LBReconcilementTestCase struct {
name string
defaults hcops.LoadBalancerDefaults
cfg config.LoadBalancerConfiguration
serviceUID string
serviceAnnotations map[annotation.Name]interface{}
servicePorts []corev1.ServicePort
Expand All @@ -607,7 +608,7 @@ func (tt *LBReconcilementTestCase) run(t *testing.T) {
t.Helper()

tt.fx = hcops.NewLoadBalancerOpsFixture(t)
tt.fx.LBOps.Defaults = tt.defaults
tt.fx.LBOps.Cfg = tt.cfg

if tt.service == nil {
tt.service = &corev1.Service{
Expand Down Expand Up @@ -1188,7 +1189,7 @@ func TestLoadBalancerOps_ReconcileHCLBTargets(t *testing.T) {
},
{
name: "enable use of private network via default",
defaults: hcops.LoadBalancerDefaults{
cfg: config.LoadBalancerConfiguration{
// Make sure the annotation overrides the default
UsePrivateIP: true,
},
Expand Down Expand Up @@ -1218,7 +1219,7 @@ func TestLoadBalancerOps_ReconcileHCLBTargets(t *testing.T) {
},
{
name: "enable use of private network via annotation",
defaults: hcops.LoadBalancerDefaults{
cfg: config.LoadBalancerConfiguration{
// Make sure the annotation overrides the default
UsePrivateIP: false,
},
Expand Down Expand Up @@ -1251,7 +1252,7 @@ func TestLoadBalancerOps_ReconcileHCLBTargets(t *testing.T) {
},
{
name: "disable use of private network via annotation",
defaults: hcops.LoadBalancerDefaults{
cfg: config.LoadBalancerConfiguration{
// Make sure the annotation overrides the default
UsePrivateIP: true,
},
Expand Down

0 comments on commit 9858b50

Please sign in to comment.