Skip to content

Commit

Permalink
remove repeated logger declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
moh-osman3 committed Dec 12, 2022
1 parent 22226d8 commit 2279a6d
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions cmd/otel-allocator/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,21 @@ func NewClient(logger logr.Logger, kubeConfig *rest.Config) (*Client, error) {
}

return &Client{
log: logger,
log: logger.WithValues("component", "opentelemetry-targetallocator"),
k8sClient: clientset,
close: make(chan struct{}),
}, nil
}

func (k *Client) Watch(ctx context.Context, labelMap map[string]string, fn func(collectors map[string]*allocation.Collector)) {
collectorMap := map[string]*allocation.Collector{}
log := k.log.WithValues("component", "opentelemetry-targetallocator")

opts := metav1.ListOptions{
LabelSelector: labels.SelectorFromSet(labelMap).String(),
}
pods, err := k.k8sClient.CoreV1().Pods(ns).List(ctx, opts)
if err != nil {
log.Error(err, "Pod failure")
k.log.Error(err, "Pod failure")
os.Exit(1)
}
for i := range pods.Items {
Expand All @@ -94,26 +93,24 @@ func (k *Client) Watch(ctx context.Context, labelMap map[string]string, fn func(
}

func (k *Client) restartWatch(ctx context.Context, opts metav1.ListOptions, collectorMap map[string]*allocation.Collector, fn func(collectors map[string]*allocation.Collector)) bool {
log := k.log.WithValues("component", "opentelemetry-targetallocator")
// add timeout to the context before calling Watch
ctx, cancel := context.WithTimeout(ctx, watcherTimeout)
defer cancel()
watcher, err := k.k8sClient.CoreV1().Pods(ns).Watch(ctx, opts)
if err != nil {
log.Error(err, "unable to create collector pod watcher")
k.log.Error(err, "unable to create collector pod watcher")
return false
}
log.Info("Successfully started a collector pod watcher")
k.log.Info("Successfully started a collector pod watcher")
if msg := runWatch(ctx, k, watcher.ResultChan(), collectorMap, fn); msg != "" {
log.Info("Collector pod watch event stopped " + msg)
k.log.Info("Collector pod watch event stopped " + msg)
return false
}

return true
}

func runWatch(ctx context.Context, k *Client, c <-chan watch.Event, collectorMap map[string]*allocation.Collector, fn func(collectors map[string]*allocation.Collector)) string {
log := k.log.WithValues("component", "opentelemetry-targetallocator")
for {
collectorsDiscovered.Set(float64(len(collectorMap)))
select {
Expand All @@ -123,13 +120,13 @@ func runWatch(ctx context.Context, k *Client, c <-chan watch.Event, collectorMap
return ""
case event, ok := <-c:
if !ok {
log.Info("No event found. Restarting watch routine")
k.log.Info("No event found. Restarting watch routine")
return ""
}

pod, ok := event.Object.(*v1.Pod)
if !ok {
log.Info("No pod found in event Object. Restarting watch routine")
k.log.Info("No pod found in event Object. Restarting watch routine")
return ""
}

Expand Down

0 comments on commit 2279a6d

Please sign in to comment.