Skip to content

Commit

Permalink
list-operands: include namespace in error message when package is not…
Browse files Browse the repository at this point in the history
… found (#88)

Signed-off-by: Joe Lanford <[email protected]>
  • Loading branch information
joelanford authored Nov 16, 2023
1 parent 07b1999 commit 019ce08
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
21 changes: 10 additions & 11 deletions pkg/action/operator_list_operands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/action/operator_list_operands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 019ce08

Please sign in to comment.