Skip to content

Commit

Permalink
wait for pod resource becoming available via the api before continuin…
Browse files Browse the repository at this point in the history
…g with the build process
  • Loading branch information
cppforlife committed Mar 26, 2019
1 parent 62174e1 commit d592f45
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 24 deletions.
51 changes: 42 additions & 9 deletions pkg/knctl/build/build_waiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@ import (
"github.com/knative/build/pkg/apis/build/v1alpha1"
buildclientset "github.com/knative/build/pkg/client/clientset/versioned"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
)

type BuildWaiter struct {
build *v1alpha1.Build
buildClient buildclientset.Interface
build *v1alpha1.Build
buildClient buildclientset.Interface
podsGetterClient typedcorev1.PodsGetter
}

func NewBuildWaiter(build *v1alpha1.Build, buildClient buildclientset.Interface) BuildWaiter {
return BuildWaiter{build, buildClient}
func NewBuildWaiter(
build *v1alpha1.Build,
buildClient buildclientset.Interface,
podsGetterClient typedcorev1.PodsGetter,
) BuildWaiter {
return BuildWaiter{build, buildClient, podsGetterClient}
}

func (w BuildWaiter) WaitForBuilderAssignment(cancelCh chan struct{}) (*v1alpha1.Build, error) {
Expand Down Expand Up @@ -85,24 +92,50 @@ func (w BuildWaiter) WaitForCompletion(cancelCh chan struct{}) (*v1alpha1.Build,
}
}

func (w BuildWaiter) WaitForClusterBuilderPodAssignment(cancelCh chan struct{}) (*v1alpha1.Build, error) {
func (w BuildWaiter) WaitForClusterBuilderPodAssignment(cancelCh chan struct{}) (*v1alpha1.Build, *corev1.Pod, error) {
var build *v1alpha1.Build

for {
// TODO infinite retry

build, err := w.buildClient.BuildV1alpha1().Builds(w.build.Namespace).Get(w.build.Name, metav1.GetOptions{})
var err error

build, err = w.buildClient.BuildV1alpha1().Builds(w.build.Namespace).Get(w.build.Name, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("Getting build while waiting for cluster build to assign pod: %s", err)
return nil, nil, fmt.Errorf("Getting build while waiting for cluster build to assign pod: %s", err)
}

if build.Status.Cluster != nil {
if len(build.Status.Cluster.Namespace) > 0 && len(build.Status.Cluster.PodName) > 0 {
return build, nil
break
}
}

select {
case <-cancelCh:
return build, nil
return build, nil, nil
default:
time.Sleep(1 * time.Second)
}
}

// Check if pod was initialized and is ready to be interacted via the API
for {
// TODO infinite retry
podsClient := w.podsGetterClient.Pods(build.Status.Cluster.Namespace)

pod, err := podsClient.Get(build.Status.Cluster.PodName, metav1.GetOptions{})
if err != nil {
if !errors.IsNotFound(err) {
return build, nil, fmt.Errorf("Getting assigned building pod: %s", err)
}
} else {
return build, pod, nil
}

select {
case <-cancelCh:
return build, pod, nil
default:
time.Sleep(1 * time.Second)
}
Expand Down
8 changes: 1 addition & 7 deletions pkg/knctl/build/cluster_builder_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/cppforlife/go-cli-ui/ui"
"github.com/cppforlife/knctl/pkg/knctl/logs"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
)

Expand All @@ -39,7 +38,7 @@ func NewClusterBuilderLogs(
}

func (l ClusterBuilderLogs) Tail(ui ui.UI, cancelCh chan struct{}) error { // TODO cancel
build, err := l.waiter.WaitForClusterBuilderPodAssignment(cancelCh)
build, pod, err := l.waiter.WaitForClusterBuilderPodAssignment(cancelCh)
if err != nil {
return fmt.Errorf("Waiting for build to be assigned a pod: %s", err)
}
Expand All @@ -50,11 +49,6 @@ func (l ClusterBuilderLogs) Tail(ui ui.UI, cancelCh chan struct{}) error { // TO

podsClient := l.podsGetterClient.Pods(build.Status.Cluster.Namespace)

pod, err := podsClient.Get(build.Status.Cluster.PodName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("Getting assigned building pod: %s", err)
}

statusWatcher := PodTerminalStatusWatcher{*pod, podsClient}
cancelPodTailCh := make(chan struct{})

Expand Down
8 changes: 1 addition & 7 deletions pkg/knctl/build/cluster_builder_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

"github.com/cppforlife/go-cli-ui/ui"
ctlkube "github.com/cppforlife/knctl/pkg/knctl/kube"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
Expand Down Expand Up @@ -57,7 +56,7 @@ func (s ClusterBuilderSource) Upload(ui ui.UI, cancelCh chan struct{}) error { /
ui.PrintLinef("[%s] Finished uploading source code...", time.Now().Format(time.RFC3339))
}()

build, err := s.waiter.WaitForClusterBuilderPodAssignment(cancelCh)
build, pod, err := s.waiter.WaitForClusterBuilderPodAssignment(cancelCh)
if err != nil {
return fmt.Errorf("Waiting for build to be assigned a pod: %s", err)
}
Expand All @@ -68,11 +67,6 @@ func (s ClusterBuilderSource) Upload(ui ui.UI, cancelCh chan struct{}) error { /

podsClient := s.coreClient.CoreV1().Pods(build.Status.Cluster.Namespace)

pod, err := podsClient.Get(build.Status.Cluster.PodName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("Getting assigned building pod: %s", err)
}

err = PodInitContainerRunningWatcher{*pod, podsClient, clusterBuilderCustomSourceStep}.Wait(cancelCh)
if err != nil {
return fmt.Errorf("Waiting for init container: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/knctl/build/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewFactory(
}

func (f Factory) New(build *v1alpha1.Build) Build {
waiter := NewBuildWaiter(build, f.buildClient)
waiter := NewBuildWaiter(build, f.buildClient, f.coreClient.CoreV1())
logs := NewLogs(waiter, f.coreClient.CoreV1())
sourceFactory := NewSourceFactory(waiter, f.coreClient, f.restConfig)
return NewBuild(waiter, logs, sourceFactory)
Expand Down

0 comments on commit d592f45

Please sign in to comment.