Skip to content

Commit 3fdb619

Browse files
committed
list catalog contents
Signed-off-by: Ankita Thomas <[email protected]>
1 parent 94e5778 commit 3fdb619

File tree

4,932 files changed

+1782991
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

4,932 files changed

+1782991
-2
lines changed

go.mod

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ require (
1919
k8s.io/api v0.32.2
2020
k8s.io/apiextensions-apiserver v0.32.2
2121
k8s.io/apimachinery v0.32.2
22+
k8s.io/cli-runtime v0.32.2
2223
k8s.io/client-go v0.32.2
2324
sigs.k8s.io/controller-runtime v0.20.2
2425
sigs.k8s.io/yaml v1.4.0
@@ -27,6 +28,7 @@ require (
2728
require (
2829
cel.dev/expr v0.18.0 // indirect
2930
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 // indirect
31+
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
3032
github.com/BurntSushi/toml v1.4.0 // indirect
3133
github.com/Microsoft/go-winio v0.6.2 // indirect
3234
github.com/Microsoft/hcsshim v0.12.9 // indirect
@@ -57,6 +59,10 @@ require (
5759
github.com/felixge/httpsnoop v1.0.4 // indirect
5860
github.com/fsnotify/fsnotify v1.8.0 // indirect
5961
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
62+
github.com/go-errors/errors v1.4.2 // indirect
63+
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
64+
github.com/go-git/go-billy/v5 v5.6.1 // indirect
65+
github.com/go-git/go-git/v5 v5.13.1 // indirect
6066
github.com/go-logr/logr v1.4.2 // indirect
6167
github.com/go-logr/stdr v1.2.2 // indirect
6268
github.com/go-openapi/jsonpointer v0.21.0 // indirect
@@ -65,34 +71,47 @@ require (
6571
github.com/gogo/protobuf v1.3.2 // indirect
6672
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
6773
github.com/golang/protobuf v1.5.4 // indirect
74+
github.com/google/btree v1.1.3 // indirect
6875
github.com/google/cel-go v0.22.1 // indirect
6976
github.com/google/gnostic-models v0.6.9 // indirect
7077
github.com/google/go-cmp v0.7.0 // indirect
7178
github.com/google/gofuzz v1.2.0 // indirect
79+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
7280
github.com/google/uuid v1.6.0 // indirect
7381
github.com/gorilla/mux v1.8.1 // indirect
82+
github.com/gorilla/websocket v1.5.0 // indirect
83+
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
7484
github.com/h2non/filetype v1.1.3 // indirect
7585
github.com/h2non/go-is-svg v0.0.0-20160927212452-35e8c4b0612c // indirect
7686
github.com/inconshreveable/mousetrap v1.1.0 // indirect
87+
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
88+
github.com/joelanford/ignore v0.1.1 // indirect
7789
github.com/josharian/intern v1.0.0 // indirect
7890
github.com/json-iterator/go v1.1.12 // indirect
7991
github.com/klauspost/compress v1.18.0 // indirect
92+
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
8093
github.com/mailru/easyjson v0.9.0 // indirect
8194
github.com/moby/locker v1.0.1 // indirect
95+
github.com/moby/spdystream v0.5.0 // indirect
8296
github.com/moby/sys/capability v0.3.0 // indirect
8397
github.com/moby/sys/mountinfo v0.7.2 // indirect
8498
github.com/moby/sys/sequential v0.5.0 // indirect
8599
github.com/moby/sys/user v0.3.0 // indirect
86100
github.com/moby/sys/userns v0.1.0 // indirect
101+
github.com/moby/term v0.5.0 // indirect
87102
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
88103
github.com/modern-go/reflect2 v1.0.2 // indirect
104+
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
89105
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
106+
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
90107
github.com/nxadm/tail v1.4.8 // indirect
91108
github.com/opencontainers/go-digest v1.0.0 // indirect
92109
github.com/opencontainers/runtime-spec v1.2.0 // indirect
110+
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
93111
github.com/pkg/errors v0.9.1 // indirect
94112
github.com/stoewer/go-strcase v1.3.0 // indirect
95113
github.com/x448/float16 v0.8.4 // indirect
114+
github.com/xlab/treeprint v1.2.0 // indirect
96115
go.etcd.io/bbolt v1.3.11 // indirect
97116
go.opencensus.io v0.24.0 // indirect
98117
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
@@ -116,10 +135,13 @@ require (
116135
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
117136
gopkg.in/inf.v0 v0.9.1 // indirect
118137
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
138+
gopkg.in/warnings.v0 v0.1.2 // indirect
119139
gopkg.in/yaml.v3 v3.0.1 // indirect
120140
k8s.io/klog/v2 v2.130.1 // indirect
121141
k8s.io/kube-openapi v0.0.0-20241212222426-2c72e554b1e7 // indirect
122142
k8s.io/utils v0.0.0-20241210054802-24370beab758 // indirect
123143
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 // indirect
144+
sigs.k8s.io/kustomize/api v0.18.0 // indirect
145+
sigs.k8s.io/kustomize/kyaml v0.18.1 // indirect
124146
sigs.k8s.io/structured-merge-diff/v4 v4.5.0 // indirect
125147
)

go.sum

Lines changed: 53 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package olmv1
2+
3+
import (
4+
"os"
5+
6+
"github.com/operator-framework/kubectl-operator/internal/cmd/internal/log"
7+
v1action "github.com/operator-framework/kubectl-operator/internal/pkg/v1/action"
8+
"github.com/operator-framework/kubectl-operator/pkg/action"
9+
"github.com/spf13/cobra"
10+
"github.com/spf13/pflag"
11+
)
12+
13+
// NewCatalogInstalledGetCmd handles get commands in the form of:
14+
// catalog(s) [catalog_name] - this will either list all the installed operators
15+
// if no catalog_name has been provided or display the details of the specific
16+
// one otherwise
17+
func NewCatalogSearchCmd(cfg *action.Configuration) *cobra.Command {
18+
i := v1action.NewCatalogSearch(cfg)
19+
i.Logf = log.Printf
20+
21+
cmd := &cobra.Command{
22+
Use: "catalog",
23+
Aliases: []string{"catalogs"},
24+
Args: cobra.RangeArgs(0, 1),
25+
Short: "Search catalogs for installable operators matching parameters",
26+
Run: func(cmd *cobra.Command, args []string) {
27+
catalogContents, err := i.Run(cmd.Context())
28+
if err != nil {
29+
log.Fatalf("failed querying catalog(s): %v", err)
30+
}
31+
switch i.OutputFormat {
32+
case "", "table":
33+
printFormattedDeclCfg(os.Stdout, catalogContents, i.ListVersions)
34+
case "json":
35+
printDeclCfgJSON(os.Stdout, catalogContents)
36+
case "yaml":
37+
printDeclCfgYAML(os.Stdout, catalogContents)
38+
default:
39+
log.Fatalf("unsupported output format %s: allwed formats are (json|yaml|table)", i.OutputFormat)
40+
}
41+
},
42+
}
43+
bindCatalogSearchFlags(cmd.Flags(), i)
44+
45+
return cmd
46+
}
47+
48+
func bindCatalogSearchFlags(fs *pflag.FlagSet, i *v1action.CatalogSearch) {
49+
fs.StringVar(&i.Catalog, "catalog", "", "Catalog to search on. If not provided, all available catalogs are searched.")
50+
fs.StringToStringVarP(&i.Selector, "selector", "l", map[string]string{}, "Selector (label query) to filter catalogs on, supports '=', '==', and '!='")
51+
fs.StringVarP(&i.OutputFormat, "output", "o", "", "output format. One of: (yaml|json)")
52+
fs.BoolVar(&i.ListVersions, "list-versions", false, "List all versions available for each package")
53+
fs.StringVar(&i.Package, "package", "", "Search for package by name. If empty, all available packages will be listed")
54+
// installable vs uninstallable, all versions, channels
55+
// fs.StringVar(&i.showAll, "image", "", "Image reference for the catalog source. Leave unset to retain the current image.")
56+
}

internal/cmd/internal/olmv1/printing.go

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@ package olmv1
33
import (
44
"cmp"
55
"fmt"
6+
"io"
67
"os"
78
"slices"
9+
"sort"
10+
"strings"
811
"text/tabwriter"
912
"time"
1013

1114
"github.com/blang/semver/v4"
15+
olmv1 "github.com/operator-framework/operator-controller/api/v1"
16+
"github.com/operator-framework/operator-registry/alpha/declcfg"
17+
"github.com/operator-framework/operator-registry/alpha/property"
1218
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1319
"k8s.io/apimachinery/pkg/util/duration"
14-
15-
olmv1 "github.com/operator-framework/operator-controller/api/v1"
20+
"k8s.io/apimachinery/pkg/util/json"
1621
)
1722

1823
func printFormattedExtensions(extensions ...olmv1.ClusterExtension) {
@@ -63,6 +68,107 @@ func printFormattedCatalogs(catalogs ...olmv1.ClusterCatalog) {
6368
_ = tw.Flush()
6469
}
6570

71+
func printFormattedDeclCfg(w io.Writer, catalogDcfg map[string]*declcfg.DeclarativeConfig, listVersions bool) {
72+
tw := tabwriter.NewWriter(w, 3, 4, 2, ' ', 0)
73+
if listVersions {
74+
_, _ = fmt.Fprint(tw, "PACKAGE\tCATALOG\tPROVIDER\tVERSION\n")
75+
} else {
76+
_, _ = fmt.Fprint(tw, "PACKAGE\tCATALOG\tPROVIDER\tCHANNELS\n")
77+
}
78+
sortedCatalogs := []string{}
79+
for catalogName := range catalogDcfg {
80+
sortedCatalogs = append(sortedCatalogs, catalogName)
81+
}
82+
sort.Strings(sortedCatalogs)
83+
for _, catalogName := range sortedCatalogs {
84+
dcfg := catalogDcfg[catalogName]
85+
type dcfgPrintMeta struct {
86+
provider string
87+
channels []string
88+
versions []semver.Version
89+
}
90+
pkgProviders := map[string]*dcfgPrintMeta{}
91+
sort.SliceStable(dcfg.Packages, func(i, j int) bool {
92+
return dcfg.Packages[i].Name < dcfg.Packages[j].Name
93+
})
94+
95+
if listVersions {
96+
for _, b := range dcfg.Bundles {
97+
if pkgProviders[b.Package] == nil {
98+
pkgProviders[b.Package] = &dcfgPrintMeta{}
99+
}
100+
if pkgProviders[b.Package].versions == nil {
101+
pkgProviders[b.Package].versions = []semver.Version{}
102+
}
103+
for _, versionProp := range b.Properties {
104+
if versionProp.Type == property.TypePackage {
105+
var pkgProp property.Package
106+
if err := json.Unmarshal(versionProp.Value, &pkgProp); err == nil && len(pkgProp.Version) > 0 {
107+
if parsedVersion, err := semver.Parse(pkgProp.Version); err == nil {
108+
pkgProviders[b.Package].versions = append(pkgProviders[b.Package].versions, parsedVersion)
109+
}
110+
}
111+
continue
112+
}
113+
if versionProp.Type == property.TypeCSVMetadata {
114+
var pkgProp property.CSVMetadata
115+
if err := json.Unmarshal(versionProp.Value, &pkgProp); err == nil && len(pkgProp.Provider.Name) > 0 {
116+
pkgProviders[b.Package].provider = pkgProp.Provider.Name
117+
}
118+
continue
119+
}
120+
}
121+
}
122+
} else {
123+
for _, c := range dcfg.Channels {
124+
if pkgProviders[c.Package] == nil {
125+
pkgProviders[c.Package] = &dcfgPrintMeta{}
126+
}
127+
if pkgProviders[c.Package].channels == nil {
128+
pkgProviders[c.Package].channels = []string{}
129+
}
130+
pkgProviders[c.Package].channels = append(pkgProviders[c.Package].channels, c.Name)
131+
}
132+
}
133+
134+
for _, p := range dcfg.Packages {
135+
if listVersions {
136+
sort.SliceStable(pkgProviders[p.Name].versions, func(i, j int) bool {
137+
return pkgProviders[p.Name].versions[i].GT(pkgProviders[p.Name].versions[j])
138+
})
139+
for _, v := range pkgProviders[p.Name].versions {
140+
_, _ = fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n",
141+
p.Name,
142+
catalogName,
143+
pkgProviders[p.Name].provider,
144+
v)
145+
}
146+
} else {
147+
sort.Strings(pkgProviders[p.Name].channels)
148+
_, _ = fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n",
149+
p.Name,
150+
catalogName,
151+
pkgProviders[p.Name].provider,
152+
strings.Join(pkgProviders[p.Name].channels, ","))
153+
}
154+
}
155+
}
156+
_ = tw.Flush()
157+
}
158+
159+
func printDeclCfgJSON(w io.Writer, catalogDcfg map[string]*declcfg.DeclarativeConfig) {
160+
for _, dcfg := range catalogDcfg {
161+
_ = declcfg.WriteJSON(*dcfg, w)
162+
}
163+
}
164+
165+
func printDeclCfgYAML(w io.Writer, catalogDcfg map[string]*declcfg.DeclarativeConfig) {
166+
for _, dcfg := range catalogDcfg {
167+
_ = declcfg.WriteYAML(*dcfg, w)
168+
_, _ = w.Write([]byte("---\n"))
169+
}
170+
}
171+
66172
// sortExtensions sorts extensions in place and uses the following sorting order:
67173
// name (asc), version (desc)
68174
func sortExtensions(extensions []olmv1.ClusterExtension) {

internal/cmd/olmv1.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,20 @@ func newOlmV1Cmd(cfg *action.Configuration) *cobra.Command {
5858
}
5959
installCmd.AddCommand(olmv1.NewExtensionInstallCmd(cfg))
6060

61+
searchCmd := &cobra.Command{
62+
Use: "search",
63+
Short: "Search for packages",
64+
Long: "Search one or all available catalogs for packages or versions",
65+
}
66+
searchCmd.AddCommand(olmv1.NewCatalogSearchCmd(cfg))
67+
6168
cmd.AddCommand(
6269
installCmd,
6370
getCmd,
6471
createCmd,
6572
deleteCmd,
6673
updateCmd,
74+
searchCmd,
6775
)
6876

6977
return cmd

0 commit comments

Comments
 (0)