Skip to content

Commit

Permalink
Fix project not created & warning message on harbor
Browse files Browse the repository at this point in the history
  • Loading branch information
STARRY-S committed May 6, 2024
1 parent c1fffc5 commit d2f9544
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 38 deletions.
44 changes: 22 additions & 22 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,28 +158,28 @@ steps:
when:
event:
- tag
- name: manifest-latest
image: plugins/manifest
settings:
username:
from_secret: docker_username
password:
from_secret: docker_password
platforms:
- linux/amd64
- linux/arm64
target: "cnrancher/hangar:latest"
template: "cnrancher/hangar:${DRONE_TAG}-ARCH"
when:
event:
- tag
ref:
include:
- refs/tags/v*
exclude:
- refs/tags/*-rc*
- refs/tags/*-alpha*
- refs/tags/*-beta*
# - name: manifest-latest
# image: plugins/manifest
# settings:
# username:
# from_secret: docker_username
# password:
# from_secret: docker_password
# platforms:
# - linux/amd64
# - linux/arm64
# target: "cnrancher/hangar:latest"
# template: "cnrancher/hangar:${DRONE_TAG}-ARCH"
# when:
# event:
# - tag
# ref:
# include:
# - refs/tags/v*
# exclude:
# - refs/tags/*-rc*
# - refs/tags/*-alpha*
# - refs/tags/*-beta*
volumes:
- name: docker
host:
Expand Down
10 changes: 6 additions & 4 deletions pkg/hangar/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,12 @@ func (l *Loader) initHarborProject(ctx context.Context) error {
!l.systemContext.OCIInsecureSkipTLSVerify)
if err != nil {
if errors.Is(err, harbor.ErrRegistryIsNotHarbor) {
logrus.Debugf("registry %q is not harbor", l.DestinationRegistry)
return nil
}
return err
}
logrus.Infof("Harbor V2 detected, creating project")
credential, err := config.GetCredentials(nil, l.DestinationRegistry)
if err != nil {
return fmt.Errorf("failed to get credential of %q: %w",
Expand All @@ -241,19 +243,19 @@ func (l *Loader) initHarborProject(ctx context.Context) error {
ctx, project, harborURL, &credential,
!l.systemContext.OCIInsecureSkipTLSVerify)
if err != nil {
return err
return fmt.Errorf("failed to detect project %q exists: %w", project, err)
}
if exists {
logrus.Infof("Project %q already exists", project)
continue
}
err = harbor.CreateProject(
ctx, project, harborURL, &credential,
!l.systemContext.OCIInsecureSkipTLSVerify)
if err != nil {
return err
return fmt.Errorf("failed to create project %q: %w", project, err)
}
logrus.Infof("Created Harbor V2 project %q for registry %q",
project, l.DestinationRegistry)
logrus.Infof("Created Harbor V2 project %q", project)
}
return nil
}
Expand Down
13 changes: 1 addition & 12 deletions pkg/harbor/harbor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -86,17 +85,7 @@ func GetURL(

switch resp.StatusCode {
case http.StatusOK:
b, _ := io.ReadAll(resp.Body)
if len(b) > 0 {
if len(b) > 20 {
b = b[:20]
}
logrus.Debugf("server response: %v", string(b))
content := strings.ToLower(string(b))
if strings.Contains(content, "pong") {
return ubase, nil
}
}
return ubase, nil
}

return "", ErrRegistryIsNotHarbor
Expand Down
16 changes: 16 additions & 0 deletions pkg/manifest/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package manifest

import (
"context"
"strings"
"time"

"github.com/containers/common/pkg/retry"
Expand Down Expand Up @@ -75,6 +76,21 @@ func NewInspector(ctx context.Context, o *InspectorOption) (*Inspector, error) {
}, &retry.Options{
MaxRetry: ins.maxRetry,
Delay: ins.delay,
IsErrorRetryable: func(err error) bool {
if !retry.IsErrorRetryable(err) {
return false
}
// https://github.com/cnrancher/hangar/issues/44
// Harbor response non-standard error code, need to detect the
// error content again to avoid the retry warning message.
s := err.Error()
switch {
case strings.Contains(s, "not found") ||
strings.Contains(s, "manifest unknow"):
return false
}
return true
},
})
if err != nil {
return nil, err
Expand Down

0 comments on commit d2f9544

Please sign in to comment.