Skip to content

Commit

Permalink
List available images
Browse files Browse the repository at this point in the history
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 <simon@rueggs.ch>
  • Loading branch information
srueg committed Mar 31, 2020
1 parent befe3c3 commit 399f81c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
19 changes: 19 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
9 changes: 7 additions & 2 deletions cmd/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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])

Expand Down
10 changes: 7 additions & 3 deletions cmd/orphans.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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])

Expand Down
14 changes: 14 additions & 0 deletions pkg/openshift/imagestream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 399f81c

Please sign in to comment.