Skip to content

Commit

Permalink
Print out status before performing deletion
Browse files Browse the repository at this point in the history
Use status output for humans

Suggest deleting orphan image tags in README
  • Loading branch information
bittner committed Apr 27, 2020
1 parent 77f627d commit 72c8386
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 21 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -222,6 +222,10 @@ becomes:
```console
seiso -n "$OPENSHIFT_PROJECT" image history "$APP_NAME" --delete
```
We suggest cleaning up orphan image tags in addition, e.g.
```console
seiso -n "$OPENSHIFT_PROJECT" image orphans "$APP_NAME" --older-than 1w --delete
```

## Development

Expand Down
18 changes: 9 additions & 9 deletions cmd/common.go
Expand Up @@ -17,10 +17,10 @@ import (
// DeleteImages deletes a list of image tags
func DeleteImages(imageTags []string, imageName string, namespace string) {
for _, inactiveTag := range imageTags {
log.Infof("Deleting %s/%s:%s", namespace, imageName, inactiveTag)

if err := openshift.DeleteImageStreamTag(namespace, openshift.BuildImageStreamTagName(imageName, inactiveTag)); err != nil {
log.WithError(err).Errorf("Failed to delete %s/%s:%s", namespace, imageName, inactiveTag)
} else {
log.Infof("Deleted %s/%s:%s", namespace, imageName, inactiveTag)
}
}
}
Expand All @@ -32,31 +32,31 @@ func DeleteResources(resources []cfg.KubernetesResource, resourceSelectorFunc cf
kind := resource.GetKind()
name := resource.GetName()

log.Infof("Deleting %s %s/%s", kind, namespace, name)

if err := openshift.DeleteResource(name, resourceSelectorFunc); err != nil {
log.WithError(err).Errorf("Failed to delete %s %s/%s", kind, namespace, name)
} else {
log.Infof("Deleted %s %s/%s", kind, namespace, name)
}
}
}

// PrintImageTags prints the given image tags line by line. In batch mode, only the tag name is printed, otherwise default
// log with info level
func PrintImageTags(imageTags []string) {
func PrintImageTags(imageTags []string, imageName string, namespace string) {
if config.Log.Batch {
for _, tag := range imageTags {
fmt.Println(tag)
}
} else {
for _, tag := range imageTags {
log.WithField("imageTag", tag).Info("Found image tag candidate")
log.Infof("Found image tag candidate: %s/%s:%s", namespace, imageName, tag)
}
}
}

// PrintResources prints the given resource line by line. In batch mode, only the resource is printed, otherwise default
// log with info level
func PrintResources(resources []cfg.KubernetesResource) {
func PrintResources(resources []cfg.KubernetesResource, namespace string) {
if len(resources) == 0 {
log.Info("Nothing found to be deleted.")
}
Expand All @@ -66,7 +66,7 @@ func PrintResources(resources []cfg.KubernetesResource) {
}
} else {
for _, resource := range resources {
log.WithField(resource.GetKind(), resource.GetName()).Info("Found resource candidate")
log.Infof("Found %s candidate: %s/%s", resource.GetKind(), namespace, resource.GetName())
}
}
}
Expand All @@ -75,7 +75,7 @@ func PrintResources(resources []cfg.KubernetesResource) {
// global, even for commands that do not need them, which might be overkill.
func addCommonFlagsForGit(cmd *cobra.Command, defaults *cfg.Configuration) {
cmd.PersistentFlags().BoolP("delete", "d", defaults.Delete, "Confirm deletion of image tags.")
cmd.PersistentFlags().BoolP("force", "f", defaults.Delete, "(deprecated) Confirm deletion. Alias for --delete")
cmd.PersistentFlags().BoolP("force", "f", defaults.Delete, "(deprecated) Alias for --delete")
cmd.PersistentFlags().IntP("commit-limit", "l", defaults.Git.CommitLimit,
"Only look at the first <l> commits to compare with tags. Use 0 (zero) for all commits. Limited effect if repo is a shallow clone.")
cmd.PersistentFlags().StringP("repo-path", "p", defaults.Git.RepoPath, "Path to Git repository")
Expand Down
2 changes: 1 addition & 1 deletion cmd/configmaps.go
Expand Up @@ -92,7 +92,7 @@ func executeConfigMapCleanupCommand(args []string) error {
return client.ConfigMaps(namespace)
})
} else {
PrintResources(filteredConfigMaps)
PrintResources(filteredConfigMaps, namespace)
}

return nil
Expand Down
16 changes: 8 additions & 8 deletions cmd/history.go
Expand Up @@ -58,11 +58,11 @@ func ExecuteHistoryCleanupCommand(args []string) error {
return listImages()
}
c := config.History
namespace, image, _ := splitNamespaceAndImagestream(args[0])
namespace, imageName, _ := splitNamespaceAndImagestream(args[0])

imageStreamObjectTags, err := openshift.GetImageStreamTags(namespace, image)
imageStreamObjectTags, err := openshift.GetImageStreamTags(namespace, imageName)
if err != nil {
return fmt.Errorf("could not retrieve image stream '%s/%s': %w", namespace, image, err)
return fmt.Errorf("could not retrieve image stream '%s/%s': %w", namespace, imageName, err)
}

var imageStreamTags []string
Expand All @@ -81,24 +81,24 @@ func ExecuteHistoryCleanupCommand(args []string) error {
}
var matchingTags = cleanup.GetMatchingTags(&gitCandidates, &imageStreamTags, matchOption)

activeImageStreamTags, err := openshift.GetActiveImageStreamTags(namespace, image, matchingTags)
activeImageStreamTags, err := openshift.GetActiveImageStreamTags(namespace, imageName, matchingTags)
if err != nil {
return fmt.Errorf("could not retrieve active image stream tags for '%s/%s': %w", namespace, image, err)
return fmt.Errorf("could not retrieve active image stream tags for '%s/%s': %w", namespace, imageName, err)
}

inactiveTags := cleanup.GetInactiveImageTags(&activeImageStreamTags, &matchingTags)
inactiveTags = cleanup.LimitTags(&inactiveTags, c.Keep)
if len(inactiveTags) == 0 {
log.WithFields(log.Fields{
"\n - namespace": namespace,
"\n - imageName": image,
"\n - 📺 image": imageName,
}).Info("No inactive image stream tags found")
return nil
}
if config.Delete {
DeleteImages(inactiveTags, image, namespace)
DeleteImages(inactiveTags, imageName, namespace)
} else {
PrintImageTags(inactiveTags)
PrintImageTags(inactiveTags, imageName, namespace)
}
return nil
}
4 changes: 2 additions & 2 deletions cmd/orphans.go
Expand Up @@ -111,15 +111,15 @@ func ExecuteOrphanCleanupCommand(args []string) error {
if len(imageTagList) == 0 {
log.WithFields(log.Fields{
"\n - namespace": namespace,
"\n - imageName": imageName,
"\n - 📺 image": imageName,
}).Info("No orphaned image stream tags found")
return nil
}

if config.Delete {
DeleteImages(imageTagList, imageName, namespace)
} else {
PrintImageTags(imageTagList)
PrintImageTags(imageTagList, imageName, namespace)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/secrets.go
Expand Up @@ -89,7 +89,7 @@ func executeSecretCleanupCommand(args []string) error {
return client.Secrets(namespace)
})
} else {
PrintResources(filteredSecrets)
PrintResources(filteredSecrets, namespace)
}

return nil
Expand Down

0 comments on commit 72c8386

Please sign in to comment.