Skip to content

Commit

Permalink
Fix some lint errors and mark nolint and TODO GoogleCloudPlatform#351
Browse files Browse the repository at this point in the history
…on the rest
  • Loading branch information
jingyuanliang committed Sep 4, 2024
1 parent ed4cb42 commit 31a0687
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
6 changes: 3 additions & 3 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func TestSysctlConfigEnsure(t *testing.T) {
func TestIPRouteConfigEnsure(t *testing.T) {
r := IPRouteConfig{
Route: netlink.Route{},
RouteAdd: func(route *netlink.Route) error { return os.ErrExist },
RouteDel: func(route *netlink.Route) error { return syscall.ESRCH },
RouteAdd: func(_ *netlink.Route) error { return os.ErrExist },
RouteDel: func(_ *netlink.Route) error { return syscall.ESRCH },
}
if err := r.Ensure(true); err != nil {
t.Error("ipRouteConfig.Ensure(true) should ignore the os.ErrExist Error.")
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestIPRuleConfigEnsure(t *testing.T) {
Rule: netlink.Rule{SuppressIfgroup: -1, SuppressPrefixlen: -1, Mark: -1, Mask: -1, Goto: -1},
RuleAdd: func(rule *netlink.Rule) error { ruleList = append(ruleList, *rule); return nil },
RuleDel: mockRuleDel,
RuleList: func(family int) ([]netlink.Rule, error) { return ruleList, nil },
RuleList: func(_ int) ([]netlink.Rule, error) { return ruleList, nil },
}
if count, _ := ipRule.count(); count != 2 {
t.Errorf("IPRuleConfig.count() should return 2.")
Expand Down
14 changes: 8 additions & 6 deletions pkg/metrics/collector/pod_ip_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ func (c *podIPMetricsCollector) listIPAddresses(dir string) error {
continue
}
f, ok := podMap[podID]
// TODO: #351 - fix lint errors
//nolint:gocritic
if !ok {
podMap[podID] = family
} else if (f == ipv4 && family == ipv6) || (f == ipv6 && family == ipv4) {
Expand Down Expand Up @@ -319,18 +321,18 @@ func (c *podIPMetricsCollector) countIPsFromRange(subnet *net.IPNet) (uint64, er
if bits-ones >= 64 {
return math.MaxUint64, nil
}
max := uint64(1) << uint(bits-ones)
max--
count := uint64(1) << uint(bits-ones)
count--
if subnet.IP.To4() != nil {
// Don't use the IPv4 network's broadcast address
if max == 0 {
if count == 0 {
return 0, fmt.Errorf("subnet includes only the network and broadcast addresses")
}
max--
} else if max == 0 {
count--
} else if count == 0 {
return 0, fmt.Errorf("subnet includes only the network address")
}
return max, nil
return count, nil
}

func (c *podIPMetricsCollector) updateAssignedIPs(subnet *net.IPNet, totalIP uint64) {
Expand Down
6 changes: 4 additions & 2 deletions pkg/metrics/collector/pod_ip_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func TestSetupDirectoryWatcher(t *testing.T) {
t.Fatal("podIPMetricsWatcherIsIntialized: want: true, got: false")
}

//Add a new file to the directory. Verify metrics
// Add a new file to the directory. Verify metrics
mustCreateFile(t, dir1, "10.0.0.3", "hash3")
time.Sleep(1 * time.Second)
if mc.usedIPv4AddrCount != 3 {
Expand All @@ -230,7 +230,7 @@ func TestSetupDirectoryWatcher(t *testing.T) {
t.Errorf("duplicateIpCount. want: 0, got %d", mc.duplicateIPCount)
}

//Remove a file from the directory. Verify metrics
// Remove a file from the directory. Verify metrics
mustDeleteFile(t, dir1, "10.0.0.3")
time.Sleep(1 * time.Second)
if mc.usedIPv4AddrCount != 2 {
Expand Down Expand Up @@ -271,6 +271,8 @@ func TestSetupDirectoryWatcher(t *testing.T) {

for _, bound := range bucketKeys {
v, ok := mc.reuseIPs.buckets[bound]
// TODO: #351 - fix lint errors
//nolint:gocritic
if !ok {
t.Errorf("reused ip: buckets are initialized with 0 values. want: ok, got %v", ok)
} else if bound == 5e3 && v != 0 {
Expand Down

0 comments on commit 31a0687

Please sign in to comment.