Skip to content

Commit

Permalink
added some new testcases, modifed error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
hamars01 committed Nov 25, 2024
1 parent 852a8e7 commit ba6630a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,11 @@ Legend:

If there is an error whilst checking an image, the tool will display the 🚫 symbol and give a short description of the error at the end of the line. The current common errors are:

- Authentication error.
- Communication error.
- Authentication error. A private image could not be checked, check the docker credentials are present and up to date.
- Communication error. Could not communication with the registry, make sure the registry host exists.
- Image not found. Some pods like `example-pod` are using an image that no longer exists.
- Unknown error, run in debug mode using the flag `-d` for more info

Authentication errors usually mean something went wrong with docker credentials.
Communication errors usually mean that the client cant reach the repository host
Image not found errors happen when the image that is running in the cluster no longer exists in the registry.

A more detailed error can be found when running the tool with the flag `-d`. It also shows you all the pods that use this image

## Private Registry Authentication

If `kubearchinspect` discovers an image from a registry that requires authentication, it uses the `docker` credential
Expand Down
6 changes: 3 additions & 3 deletions cmd/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func imagesCmdRun(_ *cobra.Command, _ []string) {
for _, image := range sortedImages {
var (
icon string
supportsArm, err = images.CheckLinuxArm64Support(image, imageMap[image])
supportsArm, err = images.CheckLinuxArm64Support(image)
)

switch {
Expand All @@ -91,13 +91,13 @@ func imagesCmdRun(_ *cobra.Command, _ []string) {
fmt.Printf("Pods: %s\n", imageMap[image])
}
if loggingEnabled {
log.Println(icon, " image: ", image, "||", "error: ", err)
log.Println(icon, " image: ", image, " error: ", err)
}
case supportsArm:
icon = successIcon
default:
latestImage := images.GetLatestImage(image)
latestSupportsArm, _ := images.CheckLinuxArm64Support(latestImage, imageMap[image])
latestSupportsArm, _ := images.CheckLinuxArm64Support(latestImage)
if latestSupportsArm {
icon = upgradeIcon
} else {
Expand Down
30 changes: 20 additions & 10 deletions internal/images/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,18 @@ func GetFriendlyErrorMessage(err error, pods []string) string {
errorMessage := err.Error()
switch {
case containsAnyOf(errorMessage, []string{"authentication", "auth", "authorized"}):
return "|| Authentication error."
return " Authentication error. A private image could not be checked, check the docker credentials are present and up to date."
case containsAnyOf(errorMessage, []string{"no image found", "image not found"}):
return "|| Image not found. Some pods like `" + pods[0] + "` are using an image that no longer exists."
return " Image not found. Some pods like `" + pods[0] + "` are using an image that no longer exists."
case containsAnyOf(errorMessage, []string{"no such host"}):
return "|| communication error, check your url host."
return " communication error. Could not communication with the registry, make sure the registry host exists."
default:
return "|| An unknown error occurred. Please run in debug mode using the flag '-d' for more details."
return " An unknown error occurred. Please run in debug mode using the flag '-d' for more details."
}
}

// CheckLinuxArm64Support checks for the existance of an arm64 linux image in the manifest
func CheckLinuxArm64Support(imgName string, pods []string) (bool, error) {
func CheckLinuxArm64Support(imgName string) (bool, error) {
sys := &types.SystemContext{
ArchitectureChoice: "arm64",
OSChoice: "linux",
Expand Down Expand Up @@ -99,6 +99,11 @@ func CheckLinuxArm64Support(imgName string, pods []string) (bool, error) {
}

func removeTagIfDigestExists(imgName string) string {
// check for empty string
if imgName == "" {
return imgName
}

if strings.Contains(imgName, "@") {
parts := strings.Split(imgName, "@")
// Check if the first part contains a colon, indicating a tag
Expand All @@ -113,10 +118,15 @@ func removeTagIfDigestExists(imgName string) string {

func GetLatestImage(imgName string) string {

// Remove everything after '@'
tag := strings.Split(imgName, "@")
// check for empty string
if imgName == "" {
return imgName
}

// Remove everything after '@' or ':'
parts := strings.FieldsFunc(imgName, func(c rune) bool {
return c == '@' || c == ':'
})

// Remove the tag and append with latest
split := strings.Split(tag[0], ":")
return split[0] + ":latest"
return parts[0] + ":latest"
}
17 changes: 16 additions & 1 deletion internal/images/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/stretchr/testify/assert"
)

func Test_removeTagIfDigestExists(t *testing.T) {
func TestRemoveTagIfDigestExists(t *testing.T) {

tests := []struct {
name string
Expand All @@ -44,6 +44,16 @@ func Test_removeTagIfDigestExists(t *testing.T) {
imgName: "testingImage:latest",
want: "testingImage:latest",
},
{
name: "Empty string",
imgName: "",
want: "",
},
{
name: "No tag or digest",
imgName: "testingImage",
want: "testingImage",
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -80,6 +90,11 @@ func TestGetLatestImage(t *testing.T) {
imgName: "nginx:v2@sha256:1114555554555",
want: "nginx:latest",
},
{
name: "Empty string",
imgName: "",
want: "",
},
}

for _, tt := range tests {
Expand Down

0 comments on commit ba6630a

Please sign in to comment.