Skip to content

Commit

Permalink
use net.IP
Browse files Browse the repository at this point in the history
  • Loading branch information
matmerr committed Feb 5, 2025
1 parent abc1bbb commit 209f70a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
21 changes: 11 additions & 10 deletions hack/tools/kapinger/clients/http_service_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package clients
import (
"context"
"fmt"
"net"
"slices"
"sync"
"time"
Expand All @@ -21,7 +22,7 @@ type serviceController struct {
sync.RWMutex
serviceInformer cache.SharedIndexInformer

ips []string
ips []net.IP
}

func newServiceController(clientset kubernetes.Interface, labelselector string) (*serviceController, error) {
Expand Down Expand Up @@ -60,43 +61,43 @@ func (c *serviceController) run(ctx context.Context) error {
func (c *serviceController) serviceAdd(obj interface{}) {
service := obj.(*v1.Service)
log.Printf("service %s/%s added with ip %s", service.Namespace, service.Name, service.Spec.ClusterIP)
c.addIP(service.Spec.ClusterIP)
c.addIP(net.ParseIP(service.Spec.ClusterIP))
}

func (c *serviceController) serviceUpdate(old, new interface{}) {
newsvc := new.(*v1.Service)
oldsvc := new.(*v1.Service)
log.Printf("service %s/%s updated with new ip %s", newsvc.Namespace, newsvc.Name, newsvc.Spec.ClusterIP)
c.removeIP(oldsvc.Spec.ClusterIP)
c.addIP(newsvc.Spec.ClusterIP)
c.removeIP(net.ParseIP(oldsvc.Spec.ClusterIP))
c.addIP(net.ParseIP(newsvc.Spec.ClusterIP))
}

func (c *serviceController) serviceDelete(obj interface{}) {
service := obj.(*v1.Service)
log.Printf("service %s/%s deleted", service.Namespace, service.Name)
c.removeIP(service.Spec.ClusterIP)
c.removeIP(net.ParseIP(service.Spec.ClusterIP))
}

func (c *serviceController) getIP() string {
func (c *serviceController) getIP() net.IP {
c.RLock()
defer c.RUnlock()
return c.ips[rand.Intn(len(c.ips))]
}

func (c *serviceController) addIP(ip string) {
func (c *serviceController) addIP(ip net.IP) {
c.Lock()
defer c.Unlock()
c.ips = append(c.ips, ip)
}

func (c *serviceController) removeIP(ip string) {
func (c *serviceController) removeIP(ip net.IP) {
c.Lock()
defer c.Unlock()

// find the index of the ip
i := -1
for j, cip := range c.ips {
if cip == ip {
if cip.Equal(ip) {
i = j
break
}
Expand Down
2 changes: 1 addition & 1 deletion hack/tools/kapinger/servers/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import (

func getResponse(addressString, protocol string) []byte {
podname := os.Getenv("POD_NAME")
return []byte(fmt.Sprintf("connected to: %s via %s, connected from: %v\n", podname, protocol, addressString))
return []byte(fmt.Sprintf("connected to: %s via %s, connected from: %v", podname, protocol, addressString))
}
2 changes: 1 addition & 1 deletion test/e2e/framework/types/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
var (
ErrEmptyDescription = fmt.Errorf("job description is empty")
ErrNonNilError = fmt.Errorf("expected error to be non-nil")
ErrNilError = fmt.Errorf("expected error to be nil")
ErrNilError = fmt.Errorf("test ")

Check failure on line 13 in test/e2e/framework/types/job.go

View workflow job for this annotation

GitHub Actions / Lint (linux, amd64)

fmt.Errorf can be replaced with errors.New (perfsprint)

Check failure on line 13 in test/e2e/framework/types/job.go

View workflow job for this annotation

GitHub Actions / Lint (linux, arm64)

fmt.Errorf can be replaced with errors.New (perfsprint)

Check failure on line 13 in test/e2e/framework/types/job.go

View workflow job for this annotation

GitHub Actions / Lint (windows, amd64)

fmt.Errorf can be replaced with errors.New (perfsprint)

Check failure on line 13 in test/e2e/framework/types/job.go

View workflow job for this annotation

GitHub Actions / Lint (windows, arm64)

fmt.Errorf can be replaced with errors.New (perfsprint)
ErrMissingParameter = fmt.Errorf("missing parameter")
ErrParameterAlreadySet = fmt.Errorf("parameter already set")
ErrOrphanSteps = fmt.Errorf("background steps with no corresponding stop")
Expand Down

0 comments on commit 209f70a

Please sign in to comment.