forked from operator-framework/kubectl-operator
/
operator_list_available.go
95 lines (81 loc) · 2.21 KB
/
operator_list_available.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package action
import (
"context"
"fmt"
"strings"
v1 "github.com/operator-framework/operator-lifecycle-manager/pkg/package-server/apis/operators/v1"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type OperatorListAvailable struct {
config *Configuration
Catalog NamespacedName
Package string
}
func NewOperatorListAvailable(cfg *Configuration) *OperatorListAvailable {
return &OperatorListAvailable{
config: cfg,
}
}
func (l *OperatorListAvailable) Run(ctx context.Context) ([]v1.PackageManifest, error) {
if l.Package != "" {
pm, err := l.getPackageManifestByName(ctx, l.Package)
if err != nil {
return nil, err
}
return []v1.PackageManifest{*pm}, nil
}
return l.getAllPackageManifests(ctx)
}
func (l *OperatorListAvailable) getPackageManifestByName(ctx context.Context, packageName string) (*v1.PackageManifest, error) {
pm := v1.PackageManifest{}
pmKey := types.NamespacedName{
Namespace: l.config.Namespace,
Name: packageName,
}
if err := l.config.Client.Get(ctx, pmKey, &pm); err != nil {
return nil, err
}
return &pm, nil
}
func (l *OperatorListAvailable) getAllPackageManifests(ctx context.Context) ([]v1.PackageManifest, error) {
labelSelector := client.MatchingLabels{}
if l.Catalog.Name != "" {
labelSelector["catalog"] = l.Catalog.Name
}
if l.Catalog.Namespace != "" {
labelSelector["catalog-namespace"] = l.Catalog.Namespace
}
pms := v1.PackageManifestList{}
if err := l.config.Client.List(ctx, &pms, labelSelector); err != nil {
return nil, err
}
return pms.Items, nil
}
func (l *OperatorListAvailable) BindFlags(fs *pflag.FlagSet) {
fs.VarP(&l.Catalog, "catalog", "c", "catalog to query (default: search all cluster catalogs)")
}
type NamespacedName struct {
types.NamespacedName
}
func (f *NamespacedName) Set(str string) error {
split := strings.Split(str, "/")
switch len(split) {
case 0:
case 1:
f.Name = split[0]
case 2:
f.Namespace = split[0]
f.Name = split[1]
default:
return fmt.Errorf("invalid namespaced name value %q", str)
}
return nil
}
func (f NamespacedName) String() string {
return f.NamespacedName.String()
}
func (f NamespacedName) Type() string {
return "NamespacedNameValue"
}