Skip to content

Commit

Permalink
Merge pull request shipwright-io#36 from gabemontero/no-dyn-client
Browse files Browse the repository at this point in the history
remove use of dynamic client; add tests with more reliable static clients
  • Loading branch information
openshift-merge-robot authored Jul 28, 2021
2 parents 88f1035 + c442fe7 commit c8174b4
Show file tree
Hide file tree
Showing 30 changed files with 956 additions and 453 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.15
require (
github.com/mailru/easyjson v0.7.1 // indirect
github.com/onsi/gomega v1.10.3
github.com/pkg/errors v0.9.1
github.com/shipwright-io/build v0.5.2-0.20210715083206-5d8fb411a1eb
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
Expand Down
10 changes: 7 additions & 3 deletions pkg/shp/cmd/build/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"github.com/shipwright-io/cli/pkg/shp/cmd/runner"
"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 Down Expand Up @@ -55,8 +56,11 @@ func (c *CreateCommand) Run(params *params.Params, io *genericclioptions.IOStrea
b := &buildv1alpha1.Build{Spec: *c.buildSpec}
flags.SanitizeBuildSpec(&b.Spec)

buildResource := resource.GetBuildResource(params)
if err := buildResource.Create(c.cmd.Context(), c.name, b); err != nil {
clientset, err := params.ShipwrightClientSet()
if err != nil {
return err
}
if _, err := clientset.ShipwrightV1alpha1().Builds(params.Namespace()).Create(c.cmd.Context(), b, metav1.CreateOptions{}); err != nil {
return err
}
fmt.Fprintf(io.Out, "Created build %q\n", c.name)
Expand Down
17 changes: 8 additions & 9 deletions pkg/shp/cmd/build/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"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"
)

// DeleteCommand contains data provided by user to the delete subcommand
Expand Down Expand Up @@ -55,24 +54,24 @@ func (c *DeleteCommand) Validate() error {

// Run contains main logic of delete subcommand
func (c *DeleteCommand) Run(params *params.Params, io *genericclioptions.IOStreams) error {
br := resource.GetBuildResource(params)

if err := br.Delete(c.cmd.Context(), c.name); err != nil {
clientset, err := params.ShipwrightClientSet()
if err != nil {
return err
}
if err := clientset.ShipwrightV1alpha1().Builds(params.Namespace()).Delete(c.Cmd().Context(), c.name, v1.DeleteOptions{}); err != nil {
return err
}

if c.deleteRuns {
brr := resource.GetBuildRunResource(params)

var brList buildv1alpha1.BuildRunList
if err := brr.ListWithOptions(c.cmd.Context(), &brList, v1.ListOptions{
var brList *buildv1alpha1.BuildRunList
if brList, err = clientset.ShipwrightV1alpha1().BuildRuns(params.Namespace()).List(c.cmd.Context(), v1.ListOptions{
LabelSelector: fmt.Sprintf("%v/name=%v", buildv1alpha1.BuildDomain, c.name),
}); err != nil {
return err
}

for _, buildrun := range brList.Items {
if err := brr.Delete(c.cmd.Context(), buildrun.Name); err != nil {
if err := clientset.ShipwrightV1alpha1().BuildRuns(params.Namespace()).Delete(c.cmd.Context(), buildrun.Name, v1.DeleteOptions{}); err != nil {
fmt.Fprintf(io.ErrOut, "Error deleting BuildRun %q: %v\n", buildrun.Name, err)
}
}
Expand Down
17 changes: 9 additions & 8 deletions pkg/shp/cmd/build/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (
"text/tabwriter"

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/spf13/cobra"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"

"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"
)

// ListCommand struct contains user input to the List subcommand of Build
Expand Down Expand Up @@ -59,10 +58,12 @@ func (c *ListCommand) Run(params *params.Params, io *genericclioptions.IOStreams
columnNames := "NAME\tOUTPUT\tSTATUS"
columnTemplate := "%s\t%s\t%s\n"

var buildList buildv1alpha1.BuildList
br := resource.GetBuildResource(params)

if err := br.List(c.cmd.Context(), &buildList); err != nil {
var buildList *buildv1alpha1.BuildList
clientset, err := params.ShipwrightClientSet()
if err != nil {
return err
}
if buildList, err = clientset.ShipwrightV1alpha1().Builds(params.Namespace()).List(c.cmd.Context(), metav1.ListOptions{}); err != nil {
return err
}

Expand Down
12 changes: 7 additions & 5 deletions pkg/shp/cmd/build/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/shipwright-io/cli/pkg/shp/flags"
"github.com/shipwright-io/cli/pkg/shp/params"
"github.com/shipwright-io/cli/pkg/shp/reactor"
"github.com/shipwright-io/cli/pkg/shp/resource"
"github.com/shipwright-io/cli/pkg/shp/tail"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -151,8 +150,11 @@ func (r *RunCommand) Run(params *params.Params, ioStreams *genericclioptions.IOS
}
flags.SanitizeBuildRunSpec(&br.Spec)

buildRunResource := resource.GetBuildRunResource(params)
err := buildRunResource.Create(r.cmd.Context(), "", br)
clientset, err := params.ShipwrightClientSet()
if err != nil {
return err
}
br, err = clientset.ShipwrightV1alpha1().BuildRuns(params.Namespace()).Create(r.cmd.Context(), br, metav1.CreateOptions{})
if err != nil {
return err
}
Expand All @@ -167,7 +169,7 @@ func (r *RunCommand) Run(params *params.Params, ioStreams *genericclioptions.IOS
return err
}

clientset, err := params.ClientSet()
kclientset, err := params.ClientSet()
if err != nil {
return err
}
Expand All @@ -180,7 +182,7 @@ func (r *RunCommand) Run(params *params.Params, ioStreams *genericclioptions.IOS
r.buildName,
br.GetName(),
)}
r.pw, err = reactor.NewPodWatcher(r.Cmd().Context(), clientset, listOpts, params.Namespace())
r.pw, err = reactor.NewPodWatcher(r.Cmd().Context(), kclientset, listOpts, params.Namespace())
if err != nil {
return err
}
Expand Down
33 changes: 24 additions & 9 deletions pkg/shp/cmd/buildrun/cancel.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package buildrun

import (
"encoding/json"
"fmt"

"github.com/spf13/cobra"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"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
Expand Down Expand Up @@ -50,20 +51,34 @@ func (c *CancelCommand) Validate() error {

// Run executes cancel sub-command logic
func (c *CancelCommand) Run(params *params.Params, ioStreams *genericclioptions.IOStreams) error {
brr := resource.GetBuildRunResource(params)
clientset, err := params.ShipwrightClientSet()
if err != nil {
return err
}

br := &buildv1alpha1.BuildRun{}
if err := brr.Get(c.cmd.Context(), c.name, br); err != nil {
if br, err = clientset.ShipwrightV1alpha1().BuildRuns(params.Namespace()).Get(c.cmd.Context(), c.name, metav1.GetOptions{}); 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 {
if br.IsDone() {
return fmt.Errorf("failed to cancel BuildRun %s: execution has already finished", 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 {
type patchStringValue struct {
Op string `json:"op"`
Path string `json:"path"`
Value string `json:"value"`
}
payload := []patchStringValue{{
Op: "replace",
Path: "/spec/state",
Value: buildv1alpha1.BuildRunStateCancel,
}}
var data []byte
if data, err = json.Marshal(payload); err != nil {
return err
}
if _, err = clientset.ShipwrightV1alpha1().BuildRuns(params.Namespace()).Patch(c.Cmd().Context(), c.name, types.JSONPatchType, data, metav1.PatchOptions{}); err != nil {
return err
}

Expand Down
125 changes: 125 additions & 0 deletions pkg/shp/cmd/buildrun/cancel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package buildrun

import (
"context"
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"

"github.com/spf13/cobra"

"github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
"github.com/shipwright-io/build/pkg/client/clientset/versioned/fake"
"github.com/shipwright-io/cli/pkg/shp/params"
)

func TestCancelBuildRun(t *testing.T) {
tests := map[string]struct {
br *v1alpha1.BuildRun
expectCancelSet bool
expectErr bool
}{
"does-not-exist": {
br: nil,
expectErr: true,
},
"completed": {
br: &v1alpha1.BuildRun{
ObjectMeta: metav1.ObjectMeta{
Name: "completed",
Namespace: metav1.NamespaceDefault,
},
Status: v1alpha1.BuildRunStatus{
Conditions: v1alpha1.Conditions{
{
Type: v1alpha1.Succeeded,
Status: corev1.ConditionTrue,
},
},
}},
expectErr: true,
},
"failed": {
br: &v1alpha1.BuildRun{
ObjectMeta: metav1.ObjectMeta{
Name: "failed",
Namespace: metav1.NamespaceDefault,
},
Status: v1alpha1.BuildRunStatus{
Conditions: v1alpha1.Conditions{
{
Type: v1alpha1.Succeeded,
Status: corev1.ConditionFalse,
},
},
}},
expectErr: true,
},
"condition-missing": {
br: &v1alpha1.BuildRun{
ObjectMeta: metav1.ObjectMeta{
Name: "failed",
Namespace: metav1.NamespaceDefault,
},
},
expectCancelSet: true,
},
"in-progress": {
br: &v1alpha1.BuildRun{
ObjectMeta: metav1.ObjectMeta{
Name: "failed",
Namespace: metav1.NamespaceDefault,
},
Status: v1alpha1.BuildRunStatus{
Conditions: v1alpha1.Conditions{
{
Type: v1alpha1.Succeeded,
Status: corev1.ConditionUnknown,
},
},
}},
expectCancelSet: true,
},
}
for testName, test := range tests {
t.Logf("running %s with args %#v", testName, test)

cmd := CancelCommand{cmd: &cobra.Command{}}
var clientset *fake.Clientset
if test.br != nil {
cmd.name = test.br.Name
clientset = fake.NewSimpleClientset(test.br)
} else {
clientset = fake.NewSimpleClientset()
}

// set up context
cmd.Cmd().ExecuteC()
param := params.NewParamsForTest(nil, clientset, nil, metav1.NamespaceDefault)

ioStreams, _, _, _ := genericclioptions.NewTestIOStreams()
err := cmd.Run(param, &ioStreams)

if err != nil && !test.expectErr {
t.Errorf("%s: did not expect err: %s", testName, err.Error())
}
if err == nil && test.expectErr {
t.Errorf("%s: did not get err when expected", testName)
}
if err != nil && test.expectErr {
continue
}

buildRun, _ := clientset.ShipwrightV1alpha1().BuildRuns(param.Namespace()).Get(context.Background(), test.br.Name, metav1.GetOptions{})

if test.expectCancelSet && !buildRun.IsCanceled() {
t.Errorf("%s: cancel not set", testName)
}

if !test.expectCancelSet && buildRun.IsCanceled() {
t.Errorf("%s: cancel set", testName)
}
}
}
10 changes: 7 additions & 3 deletions pkg/shp/cmd/buildrun/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"github.com/shipwright-io/cli/pkg/shp/cmd/runner"
"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 Down Expand Up @@ -56,8 +57,11 @@ func (c *CreateCommand) Run(params *params.Params, ioStreams *genericclioptions.
br := &buildv1alpha1.BuildRun{Spec: *c.buildRunSpec}
flags.SanitizeBuildRunSpec(&br.Spec)

buildRunResource := resource.GetBuildRunResource(params)
if err := buildRunResource.Create(c.cmd.Context(), c.name, br); err != nil {
clientset, err := params.ShipwrightClientSet()
if err != nil {
return err
}
if _, err = clientset.ShipwrightV1alpha1().BuildRuns(params.Namespace()).Create(c.cmd.Context(), br, metav1.CreateOptions{}); err != nil {
return err
}
fmt.Fprintf(ioStreams.Out, "BuildRun created %q for Build %q\n", c.name, br.Spec.BuildRef.Name)
Expand Down
9 changes: 6 additions & 3 deletions pkg/shp/cmd/buildrun/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (

"github.com/spf13/cobra"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"

"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"
)

// DeleteCommand contains data input from user for delete sub-command
Expand Down Expand Up @@ -48,9 +48,12 @@ func (c *DeleteCommand) Validate() error {

// Run executes delete sub-command logic
func (c *DeleteCommand) Run(params *params.Params, ioStreams *genericclioptions.IOStreams) error {
brr := resource.GetBuildRunResource(params)
clientset, err := params.ShipwrightClientSet()
if err != nil {
return err
}

if err := brr.Delete(c.cmd.Context(), c.name); err != nil {
if err = clientset.ShipwrightV1alpha1().BuildRuns(params.Namespace()).Delete(c.cmd.Context(), c.name, metav1.DeleteOptions{}); err != nil {
return err
}

Expand Down
Loading

0 comments on commit c8174b4

Please sign in to comment.