From 49e7a7b51da4d2dd23f348bc116d1c254971c2bb Mon Sep 17 00:00:00 2001 From: "jianfei.zhang" Date: Fri, 15 Jul 2022 15:15:23 +0800 Subject: [PATCH] fix:handle error and remove Redundant type conversion Signed-off-by: jianfei.zhang --- pkg/util/conntrack/conntrack.go | 4 ++-- pkg/util/flag/flags.go | 2 +- .../exponentialbackoff/exponential_backoff.go | 4 ++-- pkg/util/ipset/testing/fake_test.go | 10 ++++++-- pkg/util/ipvs/testing/fake.go | 24 +++++++++---------- 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/pkg/util/conntrack/conntrack.go b/pkg/util/conntrack/conntrack.go index 52759266ae4..4dc27d347e4 100644 --- a/pkg/util/conntrack/conntrack.go +++ b/pkg/util/conntrack/conntrack.go @@ -86,7 +86,7 @@ func Exists(execer exec.Interface) bool { // https://github.com/kubernetes/kubernetes/issues/31983 func ClearEntriesForPort(execer exec.Interface, port int, isIPv6 bool, protocol v1.Protocol) error { if port <= 0 { - return fmt.Errorf("Wrong port number. The port number must be greater than zero") + return fmt.Errorf("wrong port number. The port number must be greater than zero") } parameters := parametersWithFamily(isIPv6, "-D", "-p", protoStr(protocol), "--dport", strconv.Itoa(port)) err := Exec(execer, parameters...) @@ -117,7 +117,7 @@ func ClearEntriesForNAT(execer exec.Interface, origin, dest string, protocol v1. // https://github.com/kubernetes/kubernetes/issues/59368 func ClearEntriesForPortNAT(execer exec.Interface, dest string, port int, protocol v1.Protocol) error { if port <= 0 { - return fmt.Errorf("Wrong port number. The port number must be greater than zero") + return fmt.Errorf("wrong port number. The port number must be greater than zero") } parameters := parametersWithFamily(utilnet.IsIPv6String(dest), "-D", "-p", protoStr(protocol), "--dport", strconv.Itoa(port), "--dst-nat", dest) err := Exec(execer, parameters...) diff --git a/pkg/util/flag/flags.go b/pkg/util/flag/flags.go index ff8e40db8e8..9d518f7c659 100644 --- a/pkg/util/flag/flags.go +++ b/pkg/util/flag/flags.go @@ -275,7 +275,7 @@ func (t RegisterWithTaintsVar) Set(s string) error { } var taints []v1.Taint for _, ct := range corev1Taints { - taints = append(taints, v1.Taint{Key: ct.Key, Value: ct.Value, Effect: v1.TaintEffect(ct.Effect)}) + taints = append(taints, v1.Taint{Key: ct.Key, Value: ct.Value, Effect: ct.Effect}) } *t.Value = taints return nil diff --git a/pkg/util/goroutinemap/exponentialbackoff/exponential_backoff.go b/pkg/util/goroutinemap/exponentialbackoff/exponential_backoff.go index 336f80a5134..1ae4d5e1f73 100644 --- a/pkg/util/goroutinemap/exponentialbackoff/exponential_backoff.go +++ b/pkg/util/goroutinemap/exponentialbackoff/exponential_backoff.go @@ -28,13 +28,13 @@ const ( // that GoroutineMap will refuse to allow another operation to start with // the same target (if exponentialBackOffOnError is enabled). Each // successive error results in a wait 2x times the previous. - initialDurationBeforeRetry time.Duration = 500 * time.Millisecond + initialDurationBeforeRetry = 500 * time.Millisecond // maxDurationBeforeRetry is the maximum amount of time that // durationBeforeRetry will grow to due to exponential backoff. // Value is slightly offset from 2 minutes to make timeouts due to this // constant recognizable. - maxDurationBeforeRetry time.Duration = 2*time.Minute + 2*time.Second + maxDurationBeforeRetry = 2*time.Minute + 2*time.Second ) // ExponentialBackoff contains the last occurrence of an error and the duration diff --git a/pkg/util/ipset/testing/fake_test.go b/pkg/util/ipset/testing/fake_test.go index f1f5d1e3417..433e5970671 100644 --- a/pkg/util/ipset/testing/fake_test.go +++ b/pkg/util/ipset/testing/fake_test.go @@ -45,8 +45,14 @@ func TestSetEntry(t *testing.T) { } // add two entries - fake.AddEntry("192.168.1.1,tcp:8080", set, true) - fake.AddEntry("192.168.1.2,tcp:8081", set, true) + err = fake.AddEntry("192.168.1.1,tcp:8080", set, true) + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + err = fake.AddEntry("192.168.1.2,tcp:8081", set, true) + if err != nil { + t.Errorf("Unexpected error: %v", err) + } entries, err := fake.ListEntries(set.Name) if err != nil { t.Errorf("Unexpected error: %v", err) diff --git a/pkg/util/ipvs/testing/fake.go b/pkg/util/ipvs/testing/fake.go index f9badbd282a..c674023941d 100644 --- a/pkg/util/ipvs/testing/fake.go +++ b/pkg/util/ipvs/testing/fake.go @@ -79,7 +79,7 @@ func toRealServerKey(rs *utilipvs.RealServer) *RealServerKey { //AddVirtualServer is a fake implementation, it simply adds the VirtualServer into the cache store. func (f *FakeIPVS) AddVirtualServer(serv *utilipvs.VirtualServer) error { if serv == nil { - return fmt.Errorf("Failed to add virtual server, error: virtual server can't be nil") + return fmt.Errorf("failed to add virtual server, error: virtual server can't be nil") } key := toServiceKey(serv) f.Services[key] = serv @@ -91,7 +91,7 @@ func (f *FakeIPVS) AddVirtualServer(serv *utilipvs.VirtualServer) error { //UpdateVirtualServer is a fake implementation, it updates the VirtualServer in the cache store. func (f *FakeIPVS) UpdateVirtualServer(serv *utilipvs.VirtualServer) error { if serv == nil { - return fmt.Errorf("Failed to update service, service can't be nil") + return fmt.Errorf("failed to update service, service can't be nil") } key := toServiceKey(serv) f.Services[key] = serv @@ -101,7 +101,7 @@ func (f *FakeIPVS) UpdateVirtualServer(serv *utilipvs.VirtualServer) error { //DeleteVirtualServer is a fake implementation, it simply deletes the VirtualServer from the cache store. func (f *FakeIPVS) DeleteVirtualServer(serv *utilipvs.VirtualServer) error { if serv == nil { - return fmt.Errorf("Failed to delete service: service can't be nil") + return fmt.Errorf("failed to delete service: service can't be nil") } key := toServiceKey(serv) delete(f.Services, key) @@ -113,14 +113,14 @@ func (f *FakeIPVS) DeleteVirtualServer(serv *utilipvs.VirtualServer) error { //GetVirtualServer is a fake implementation, it tries to find a specific VirtualServer from the cache store. func (f *FakeIPVS) GetVirtualServer(serv *utilipvs.VirtualServer) (*utilipvs.VirtualServer, error) { if serv == nil { - return nil, fmt.Errorf("Failed to get service: service can't be nil") + return nil, fmt.Errorf("failed to get service: service can't be nil") } key := toServiceKey(serv) svc, found := f.Services[key] if found { return svc, nil } - return nil, fmt.Errorf("Not found serv: %v", key.String()) + return nil, fmt.Errorf("not found serv: %v", key.String()) } //GetVirtualServers is a fake implementation, it simply returns all VirtualServers in the cache store. @@ -143,11 +143,11 @@ func (f *FakeIPVS) Flush() error { //AddRealServer is a fake implementation, it simply creates a RealServer for a VirtualServer in the cache store. func (f *FakeIPVS) AddRealServer(serv *utilipvs.VirtualServer, dest *utilipvs.RealServer) error { if serv == nil || dest == nil { - return fmt.Errorf("Failed to add destination for service, neither service nor destination shouldn't be nil") + return fmt.Errorf("failed to add destination for service, neither service nor destination shouldn't be nil") } key := toServiceKey(serv) if _, ok := f.Services[key]; !ok { - return fmt.Errorf("Failed to add destination for service %v, service not found", key.String()) + return fmt.Errorf("failed to add destination for service %v, service not found", key.String()) } dests := f.Destinations[key] if dests == nil { @@ -161,11 +161,11 @@ func (f *FakeIPVS) AddRealServer(serv *utilipvs.VirtualServer, dest *utilipvs.Re //GetRealServers is a fake implementation, it simply returns all RealServers in the cache store. func (f *FakeIPVS) GetRealServers(serv *utilipvs.VirtualServer) ([]*utilipvs.RealServer, error) { if serv == nil { - return nil, fmt.Errorf("Failed to get destination for nil service") + return nil, fmt.Errorf("failed to get destination for nil service") } key := toServiceKey(serv) if _, ok := f.Services[key]; !ok { - return nil, fmt.Errorf("Failed to get destinations for service %v, service not found", key.String()) + return nil, fmt.Errorf("failed to get destinations for service %v, service not found", key.String()) } return f.Destinations[key], nil } @@ -173,11 +173,11 @@ func (f *FakeIPVS) GetRealServers(serv *utilipvs.VirtualServer) ([]*utilipvs.Rea //DeleteRealServer is a fake implementation, it deletes the real server in the cache store. func (f *FakeIPVS) DeleteRealServer(serv *utilipvs.VirtualServer, dest *utilipvs.RealServer) error { if serv == nil || dest == nil { - return fmt.Errorf("Failed to delete destination, neither service nor destination can't be nil") + return fmt.Errorf("failed to delete destination, neither service nor destination can't be nil") } key := toServiceKey(serv) if _, ok := f.Services[key]; !ok { - return fmt.Errorf("Failed to delete destination for service %v, service not found", key.String()) + return fmt.Errorf("failed to delete destination for service %v, service not found", key.String()) } dests := f.Destinations[key] exist := false @@ -191,7 +191,7 @@ func (f *FakeIPVS) DeleteRealServer(serv *utilipvs.VirtualServer, dest *utilipvs } // Not Found if !exist { - return fmt.Errorf("Failed to delete real server for service %v, real server not found", key.String()) + return fmt.Errorf("failed to delete real server for service %v, real server not found", key.String()) } return nil }