-
Notifications
You must be signed in to change notification settings - Fork 885
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
feat(log): use short image/container IDs in logs #888
Merged
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 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 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,23 @@ | ||
package container | ||
|
||
import "strings" | ||
|
||
// ShortID returns the 12-character (hex) short version of an image ID hash, removing any "sha256:" prefix if present | ||
func ShortID(imageID string) (short string) { | ||
prefixSep := strings.IndexRune(imageID, ':') | ||
offset := 0 | ||
length := 12 | ||
if prefixSep >= 0 { | ||
if imageID[0:prefixSep] == "sha256" { | ||
offset = prefixSep + 1 | ||
} else { | ||
length += prefixSep + 1 | ||
} | ||
} | ||
|
||
if len(imageID) >= offset+length { | ||
return imageID[offset : offset+length] | ||
} | ||
|
||
return imageID | ||
} |
This file contains 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,46 @@ | ||
package container_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
. "github.com/containrrr/watchtower/pkg/container" | ||
) | ||
|
||
var _ = Describe("container utils", func() { | ||
Describe("ShortID", func() { | ||
When("given a normal image ID", func() { | ||
When("it contains a sha256 prefix", func() { | ||
It("should return that ID in short version", func() { | ||
actual := ShortID("sha256:0123456789abcd00000000001111111111222222222233333333334444444444") | ||
Expect(actual).To(Equal("0123456789ab")) | ||
}) | ||
}) | ||
When("it doesn't contain a prefix", func() { | ||
It("should return that ID in short version", func() { | ||
actual := ShortID("0123456789abcd00000000001111111111222222222233333333334444444444") | ||
Expect(actual).To(Equal("0123456789ab")) | ||
}) | ||
}) | ||
}) | ||
When("given a short image ID", func() { | ||
When("it contains no prefix", func() { | ||
It("should return the same string", func() { | ||
Expect(ShortID("0123456789ab")).To(Equal("0123456789ab")) | ||
}) | ||
}) | ||
When("it contains a the sha256 prefix", func() { | ||
It("should return the ID without the prefix", func() { | ||
Expect(ShortID("sha256:0123456789ab")).To(Equal("0123456789ab")) | ||
}) | ||
}) | ||
}) | ||
When("given an ID with an unknown prefix", func() { | ||
It("should return a short version of that ID including the prefix", func() { | ||
Expect(ShortID("md5:0123456789ab")).To(Equal("md5:0123456789ab")) | ||
Expect(ShortID("md5:0123456789abcdefg")).To(Equal("md5:0123456789ab")) | ||
Expect(ShortID("md5:01")).To(Equal("md5:01")) | ||
}) | ||
}) | ||
}) | ||
}) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not actually part of the intended changes, but got a lint warning about the use of opts before checking if an error was returned. Shouldn't matter, but doing it in this order is at least more correct.