Skip to content

Commit

Permalink
fix: remove cmd; just export library
Browse files Browse the repository at this point in the history
  • Loading branch information
exdx committed Apr 7, 2021
1 parent d004c0a commit 5abae9e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 125 deletions.
76 changes: 0 additions & 76 deletions internal/cmd/operator_list_custom_resources.go

This file was deleted.

35 changes: 0 additions & 35 deletions internal/pkg/action/operator_list_custom_resources.go

This file was deleted.

55 changes: 41 additions & 14 deletions pkg/action/custom_resource_lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,49 @@ package action
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"

internal "github.com/operator-framework/kubectl-operator/internal/pkg/action" // TODO move this file here

"github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/operator-framework/api/pkg/operators/v2alpha1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// OperatorListOperands knows how to find and list custom resources given a package name and namespace.
type OperatorListOperands struct {
config *internal.Configuration
PackageName string
}

func NewOperatorListOperands(cfg *internal.Configuration) *OperatorListOperands {
return &OperatorListOperands{
config: cfg,
}
}

func (o *OperatorListOperands) Run(ctx context.Context) (*unstructured.UnstructuredList, error) {
opKey := types.NamespacedName{
Name: fmt.Sprintf("%s.%s", o.PackageName, o.config.Namespace),
}

result, err := o.listAll(ctx, opKey)
if err != nil {
return nil, err
}

return result, nil
}

// FindOperator finds an operator object on-cluster provided a package and namespace.
func FindOperator(ctx context.Context, client client.Client, key types.NamespacedName) (*v2alpha1.Operator, error) {
func (o *OperatorListOperands) findOperator(ctx context.Context, key types.NamespacedName) (*v2alpha1.Operator, error) {
operator := v2alpha1.Operator{}

err := client.Get(ctx, key, &operator)
err := o.config.Client.Get(ctx, key, &operator)
if err != nil {
if k8serrors.IsNotFound(err) {
return nil, fmt.Errorf("package %s not found", key.Name)
Expand All @@ -30,7 +57,7 @@ func FindOperator(ctx context.Context, client client.Client, key types.Namespace

// Unzip finds the CSV referenced by the provided operator and then inspects the spec.customresourcedefinitions.owned
// section of the CSV to return a list of APIs that are owned by the CSV.
func Unzip(ctx context.Context, client client.Client, operator *v2alpha1.Operator) ([]v1alpha1.CRDDescription, error) {
func (o *OperatorListOperands) unzip(ctx context.Context, operator *v2alpha1.Operator) ([]v1alpha1.CRDDescription, error) {
csv := v1alpha1.ClusterServiceVersion{}
csvKey := types.NamespacedName{}

Expand All @@ -49,7 +76,7 @@ func Unzip(ctx context.Context, client client.Client, operator *v2alpha1.Operato
return nil, fmt.Errorf("could not find underlying CSV for operator %s", operator.Name)
}

err := client.Get(ctx, csvKey, &csv)
err := o.config.Client.Get(ctx, csvKey, &csv)
if err != nil {
return nil, fmt.Errorf("could not get %s CSV on cluster: %s", csvKey.String(), err)
}
Expand All @@ -64,15 +91,15 @@ func Unzip(ctx context.Context, client client.Client, operator *v2alpha1.Operato

// List takes in a CRD description and finds the associated CRs on-cluster.
// List can return a potentially unbounded list that callers may need to paginate.
func List(ctx context.Context, crClient client.Client, crdDesc v1alpha1.CRDDescription, namespace string) (*unstructured.UnstructuredList, error) {
func (o *OperatorListOperands) list(ctx context.Context, crdDesc v1alpha1.CRDDescription, namespace string) (*unstructured.UnstructuredList, error) {
result := &unstructured.UnstructuredList{}

// find CRD on-cluster to determine CRD scope (not included in description)
crd := apiextensionsv1.CustomResourceDefinition{}
crdKey := types.NamespacedName{
Name: crdDesc.Name,
}
err := crClient.Get(ctx, crdKey, &crd)
err := o.config.Client.Get(ctx, crdKey, &crd)
if err != nil {
return nil, nil
}
Expand All @@ -87,7 +114,7 @@ func List(ctx context.Context, crClient client.Client, crdDesc v1alpha1.CRDDescr
Kind: crdDesc.Kind,
}
result.SetGroupVersionKind(gvk)
if err := crClient.List(ctx, &result); err != nil {
if err := o.config.Client.List(ctx, &result); err != nil {
return nil, err
}
return &result, nil
Expand All @@ -103,7 +130,7 @@ func List(ctx context.Context, crClient client.Client, crdDesc v1alpha1.CRDDescr
result.SetGroupVersionKind(gvk)

options := client.ListOptions{Namespace: namespace}
if err := crClient.List(ctx, &result, &options); err != nil {
if err := o.config.Client.List(ctx, &result, &options); err != nil {
return nil, err
}
return &result, nil
Expand All @@ -113,20 +140,20 @@ func List(ctx context.Context, crClient client.Client, crdDesc v1alpha1.CRDDescr
}

// ListAll wraps the above functions to provide a convenient command to go from package/namespace to custom resources.
func ListAll(ctx context.Context, client client.Client, opKey types.NamespacedName) (*unstructured.UnstructuredList, error) {
operator, err := FindOperator(ctx, client, opKey)
func (o *OperatorListOperands) listAll(ctx context.Context, opKey types.NamespacedName) (*unstructured.UnstructuredList, error) {
operator, err := o.findOperator(ctx, opKey)
if err != nil {
return nil, err
}

crdDescs, err := Unzip(ctx, client, operator)
crdDescs, err := o.unzip(ctx, operator)
if err != nil {
return nil, err
}

var result unstructured.UnstructuredList
for _, crd := range crdDescs {
list, err := List(ctx, client, crd, opKey.Namespace)
list, err := o.list(ctx, crd, opKey.Namespace)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 5abae9e

Please sign in to comment.