Skip to content

Commit

Permalink
Merge pull request #273 from zong-zhe/fix-push-harbor
Browse files Browse the repository at this point in the history
fix: fix the failure during push to harbor
  • Loading branch information
Peefy authored Feb 28, 2024
2 parents b197e0b + 7db26cc commit 53aece1
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions pkg/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import (

const OCI_SCHEME = "oci"
const DEFAULT_OCI_ARTIFACT_TYPE = "application/vnd.oci.image.layer.v1.tar"
const (
OciErrorCodeNameUnknown = "NAME_UNKNOWN"
OciErrorCodeRepoNotFound = "NOT_FOUND"
)

// Login will login 'hostname' by 'username' and 'password'.
func Login(hostname, username, password string, setting *settings.Settings) error {
Expand Down Expand Up @@ -180,6 +184,21 @@ func (ociClient *OciClient) TheLatestTag() (string, error) {
return tagSelected, nil
}

// RepoIsNotExist will check if the error is caused by the repo not found.
func RepoIsNotExist(err error) bool {
errRes, ok := err.(*errcode.ErrorResponse)
if ok {
if len(errRes.Errors) == 1 &&
// docker.io and gchr.io will return NAME_UNKNOWN
(errRes.Errors[0].Code == OciErrorCodeNameUnknown ||
// harbor will return NOT_FOUND
errRes.Errors[0].Code == OciErrorCodeRepoNotFound) {
return true
}
}
return false
}

// ContainsTag will check if the tag exists in the repo.
func (ociClient *OciClient) ContainsTag(tag string) (bool, *reporter.KpmEvent) {
var exists bool
Expand All @@ -191,11 +210,8 @@ func (ociClient *OciClient) ContainsTag(tag string) (bool, *reporter.KpmEvent) {

if err != nil {
// If the repo with tag is not found, return false.
errRes, ok := err.(*errcode.ErrorResponse)
if ok {
if len(errRes.Errors) == 1 && errRes.Errors[0].Code == errcode.ErrorCodeNameUnknown {
return false, nil
}
if RepoIsNotExist(err) {
return false, nil
}
// If the user not login, return error.
return false, reporter.NewErrorEvent(
Expand Down

0 comments on commit 53aece1

Please sign in to comment.