diff --git a/runtime/kubernetes/build.go b/runtime/kubernetes/build.go index 1f636476..aab45517 100644 --- a/runtime/kubernetes/build.go +++ b/runtime/kubernetes/build.go @@ -7,7 +7,6 @@ package kubernetes import ( "context" "fmt" - "k8s.io/client-go/tools/cache" "time" "github.com/go-vela/types/pipeline" @@ -15,6 +14,7 @@ import ( v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/tools/cache" // The k8s libraries have some quirks around yaml marshaling (see opts.go). // So, just use the same library for all kubernetes-related YAML. @@ -127,6 +127,7 @@ func (c *client) SetupBuild(ctx context.Context, b *pipeline.Build) error { } } + // initialize the PodTracker now that we have a Pod for it to track tracker, err := NewPodTracker(c.Logger, c.Kubernetes, c.Pod, time.Second*30) if err != nil { return err @@ -192,7 +193,7 @@ func (c *client) AssembleBuild(ctx context.Context, b *pipeline.Build) error { } } - // Populate the PodTracker caches + // Populate the PodTracker caches before creating the pipeline pod c.PodTracker.Start(ctx.Done()) if ok := cache.WaitForCacheSync(ctx.Done(), c.PodTracker.PodSynced); !ok { return fmt.Errorf("failed to wait for caches to sync") diff --git a/runtime/kubernetes/container.go b/runtime/kubernetes/container.go index 2aae8a41..9aa92c07 100644 --- a/runtime/kubernetes/container.go +++ b/runtime/kubernetes/container.go @@ -316,6 +316,7 @@ func (c *client) TailContainer(ctx context.Context, ctn *pipeline.Container) (io func (c *client) WaitContainer(ctx context.Context, ctn *pipeline.Container) error { c.Logger.Tracef("waiting for container %s", ctn.ID) + // get the containerTracker for this container tracker, ok := c.PodTracker.Containers[ctn.ID] if !ok { return fmt.Errorf("containerTracker is missing for %s", ctn.ID) @@ -339,6 +340,7 @@ func (p podTracker) inspectContainerStatuses(pod *v1.Pod) { // iterate through each container in the pod for _, cst := range pod.Status.ContainerStatuses { + // get the containerTracker for this container tracker, ok := p.Containers[cst.Name] if !ok { // unknown container diff --git a/runtime/kubernetes/pod_tracker.go b/runtime/kubernetes/pod_tracker.go index 22add10d..321bafeb 100644 --- a/runtime/kubernetes/pod_tracker.go +++ b/runtime/kubernetes/pod_tracker.go @@ -167,6 +167,7 @@ func NewPodTracker(log *logrus.Entry, clientset kubernetes.Interface, pod *v1.Po } } + // initialize podTracker tracker := podTracker{ Logger: log, TrackedPod: trackedPod, @@ -177,7 +178,8 @@ func NewPodTracker(log *logrus.Entry, clientset kubernetes.Interface, pod *v1.Po Containers: containers, } - tracker.podInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{ + // register event handler funcs in podInformer + podInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{ AddFunc: tracker.HandlePodAdd, UpdateFunc: tracker.HandlePodUpdate, DeleteFunc: tracker.HandlePodDelete,