From 399f81ce6ba3800c13bf7c0b1bd1ea12d87b8856 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20R=C3=BCegg?= Date: Tue, 31 Mar 2020 09:30:04 +0200 Subject: [PATCH] List available images MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the history or orphan cleanup command is called without an image name, list all the available images in the current project. Signed-off-by: Simon Rüegg --- cmd/common.go | 19 +++++++++++++++++++ cmd/history.go | 9 +++++++-- cmd/orphans.go | 10 +++++++--- pkg/openshift/imagestream.go | 14 ++++++++++++++ 4 files changed, 47 insertions(+), 5 deletions(-) diff --git a/cmd/common.go b/cmd/common.go index cadd4e9..7c12a56 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -2,8 +2,10 @@ package cmd import ( "fmt" + "github.com/appuio/seiso/cfg" "github.com/appuio/seiso/pkg/git" + "github.com/appuio/seiso/pkg/kubernetes" "github.com/appuio/seiso/pkg/openshift" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -46,3 +48,20 @@ func addCommonFlagsForGit(cmd *cobra.Command, defaults *cfg.Configuration) { cmd.PersistentFlags().String("sort", defaults.Git.SortCriteria, fmt.Sprintf("Sort git tags by criteria. Only effective with --tags. Allowed values: [%s, %s]", git.SortOptionVersion, git.SortOptionAlphabetic)) } + +func listImages() error { + ns, err := kubernetes.Namespace() + if err != nil { + return err + } + imageStreams, err := openshift.ListImageStreams(ns) + if err != nil { + return err + } + imageNames := []string{} + for _, image := range imageStreams { + imageNames = append(imageNames, image.Name) + } + log.WithField("project", ns).WithField("images", imageNames).Info("Please select an image. The following images are available") + return nil +} diff --git a/cmd/history.go b/cmd/history.go index b2e13e7..2b7728b 100644 --- a/cmd/history.go +++ b/cmd/history.go @@ -17,7 +17,7 @@ var ( Aliases: []string{"hist"}, Short: "Clean up excessive image tags", Long: `Clean up excessive image tags matching the commit hashes (prefix) of the git repository`, - Args: cobra.ExactArgs(1), + Args: cobra.MaximumNArgs(1), SilenceUsage: true, RunE: func(cmd *cobra.Command, args []string) error { if err := validateHistoryCommandInput(args); err != nil { @@ -40,6 +40,9 @@ func init() { } func validateHistoryCommandInput(args []string) error { + if len(args) == 0 { + return nil + } if _, _, err := splitNamespaceAndImagestream(args[0]); err != nil { return fmt.Errorf("could not parse image name: %w", err) } @@ -51,7 +54,9 @@ func validateHistoryCommandInput(args []string) error { // ExecuteHistoryCleanupCommand executes the history cleanup command func ExecuteHistoryCleanupCommand(args []string) error { - + if len(args) == 0 { + return listImages() + } c := config.History namespace, image, _ := splitNamespaceAndImagestream(args[0]) diff --git a/cmd/orphans.go b/cmd/orphans.go index b5821d8..f11240c 100644 --- a/cmd/orphans.go +++ b/cmd/orphans.go @@ -31,7 +31,7 @@ var ( Short: "Clean up unknown image tags", Long: orphanCommandLongDescription, Aliases: []string{"orph"}, - Args: cobra.ExactArgs(1), + Args: cobra.MaximumNArgs(1), SilenceUsage: true, RunE: func(cmd *cobra.Command, args []string) error { if err := validateOrphanCommandInput(args); err != nil { @@ -55,7 +55,9 @@ func init() { } func validateOrphanCommandInput(args []string) error { - + if len(args) == 0 { + return nil + } o := config.Orphan if _, _, err := splitNamespaceAndImagestream(args[0]); err != nil { return err @@ -76,7 +78,9 @@ func validateOrphanCommandInput(args []string) error { // ExecuteOrphanCleanupCommand executes the orphan cleanup command func ExecuteOrphanCleanupCommand(args []string) error { - + if len(args) == 0 { + return listImages() + } o := config.Orphan namespace, imageName, _ := splitNamespaceAndImagestream(args[0]) diff --git a/pkg/openshift/imagestream.go b/pkg/openshift/imagestream.go index 61bce58..d68c873 100644 --- a/pkg/openshift/imagestream.go +++ b/pkg/openshift/imagestream.go @@ -75,3 +75,17 @@ func DeleteImageStreamTag(namespace, name string) error { func BuildImageStreamTagName(imageStream string, imageStreamTag string) string { return imageStream + ":" + imageStreamTag } + +// ListImageStreams lists all available image streams in a namespace +func ListImageStreams(namespace string) ([]imagev1.ImageStream, error) { + imageClient, err := NewImageV1Client() + if err != nil { + return nil, err + } + + imageStreams, err := imageClient.ImageStreams(namespace).List(metav1.ListOptions{}) + if err != nil { + return nil, err + } + return imageStreams.Items, nil +}