Skip to content

Commit

Permalink
Merge pull request #34 from SimonXming/fix-check-failure
Browse files Browse the repository at this point in the history
Make sure check won't fail if image tag not exist on registry server.
  • Loading branch information
vito authored May 13, 2019
2 parents 3babcea + 1b442b2 commit 99b5f6b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
32 changes: 32 additions & 0 deletions check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,36 @@ var _ = Describe("Check", func() {
})
})
})

Context("when invoked with not exist image", func() {
BeforeEach(func() {
req.Source = resource.Source{
Repository: "concourse/test-image-static",
RawTag: "not-exist-image",
}
req.Version = nil
})

It("returns empty digest", func() {
Expect(res).To(Equal([]resource.Version{}))
})

Context("against a private repo with credentials", func() {
BeforeEach(func() {
req.Source = resource.Source{
Repository: dockerPrivateRepo,
RawTag: "not-exist-image",

Username: dockerPrivateUsername,
Password: dockerPrivatePassword,
}

checkDockerPrivateUserConfigured()
})

It("returns empty digest", func() {
Expect(res).To(Equal([]resource.Version{}))
})
})
})
})
41 changes: 26 additions & 15 deletions cmd/check/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,15 @@ func main() {
return
}

var missingTag bool
digest, err := image.Digest()
if err != nil {
logrus.Errorf("failed get image digest: %s", err)
os.Exit(1)
return
missingTag = checkMissingManifest(err)
if !missingTag {
logrus.Errorf("failed to get cursor image digest: %s", err)
os.Exit(1)
return
}
}

response := CheckResponse{}
Expand All @@ -93,15 +97,7 @@ func main() {
var missingDigest bool
_, err = digestImage.Digest()
if err != nil {
if rErr, ok := err.(*remote.Error); ok {
for _, e := range rErr.Errors {
if e.Code == remote.ManifestUnknownErrorCode {
missingDigest = true
break
}
}
}

missingDigest = checkMissingManifest(err)
if !missingDigest {
logrus.Errorf("failed to get cursor image digest: %s", err)
os.Exit(1)
Expand All @@ -114,9 +110,24 @@ func main() {
}
}

response = append(response, resource.Version{
Digest: digest.String(),
})
if !missingTag {
response = append(response, resource.Version{
Digest: digest.String(),
})
}

json.NewEncoder(os.Stdout).Encode(response)
}

func checkMissingManifest(err error) bool {
var missing bool
if rErr, ok := err.(*remote.Error); ok {
for _, e := range rErr.Errors {
if e.Code == remote.ManifestUnknownErrorCode {
missing = true
break
}
}
}
return missing
}

0 comments on commit 99b5f6b

Please sign in to comment.