Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
WIP/fixup: rename to NetworkCIDR
Browse files Browse the repository at this point in the history
  • Loading branch information
ederst committed Jul 25, 2022
1 parent d85acd5 commit b4bc58c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 56 deletions.
30 changes: 16 additions & 14 deletions etcd-manager/cmd/etcd-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func main() {
var volumeTags stringSliceFlag
flag.Var(&volumeTags, "volume-tag", "tag which volume is required to have")

flag.StringVar(&o.IPFilter, "ip-filter", o.IPFilter, "filter IP addresses with CIDR notation")
flag.StringVar(&o.NetworkCIDR, "network-cidr", o.NetworkCIDR, "filtering for IP addresses by defining a comma separated list of CIDRs.")

flag.Parse()

Expand Down Expand Up @@ -164,8 +164,10 @@ type EtcdManagerOptions struct {
// EtcdManagerMetricsPort allows exposing statistics from etcd-manager
EtcdManagerMetricsPort int

// IPFilter allows filtering for IP addresses with CIDR notation
IPFilter string
// NetworkCIDR allows filtering for IP addresses by defining a comma separated list of CIDRs.
// When defining multiple CIDRs then etcd-manager uses the IP first matching the CIDR with the highest priority.
// The order of which the CIRRs are in define the priority, where the first item having the highest.
NetworkCIDR string
}

// InitDefaults populates the default flag values
Expand Down Expand Up @@ -195,31 +197,31 @@ func (o *EtcdManagerOptions) InitDefaults() {
o.EtcdInsecure = false
o.EtcdManagerMetricsPort = 0

o.IPFilter = os.Getenv("ETCD_MANAGER_IP_FILTER")
o.NetworkCIDR = os.Getenv("ETCD_MANAGER_NETWORK_CIDR")
}

func parseIPFilter(o *EtcdManagerOptions) ([]*net.IPNet, error) {
if o.IPFilter == "" {
func parseNetworkCIDR(o *EtcdManagerOptions) ([]*net.IPNet, error) {
if o.NetworkCIDR == "" {
return nil, nil
}

if o.VolumeProviderID != "openstack" {
return nil, fmt.Errorf("is only supported with provider 'openstack'")
}

var parsedIPFilters []*net.IPNet
var networkCIDRs []*net.IPNet

for _, cidr := range strings.Split(o.IPFilter, ",") {
for _, cidr := range strings.Split(o.NetworkCIDR, ",") {
cidr = strings.TrimSpace(cidr)
_, parsedIPFilter, err := net.ParseCIDR(cidr)
_, parsedCIDR, err := net.ParseCIDR(cidr)
if err != nil {
return nil, err
}

parsedIPFilters = append(parsedIPFilters, parsedIPFilter)
networkCIDRs = append(networkCIDRs, parsedCIDR)
}

return parsedIPFilters, nil
return networkCIDRs, nil
}

// RunEtcdManager runs the etcd-manager, returning only we should exit.
Expand All @@ -232,9 +234,9 @@ func RunEtcdManager(o *EtcdManagerOptions) error {
return fmt.Errorf("backup-store is required")
}

parsedIPFilter, err := parseIPFilter(o)
networkCIDRs, err := parseNetworkCIDR(o)
if err != nil {
return fmt.Errorf("ip-filter %s", err)
return fmt.Errorf("network-cidr %s", err)
}

backupInterval, err := time.ParseDuration(o.BackupInterval)
Expand Down Expand Up @@ -279,7 +281,7 @@ func RunEtcdManager(o *EtcdManagerOptions) error {
discoveryProvider = gceVolumeProvider

case "openstack":
osVolumeProvider, err := openstack.NewOpenstackVolumes(o.ClusterName, o.VolumeTags, o.NameTag, parsedIPFilter)
osVolumeProvider, err := openstack.NewOpenstackVolumes(o.ClusterName, o.VolumeTags, o.NameTag, networkCIDRs)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
Expand Down
38 changes: 19 additions & 19 deletions etcd-manager/cmd/etcd-manager/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import (
"testing"
)

func getTestData(ipFilter string, volumeProviderID string) *EtcdManagerOptions {
func getTestData(networkCIDR string, volumeProviderID string) *EtcdManagerOptions {
var o EtcdManagerOptions
o.InitDefaults()

o.IPFilter = ipFilter
o.NetworkCIDR = networkCIDR
o.VolumeProviderID = volumeProviderID

return &o
Expand All @@ -44,60 +44,60 @@ func assertTestResults(t *testing.T, err error, expected interface{}, actual int
}
}

func TestParseIPFilterReturnsNilByDefault(t *testing.T) {
func TestParseNetworkCIDRReturnsNilByDefault(t *testing.T) {
o := getTestData("", "")

_, actualErr := parseIPFilter(o)
_, actualErr := parseNetworkCIDR(o)

assertTestResults(t, nil, nil, actualErr)
}

func TestParseIPFilterReturnsUnsupportedProviderError(t *testing.T) {
func TestParseNetworkCIDRReturnsUnsupportedProviderError(t *testing.T) {
o := getTestData("192.168.0.0/16", "")

expectedErr := fmt.Errorf("is only supported with provider 'openstack'")

_, actualErr := parseIPFilter(o)
_, actualErr := parseNetworkCIDR(o)

assertTestResults(t, nil, expectedErr, actualErr)
}

func TestParseIPFilterReturnsErrorOnInvalidCIDR(t *testing.T) {
func TestParseNetworkCIDRReturnsErrorOnInvalidCIDR(t *testing.T) {
o := getTestData("192.168.0.0/123, 2001:db8::/64", "openstack")

expectedErr := &net.ParseError{Type: "CIDR address", Text: "192.168.0.0/123"}

_, actualErr := parseIPFilter(o)
_, actualErr := parseNetworkCIDR(o)

assertTestResults(t, nil, expectedErr, actualErr)
}

func TestParseIPFilterReturnsParsedCIDR(t *testing.T) {
func TestParseNetworkCIDRReturnsParsedCIDR(t *testing.T) {
o := getTestData("192.168.0.0/16, 2001:db8::/64", "openstack")

var expectedIPFilter []*net.IPNet
var expectedNetworkCIDRs []*net.IPNet
_, cidr1, _ := net.ParseCIDR("192.168.0.0/16")
_, cidr2, _ := net.ParseCIDR("2001:db8::/64")
expectedIPFilter = append(expectedIPFilter, cidr1, cidr2)
expectedNetworkCIDRs = append(expectedNetworkCIDRs, cidr1, cidr2)

actualIPFilter, err := parseIPFilter(o)
actualNetworkCIDRs, err := parseNetworkCIDR(o)

assertTestResults(t, err, expectedIPFilter, actualIPFilter)
assertTestResults(t, err, expectedNetworkCIDRs, actualNetworkCIDRs)
}

func TestParseInitDefaultReturnsEmptyStringForIPFilter(t *testing.T) {
func TestParseInitDefaultReturnsEmptyStringForNetworkCIDRs(t *testing.T) {
var o EtcdManagerOptions
o.InitDefaults()

assertTestResults(t, nil, "", o.IPFilter)
assertTestResults(t, nil, "", o.NetworkCIDR)
}

func TestParseInitDefaultReturnsValueOfEnvVarForIPFilter(t *testing.T) {
expectedIPFilter := "192.168.0.0/16, 2001:db8::/64"
os.Setenv("ETCD_MANAGER_IP_FILTER", expectedIPFilter)
func TestParseInitDefaultReturnsValueOfEnvVarForNetworkCIDRs(t *testing.T) {
expectedNetworkCIDR := "192.168.0.0/16, 2001:db8::/64"
os.Setenv("ETCD_MANAGER_NETWORK_CIDR", expectedNetworkCIDR)

var o EtcdManagerOptions
o.InitDefaults()

assertTestResults(t, nil, expectedIPFilter, o.IPFilter)
assertTestResults(t, nil, expectedNetworkCIDR, o.NetworkCIDR)
}
2 changes: 1 addition & 1 deletion etcd-manager/pkg/volumes/openstack/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (os *OpenstackVolumes) Poll() (map[string]discovery.Node, error) {
node := discovery.Node{
ID: volume.EtcdName,
}
address, err := GetServerFixedIP(server.Addresses, server.Name, os.ipFilter)
address, err := GetServerFixedIP(server.Addresses, server.Name, os.networkCIDRs)
if err != nil {
klog.Warningf("Could not find servers fixed ip %s: %v", server.Name, err)
continue
Expand Down
6 changes: 3 additions & 3 deletions etcd-manager/pkg/volumes/openstack/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ func getAllServerFixedIPs(addrs map[string]interface{}) []string {
return fixedIPs
}

func GetServerFixedIP(addrs map[string]interface{}, name string, ipFilter []*net.IPNet) (poolAddress string, err error) {
func GetServerFixedIP(addrs map[string]interface{}, name string, networkCIDRs []*net.IPNet) (poolAddress string, err error) {
fixedIPs := getAllServerFixedIPs(addrs)

if ipFilter != nil {
for _, cidr := range ipFilter {
if networkCIDRs != nil {
for _, cidr := range networkCIDRs {
for _, fixedIP := range fixedIPs {
if cidr.Contains(net.ParseIP(fixedIP)) {
return fixedIP, nil
Expand Down
22 changes: 11 additions & 11 deletions etcd-manager/pkg/volumes/openstack/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,45 +108,45 @@ func TestReturnFirstFixedIP(t *testing.T) {
func TestReturnErrorOnNonMatchingCIDR(t *testing.T) {
td := getTestData()

var ipFilter []*net.IPNet
var networkCIDRs []*net.IPNet
_, cidr, _ := net.ParseCIDR("172.16.0.0/16")
ipFilter = append(ipFilter, cidr)
networkCIDRs = append(networkCIDRs, cidr)

expectedErr := fmt.Errorf("failed to find Fixed IP address for server %s", td.clusterName)

_, actualErr := GetServerFixedIP(td.addrs, td.clusterName, ipFilter)
_, actualErr := GetServerFixedIP(td.addrs, td.clusterName, networkCIDRs)

assertTestResults(t, nil, expectedErr, actualErr)
}

func TestReturnFirstIPMatchingCIDR(t *testing.T) {
td := getTestData()

var ipFilter []*net.IPNet
var networkCIDRs []*net.IPNet
_, cidr1, _ := net.ParseCIDR("192.168.2.0/24")
_, cidr2, _ := net.ParseCIDR("2001:db8::/64")
ipFilter = append(ipFilter, cidr1, cidr2)
t.Log(ipFilter)
networkCIDRs = append(networkCIDRs, cidr1, cidr2)
t.Log(networkCIDRs)

expectedIP := td.ips[3]

actualIP, err := GetServerFixedIP(td.addrs, td.clusterName, ipFilter)
actualIP, err := GetServerFixedIP(td.addrs, td.clusterName, networkCIDRs)

assertTestResults(t, err, expectedIP, actualIP)
}

func TestReturnFirstIPv6MatchingCIDR(t *testing.T) {
td := getTestData()

var ipFilter []*net.IPNet
var networkCIDRs []*net.IPNet
_, cidr1, _ := net.ParseCIDR("2001:db8::/64")
_, cidr2, _ := net.ParseCIDR("192.168.2.0/24")
ipFilter = append(ipFilter, cidr1, cidr2)
t.Log(ipFilter)
networkCIDRs = append(networkCIDRs, cidr1, cidr2)
t.Log(networkCIDRs)

expectedIP := td.ips[1]

actualIP, err := GetServerFixedIP(td.addrs, td.clusterName, ipFilter)
actualIP, err := GetServerFixedIP(td.addrs, td.clusterName, networkCIDRs)

assertTestResults(t, err, expectedIP, actualIP)
}
16 changes: 8 additions & 8 deletions etcd-manager/pkg/volumes/openstack/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type OpenstackVolumes struct {

matchTagKeys []string
matchTags map[string]string
ipFilter []*net.IPNet
networkCIDRs []*net.IPNet

computeClient *gophercloud.ServiceClient
volumeClient *gophercloud.ServiceClient
Expand All @@ -70,19 +70,19 @@ type OpenstackVolumes struct {
var _ volumes.Volumes = &OpenstackVolumes{}

// NewOpenstackVolumes builds a OpenstackVolume
func NewOpenstackVolumes(clusterName string, volumeTags []string, nameTag string, ipFilter []*net.IPNet) (*OpenstackVolumes, error) {
func NewOpenstackVolumes(clusterName string, volumeTags []string, nameTag string, networkCIDRs []*net.IPNet) (*OpenstackVolumes, error) {

metadata, err := getLocalMetadata()
if err != nil {
return nil, fmt.Errorf("failed to get server metadata: %v", err)
}

stack := &OpenstackVolumes{
clusterName: clusterName,
meta: metadata,
matchTags: make(map[string]string),
nameTag: nameTag,
ipFilter: ipFilter,
clusterName: clusterName,
meta: metadata,
matchTags: make(map[string]string),
nameTag: nameTag,
networkCIDRs: networkCIDRs,
}

for _, volumeTag := range volumeTags {
Expand Down Expand Up @@ -236,7 +236,7 @@ func (stack *OpenstackVolumes) discoverTags() error {
if mc.ObserveRequest(err) != nil {
return fmt.Errorf("failed to retrieve server information from cloud: %v", err)
}
ip, err := GetServerFixedIP(extendedServer.Addresses, extendedServer.Name, stack.ipFilter)
ip, err := GetServerFixedIP(extendedServer.Addresses, extendedServer.Name, stack.networkCIDRs)
if err != nil {
return fmt.Errorf("error querying InternalIP from name: %v", err)
}
Expand Down

0 comments on commit b4bc58c

Please sign in to comment.