refactor/checkout (#8)
Co-authored-by: matst80 <mats.tornberg@gmail.com> Reviewed-on: #8 Co-authored-by: Mats Törnberg <mats@tornberg.me> Co-committed-by: Mats Törnberg <mats@tornberg.me>
This commit was merged in pull request #8.
This commit is contained in:
@@ -14,17 +14,16 @@ import (
|
||||
)
|
||||
|
||||
type K8sDiscovery struct {
|
||||
ctx context.Context
|
||||
client *kubernetes.Clientset
|
||||
ctx context.Context
|
||||
client *kubernetes.Clientset
|
||||
listOptions metav1.ListOptions
|
||||
}
|
||||
|
||||
func (k *K8sDiscovery) Discover() ([]string, error) {
|
||||
return k.DiscoverInNamespace("")
|
||||
}
|
||||
func (k *K8sDiscovery) DiscoverInNamespace(namespace string) ([]string, error) {
|
||||
pods, err := k.client.CoreV1().Pods(namespace).List(k.ctx, metav1.ListOptions{
|
||||
LabelSelector: "actor-pool=cart",
|
||||
})
|
||||
pods, err := k.client.CoreV1().Pods(namespace).List(k.ctx, k.listOptions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -44,14 +43,10 @@ func hasReadyCondition(pod *v1.Pod) bool {
|
||||
}
|
||||
|
||||
func (k *K8sDiscovery) Watch() (<-chan HostChange, error) {
|
||||
timeout := int64(30)
|
||||
ipsThatAreReady := make(map[string]bool)
|
||||
m := sync.Mutex{}
|
||||
watcherFn := func(options metav1.ListOptions) (watch.Interface, error) {
|
||||
return k.client.CoreV1().Pods("").Watch(k.ctx, metav1.ListOptions{
|
||||
LabelSelector: "actor-pool=cart",
|
||||
TimeoutSeconds: &timeout,
|
||||
})
|
||||
return k.client.CoreV1().Pods("").Watch(k.ctx, k.listOptions)
|
||||
}
|
||||
watcher, err := toolsWatch.NewRetryWatcherWithContext(k.ctx, "1", &cache.ListWatch{WatchFunc: watcherFn})
|
||||
if err != nil {
|
||||
@@ -82,9 +77,10 @@ func (k *K8sDiscovery) Watch() (<-chan HostChange, error) {
|
||||
return ch, nil
|
||||
}
|
||||
|
||||
func NewK8sDiscovery(client *kubernetes.Clientset) *K8sDiscovery {
|
||||
func NewK8sDiscovery(client *kubernetes.Clientset, listOptions metav1.ListOptions) *K8sDiscovery {
|
||||
return &K8sDiscovery{
|
||||
ctx: context.Background(),
|
||||
client: client,
|
||||
ctx: context.Background(),
|
||||
client: client,
|
||||
listOptions: listOptions,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
)
|
||||
@@ -17,7 +18,9 @@ func TestDiscovery(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Errorf("Error creating client: %v", err)
|
||||
}
|
||||
d := NewK8sDiscovery(client)
|
||||
d := NewK8sDiscovery(client, metav1.ListOptions{
|
||||
LabelSelector: "app",
|
||||
})
|
||||
res, err := d.DiscoverInNamespace("")
|
||||
if err != nil {
|
||||
t.Errorf("Error discovering: %v", err)
|
||||
@@ -36,7 +39,9 @@ func TestWatch(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Errorf("Error creating client: %v", err)
|
||||
}
|
||||
d := NewK8sDiscovery(client)
|
||||
d := NewK8sDiscovery(client, metav1.ListOptions{
|
||||
LabelSelector: "app",
|
||||
})
|
||||
ch, err := d.Watch()
|
||||
if err != nil {
|
||||
t.Errorf("Error watching: %v", err)
|
||||
|
||||
Reference in New Issue
Block a user