Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Optimize quota checking when pushing images #17392

Merged
merged 1 commit into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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] {
chlins marked this conversation as resolved.
Show resolved Hide resolved
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