Skip to content

Commit

Permalink
✨ Update blob cacher (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
tosone authored Mar 17, 2024
1 parent 5ebebc2 commit 2fa292c
Show file tree
Hide file tree
Showing 15 changed files with 225 additions and 3,574 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ ifeq ($(shell test -e ./Dockerfile && echo -n yes),yes)
endif

lint-go: ## Use golintci-lint on your project
@golangci-lint run --deadline=10m
@golangci-lint run --deadline=10m --build-tags "timetzdata,exclude_graphdriver_devicemapper,exclude_graphdriver_btrfs,containers_image_openpgp"

lint-yaml: ## Use yamllint on the yaml file of your projects
ifeq ($(EXPORT_RESULT), true)
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ require (
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jackc/pgx/v5 v5.4.3 // indirect
github.com/jackc/pgx/v5 v5.5.4 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect
Expand Down Expand Up @@ -341,7 +342,7 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20240116215550-a9fa1716bcac // indirect
google.golang.org/grpc v1.61.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/go-jose/go-jose.v2 v2.6.1 // indirect
gopkg.in/go-jose/go-jose.v2 v2.6.3 // indirect
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
19 changes: 6 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,6 @@ github.com/dnaeon/go-vcr v1.0.1/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyG
github.com/dnaeon/go-vcr v1.1.0/go.mod h1:M7tiix8f0r6mKKJ3Yq/kqU1OYf3MnfmBWVbPx/yU9ko=
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
github.com/docker/cli v0.0.0-20191017083524-a8ff7f821017/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
github.com/docker/cli v25.0.3+incompatible h1:KLeNs7zws74oFuVhgZQ5ONGZiXUUdgsdy6/EsX/6284=
github.com/docker/cli v25.0.3+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
github.com/docker/cli v25.0.4+incompatible h1:DatRkJ+nrFoYL2HZUzjM5Z5sAmcA5XGp+AW0oEw2+cA=
github.com/docker/cli v25.0.4+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
github.com/docker/distribution v0.0.0-20190905152932-14b96e55d84c/go.mod h1:0+TTO4EOBfRPhZXAeF1Vu+W3hHZ8eLp8PgKVZlcvtFY=
Expand Down Expand Up @@ -606,8 +604,6 @@ github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbS
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/fullsailor/pkcs7 v0.0.0-20190404230743-d7302db945fa/go.mod h1:KnogPXtdwXqoenmZCw6S+25EAm2MkxbG0deNDu4cbSA=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/garyburd/redigo v0.0.0-20150301180006-535138d7bcd7/go.mod h1:NR3MbYisc3/PwhQ00EMzDiPmrwpPxAn5GI05/YaO1SY=
Expand Down Expand Up @@ -638,8 +634,6 @@ github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-ini/ini v1.25.4/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/go-jose/go-jose/v3 v3.0.1 h1:pWmKFVtt+Jl0vBZTIpz/eAKwsm6LkIxDVVbFHKkchhA=
github.com/go-jose/go-jose/v3 v3.0.1/go.mod h1:RNkWWRld676jZEYoV3+XK8L2ZnNSvIsxFMht0mSX+u8=
github.com/go-jose/go-jose/v3 v3.0.3 h1:fFKWeig/irsp7XD2zBxvnmA/XaRWp5V3CBsZXJF7G7k=
github.com/go-jose/go-jose/v3 v3.0.3/go.mod h1:5b+7YgP7ZICgJDBdfjZaIt+H/9L9T/YQrVfLAMboGkQ=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
Expand Down Expand Up @@ -1027,13 +1021,13 @@ github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQ
github.com/jackc/pgx/v4 v4.12.1-0.20210724153913-640aa07df17c/go.mod h1:1QD0+tgSXP7iUjYm9C1NxKhny7lq6ee99u/z+IHFcgs=
github.com/jackc/pgx/v4 v4.18.3 h1:dE2/TrEsGX3RBprb3qryqSV9Y60iZN1C6i8IrmW9/BA=
github.com/jackc/pgx/v4 v4.18.3/go.mod h1:Ey4Oru5tH5sB6tV7hDmfWFahwF15Eb7DNXlRKx2CkVw=
github.com/jackc/pgx/v5 v5.4.3 h1:cxFyXhxlvAifxnkKKdlxv8XqUf59tDlYjnV5YYfsJJY=
github.com/jackc/pgx/v5 v5.4.3/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA=
github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/pgx/v5 v5.5.4 h1:Xp2aQS8uXButQdnCMWNmvx6UysWQQC+u1EoizjguY+8=
github.com/jackc/pgx/v5 v5.5.4/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jcmturner/aescts/v2 v2.0.0 h1:9YKLH6ey7H4eDBXW8khjYslgyqG2xZikXP0EQFKrle8=
Expand Down Expand Up @@ -1783,7 +1777,6 @@ golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190923035154-9ee001bba392/go.mod h1:/lpIB1dKB+9EgE3H3cr1v9wB50oz8l4C4h62xy7jSTY=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand Down Expand Up @@ -2384,8 +2377,8 @@ gopkg.in/cheggaaa/pb.v1 v1.0.25/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qS
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2/go.mod h1:Xk6kEKp8OKb+X14hQBKWaSkCsqBpgog8nAV2xsGOxlo=
gopkg.in/go-jose/go-jose.v2 v2.6.1 h1:qEzJlIDmG9q5VO0M/o8tGS65QMHMS1w01TQJB1VPJ4U=
gopkg.in/go-jose/go-jose.v2 v2.6.1/go.mod h1:zzZDPkNNw/c9IE7Z9jr11mBZQhKQTMzoEEIoEdZlFBI=
gopkg.in/go-jose/go-jose.v2 v2.6.3 h1:nt80fvSDlhKWQgSWyHyy5CfmlQr+asih51R8PTWNKKs=
gopkg.in/go-jose/go-jose.v2 v2.6.3/go.mod h1:zzZDPkNNw/c9IE7Z9jr11mBZQhKQTMzoEEIoEdZlFBI=
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s=
Expand Down
3 changes: 2 additions & 1 deletion pkg/daemon/pushed/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func init() {
}
return r.run(ctx, payload)
},
MaxRetry: 6,
MaxRetry: 1,
Concurrency: 10,
Timeout: time.Minute * 10,
}
Expand Down Expand Up @@ -87,6 +87,7 @@ func (r runnerArtifact) run(ctx context.Context, payload types.DaemonArtifactPus
query.Repository.Size.ColumnName().String(): repositorySize,
})
if err != nil {
log.Error().Err(err).Msg("Update repository failed")
return err
}
return nil
Expand Down
10 changes: 6 additions & 4 deletions pkg/daemon/scan/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func runnerSbom(ctx context.Context, artifact *models.Artifact, statusChan chan
cmd.Env = append(cmd.Env, "SYFT_REGISTRY_INSECURE_SKIP_TLS_VERIFY=true")
}

log.Info().Str("artifactDigest", artifact.Digest).Strs("env", cmd.Env).Str("command", cmd.String()).Msg("Start sbom artifact")
log.Info().Str("artifactDigest", artifact.Digest).Strs("env", cmd.Env).Str("cmd", cmd.String()).Msg("Start sbom artifact")

defer func() {
if utils.IsFile(filename) {
Expand All @@ -112,12 +112,14 @@ func runnerSbom(ctx context.Context, artifact *models.Artifact, statusChan chan

err = cmd.Run()
if err != nil {
log.Error().Err(err).Msg("Run syft failed")
var stdoutBytes = stdout.Bytes()
var stderrBytes = stderr.Bytes()
log.Error().Err(err).Str("stdout", string(stdoutBytes)).Str("stderr", string(stderrBytes)).Msg("Run syft failed")
statusChan <- decoratorArtifactStatus{
Daemon: enums.DaemonSbom,
Status: enums.TaskCommonStatusFailed,
Stdout: stdout.Bytes(),
Stderr: stderr.Bytes(),
Stdout: stdoutBytes,
Stderr: stderrBytes,
Message: fmt.Sprintf("Run syft failed: %s", err.Error()),
}
return err
Expand Down
12 changes: 7 additions & 5 deletions pkg/daemon/scan/vulnerability.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ func runnerVulnerability(ctx context.Context, artifact *models.Artifact, statusC
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()

log.Info().Str("artifactDigest", artifact.Digest).Str("command", cmd.String()).Msg("Start scan artifact")
log.Info().Str("artifactDigest", artifact.Digest).Str("cmd", cmd.String()).Msg("Start scan artifact")

defer func() {
if utils.IsFile(filename) {
Expand All @@ -107,13 +106,16 @@ func runnerVulnerability(ctx context.Context, artifact *models.Artifact, statusC
}
}()

err = cmd.Run()
if err != nil {
log.Error().Err(err).Msg("Run trivy failed")
var stdoutBytes = stdout.Bytes()
var stderrBytes = stderr.Bytes()
log.Error().Err(err).Str("stdout", string(stdoutBytes)).Str("stderr", string(stderrBytes)).Strs("env", cmd.Env).Str("cmd", cmd.String()).Msg("Run trivy failed")
statusChan <- decoratorArtifactStatus{
Daemon: enums.DaemonVulnerability,
Status: enums.TaskCommonStatusFailed,
Stdout: stdout.Bytes(),
Stderr: stderr.Bytes(),
Stdout: stdoutBytes,
Stderr: stderrBytes,
Message: fmt.Sprintf("Run trivy failed: %s", err.Error()),
}
return err
Expand Down
10 changes: 2 additions & 8 deletions pkg/dal/dao/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,10 @@ func (s *repositoryService) UpdateRepository(ctx context.Context, id int64, upda
if len(updates) == 0 {
return nil
}
result, err := s.tx.Repository.WithContext(ctx).Where(s.tx.Repository.ID.Eq(id)).UpdateColumns(updates)
_, err := s.tx.Repository.WithContext(ctx).Where(s.tx.Repository.ID.Eq(id)).UpdateColumns(updates)
if err != nil {
return err
}
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
return nil
}

Expand All @@ -346,13 +343,10 @@ func (s *repositoryService) CountRepository(ctx context.Context, namespaceID int

// DeleteByID deletes the repository with the specified repository ID.
func (s *repositoryService) DeleteByID(ctx context.Context, id int64) error {
matched, err := s.tx.Repository.WithContext(ctx).Where(s.tx.Repository.ID.Eq(id)).Delete()
_, err := s.tx.Repository.WithContext(ctx).Where(s.tx.Repository.ID.Eq(id)).Delete()
if err != nil {
return err
}
if matched.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
return nil
}

Expand Down
7 changes: 0 additions & 7 deletions pkg/dal/dao/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"gorm.io/gorm"

"github.com/go-sigma/sigma/pkg/dal"
"github.com/go-sigma/sigma/pkg/dal/dao"
Expand Down Expand Up @@ -112,15 +111,9 @@ func TestRepositoryService(t *testing.T) {
err = repositoryService.UpdateRepository(ctx, repository1.ID, map[string]any{"description": ptr.Of("test"), "overview": []byte("test")})
assert.NoError(t, err)

err = repositoryService.UpdateRepository(ctx, 10, map[string]any{"description": ptr.Of("test"), "overview": []byte("test")})
assert.ErrorIs(t, err, gorm.ErrRecordNotFound)

err = repositoryService.DeleteByID(ctx, repositoryObj.ID)
assert.NoError(t, err)

err = repositoryService.DeleteByID(ctx, repositoryObj.ID)
assert.ErrorIs(t, err, gorm.ErrRecordNotFound)

return nil
})
assert.NoError(t, err)
Expand Down
14 changes: 3 additions & 11 deletions pkg/handlers/distribution/blob/blob_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"github.com/rs/zerolog/log"
"gorm.io/gorm"

"github.com/go-sigma/sigma/pkg/consts"
"github.com/go-sigma/sigma/pkg/dal/models"
"github.com/go-sigma/sigma/pkg/types/enums"
"github.com/go-sigma/sigma/pkg/utils"
"github.com/go-sigma/sigma/pkg/utils/imagerefs"
Expand All @@ -39,15 +37,9 @@ import (
func (h *handler) DeleteBlob(c echo.Context) error {
ctx := log.Logger.WithContext(c.Request().Context())

iuser := c.Get(consts.ContextUser)
if iuser == nil {
log.Error().Msg("Get user from header failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
}
user, ok := iuser.(*models.User)
if !ok {
log.Error().Msg("Convert user from header failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
user, err := utils.GetUserFromCtx(c)
if err != nil {
return err
}

uri := c.Request().URL.Path
Expand Down
12 changes: 3 additions & 9 deletions pkg/handlers/distribution/blob/blob_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,9 @@ import (
func (h *handler) GetBlob(c echo.Context) error {
ctx := log.Logger.WithContext(c.Request().Context())

iuser := c.Get(consts.ContextUser)
if iuser == nil {
log.Error().Msg("Get user from header failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
}
user, ok := iuser.(*models.User)
if !ok {
log.Error().Msg("Convert user from header failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
user, err := utils.GetUserFromCtx(c)
if err != nil {
return err
}

uri := c.Request().URL.Path
Expand Down
69 changes: 6 additions & 63 deletions pkg/handlers/distribution/blob/blob_head.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"errors"
"fmt"
"net/http"
"strconv"
"strings"

"github.com/labstack/echo/v4"
Expand All @@ -27,9 +26,6 @@ import (
"gorm.io/gorm"

"github.com/go-sigma/sigma/pkg/consts"
"github.com/go-sigma/sigma/pkg/dal/models"
"github.com/go-sigma/sigma/pkg/handlers/distribution/clients"
"github.com/go-sigma/sigma/pkg/modules/cacher"
"github.com/go-sigma/sigma/pkg/types/enums"
"github.com/go-sigma/sigma/pkg/utils"
"github.com/go-sigma/sigma/pkg/utils/imagerefs"
Expand All @@ -42,15 +38,9 @@ import (
func (h *handler) HeadBlob(c echo.Context) error {
ctx := log.Logger.WithContext(c.Request().Context())

iuser := c.Get(consts.ContextUser)
if iuser == nil {
log.Error().Msg("Get user from header failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
}
user, ok := iuser.(*models.User)
if !ok {
log.Error().Msg("Convert user from header failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
user, err := utils.GetUserFromCtx(c)
if err != nil {
return err
}

uri := c.Request().URL.Path
Expand Down Expand Up @@ -90,59 +80,12 @@ func (h *handler) HeadBlob(c echo.Context) error {
return xerrors.NewDSError(c, xerrors.DSErrCodeDigestInvalid)
}
c.Response().Header().Set(consts.ContentDigest, dgest.String())

cache, err := cacher.New(consts.CacherBlob, func(key string) (*models.Blob, error) {
dgest, err := digest.Parse(key)
if err != nil {
log.Error().Err(err).Str("digest", key).Msg("Parse digest failed")
return nil, xerrors.DSErrCodeUnknown
}
blobService := h.blobServiceFactory.New()
blob, err := blobService.FindByDigest(ctx, dgest.String())
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
if !h.config.Proxy.Enabled {
log.Error().Err(err).Str("digest", dgest.String()).Msg("Blob not found")
return nil, xerrors.DSErrCodeBlobUnknown
}
f := clients.NewClientsFactory()
cli, err := f.New(ptr.To(h.config))
if err != nil {
log.Error().Err(err).Str("digest", dgest.String()).Msg("New proxy server failed")
return nil, xerrors.DSErrCodeUnknown
}
statusCode, header, _, err := cli.DoRequest(ctx, c.Request().Method, c.Request().URL.Path, nil)
if err != nil {
log.Error().Err(err).Str("digest", dgest.String()).Msg("Request proxy server failed")
return nil, xerrors.DSErrCodeUnknown
}
if statusCode != http.StatusOK {
log.Error().Err(err).Str("digest", dgest.String()).Int("statusCode", statusCode).Msg("Request proxy server failed")
return nil, xerrors.DSErrCodeUnknown
}
contentLength, err := strconv.ParseInt(header.Get(echo.HeaderContentLength), 10, 64)
if err != nil {
log.Error().Err(err).Str("digest", dgest.String()).Msg("Parse content length failed")
return nil, xerrors.DSErrCodeUnknown
}
blob = &models.Blob{
Digest: dgest.String(),
Size: contentLength,
ContentType: header.Get(echo.HeaderContentType),
}
c.Response().Header().Set("Content-Length", header.Get(echo.HeaderContentLength))
return blob, nil
}
log.Error().Err(err).Str("digest", dgest.String()).Msg("Check blob exist failed")
return nil, xerrors.DSErrCodeBlobUnknown
}
return blob, nil
})
cacher, err := h.blobCacher(c)
if err != nil {
log.Error().Err(err).Msg("Head blob failed")
log.Error().Err(err).Msg("New blob cacher failed")
return xerrors.NewDSError(c, xerrors.DSErrCodeUnknown)
}
blobObj, err := cache.Get(ctx, dgest.String())
blobObj, err := cacher.Get(ctx, dgest.String())
if err != nil {
if err, ok := err.(xerrors.ErrCode); ok {
return xerrors.NewDSError(c, err)
Expand Down
Loading

0 comments on commit 2fa292c

Please sign in to comment.