-
Notifications
You must be signed in to change notification settings - Fork 43
✨ Add commands to list and get olmv1 operators and catalogs #218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
perdasilva
merged 1 commit into
operator-framework:main
from
azych:olmv1-list-get-catalogs-and-operators
Feb 28, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package olmv1 | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestCommand(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Internal action Suite") | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package olmv1 | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/operator-framework/kubectl-operator/internal/cmd/internal/log" | ||
v1action "github.com/operator-framework/kubectl-operator/internal/pkg/v1/action" | ||
"github.com/operator-framework/kubectl-operator/pkg/action" | ||
) | ||
|
||
// NewCatalogInstalledGetCmd handles get commands in the form of: | ||
// catalog(s) [catalog_name] - this will either list all the installed operators | ||
// if no catalog_name has been provided or display the details of the specific | ||
// one otherwise | ||
func NewCatalogInstalledGetCmd(cfg *action.Configuration) *cobra.Command { | ||
i := v1action.NewCatalogInstalledGet(cfg) | ||
i.Logf = log.Printf | ||
|
||
cmd := &cobra.Command{ | ||
Use: "catalog [catalog_name]", | ||
Aliases: []string{"catalogs"}, | ||
Args: cobra.RangeArgs(0, 1), | ||
Short: "Display one or many installed catalogs", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) == 1 { | ||
i.CatalogName = args[0] | ||
} | ||
installedCatalogs, err := i.Run(cmd.Context()) | ||
if err != nil { | ||
log.Fatalf("failed getting installed catalog(s): %v", err) | ||
} | ||
|
||
printFormattedCatalogs(installedCatalogs...) | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package olmv1 | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/operator-framework/kubectl-operator/internal/cmd/internal/log" | ||
v1action "github.com/operator-framework/kubectl-operator/internal/pkg/v1/action" | ||
"github.com/operator-framework/kubectl-operator/pkg/action" | ||
) | ||
|
||
// NewOperatorInstalledGetCmd handles get commands in the form of: | ||
// operator(s) [operator_name] - this will either list all the installed operators | ||
// if no operator_name has been provided or display the details of the specific | ||
// one otherwise | ||
func NewOperatorInstalledGetCmd(cfg *action.Configuration) *cobra.Command { | ||
i := v1action.NewOperatorInstalledGet(cfg) | ||
i.Logf = log.Printf | ||
|
||
cmd := &cobra.Command{ | ||
Use: "operator [operator_name]", | ||
Aliases: []string{"operators"}, | ||
Args: cobra.RangeArgs(0, 1), | ||
Short: "Display one or many installed operators", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) == 1 { | ||
i.OperatorName = args[0] | ||
} | ||
installedExtensions, err := i.Run(cmd.Context()) | ||
if err != nil { | ||
log.Fatalf("failed getting installed operator(s): %v", err) | ||
} | ||
|
||
printFormattedOperators(installedExtensions...) | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package olmv1 | ||
|
||
import ( | ||
"cmp" | ||
"fmt" | ||
"os" | ||
"slices" | ||
"text/tabwriter" | ||
"time" | ||
|
||
"github.com/blang/semver/v4" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/duration" | ||
|
||
catalogdv1 "github.com/operator-framework/catalogd/api/v1" | ||
olmv1 "github.com/operator-framework/operator-controller/api/v1" | ||
) | ||
|
||
func printFormattedOperators(extensions ...olmv1.ClusterExtension) { | ||
tw := tabwriter.NewWriter(os.Stdout, 3, 4, 2, ' ', 0) | ||
_, _ = fmt.Fprint(tw, "NAME\tINSTALLED BUNDLE\tVERSION\tSOURCE TYPE\tINSTALLED\tPROGRESSING\tAGE\n") | ||
|
||
sortOperators(extensions) | ||
for _, ext := range extensions { | ||
age := time.Since(ext.CreationTimestamp.Time) | ||
_, _ = fmt.Fprintf(tw, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", | ||
ext.Name, | ||
ext.Status.Install.Bundle.Name, | ||
ext.Status.Install.Bundle.Version, | ||
ext.Spec.Source.SourceType, | ||
status(ext.Status.Conditions, olmv1.TypeInstalled), | ||
bentito marked this conversation as resolved.
Show resolved
Hide resolved
|
||
status(ext.Status.Conditions, olmv1.TypeProgressing), | ||
duration.HumanDuration(age), | ||
) | ||
} | ||
_ = tw.Flush() | ||
} | ||
|
||
func printFormattedCatalogs(catalogs ...catalogdv1.ClusterCatalog) { | ||
tw := tabwriter.NewWriter(os.Stdout, 3, 4, 2, ' ', 0) | ||
_, _ = fmt.Fprint(tw, "NAME\tAVAILABILITY\tPRIORITY\tLASTUNPACKED\tSERVING\tAGE\n") | ||
|
||
sortCatalogs(catalogs) | ||
for _, cat := range catalogs { | ||
azych marked this conversation as resolved.
Show resolved
Hide resolved
|
||
age := time.Since(cat.CreationTimestamp.Time) | ||
lastUnpacked := time.Since(cat.Status.LastUnpacked.Time) | ||
_, _ = fmt.Fprintf(tw, "%s\t%s\t%d\t%s\t%s\t%s\n", | ||
cat.Name, | ||
string(cat.Spec.AvailabilityMode), | ||
cat.Spec.Priority, | ||
duration.HumanDuration(lastUnpacked), | ||
status(cat.Status.Conditions, catalogdv1.TypeServing), | ||
duration.HumanDuration(age), | ||
) | ||
} | ||
_ = tw.Flush() | ||
} | ||
|
||
// sortOperators sorts operators in place and uses the following sorting order: | ||
// name (asc), version (desc) | ||
func sortOperators(extensions []olmv1.ClusterExtension) { | ||
slices.SortFunc(extensions, func(a, b olmv1.ClusterExtension) int { | ||
return cmp.Or( | ||
cmp.Compare(a.Name, b.Name), | ||
-semver.MustParse(a.Status.Install.Bundle.Version).Compare(semver.MustParse(b.Status.Install.Bundle.Version)), | ||
) | ||
}) | ||
} | ||
|
||
// sortCatalogs sorts catalogs in place and uses the following sorting order: | ||
// availability (asc), priority (desc), name (asc) | ||
func sortCatalogs(catalogs []catalogdv1.ClusterCatalog) { | ||
slices.SortFunc(catalogs, func(a, b catalogdv1.ClusterCatalog) int { | ||
return cmp.Or( | ||
cmp.Compare(a.Spec.AvailabilityMode, b.Spec.AvailabilityMode), | ||
-cmp.Compare(a.Spec.Priority, b.Spec.Priority), | ||
cmp.Compare(a.Name, b.Name), | ||
) | ||
}) | ||
} | ||
|
||
func status(conditions []metav1.Condition, typ string) string { | ||
for _, condition := range conditions { | ||
if condition.Type == typ { | ||
return string(condition.Status) | ||
} | ||
} | ||
|
||
return "Unknown" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package olmv1 | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
olmv1catalogd "github.com/operator-framework/catalogd/api/v1" | ||
olmv1 "github.com/operator-framework/operator-controller/api/v1" | ||
) | ||
|
||
var _ = Describe("SortCatalogs", func() { | ||
It("sorts catalogs in correct order", func() { | ||
catalogs := []olmv1catalogd.ClusterCatalog{ | ||
newClusterCatalog("cat-unavailable-0", olmv1catalogd.AvailabilityModeUnavailable, 0), | ||
newClusterCatalog("cat-unavailable-1", olmv1catalogd.AvailabilityModeUnavailable, 1), | ||
newClusterCatalog("cat-available-0", olmv1catalogd.AvailabilityModeAvailable, 0), | ||
newClusterCatalog("cat-available-1", olmv1catalogd.AvailabilityModeAvailable, 1), | ||
} | ||
sortCatalogs(catalogs) | ||
|
||
Expect(catalogs[0].Name).To(Equal("cat-available-1")) | ||
Expect(catalogs[1].Name).To(Equal("cat-available-0")) | ||
Expect(catalogs[2].Name).To(Equal("cat-unavailable-1")) | ||
Expect(catalogs[3].Name).To(Equal("cat-unavailable-0")) | ||
}) | ||
}) | ||
|
||
var _ = Describe("SortOperators", func() { | ||
It("sorts operators in correct order", func() { | ||
operators := []olmv1.ClusterExtension{ | ||
newClusterExtension("op-1", "1.0.0"), | ||
newClusterExtension("op-1", "1.0.1"), | ||
newClusterExtension("op-1", "1.0.1-rc4"), | ||
newClusterExtension("op-1", "1.0.1-rc2"), | ||
newClusterExtension("op-2", "2.0.0"), | ||
} | ||
sortOperators(operators) | ||
|
||
Expect(operators[0].Status.Install.Bundle.Version).To(Equal("1.0.1")) | ||
Expect(operators[1].Status.Install.Bundle.Version).To(Equal("1.0.1-rc4")) | ||
Expect(operators[2].Status.Install.Bundle.Version).To(Equal("1.0.1-rc2")) | ||
Expect(operators[3].Status.Install.Bundle.Version).To(Equal("1.0.0")) | ||
Expect(operators[4].Status.Install.Bundle.Version).To(Equal("2.0.0")) | ||
}) | ||
}) | ||
|
||
func newClusterCatalog(name string, availabilityMode olmv1catalogd.AvailabilityMode, priority int32) olmv1catalogd.ClusterCatalog { | ||
return olmv1catalogd.ClusterCatalog{ | ||
ObjectMeta: metav1.ObjectMeta{Name: name}, | ||
Spec: olmv1catalogd.ClusterCatalogSpec{AvailabilityMode: availabilityMode, Priority: priority}, | ||
} | ||
} | ||
|
||
func newClusterExtension(name, version string) olmv1.ClusterExtension { | ||
return olmv1.ClusterExtension{ | ||
ObjectMeta: metav1.ObjectMeta{Name: name}, | ||
Status: olmv1.ClusterExtensionStatus{ | ||
Install: &olmv1.ClusterExtensionInstallStatus{ | ||
Bundle: olmv1.BundleMetadata{ | ||
Name: name, | ||
Version: version, | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package action_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestCommand(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Internal action Suite") | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package action | ||
|
||
import ( | ||
"context" | ||
|
||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
olmv1catalogd "github.com/operator-framework/catalogd/api/v1" | ||
|
||
"github.com/operator-framework/kubectl-operator/pkg/action" | ||
) | ||
|
||
type CatalogInstalledGet struct { | ||
config *action.Configuration | ||
CatalogName string | ||
|
||
Logf func(string, ...interface{}) | ||
} | ||
|
||
func NewCatalogInstalledGet(cfg *action.Configuration) *CatalogInstalledGet { | ||
return &CatalogInstalledGet{ | ||
config: cfg, | ||
Logf: func(string, ...interface{}) {}, | ||
} | ||
} | ||
|
||
func (i *CatalogInstalledGet) Run(ctx context.Context) ([]olmv1catalogd.ClusterCatalog, error) { | ||
// get | ||
azych marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if i.CatalogName != "" { | ||
var result olmv1catalogd.ClusterCatalog | ||
|
||
opKey := types.NamespacedName{Name: i.CatalogName} | ||
err := i.config.Client.Get(ctx, opKey, &result) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return []olmv1catalogd.ClusterCatalog{result}, nil | ||
} | ||
|
||
// list | ||
azych marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var result olmv1catalogd.ClusterCatalogList | ||
err := i.config.Client.List(ctx, &result, &client.ListOptions{}) | ||
|
||
return result.Items, err | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.