Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved error messaging for Nonexistent namespaces,builds and buildr… #278

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading