feat: Optimize quota checking when pushing images (#17392)

Signed-off-by: lengrongfu <1275177125@qq.com>
This commit is contained in:
rongfu.leng 2023-06-05 17:19:55 +08:00 committed by GitHub
parent fbeeaa7537
commit 8fe578e7ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 3 deletions

View File

@ -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{})
}

View File

@ -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
}

View File

@ -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()

View File

@ -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) {