Skip to content

Commit

Permalink
use additionalNetworks passed from gepm instead of querying them (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 authored Nov 19, 2021
1 parent e753a20 commit 46dc26a
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 180 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/latest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Lint
uses: golangci/golangci-lint-action@v2
with:
args: -p bugs -p unused --timeout=3m
args: -p bugs -p unused --timeout=10m

- name: Build the Docker images
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pull_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: Lint
uses: golangci/golangci-lint-action@v2
with:
args: -p bugs -p unused --timeout=3m
args: -p bugs -p unused --timeout=10m

- name: Build
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Lint
uses: golangci/golangci-lint-action@v2
with:
args: -p bugs -p unused --timeout=3m
args: -p bugs -p unused --timeout=10m

- name: Build the Docker images
run: |
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
k8s.io/cloud-provider v0.22.2
k8s.io/component-base v0.22.2
k8s.io/klog/v2 v2.10.0
k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a
k8s.io/utils v0.0.0-20210930125809-cb0fa318a74b
sigs.k8s.io/yaml v1.3.0
)

Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1609,8 +1609,9 @@ k8s.io/kube-openapi v0.0.0-20210421082810-95288971da7e h1:KLHHjkdQFomZy8+06csTWZ
k8s.io/kube-openapi v0.0.0-20210421082810-95288971da7e/go.mod h1:vHXdDvt9+2spS2Rx9ql3I8tycm3H9FDfdUoIuKCefvw=
k8s.io/kubernetes v1.13.0/go.mod h1:ocZa8+6APFNC2tX1DZASIbocyYT5jHzqFVsY5aoB7Jk=
k8s.io/utils v0.0.0-20201110183641-67b214c5f920/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a h1:8dYfu/Fc9Gz2rNJKB9IQRGgQOh2clmRzNIPPY1xLY5g=
k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
k8s.io/utils v0.0.0-20210930125809-cb0fa318a74b h1:wxEMGetGMur3J1xuGLQY7GEQYg9bZxKn3tKo5k/eYcs=
k8s.io/utils v0.0.0-20210930125809-cb0fa318a74b/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
Expand Down
14 changes: 13 additions & 1 deletion metal/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"strings"

metalgo "github.com/metal-stack/metal-go"
"github.com/metal-stack/metal-lib/rest"
Expand Down Expand Up @@ -40,6 +41,17 @@ func NewCloud(_ io.Reader) (cloudprovider.Interface, error) {
clusterID := os.Getenv(constants.MetalClusterIDEnvVar)
defaultExternalNetworkID := os.Getenv(constants.MetalDefaultExternalNetworkEnvVar)

var (
additionalNetworksString = os.Getenv(constants.MetalAdditionalNetworks)
additionalNetworks []string
)
for _, n := range strings.Split(additionalNetworksString, ",") {
n := strings.TrimSpace(n)
if n != "" {
additionalNetworks = append(additionalNetworks, n)
}
}

if projectID == "" {
return nil, fmt.Errorf("environment variable %q is required", constants.MetalProjectIDEnvVar)
}
Expand Down Expand Up @@ -80,7 +92,7 @@ func NewCloud(_ io.Reader) (cloudprovider.Interface, error) {

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

logger.Println("initialized cloud controller manager")
return &cloud{
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/loadbalancer/addresspool.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewBGPAddressPool(name string, autoAssign bool) *AddressPool {
}
}

func (pool *AddressPool) ContainsCIDR(cidr string) bool {
func (pool *AddressPool) containsCIDR(cidr string) bool {
for _, CIDR := range pool.CIDRs {
if cidr == CIDR {
return true
Expand All @@ -30,10 +30,10 @@ func (pool *AddressPool) ContainsCIDR(cidr string) bool {
return false
}

func (pool *AddressPool) AppendIP(ip string) {
func (pool *AddressPool) appendIP(ip string) {
cidr := ip + "/32"

if pool.ContainsCIDR(cidr) {
if pool.containsCIDR(cidr) {
return
}

Expand Down
12 changes: 5 additions & 7 deletions pkg/controllers/loadbalancer/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
metalgo "github.com/metal-stack/metal-go"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"

retrygo "github.com/avast/retry-go/v3"
clientset "k8s.io/client-go/kubernetes"
Expand All @@ -32,6 +33,7 @@ type LoadBalancerController struct {
projectID string
clusterID string
defaultExternalNetworkID string
additionalNetworks sets.String
logger *log.Logger
K8sClient clientset.Interface
configWriteMutex *sync.Mutex
Expand All @@ -40,7 +42,7 @@ type LoadBalancerController struct {
}

// New returns a new load balancer controller that satisfies the kubernetes cloud provider load balancer interface
func New(client *metalgo.Driver, partitionID, projectID, clusterID, defaultExternalNetworkID string) *LoadBalancerController {
func New(client *metalgo.Driver, partitionID, projectID, clusterID, defaultExternalNetworkID string, additionalNetworks []string) *LoadBalancerController {
logs.InitLogs()
logger := logs.NewLogger("metal-ccm loadbalancer | ")

Expand All @@ -51,6 +53,7 @@ func New(client *metalgo.Driver, partitionID, projectID, clusterID, defaultExter
projectID: projectID,
clusterID: clusterID,
defaultExternalNetworkID: defaultExternalNetworkID,
additionalNetworks: sets.NewString(additionalNetworks...),
configWriteMutex: &sync.Mutex{},
ipAllocateMutex: &sync.Mutex{},
ipUpdateMutex: &sync.Mutex{},
Expand Down Expand Up @@ -338,14 +341,9 @@ func (l *LoadBalancerController) updateLoadBalancerConfig(nodes []v1.Node) error
if err != nil {
return fmt.Errorf("could not find ips of this project's cluster: %w", err)
}
networks, err := metal.ListNetworks(l.client)
if err != nil {
return fmt.Errorf("could not list networks: %w", err)
}
networkMap := metal.NetworksByID(networks)

config := newMetalLBConfig(l.defaultExternalNetworkID)
err = config.CalculateConfig(ips, networkMap, nodes)
err = config.CalculateConfig(ips, l.additionalNetworks, nodes)
if err != nil {
return err
}
Expand Down
23 changes: 5 additions & 18 deletions pkg/controllers/loadbalancer/metallb.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/metal-stack/metal-lib/pkg/tag"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/component-base/logs"

Expand Down Expand Up @@ -43,7 +44,7 @@ func newMetalLBConfig(defaultNetworkID string) *MetalLBConfig {
}

// CalculateConfig computes the metallb config from given parameter input.
func (cfg *MetalLBConfig) CalculateConfig(ips []*models.V1IPResponse, nws map[string]*models.V1NetworkResponse, nodes []v1.Node) error {
func (cfg *MetalLBConfig) CalculateConfig(ips []*models.V1IPResponse, nws sets.String, nodes []v1.Node) error {
err := cfg.computeAddressPools(ips, nws)
if err != nil {
return err
Expand All @@ -55,25 +56,11 @@ func (cfg *MetalLBConfig) CalculateConfig(ips []*models.V1IPResponse, nws map[st
return nil
}

func (cfg *MetalLBConfig) computeAddressPools(ips []*models.V1IPResponse, nws map[string]*models.V1NetworkResponse) error {
func (cfg *MetalLBConfig) computeAddressPools(ips []*models.V1IPResponse, nws sets.String) error {
for _, ip := range ips {
nw, ok := nws[*ip.Networkid]
if !ok {
continue
}
if *nw.Underlay {
if !nws.Has(*ip.Networkid) {
continue
}
// we do not want IPs from networks where the parent networks are private
if nw.Parentnetworkid != "" && !nw.Shared {
parent, ok := nws[nw.Parentnetworkid]
if !ok {
continue
}
if *parent.Privatesuper {
continue
}
}
net := *ip.Networkid
cfg.addIPToPool(net, *ip)
}
Expand Down Expand Up @@ -143,7 +130,7 @@ func (cfg *MetalLBConfig) addIPToPool(network string, ip models.V1IPResponse) {
}
poolName := fmt.Sprintf("%s-%s", network, poolType)
pool := cfg.getOrCreateAddressPool(poolName, autoAssign)
pool.AppendIP(*ip.Ipaddress)
pool.appendIP(*ip.Ipaddress)
}

// ToYAML returns this config in YAML format.
Expand Down
130 changes: 8 additions & 122 deletions pkg/controllers/loadbalancer/metallb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,139 +10,25 @@ import (
"github.com/metal-stack/metal-lib/pkg/tag"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/utils/pointer"
"sigs.k8s.io/yaml"
)

var (
testNetworks = map[string]*models.V1NetworkResponse{
"internet": {
Destinationprefixes: []string{"0.0.0.0/0"},
ID: pointer.StringPtr("internet"),
Labels: map[string]string{
"network.metal-stack.io/default": "",
"network.metal-stack.io/default-external": "",
},
Nat: pointer.BoolPtr(true),
Parentnetworkid: "",
Partitionid: "",
Privatesuper: pointer.BoolPtr(false),
Projectid: "",
Shared: false,
Underlay: pointer.BoolPtr(false),
Vrf: 104009,
Vrfshared: false,
},
"tenant-super-network-partition-a": {
Destinationprefixes: []string{},
ID: pointer.StringPtr("tenant-super-network-partition-a"),
Labels: map[string]string{},
Nat: pointer.BoolPtr(false),
Parentnetworkid: "",
Partitionid: "",
Privatesuper: pointer.BoolPtr(true),
Projectid: "",
Shared: false,
Underlay: pointer.BoolPtr(false),
Vrf: 0,
Vrfshared: false,
},
"underlay-partition-a": {
Destinationprefixes: []string{},
ID: pointer.StringPtr("underlay-partition-a"),
Labels: map[string]string{},
Nat: pointer.BoolPtr(false),
Parentnetworkid: "",
Partitionid: "",
Privatesuper: pointer.BoolPtr(false),
Projectid: "",
Shared: false,
Underlay: pointer.BoolPtr(true),
Vrf: 0,
Vrfshared: false,
},
"this-cluster-private-network": {
Destinationprefixes: []string{"10.129.28.0/22"},
ID: pointer.StringPtr("this-cluster-private-network"),
Labels: map[string]string{},
Nat: pointer.BoolPtr(false),
Parentnetworkid: "tenant-super-network-partition-a",
Partitionid: "partition-a",
Privatesuper: pointer.BoolPtr(false),
Projectid: "project-a",
Shared: false,
Underlay: pointer.BoolPtr(false),
Vrf: 30,
Vrfshared: false,
},
"foreign-cluster-private-network": {
Destinationprefixes: []string{"10.128.244.0/22"},
ID: pointer.StringPtr("foreign-cluster-private-network"),
Labels: map[string]string{},
Nat: pointer.BoolPtr(false),
Parentnetworkid: "tenant-super-network-partition-a",
Partitionid: "partition-a",
Privatesuper: pointer.BoolPtr(false),
Projectid: "project-b",
Shared: false,
Underlay: pointer.BoolPtr(false),
Vrf: 40,
Vrfshared: false,
},

"shared-storage-network": {
Destinationprefixes: []string{""},
Prefixes: []string{"10.131.44.0/22"},
ID: pointer.StringPtr("shared-storage-network"),
Labels: map[string]string{},
Nat: pointer.BoolPtr(false),
Parentnetworkid: "tenant-super-network-partition-a",
Partitionid: "partition-a",
Privatesuper: pointer.BoolPtr(false),
Projectid: "project-c",
Shared: true,
Underlay: pointer.BoolPtr(false),
Vrf: 20,
Vrfshared: false,
},
"mpls-network": {
Destinationprefixes: []string{"100.127.0.0/16"},
Prefixes: []string{"100.127.130.0/23"},
ID: pointer.StringPtr("mpls-network"),
Labels: map[string]string{},
Nat: pointer.BoolPtr(true),
Parentnetworkid: "",
Partitionid: "",
Privatesuper: pointer.BoolPtr(false),
Projectid: "",
Shared: false,
Underlay: pointer.BoolPtr(false),
Vrf: 104010,
Vrfshared: false,
},
"dmz-network": {
Destinationprefixes: []string{"0.0.0.0/0"},
Prefixes: []string{"10.129.172.0/22"},
ID: pointer.StringPtr("dmz-network"),
Labels: map[string]string{},
Nat: pointer.BoolPtr(false),
Parentnetworkid: "tenant-super-network-partition-a",
Partitionid: "partition-a",
Privatesuper: pointer.BoolPtr(false),
Projectid: "project-a",
Shared: true,
Underlay: pointer.BoolPtr(false),
Vrf: 52,
Vrfshared: false,
},
}
testNetworks = sets.NewString(
"internet",
"shared-storage-network",
"mpls-network",
"dmz-network",
)
)

func TestMetalLBConfig_CalculateConfig(t *testing.T) {
tests := []struct {
name string
defaultNetworkID string
nws map[string]*models.V1NetworkResponse
nws sets.String
ips []*models.V1IPResponse
nodes []v1.Node
wantErr error
Expand Down
1 change: 1 addition & 0 deletions pkg/resources/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const (
MetalPartitionIDEnvVar = "METAL_PARTITION_ID"
MetalClusterIDEnvVar = "METAL_CLUSTER_ID"
MetalDefaultExternalNetworkEnvVar = "METAL_DEFAULT_EXTERNAL_NETWORK_ID"
MetalAdditionalNetworks = "METAL_ADDITIONAL_NETWORKS"

ProviderName = "metal"

Expand Down
24 changes: 0 additions & 24 deletions pkg/resources/metal/network.go

This file was deleted.

0 comments on commit 46dc26a

Please sign in to comment.