Skip to content

Commit

Permalink
add buildrun cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
gabemontero committed Jul 15, 2021
1 parent 2155203 commit 0264407
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/shp/cmd/buildrun/buildrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func Command(p *params.Params, ioStreams *genericclioptions.IOStreams) *cobra.Co
runner.NewRunner(p, ioStreams, logsCmd()).Cmd(),
runner.NewRunner(p, ioStreams, logsCmd()).Cmd(),
runner.NewRunner(p, ioStreams, createCmd()).Cmd(),
runner.NewRunner(p, ioStreams, cancelCmd()).Cmd(),
)
return command
}
74 changes: 74 additions & 0 deletions pkg/shp/cmd/buildrun/cancel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package buildrun

import (
"fmt"

"github.com/spf13/cobra"

corev1 "k8s.io/api/core/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"

buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
"github.com/shipwright-io/cli/pkg/shp/cmd/runner"
"github.com/shipwright-io/cli/pkg/shp/params"
"github.com/shipwright-io/cli/pkg/shp/resource"
)

// CancelCommand contains data input from user for delete sub-command
type CancelCommand struct {
cmd *cobra.Command

name string
}

func cancelCmd() runner.SubCommand {
return &CancelCommand{
cmd: &cobra.Command{
Use: "cancel <name>",
Short: "Cancel BuildRun",
Args: cobra.ExactArgs(1),
},
}
}

// Cmd returns cobra command object
func (c *CancelCommand) Cmd() *cobra.Command {
return c.cmd
}

// Complete fills in data provided by user
func (c *CancelCommand) Complete(params *params.Params, args []string) error {
c.name = args[0]

return nil
}

// Validate validates data input by user
func (c *CancelCommand) Validate() error {
return nil
}

// Run executes cancel sub-command logic
func (c *CancelCommand) Run(params *params.Params, ioStreams *genericclioptions.IOStreams) error {
brr := resource.GetBuildRunResource(params)

br := &buildv1alpha1.BuildRun{}
err := brr.Get(c.cmd.Context(), c.name, br)
if err != nil {
return fmt.Errorf("failed to retrieve BuildRun %s: %s", c.name, err.Error())
}
//TODO replace with br.IsDone() when that is available and vendored in
cond := br.Status.GetCondition(buildv1alpha1.Succeeded)
if cond != nil && cond.GetStatus() != corev1.ConditionUnknown {
return fmt.Errorf("failed to cancel BuildRun %s: BuildRun has already finished execution", c.name)
}

//TODO use constant when vendor in api changes
if err := brr.Patch(c.cmd.Context(), c.name, "replace", "/spec/state", "BuildRunCanceled"); err != nil {
return err
}

fmt.Fprintf(ioStreams.Out, "BuildRun cancelled '%v'\n", c.name)

return nil
}
9 changes: 9 additions & 0 deletions pkg/shp/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ func (r *Resource) Update(ctx context.Context, name string, obj interface{}) err
return util.UpdateObject(ctx, ri, name, r.gv.WithKind(r.kind), obj)
}

func (r *Resource) Patch(ctx context.Context, name, op, path, value string) error {
ri, err := r.getResourceInterface()
if err != nil {
return err
}

return util.PatchObject(ctx, ri, name, op, path, value)
}

// Delete deletes the object identified by name
func (r *Resource) Delete(ctx context.Context, name string) error {
ri, err := r.getResourceInterface()
Expand Down
19 changes: 19 additions & 0 deletions pkg/shp/util/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package util

import (
"context"
"encoding/json"

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

Expand Down Expand Up @@ -83,3 +85,20 @@ func ListObjectWithOptions(ctx context.Context, resource dynamic.ResourceInterfa

return fromUnstructured(u.UnstructuredContent(), result)
}

type patchStringValue struct {
Op string `json:"op"`
Path string `json:"path"`
Value string `json:"value"`
}

func PatchObject(ctx context.Context, resource dynamic.ResourceInterface, name, op, path, value string) error {
payload := []patchStringValue{{
Op: op,
Path: path,
Value: value,
}}
data, _ := json.Marshal(payload)
_, err := resource.Patch(ctx, name, types.JSONPatchType, data, v1.PatchOptions{})
return err
}

0 comments on commit 0264407

Please sign in to comment.