Skip to content

Commit

Permalink
don't match on region and drop from config
Browse files Browse the repository at this point in the history
  • Loading branch information
JorritSalverda committed Oct 5, 2020
1 parent 2334b81 commit b35ce5b
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 70 deletions.
5 changes: 0 additions & 5 deletions api/network/v1/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,28 @@
"range_configs": [
{
"type": "node",
"region": "europe-west1",
"ip_cidr_range_type": "primary",
"comment": "Every subnet has four reserved IP addresses in its primary IP range",
"network": "172.28.0.0/14",
"subnet_mask": 21
}, {
"type": "pod",
"region": "europe-west1",
"ip_cidr_range_type": "secondary",
"network": "10.0.0.0/9",
"comment": "Large enough to fit (total number of nodes X 256) IP addresses",
"subnet_mask": 16
}, {
"type": "service",
"region": "europe-west1",
"ip_cidr_range_type": "secondary",
"network": "172.24.0.0/14",
"subnet_mask": 22
}, {
"type": "master",
"region": "europe-west1",
"ip_cidr_range_type": "secondary",
"network": "192.168.0.0/18",
"subnet_mask": 28
}, {
"type": "other",
"region": "europe-west1",
"ip_cidr_range_type": "secondary",
"network": "192.168.64.0/18",
"subnet_mask": 22
Expand Down
2 changes: 0 additions & 2 deletions api/network/v1/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ func getValidConfig() Config {
RangeConfigs: []RangeConfig{
{
Type: TypeNode,
Region: "europe-west1",
RangeType: RangeTypePrimary,
NetworkCIDR: "172.28.0.0/14",
SubnetMask: 21,
},
{
Type: TypePod,
Region: "europe-west1",
RangeType: RangeTypeSecondary,
NetworkCIDR: "10.128.0.0/9",
SubnetMask: 14,
Expand Down
1 change: 0 additions & 1 deletion api/network/v1/range_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

type RangeConfig struct {
Type Type `json:"type"`
Region string `json:"region"`
RangeType RangeType `json:"ip_cidr_range_type"`
NetworkCIDR string `json:"network"`
Comment string `json:"comment"`
Expand Down
1 change: 0 additions & 1 deletion api/network/v1/range_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ func TestGetAvailableSubnetworkRanges(t *testing.T) {
func getValidRangeConfig() RangeConfig {
return RangeConfig{
Type: TypeNode,
Region: "europe-west1",
RangeType: RangeTypePrimary,
NetworkCIDR: "172.28.0.0/14",
SubnetMask: 21,
Expand Down
4 changes: 1 addition & 3 deletions cmd/suggest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import (
)

var (
region string
filter string
)

func init() {
rootCmd.AddCommand(suggestCmd)

// command-specific flags
suggestCmd.Flags().StringVar(&region, "region", "europe-west1", "Region to request subnetwork range for")
suggestCmd.Flags().StringVar(&filter, "filter", "", "Filter for limiting projects to retrieve existing network ranges for, see https://cloud.google.com/resource-manager/reference/rest/v1/projects/list#query-parameters")
}

Expand All @@ -36,7 +34,7 @@ var suggestCmd = &cobra.Command{
return err
}

_, err = plannerService.Suggest(cmd.Context(), region, filter)
_, err = plannerService.Suggest(cmd.Context(), filter)
if err != nil {
return err
}
Expand Down
16 changes: 8 additions & 8 deletions services/planner/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 9 additions & 14 deletions services/planner/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io/ioutil"
"net"
"strings"

"github.com/apparentlymart/go-cidr/cidr"
networkv1 "github.com/estafette/estafette-gcp-network-planner/api/network/v1"
Expand All @@ -18,8 +17,8 @@ import (
//go:generate mockgen -package=planner -destination ./mock.go -source=service.go
type Service interface {
LoadConfig(ctx context.Context) (config *networkv1.Config, err error)
Suggest(ctx context.Context, region, filter string, networkTypes ...networkv1.Type) (subnetsMap map[networkv1.Type]*net.IPNet, err error)
SuggestSingleNetworkRange(ctx context.Context, rangeConfigs []networkv1.RangeConfig, subnetworks []*computev1.Subnetwork, routes []*computev1.Route, region string, networkType networkv1.Type) (subnetworkRange *net.IPNet, err error)
Suggest(ctx context.Context, filter string, networkTypes ...networkv1.Type) (subnetsMap map[networkv1.Type]*net.IPNet, err error)
SuggestSingleNetworkRange(ctx context.Context, rangeConfigs []networkv1.RangeConfig, subnetworks []*computev1.Subnetwork, routes []*computev1.Route, networkType networkv1.Type) (subnetworkRange *net.IPNet, err error)
}

func NewService(ctx context.Context, gcpClient gcp.Client, configPath string) (Service, error) {
Expand Down Expand Up @@ -60,7 +59,7 @@ func (s *service) LoadConfig(ctx context.Context) (config *networkv1.Config, err
return
}

func (s *service) Suggest(ctx context.Context, region, filter string, networkTypes ...networkv1.Type) (subnetsMap map[networkv1.Type]*net.IPNet, err error) {
func (s *service) Suggest(ctx context.Context, filter string, networkTypes ...networkv1.Type) (subnetsMap map[networkv1.Type]*net.IPNet, err error) {

config, err := s.LoadConfig(ctx)
if err != nil {
Expand Down Expand Up @@ -101,7 +100,7 @@ func (s *service) Suggest(ctx context.Context, region, filter string, networkTyp
// get suggested subnets
subnetsMap = map[networkv1.Type]*net.IPNet{}
for _, t := range networkTypes {
subnetRange, err := s.SuggestSingleNetworkRange(ctx, config.RangeConfigs, subnetworks, routes, region, t)
subnetRange, err := s.SuggestSingleNetworkRange(ctx, config.RangeConfigs, subnetworks, routes, t)
if err != nil {
return subnetsMap, err
}
Expand All @@ -116,35 +115,31 @@ func (s *service) Suggest(ctx context.Context, region, filter string, networkTyp
return
}

func (s *service) SuggestSingleNetworkRange(ctx context.Context, rangeConfigs []networkv1.RangeConfig, subnetworks []*computev1.Subnetwork, routes []*computev1.Route, region string, networkType networkv1.Type) (subnetworkRange *net.IPNet, err error) {
func (s *service) SuggestSingleNetworkRange(ctx context.Context, rangeConfigs []networkv1.RangeConfig, subnetworks []*computev1.Subnetwork, routes []*computev1.Route, networkType networkv1.Type) (subnetworkRange *net.IPNet, err error) {

log.Debug().Msgf("Suggesting subnetwork range for region %v and network type %v (with %v range configs and %v subnetworks and %v routes)...", region, networkType, len(rangeConfigs), len(subnetworks), len(routes))
log.Debug().Msgf("Suggesting subnetwork range for network type %v (with %v range configs and %v subnetworks and %v routes)...", networkType, len(rangeConfigs), len(subnetworks), len(routes))

// find range config for region and network type
filteredRangeConfigs := []networkv1.RangeConfig{}
for _, rc := range rangeConfigs {
if rc.Type == networkType && rc.Region == region {
if rc.Type == networkType {
filteredRangeConfigs = append(filteredRangeConfigs, rc)
}
}

if len(filteredRangeConfigs) == 0 {
return subnetworkRange, fmt.Errorf("No ranges have been configured for type %v and region %v, can't suggest a subnetwork range", networkType, region)
return subnetworkRange, fmt.Errorf("No ranges have been configured for type %v, can't suggest a subnetwork range", networkType)
}

if len(filteredRangeConfigs) > 1 {
return subnetworkRange, fmt.Errorf("Multiple ranges have been configured for type %v and region %v, can't suggest a subnetwork range", networkType, region)
return subnetworkRange, fmt.Errorf("Multiple ranges have been configured for type %v, can't suggest a subnetwork range", networkType)
}

rangeConfig := filteredRangeConfigs[0]

// filter subnetworks on whether they're contained in the range config network CIDR
filteredSubnetworkCIDRs := []string{}
for _, sn := range subnetworks {
if !strings.HasSuffix(sn.Region, "/"+rangeConfig.Region) {
continue
}

switch rangeConfig.RangeType {
case networkv1.RangeTypePrimary:
overlap, overlapErr := s.rangesOverlap(rangeConfig.NetworkCIDR, sn.IpCidrRange)
Expand Down
Loading

0 comments on commit b35ce5b

Please sign in to comment.