-
Notifications
You must be signed in to change notification settings - Fork 215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: kapinger use service IP instead of name #1283
Open
matmerr
wants to merge
3
commits into
microsoft:main
Choose a base branch
from
matmerr:matmerr/servicecontroller
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+194
−23
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package clients | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"slices" | ||
"sync" | ||
"time" | ||
|
||
"log" | ||
|
||
"golang.org/x/exp/rand" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/informers" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/cache" | ||
) | ||
|
||
type serviceController struct { | ||
sync.RWMutex | ||
serviceInformer cache.SharedIndexInformer | ||
|
||
ips []string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thoughts on using net.IP instead of string? What stops me from putting 'wingding' as a value? Edit: It looks like we are pulling this from clusterIP which is a string as well. One day the service resource will go away .... My comment is probably unimportant |
||
} | ||
|
||
func newServiceController(clientset kubernetes.Interface, labelselector string) (*serviceController, error) { | ||
serviceInformer := informers.NewSharedInformerFactoryWithOptions(clientset, time.Hour*24, | ||
informers.WithTweakListOptions(func(options *metav1.ListOptions) { | ||
options.LabelSelector = labelselector // Filter by label selector | ||
}), | ||
).Core().V1().Services().Informer() | ||
c := &serviceController{ | ||
serviceInformer: serviceInformer, | ||
} | ||
serviceInformer.AddEventHandler( | ||
cache.ResourceEventHandlerFuncs{ | ||
AddFunc: c.serviceAdd, | ||
UpdateFunc: c.serviceUpdate, | ||
DeleteFunc: c.serviceDelete, | ||
}, | ||
) | ||
|
||
return c, nil | ||
} | ||
|
||
func (c *serviceController) run(ctx context.Context) error { | ||
stopCh := make(chan struct{}) | ||
go func() { | ||
<-ctx.Done() | ||
close(stopCh) | ||
}() | ||
c.serviceInformer.Run(stopCh) | ||
if !cache.WaitForCacheSync(ctx.Done(), c.serviceInformer.HasSynced) { | ||
return fmt.Errorf("failed to sync") | ||
} | ||
return nil | ||
} | ||
|
||
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) | ||
} | ||
|
||
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) | ||
} | ||
|
||
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) | ||
} | ||
|
||
func (c *serviceController) getIP() string { | ||
c.RLock() | ||
defer c.RUnlock() | ||
return c.ips[rand.Intn(len(c.ips))] | ||
} | ||
|
||
func (c *serviceController) addIP(ip string) { | ||
c.Lock() | ||
defer c.Unlock() | ||
c.ips = append(c.ips, ip) | ||
} | ||
|
||
func (c *serviceController) removeIP(ip string) { | ||
c.Lock() | ||
defer c.Unlock() | ||
|
||
// find the index of the ip | ||
i := -1 | ||
for j, cip := range c.ips { | ||
if cip == ip { | ||
i = j | ||
break | ||
} | ||
} | ||
if i == -1 { | ||
log.Printf("service with ip %s not found", ip) | ||
return | ||
} | ||
|
||
c.ips = slices.Delete(c.ips, i, i+1) | ||
c.ips = slices.Clip(c.ips) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"fmt" | ||
"log" | ||
"net/http" | ||
_ "net/http/pprof" | ||
"strconv" | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although a slice is more memory-efficient, if the order of IPs is not important, have you considered using a map instead? The lookup time would be much faster, especially with large number of IPs. Using a map would also simplify the code for removeIP func.