Skip to content

Commit

Permalink
feat: Optimize quota checking when pushing images (goharbor#17392)
Browse files Browse the repository at this point in the history
Signed-off-by: lengrongfu <[email protected]>
  • Loading branch information
lengrongfu authored and WilfredAlmeida committed Jul 8, 2023
1 parent 4734cf5 commit eae5cab
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/controller/quota/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ func (suite *ControllerTestSuite) TestRequestFunctionFailed() {
suite.Error(suite.ctl.Request(ctx, suite.reference, referenceID, resources, func() error { return fmt.Errorf("error") }))
}

func (suite *ControllerTestSuite) TestRequestResourceIsZero() {
suite.PrepareForUpdate(suite.quota, nil)

ctx := orm.NewContext(context.TODO(), &ormtesting.FakeOrmer{})
referenceID := uuid.New().String()
f := func() error {
return nil
}
res := types.ResourceList{types.ResourceStorage: 0}
err := suite.ctl.Request(ctx, suite.reference, referenceID, res, f)
suite.Nil(err)
}

func TestControllerTestSuite(t *testing.T) {
suite.Run(t, &ControllerTestSuite{})
}
2 changes: 1 addition & 1 deletion src/pkg/quota/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func IsSafe(hardLimits types.ResourceList, currentUsed types.ResourceList, newUs
continue
}

if hardLimit == types.UNLIMITED || value == currentUsed[resource] {
if hardLimit == types.UNLIMITED {
continue
}

Expand Down
4 changes: 2 additions & 2 deletions src/server/middleware/quota/post_initiate_blob_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func postInitiateBlobUploadResources(r *http.Request, reference, referenceID str
query := r.URL.Query()
mount := query.Get("mount")
if mount == "" {
// it is not mount blob http request, skip to request the resources
return nil, nil
// it is not mount blob http request, create length is zero resource to check quota is full
return types.ResourceList{types.ResourceStorage: 0}, nil
}

ctx := r.Context()
Expand Down
19 changes: 19 additions & 0 deletions src/server/middleware/quota/post_initiate_blob_upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ func (suite *PostInitiateBlobUploadMiddlewareTestSuite) TestMiddleware() {
PostInitiateBlobUploadMiddleware()(next).ServeHTTP(rr, req)
suite.Equal(http.StatusOK, rr.Code)
}

{
url = "/v2/library/photon/blobs/uploads"
mock.OnAnything(suite.quotaController, "Request").Return(nil).Once().Run(func(args mock.Arguments) {
resources := args.Get(3).(types.ResourceList)
suite.Len(resources, 1)
suite.Equal(resources[types.ResourceStorage], int64(0))

f := args.Get(4).(func() error)
f()
})
mock.OnAnything(suite.quotaController, "GetByRef").Return(&quota.Quota{}, nil).Once()

req := httptest.NewRequest(http.MethodPost, url, nil)
rr := httptest.NewRecorder()

PostInitiateBlobUploadMiddleware()(next).ServeHTTP(rr, req)
suite.Equal(http.StatusOK, rr.Code)
}
}

func TestPostInitiateBlobUploadMiddlewareTestSuite(t *testing.T) {
Expand Down

0 comments on commit eae5cab

Please sign in to comment.