Skip to content

Commit

Permalink
Refactor loadbalancer config package.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 committed Oct 24, 2024
1 parent 6807b30 commit 8a3b151
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 224 deletions.
25 changes: 6 additions & 19 deletions metal/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"github.com/metal-stack/metal-ccm/pkg/controllers/housekeeping"
"github.com/metal-stack/metal-ccm/pkg/controllers/instances"
"github.com/metal-stack/metal-ccm/pkg/controllers/loadbalancer"
"github.com/metal-stack/metal-ccm/pkg/controllers/loadbalancer/cilium"
"github.com/metal-stack/metal-ccm/pkg/controllers/loadbalancer/metallb"
"github.com/metal-stack/metal-ccm/pkg/controllers/zones"
"github.com/metal-stack/metal-ccm/pkg/resources/constants"
"github.com/metal-stack/metal-ccm/pkg/resources/metal"
Expand Down Expand Up @@ -49,6 +47,7 @@ func NewCloud(_ io.Reader) (cloudprovider.Interface, error) {
partitionID := os.Getenv(constants.MetalPartitionIDEnvVar)
clusterID := os.Getenv(constants.MetalClusterIDEnvVar)
defaultExternalNetworkID := os.Getenv(constants.MetalDefaultExternalNetworkEnvVar)
loadbalancerType := os.Getenv(constants.Loadbalancer)

var (
additionalNetworksString = os.Getenv(constants.MetalAdditionalNetworks)
Expand Down Expand Up @@ -81,6 +80,10 @@ func NewCloud(_ io.Reader) (cloudprovider.Interface, error) {
return nil, fmt.Errorf("environment variable %q or %q is required", constants.MetalAuthTokenEnvVar, constants.MetalAuthHMACEnvVar)
}

if !slices.Contains([]string{"cilium", "metallb", ""}, loadbalancerType) {
klog.Fatalf("only cilium or metallb load balancer types are supported")
}

var err error
metalclient, err = metalgo.NewDriver(url, token, hmac)
if err != nil {
Expand All @@ -97,7 +100,7 @@ func NewCloud(_ io.Reader) (cloudprovider.Interface, error) {

instancesController := instances.New(defaultExternalNetworkID)
zonesController := zones.New()
loadBalancerController := loadbalancer.New(partitionID, projectID, clusterID, defaultExternalNetworkID, additionalNetworks)
loadBalancerController := loadbalancer.New(partitionID, projectID, clusterID, defaultExternalNetworkID, additionalNetworks, loadbalancerType)

klog.Info("initialized cloud controller manager")
return &cloud{
Expand All @@ -116,11 +119,6 @@ func (c *cloud) Initialize(clientBuilder cloudprovider.ControllerClientBuilder,
projectID := os.Getenv(constants.MetalProjectIDEnvVar)
sshPublicKey := os.Getenv(constants.MetalSSHPublicKey)
clusterID := os.Getenv(constants.MetalClusterIDEnvVar)
loadbalancerType := os.Getenv(constants.Loadbalancer)

if !slices.Contains([]string{"cilium", "metallb", ""}, loadbalancerType) {
klog.Fatalf("only cilium or metallb load balancer types are supported")
}

k8sClientSet := clientBuilder.ClientOrDie("cloud-controller-manager")
k8sRestConfig, err := clientBuilder.Config("cloud-controller-manager")
Expand All @@ -145,24 +143,13 @@ func (c *cloud) Initialize(clientBuilder cloudprovider.ControllerClientBuilder,
klog.Fatalf("unable to create k8s client: %v", err)
}

var config loadbalancer.LoadBalancerConfig
switch loadbalancerType {
case "metallb":
config = metallb.NewMetalLBConfig()
case "cilium":
config = cilium.NewCiliumConfig(k8sClientSet)
default:
config = metallb.NewMetalLBConfig()
}

housekeeper := housekeeping.New(metalclient, stop, c.loadBalancer, k8sClientSet, projectID, sshPublicKey, clusterID)
ms := metal.New(metalclient, k8sClientSet, projectID)

c.instances.MetalService = ms
c.loadBalancer.K8sClientSet = k8sClientSet
c.loadBalancer.K8sClient = k8sClient
c.loadBalancer.MetalService = ms
c.loadBalancer.Config = config
c.zones.MetalService = ms

go housekeeper.Run()
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/housekeeping/housekeeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ func (h *Housekeeper) watchNodes() {
oldNode := oldObj.(*v1.Node)
newNode := newObj.(*v1.Node)

oldTunnelAddress, _ := loadbalancer.NodeAddress(*oldNode)
newTunnelAddress, err := loadbalancer.NodeAddress(*newNode)
oldTunnelAddress, _ := kubernetes.NodeAddress(*oldNode)
newTunnelAddress, err := kubernetes.NodeAddress(*newNode)
if err != nil {
klog.Error("newNode does not have a tunnelAddress, ignoring")
return
Expand Down
122 changes: 0 additions & 122 deletions pkg/controllers/loadbalancer/config.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package loadbalancer
package config

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package cilium
package config

import (
"context"
"fmt"
"time"

"github.com/metal-stack/metal-ccm/pkg/controllers/loadbalancer"
"github.com/metal-stack/metal-ccm/pkg/resources/kubernetes"
"github.com/metal-stack/metal-lib/pkg/pointer"

Expand All @@ -22,12 +21,12 @@ import (
)

type ciliumConfig struct {
loadbalancer.Config
cfg *baseConfig
k8sClient clientset.Interface
}

func NewCiliumConfig(k8sClient clientset.Interface) *ciliumConfig {
return &ciliumConfig{k8sClient: k8sClient}
func newCiliumConfig(cfg *baseConfig, k8sClient clientset.Interface) *ciliumConfig {
return &ciliumConfig{cfg: cfg, k8sClient: k8sClient}
}

func (cfg *ciliumConfig) WriteCRs(ctx context.Context, c client.Client) error {
Expand Down Expand Up @@ -58,7 +57,7 @@ func (cfg *ciliumConfig) writeCiliumBGPPeeringPolicies(ctx context.Context, c cl
for _, existingPolicy := range existingPolicies.Items {
existingPolicy := existingPolicy
found := false
for _, peer := range cfg.Peers {
for _, peer := range cfg.cfg.Peers {
if fmt.Sprintf("%d", peer.ASN) == existingPolicy.Name {
found = true
break
Expand All @@ -72,7 +71,7 @@ func (cfg *ciliumConfig) writeCiliumBGPPeeringPolicies(ctx context.Context, c cl
}
}

for _, peer := range cfg.Peers {
for _, peer := range cfg.cfg.Peers {
bgpPeeringPolicy := &ciliumv2alpha1.CiliumBGPPeeringPolicy{
TypeMeta: metav1.TypeMeta{
APIVersion: ciliumv2alpha1.CustomResourceDefinitionGroup + "/" + ciliumv2alpha1.CustomResourceDefinitionVersion,
Expand Down Expand Up @@ -131,7 +130,7 @@ func (cfg *ciliumConfig) writeCiliumLoadBalancerIPPools(ctx context.Context, c c
for _, existingPool := range existingPools.Items {
existingPool := existingPool
found := false
for _, pool := range cfg.AddressPools {
for _, pool := range cfg.cfg.AddressPools {
if pool.Name == existingPool.Name {
found = true
break
Expand All @@ -145,7 +144,7 @@ func (cfg *ciliumConfig) writeCiliumLoadBalancerIPPools(ctx context.Context, c c
}
}

for _, pool := range cfg.AddressPools {
for _, pool := range cfg.cfg.AddressPools {
ipPool := &ciliumv2alpha1.CiliumLoadBalancerIPPool{
TypeMeta: metav1.TypeMeta{
APIVersion: ciliumv2alpha1.CustomResourceDefinitionGroup + "/" + ciliumv2alpha1.CustomResourceDefinitionVersion,
Expand Down Expand Up @@ -190,7 +189,7 @@ func (cfg *ciliumConfig) writeNodeAnnotations(ctx context.Context) error {
Jitter: 1.0,
}
for _, n := range nodes {
asn, err := cfg.GetASNFromNodeLabels(n)
asn, err := getASNFromNodeLabels(n)
if err != nil {
return fmt.Errorf("failed to write node annotations for node %s: %w", n.Name, err)
}
Expand Down
Loading

0 comments on commit 8a3b151

Please sign in to comment.