From b4bc58c14afb7c19d192f4e6c1bf8222da6f23b0 Mon Sep 17 00:00:00 2001 From: ederst Date: Mon, 25 Jul 2022 15:02:37 +0200 Subject: [PATCH] WIP/fixup: rename to NetworkCIDR --- etcd-manager/cmd/etcd-manager/main.go | 30 ++++++++------- etcd-manager/cmd/etcd-manager/main_test.go | 38 +++++++++---------- .../pkg/volumes/openstack/discovery.go | 2 +- etcd-manager/pkg/volumes/openstack/util.go | 6 +-- .../pkg/volumes/openstack/util_test.go | 22 +++++------ etcd-manager/pkg/volumes/openstack/volumes.go | 16 ++++---- 6 files changed, 58 insertions(+), 56 deletions(-) diff --git a/etcd-manager/cmd/etcd-manager/main.go b/etcd-manager/cmd/etcd-manager/main.go index f97be1bea..9a325561a 100644 --- a/etcd-manager/cmd/etcd-manager/main.go +++ b/etcd-manager/cmd/etcd-manager/main.go @@ -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() @@ -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 @@ -195,11 +197,11 @@ 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 } @@ -207,19 +209,19 @@ func parseIPFilter(o *EtcdManagerOptions) ([]*net.IPNet, error) { 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. @@ -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) @@ -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) diff --git a/etcd-manager/cmd/etcd-manager/main_test.go b/etcd-manager/cmd/etcd-manager/main_test.go index cac89ee72..32e716a0a 100644 --- a/etcd-manager/cmd/etcd-manager/main_test.go +++ b/etcd-manager/cmd/etcd-manager/main_test.go @@ -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 @@ -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) } diff --git a/etcd-manager/pkg/volumes/openstack/discovery.go b/etcd-manager/pkg/volumes/openstack/discovery.go index 87d274d04..f28fd06a2 100644 --- a/etcd-manager/pkg/volumes/openstack/discovery.go +++ b/etcd-manager/pkg/volumes/openstack/discovery.go @@ -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 diff --git a/etcd-manager/pkg/volumes/openstack/util.go b/etcd-manager/pkg/volumes/openstack/util.go index 681b141b3..42a141f15 100644 --- a/etcd-manager/pkg/volumes/openstack/util.go +++ b/etcd-manager/pkg/volumes/openstack/util.go @@ -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 diff --git a/etcd-manager/pkg/volumes/openstack/util_test.go b/etcd-manager/pkg/volumes/openstack/util_test.go index 60ebb8fd0..e2e5a153c 100644 --- a/etcd-manager/pkg/volumes/openstack/util_test.go +++ b/etcd-manager/pkg/volumes/openstack/util_test.go @@ -108,13 +108,13 @@ 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) } @@ -122,15 +122,15 @@ func TestReturnErrorOnNonMatchingCIDR(t *testing.T) { 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) } @@ -138,15 +138,15 @@ func TestReturnFirstIPMatchingCIDR(t *testing.T) { 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) } diff --git a/etcd-manager/pkg/volumes/openstack/volumes.go b/etcd-manager/pkg/volumes/openstack/volumes.go index 30cd8624a..429ce39e8 100644 --- a/etcd-manager/pkg/volumes/openstack/volumes.go +++ b/etcd-manager/pkg/volumes/openstack/volumes.go @@ -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 @@ -70,7 +70,7 @@ 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 { @@ -78,11 +78,11 @@ func NewOpenstackVolumes(clusterName string, volumeTags []string, nameTag string } 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 { @@ -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) }