From 019ce08931fb634fa15e5ad68d1d74e2c318d2be Mon Sep 17 00:00:00 2001 From: Joe Lanford Date: Thu, 16 Nov 2023 16:15:26 -0500 Subject: [PATCH] list-operands: include namespace in error message when package is not found (#88) Signed-off-by: Joe Lanford --- pkg/action/operator_list_operands.go | 21 ++++++++++----------- pkg/action/operator_list_operands_test.go | 2 +- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/pkg/action/operator_list_operands.go b/pkg/action/operator_list_operands.go index 38d5cb4..a1194fd 100644 --- a/pkg/action/operator_list_operands.go +++ b/pkg/action/operator_list_operands.go @@ -27,11 +27,7 @@ func NewOperatorListOperands(cfg *Configuration) *OperatorListOperands { } func (o *OperatorListOperands) Run(ctx context.Context, packageName string) (*unstructured.UnstructuredList, error) { - opKey := types.NamespacedName{ - Name: fmt.Sprintf("%s.%s", packageName, o.config.Namespace), - } - - result, err := o.listAll(ctx, opKey) + result, err := o.listAll(ctx, packageName) if err != nil { return nil, err } @@ -40,13 +36,16 @@ func (o *OperatorListOperands) Run(ctx context.Context, packageName string) (*un } // FindOperator finds an operator object on-cluster provided a package and namespace. -func (o *OperatorListOperands) findOperator(ctx context.Context, key types.NamespacedName) (*v1.Operator, error) { - operator := v1.Operator{} +func (o *OperatorListOperands) findOperator(ctx context.Context, packageName string) (*v1.Operator, error) { + opKey := types.NamespacedName{ + Name: fmt.Sprintf("%s.%s", packageName, o.config.Namespace), + } - err := o.config.Client.Get(ctx, key, &operator) + operator := v1.Operator{} + err := o.config.Client.Get(ctx, opKey, &operator) if err != nil { if k8serrors.IsNotFound(err) { - return nil, fmt.Errorf("package %s not found", key.Name) + return nil, fmt.Errorf("package %q not found in namespace %q", packageName, o.config.Namespace) } return nil, err } @@ -132,8 +131,8 @@ func (o *OperatorListOperands) list(ctx context.Context, crdDesc v1alpha1.CRDDes } // ListAll wraps the above functions to provide a convenient command to go from package/namespace to custom resources. -func (o *OperatorListOperands) listAll(ctx context.Context, opKey types.NamespacedName) (*unstructured.UnstructuredList, error) { - operator, err := o.findOperator(ctx, opKey) +func (o *OperatorListOperands) listAll(ctx context.Context, packageName string) (*unstructured.UnstructuredList, error) { + operator, err := o.findOperator(ctx, packageName) if err != nil { return nil, err } diff --git a/pkg/action/operator_list_operands_test.go b/pkg/action/operator_list_operands_test.go index 0a01606..6f9f1a2 100644 --- a/pkg/action/operator_list_operands_test.go +++ b/pkg/action/operator_list_operands_test.go @@ -131,7 +131,7 @@ var _ = Describe("OperatorListOperands", func() { It("should fail due to missing operator", func() { lister := action.NewOperatorListOperands(&cfg) _, err := lister.Run(context.TODO(), "missing") - Expect(err.Error()).To(ContainSubstring("package missing.etcd-namespace not found")) + Expect(err.Error()).To(ContainSubstring(`package "missing" not found in namespace "etcd-namespace"`)) }) It("should fail due to missing operator components", func() {