Skip to content

Commit

Permalink
fix:handle error and remove Redundant type conversion
Browse files Browse the repository at this point in the history
Signed-off-by: jianfei.zhang <jianfei.zhang@daocloud.io>
  • Loading branch information
falser101 committed Jul 19, 2022
1 parent f365438 commit 49e7a7b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
4 changes: 2 additions & 2 deletions pkg/util/conntrack/conntrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down Expand Up @@ -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...)
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/flag/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions pkg/util/ipset/testing/fake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 12 additions & 12 deletions pkg/util/ipvs/testing/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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.
Expand All @@ -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 {
Expand All @@ -161,23 +161,23 @@ 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
}

//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
Expand All @@ -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
}
Expand Down

0 comments on commit 49e7a7b

Please sign in to comment.