Skip to content

Commit

Permalink
Merge pull request #278 from Adarsh-jaiss/feat/244
Browse files Browse the repository at this point in the history
improved error messaging for Nonexistent namespaces,builds and buildr…
  • Loading branch information
openshift-merge-bot[bot] authored Sep 24, 2024
2 parents 3f6e62a + c6b61e8 commit 5ecd5e9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
19 changes: 19 additions & 0 deletions pkg/shp/cmd/build/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/shipwright-io/cli/pkg/shp/params"
"github.com/spf13/cobra"

k8serrors "k8s.io/apimachinery/pkg/api/errors" // Import the k8serrors package
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
)
Expand Down Expand Up @@ -63,9 +64,27 @@ func (c *ListCommand) Run(params *params.Params, io *genericclioptions.IOStreams
if err != nil {
return err
}

k8sclient, err := params.ClientSet()
if err != nil {
return fmt.Errorf("failed to get k8s client: %w", err)
}
_, err = k8sclient.CoreV1().Namespaces().Get(c.cmd.Context(), params.Namespace(), metav1.GetOptions{})
if err != nil {
if k8serrors.IsNotFound(err) {
fmt.Fprintf(io.Out, "Namespace '%s' not found. Please ensure that the namespace exists and try again.\n", params.Namespace())
return nil
}
return err
}

if buildList, err = clientset.ShipwrightV1alpha1().Builds(params.Namespace()).List(c.cmd.Context(), metav1.ListOptions{}); err != nil {
return err
}
if len(buildList.Items) == 0 {
fmt.Fprintf(io.Out, "No builds found in namespace '%s'. Please create a build or verify the namespace.\n", params.Namespace())
return nil
}

if !c.noHeader {
fmt.Fprintln(writer, columnNames)
Expand Down
20 changes: 19 additions & 1 deletion pkg/shp/cmd/buildrun/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/spf13/cobra"
k8serrors "k8s.io/apimachinery/pkg/api/errors"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/duration"
Expand Down Expand Up @@ -54,7 +55,7 @@ func (c *ListCommand) Validate() error {
}

// Run executes list sub-command logic
func (c *ListCommand) Run(params *params.Params, _ *genericclioptions.IOStreams) error {
func (c *ListCommand) Run(params *params.Params, io *genericclioptions.IOStreams) error {
// TODO: Support multiple output formats here, not only tabwriter
// find out more in kubectl libraries and use them

Expand All @@ -67,10 +68,27 @@ func (c *ListCommand) Run(params *params.Params, _ *genericclioptions.IOStreams)
return err
}

k8sclient, err := params.ClientSet()
if err != nil {
return fmt.Errorf("failed to get k8s client: %w", err)
}
_, err = k8sclient.CoreV1().Namespaces().Get(c.cmd.Context(), params.Namespace(), metav1.GetOptions{})
if err != nil {
if k8serrors.IsNotFound(err) {
fmt.Fprintf(io.Out, "Namespace '%s' not found. Please ensure that the namespace exists and try again.\n", params.Namespace())
return nil
}
return err
}

var brs *buildv1alpha1.BuildRunList
if brs, err = clientset.ShipwrightV1alpha1().BuildRuns(params.Namespace()).List(c.cmd.Context(), metav1.ListOptions{}); err != nil {
return err
}
if len(brs.Items) == 0 {
fmt.Fprintf(io.Out, "No buildruns found in namespace '%s'. Please create a buildrun or verify the namespace.\n", params.Namespace())
return nil
}

if !c.noHeader {
fmt.Fprintln(writer, columnNames)
Expand Down

0 comments on commit 5ecd5e9

Please sign in to comment.