Skip to content

Commit

Permalink
initial wiring of -F/--follow flag, remnants of tkn based prototype; …
Browse files Browse the repository at this point in the history
…minor crud/resources/logs updates
  • Loading branch information
gabemontero committed Jun 22, 2021
1 parent c594997 commit 6fdb5d0
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 7 deletions.
59 changes: 56 additions & 3 deletions pkg/shp/cmd/build/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package build
import (
"errors"
"fmt"

buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
"github.com/shipwright-io/cli/pkg/shp/cmd/pod"
"github.com/shipwright-io/cli/pkg/shp/cmd/runner"
"github.com/shipwright-io/cli/pkg/shp/cmd/taskrun"
"github.com/shipwright-io/cli/pkg/shp/flags"
"github.com/shipwright-io/cli/pkg/shp/params"
"github.com/shipwright-io/cli/pkg/shp/resource"
"github.com/spf13/cobra"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
)
Expand All @@ -21,6 +23,7 @@ type RunCommand struct {

buildName string // build name
buildRunSpec *buildv1alpha1.BuildRunSpec // stores command-line flags
Follow bool
}

const buildRunLongDesc = `
Expand Down Expand Up @@ -70,8 +73,56 @@ func (r *RunCommand) Run(params *params.Params, ioStreams *genericclioptions.IOS
if err := buildRunResource.Create(r.cmd.Context(), "", br); err != nil {
return err
}
fmt.Fprintf(ioStreams.Out, "BuildRun created %q for build %q\n", br.GetName(), r.buildName)

if !r.Follow {
fmt.Fprintf(ioStreams.Out, "BuildRun created %q for build %q\n", br.GetName(), r.buildName)
return nil
}

br, err := waitForBuildRunToHaveTaskRun(r.cmd.Context(), br.Name, buildRunResource, ioStreams)
if err != nil {
return err
}
if br == nil {
// not expected, but sanity check to avoid panic
return fmt.Errorf("build run watch function exitted unexpectedly")
}

if br.ObjectMeta.DeletionTimestamp != nil {
return fmt.Errorf("build run %s was deleted before it terminated", br.Name)
}

if br.Status.LatestTaskRunRef == nil {
return fmt.Errorf("build run %s terminated before task run ref was set, inspect build run status for details", br.Name)
}

taskRunResource := resource.GetTaskRunResource(params)
taskRunName := *br.Status.LatestTaskRunRef

tr, err := taskrun.WaitForTaskRunToHavePod(r.cmd.Context(), taskRunName, taskRunResource, ioStreams)
if err != nil {
return err
}
if tr == nil {
return fmt.Errorf("task run watch function exitted unexpectedly")
}
if tr.ObjectMeta.DeletionTimestamp != nil {
return fmt.Errorf("task run %s was deleted before it terminated", tr.Name)
}
podName := tr.Status.PodName

kube, err := params.ClientSet()
if err != nil {
return fmt.Errorf("could not build k8s client: %s", err.Error())
}
pod.Tail(podName, taskRunName, br.Namespace, kube, ioStreams)
return nil

/*tparams := &tkncli.TektonParams{}
tparams.SetNamespace(br.Namespace)
logOpts := getTKNLogOpts(tparams, ioStreams, *br.status.LatestTaskRunRef)
return Tail(logOpts)*/
}

// runCmd instantiate the "build run" sub-command using common BuildRun flags.
Expand All @@ -81,8 +132,10 @@ func runCmd() runner.SubCommand {
Short: "Start a build specified by 'name'",
Long: buildRunLongDesc,
}
return &RunCommand{
runCommand := &RunCommand{
cmd: cmd,
buildRunSpec: flags.BuildRunSpecFromFlags(cmd.Flags()),
}
cmd.Flags().BoolVarP(&runCommand.Follow, "follow", "F", runCommand.Follow, "Start a build and watch its log until it completes or fails.")
return runCommand
}
5 changes: 2 additions & 3 deletions pkg/shp/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package params
import (
"github.com/pkg/errors"
"github.com/spf13/pflag"

"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
Expand All @@ -13,7 +12,7 @@ import (
// including configured dynamic client and global flags
type Params struct {
client dynamic.Interface
clientset *kubernetes.Clientset
clientset kubernetes.Interface

configFlags *genericclioptions.ConfigFlags
namespace string
Expand Down Expand Up @@ -51,7 +50,7 @@ func (p *Params) Client() (dynamic.Interface, error) {
return p.client, nil
}

func (p *Params) ClientSet() (*kubernetes.Clientset, error) {
func (p *Params) ClientSet() (kubernetes.Interface, error) {
if p.clientset != nil {
return p.clientset, nil
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/shp/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"

"github.com/shipwright-io/cli/pkg/shp/params"
Expand Down Expand Up @@ -47,6 +48,14 @@ func (r *Resource) Create(ctx context.Context, name string, obj interface{}) err
return util.CreateObject(ctx, ri, name, r.gv.WithKind(r.kind), obj)
}

func (r *Resource) Update(ctx context.Context, name string, obj interface{}) error {
ri, err := r.getResourceInterface()
if err != nil {
return err
}
return util.UpdateObject(ctx, ri, name, r.gv.WithKind(r.kind), obj)
}

// Delete deletes the object identified by name
func (r *Resource) Delete(ctx context.Context, name string) error {
ri, err := r.getResourceInterface()
Expand Down Expand Up @@ -82,6 +91,14 @@ func (r *Resource) Get(ctx context.Context, name string, result interface{}) err
return util.GetObject(ctx, ri, name, result)
}

func (r *Resource) Watch(ctx context.Context, opts v1.ListOptions) (watch.Interface, error) {
ri, err := r.getResourceInterface()
if err != nil {
return nil, err
}
return ri.Watch(ctx, opts)
}

func newResource(p *params.Params, gv schema.GroupVersion, kind, resource string) *Resource {
r := &Resource{
gv: gv,
Expand Down
13 changes: 13 additions & 0 deletions pkg/shp/util/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ func CreateObject(ctx context.Context, resource dynamic.ResourceInterface, name
return fromUnstructured(result.Object, obj)
}

func UpdateObject(ctx context.Context, resource dynamic.ResourceInterface, name string, gvk schema.GroupVersionKind, obj interface{}) error {
u, err := toUnstructured(name, gvk, obj)
if err != nil {
return err
}

result, err := resource.Update(ctx, u, v1.UpdateOptions{})
if err != nil {
return err
}
return fromUnstructured(result.Object, obj)
}

// GetObject returns the object using dynamic client
func GetObject(ctx context.Context, resource dynamic.ResourceInterface, name string, obj interface{}) error {
u, err := resource.Get(context.TODO(), name, v1.GetOptions{})
Expand Down
2 changes: 1 addition & 1 deletion pkg/shp/util/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// GetPodLogs returns log output of the k8s container provided by pod and name
func GetPodLogs(ctx context.Context, client *kubernetes.Clientset, pod corev1.Pod, container string) (string, error) {
func GetPodLogs(ctx context.Context, client kubernetes.Interface, pod corev1.Pod, container string) (string, error) {
podLogOpts := corev1.PodLogOptions{
Container: container,
}
Expand Down

0 comments on commit 6fdb5d0

Please sign in to comment.