Skip to content

Commit

Permalink
Write logs using the logging framework
Browse files Browse the repository at this point in the history
Logs should be written with the logging framework instead of a plain
`panic()` or `fmt.Print`.
  • Loading branch information
anothertobi committed Dec 17, 2019
1 parent 5a25c97 commit dd2eab4
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 48 deletions.
21 changes: 9 additions & 12 deletions cleanup/cleanup.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package cleanup

import (
"fmt"
"strings"

log "github.com/sirupsen/logrus"

"github.com/appuio/image-cleanup/git"
"github.com/appuio/image-cleanup/kubernetes"
"github.com/appuio/image-cleanup/openshift"
Expand Down Expand Up @@ -50,34 +51,30 @@ func NewCleanupCommand() *cobra.Command {
func (o *Options) cleanupImageStreamTags(cmd *cobra.Command, args []string) {
o.ImageStream = args[0]

fmt.Println(o.ImageStream)

commitHashes, err := git.GetCommitHashes(o.RepoPath, o.CommitLimit)
if err != nil {
panic(err)
}
commitHashes := git.GetCommitHashes(o.RepoPath, o.CommitLimit)

imageStreamTags := openshift.GetImageStreamTags(o.ImageStream)

deletionCandidates := getDeletionCandidates(commitHashes, imageStreamTags, o.Keep)

activeimagestreamtags := getActiveImageStreamTags(o.ImageStream, imageStreamTags)
activeImageStreamTags := getActiveImageStreamTags(o.ImageStream, imageStreamTags)

for _, activeimagestreamtag := range activeimagestreamtags {
// Remove the activeImageStreamTags from the deletionCandidates
for _, activeImageStreamTag := range activeImageStreamTags {
for i, deletionCandidate := range deletionCandidates {
if activeimagestreamtag == deletionCandidate {
if activeImageStreamTag == deletionCandidate {
deletionCandidates[i] = deletionCandidates[len(deletionCandidates)-1]
deletionCandidates = deletionCandidates[:len(deletionCandidates)-1]
}
}
}

fmt.Println("candidates for deletion:", deletionCandidates)
log.Printf("Candidates for deletion: %s", deletionCandidates)

if o.Force {
for _, deletionCandidate := range deletionCandidates {
openshift.DeleteImageStreamTag(openshift.BuildImageStreamTagName(o.ImageStream, deletionCandidate))
fmt.Println("deleted imagestreamtag: ", deletionCandidate)
log.Printf("Deleted image stream tag: %s", deletionCandidate)
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions docker/tag.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package docker

import (
"fmt"

"github.com/heroku/docker-registry-client/registry"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -37,6 +35,6 @@ func printImageStreamTags(cmd *cobra.Command, args []string) {
}

for _, tag := range tags {
fmt.Println(tag)
log.Println(tag)
}
}
20 changes: 10 additions & 10 deletions git/git.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package git

import (
"fmt"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"gopkg.in/src-d/go-git.v4"
Expand Down Expand Up @@ -30,29 +28,31 @@ func printGitHEAD(cmd *cobra.Command, args []string) {
log.WithError(err).WithField("path", path).Fatal("Could not open repository")
}

fmt.Println(repository.Head())
log.Println(repository.Head())
}

// GetCommitHashes returns the commit hashes of a given repository ordered by the `git.LogOrderCommitterTime`
func GetCommitHashes(repoPath string, commitLimit int) (commits []string, err error) {
func GetCommitHashes(repoPath string, commitLimit int) []string {
var commitHashes []string

r, err := git.PlainOpen(repoPath)
if err != nil {
panic(err)
log.WithError(err).WithField("repoPath", repoPath).Fatal("Could not open Git repository.")
}

commitIter, err := r.Log(&git.LogOptions{Order: git.LogOrderCommitterTime})
if err != nil {
return nil, err
log.WithError(err).Fatal("Could not get commits from repository.")
}

for i := 0; i < commitLimit; i++ {
commit, err := commitIter.Next()
if err != nil {
break
log.WithError(err).Fatal("Could not get commit.")
} else {
commitHashes = append(commitHashes, commit.Hash.String())
}

commits = append(commits, commit.Hash.String())
}

return
return commitHashes
}
14 changes: 8 additions & 6 deletions kubernetes/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,29 @@ package kubernetes
import (
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"

log "github.com/sirupsen/logrus"
)

// RestConfig from the kubeconfig
func RestConfig() (restConfig *rest.Config) {
func RestConfig() *rest.Config {
// Get a rest.Config from the kubeconfig file. This will be passed into all
// the client objects we create.
restConfig, err := kubeconfig().ClientConfig()
if err != nil {
panic(err)
log.WithError(err).Fatal("Could not create restConfig from kubeconfig")
}

return
return restConfig
}

// Namespace from the kubeconfig
func Namespace() (namespace string) {
func Namespace() string {
namespace, _, err := kubeconfig().Namespace()
if err != nil {
panic(err)
log.WithError(err).Fatal("Could not determine namespace from kubeconfig")
}
return
return namespace
}

func kubeconfig() clientcmd.ClientConfig {
Expand Down
3 changes: 2 additions & 1 deletion kubernetes/clients.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kubernetes

import (
log "github.com/sirupsen/logrus"
"k8s.io/client-go/dynamic"
apps "k8s.io/client-go/kubernetes/typed/apps/v1"
batch "k8s.io/client-go/kubernetes/typed/batch/v1beta1"
Expand All @@ -20,7 +21,7 @@ func NewBatchV1beta1Client() *batch.BatchV1beta1Client {
func NewDynamicClient() dynamic.Interface {
dynamicInterface, err := dynamic.NewForConfig(RestConfig())
if err != nil {
panic(err)
log.WithError(err).Fatal("Could not create dynamic client for rest config.")
}

return dynamicInterface
Expand Down
3 changes: 2 additions & 1 deletion kubernetes/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kubernetes
import (
"strings"

log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)
Expand All @@ -13,7 +14,7 @@ func ResourceContains(value string, resource schema.GroupVersionResource) bool {

objectlist, err := dynamicclient.Resource(resource).Namespace(Namespace()).List(metav1.ListOptions{})
if err != nil {
panic(err)
log.WithError(err).WithField("resource", resource).Fatal("Could not load objects.")
}
for _, item := range objectlist.Items {
return ObjectContains(value, item.Object)
Expand Down
24 changes: 11 additions & 13 deletions openshift/imagestream.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package openshift

import (
"fmt"
log "github.com/sirupsen/logrus"

"github.com/appuio/image-cleanup/kubernetes"
"github.com/spf13/cobra"
Expand All @@ -21,30 +21,30 @@ func NewImageStreamCommand() *cobra.Command {
}

func printImageStreamsFromNamespace(cmd *cobra.Command, args []string) {
imageclient := NewImageV1Client()
imageClient := NewImageV1Client()

imagestreamlist, err := imageclient.ImageStreams(kubernetes.Namespace()).List(metav1.ListOptions{})
imageStreams, err := imageClient.ImageStreams(kubernetes.Namespace()).List(metav1.ListOptions{})
if err != nil {
panic(err)
log.WithError(err).Fatal("Could not retrieve list of image streams.")
}

for _, imagestream := range imagestreamlist.Items {
fmt.Println(imagestream.ObjectMeta.Name)
for _, imageStream := range imageStreams.Items {
log.Println(imageStream.ObjectMeta.Name)
}
}

// GetImageStreamTags retrieves the tags for an image stream
func GetImageStreamTags(imageStreamName string) []string {
var imageStreamTags []string

imageclient := NewImageV1Client()
imageClient := NewImageV1Client()

imagestream, err := imageclient.ImageStreams(kubernetes.Namespace()).Get(imageStreamName, metav1.GetOptions{})
imageStream, err := imageClient.ImageStreams(kubernetes.Namespace()).Get(imageStreamName, metav1.GetOptions{})
if err != nil {
panic(err)
log.WithError(err).WithField("imageStreamName", imageStreamName).Fatal("Could not retrieve image stream.")
}

for _, imageStreamTag := range imagestream.Status.Tags {
for _, imageStreamTag := range imageStream.Status.Tags {
imageStreamTags = append(imageStreamTags, imageStreamTag.Tag)
}

Expand All @@ -57,10 +57,8 @@ func DeleteImageStreamTag(name string) {

err := imageclient.ImageStreamTags(kubernetes.Namespace()).Delete(name, &metav1.DeleteOptions{})
if err != nil {
panic(err)
log.WithError(err).WithField("name", name).Fatal("Could not delete image stream.")
}

return
}

// BuildImageStreamTagName builds the name of an image stream tag
Expand Down
4 changes: 2 additions & 2 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package version

import (
"fmt"
log "github.com/sirupsen/logrus"

"github.com/spf13/cobra"
)
Expand All @@ -19,7 +19,7 @@ func NewVersionCommand() *cobra.Command {
Short: "Print the version and exit",
Long: `Print the version and exit`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("version = %s\ncommit = %s\ndate = %s", Version, Commit, Date)
log.Printf("version = %s\ncommit = %s\ndate = %s", Version, Commit, Date)
},
}

Expand Down

0 comments on commit dd2eab4

Please sign in to comment.