Skip to content

Commit

Permalink
Merge pull request aws#93 from mattlandis/fix-nil
Browse files Browse the repository at this point in the history
Fix nil error in discovery of pods
  • Loading branch information
liwenwu-amazon authored May 30, 2018
2 parents 01c0390 + fb1aa68 commit 1d4c6df
Showing 1 changed file with 27 additions and 28 deletions.
55 changes: 27 additions & 28 deletions pkg/k8sapi/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package k8sapi
import (
"fmt"
"os"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -201,35 +200,35 @@ func (d *Controller) handlePodUpdate(key string) error {
}

if !exists {
podName := obj.(*v1.Pod).GetName()
// Below we will warm up our cache with a Pod, so that we will see a delete for one pod
if strings.Compare(d.myNodeName, obj.(*v1.Pod).Spec.NodeName) == 0 && !obj.(*v1.Pod).Spec.HostNetwork {
log.Infof(" Pods deleted on my node: %v", podName)
d.workerPodsLock.Lock()
defer d.workerPodsLock.Unlock()

delete(d.workerPods, key)
}
} else {
// Note that you also have to check the uid if you have a local controlled resource, which
// is dependent on the actual instance, to detect that a Pod was recreated with the same name
podName := obj.(*v1.Pod).GetName()

// check to see if this is one of worker pod on my nodes
if strings.Compare(d.myNodeName, obj.(*v1.Pod).Spec.NodeName) == 0 && !obj.(*v1.Pod).Spec.HostNetwork {
d.workerPodsLock.Lock()
defer d.workerPodsLock.Unlock()

d.workerPods[key] = &K8SPodInfo{
Name: podName,
Namespace: obj.(*v1.Pod).GetNamespace(),
UID: string(obj.(*v1.Pod).GetUID()),
IP: obj.(*v1.Pod).Status.PodIP}

log.Infof(" Add/Update for Pod %s on my node, namespace = %s, IP = %s",
podName, d.workerPods[key].Namespace, d.workerPods[key].IP)
log.Infof(" Pods deleted on my node: %s", key)
d.workerPodsLock.Lock()
defer d.workerPodsLock.Unlock()
delete(d.workerPods, key)
return nil
}

pod, ok := obj.(*v1.Pod)
if !ok {
log.Errorf("Updated object recieved was not a pod: %+v", obj)
return errors.New("recieved a non-pod object update")
}
// Note that you also have to check the uid if you have a local controlled resource, which
// is dependent on the actual instance, to detect that a Pod was recreated with the same name
podName := pod.GetName()

// check to see if this is one of worker pod on my nodes
if d.myNodeName == pod.Spec.NodeName && !pod.Spec.HostNetwork {
d.workerPodsLock.Lock()
defer d.workerPodsLock.Unlock()

d.workerPods[key] = &K8SPodInfo{
Name: podName,
Namespace: pod.GetNamespace(),
UID: string(pod.GetUID()),
IP: pod.Status.PodIP,
}

log.Infof(" Add/Update for Pod %s on my node, namespace = %s, IP = %s", podName, d.workerPods[key].Namespace, d.workerPods[key].IP)
}
return nil
}
Expand Down

0 comments on commit 1d4c6df

Please sign in to comment.