Merge pull request #11468 from wy65701436/remove-count-quota-code

remove the chart handling in quota
This commit is contained in:
He Weiwei 2020-04-07 16:51:07 +08:00 committed by GitHub
commit 3f567514b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 5 additions and 174 deletions

View File

@ -98,15 +98,6 @@ FROM (
) AS s
WHERE artifact.digest=s.digest;
/*repair the count usage as we calculate the count quota against artifact rather than tag*/
/*count=count-(tag count-artifact count)*/
UPDATE quota_usage SET used=jsonb_set(used, '{count}', ((used->>'count')::int - (SELECT (
SELECT COUNT (*) FROM tag
JOIN artifact ON tag.artifact_id=artifact.id
WHERE artifact.project_id=quota_usage.reference_id::int)-(
SELECT COUNT (*) FROM artifact
WHERE project_id=quota_usage.reference_id::int)))::text::jsonb);
/* artifact_reference records the child artifact referenced by parent artifact */
CREATE TABLE artifact_reference
(

View File

@ -1,144 +0,0 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package chartmuseum
import (
"context"
"errors"
"net/http"
"net/url"
"strings"
"sync"
"github.com/goharbor/harbor/src/chartserver"
"github.com/goharbor/harbor/src/common"
"github.com/goharbor/harbor/src/common/config"
commonhttp "github.com/goharbor/harbor/src/common/http"
"github.com/goharbor/harbor/src/controller/project"
)
var (
// Ctl is a global chartmuseum controller instance
Ctl = NewController()
)
// Controller defines the operations related with chartmuseum which only used by quota now
type Controller interface {
// Count returns charts count in the project
Count(ctx context.Context, projectID int64) (int64, error)
// Exist returns true when chart exist in the project
Exist(ctx context.Context, projectID int64, chartName, version string) (bool, error)
}
// NewController creates an instance of the default repository controller
func NewController() Controller {
return &controller{
projectCtl: project.Ctl,
}
}
type controller struct {
projectCtl project.Controller
cc *chartserver.Controller
withChartMuseum bool
initializeError error
initializeOnce sync.Once
}
func (c *controller) initialize() error {
c.initializeOnce.Do(func() {
cfg := config.NewDBCfgManager()
c.withChartMuseum = cfg.Get(common.WithChartMuseum).GetBool()
if !c.withChartMuseum {
return
}
chartEndpoint := strings.TrimSpace(cfg.Get(common.ChartRepoURL).GetString())
if len(chartEndpoint) == 0 {
c.initializeError = errors.New("empty chartmuseum endpoint")
return
}
url, err := url.Parse(strings.TrimSuffix(chartEndpoint, "/"))
if err != nil {
c.initializeError = errors.New("endpoint URL of chart storage server is malformed")
return
}
ctr, err := chartserver.NewController(url)
if err != nil {
c.initializeError = errors.New("failed to initialize chart API controller")
return
}
c.cc = ctr
})
return c.initializeError
}
func (c *controller) Count(ctx context.Context, projectID int64) (int64, error) {
if err := c.initialize(); err != nil {
return 0, err
}
if !c.withChartMuseum {
return 0, nil
}
proj, err := c.projectCtl.Get(ctx, projectID)
if err != nil {
return 0, err
}
count, err := c.cc.GetCountOfCharts([]string{proj.Name})
if err != nil {
return 0, err
}
return int64(count), nil
}
func (c *controller) Exist(ctx context.Context, projectID int64, chartName, version string) (bool, error) {
if err := c.initialize(); err != nil {
return false, err
}
if !c.withChartMuseum {
return false, nil
}
proj, err := c.projectCtl.Get(ctx, projectID)
if err != nil {
return false, err
}
chartVersion, err := c.cc.GetChartVersion(proj.Name, chartName, version)
if err != nil {
var httpErr *commonhttp.Error
if errors.As(err, &httpErr) {
if httpErr.Code == http.StatusNotFound {
return false, nil
}
}
return false, err
}
return !chartVersion.Removed, nil
}

View File

@ -22,9 +22,7 @@ import (
"github.com/goharbor/harbor/src/common"
"github.com/goharbor/harbor/src/common/config"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/controller/artifact"
"github.com/goharbor/harbor/src/controller/blob"
"github.com/goharbor/harbor/src/controller/chartmuseum"
"github.com/goharbor/harbor/src/lib/log"
dr "github.com/goharbor/harbor/src/pkg/quota/driver"
"github.com/goharbor/harbor/src/pkg/types"
@ -39,9 +37,7 @@ type driver struct {
cfg *config.CfgManager
loader *dataloader.Loader
artifactCtl artifact.Controller
blobCtl blob.Controller
chartCtl chartmuseum.Controller
blobCtl blob.Controller
}
func (d *driver) Enabled(ctx context.Context, key string) (bool, error) {
@ -127,10 +123,8 @@ func newDriver() dr.Driver {
loader := dataloader.NewBatchedLoader(getProjectsBatchFn)
return &driver{
cfg: cfg,
loader: loader,
artifactCtl: artifact.Ctl,
blobCtl: blob.Ctl,
chartCtl: chartmuseum.Ctl,
cfg: cfg,
loader: loader,
blobCtl: blob.Ctl,
}
}

View File

@ -21,7 +21,6 @@ import (
"github.com/goharbor/harbor/src/pkg/types"
artifacttesting "github.com/goharbor/harbor/src/testing/controller/artifact"
blobtesting "github.com/goharbor/harbor/src/testing/controller/blob"
charttesting "github.com/goharbor/harbor/src/testing/controller/chartmuseum"
"github.com/goharbor/harbor/src/testing/mock"
"github.com/stretchr/testify/suite"
)
@ -31,7 +30,6 @@ type DriverTestSuite struct {
artifactCtl *artifacttesting.Controller
blobCtl *blobtesting.Controller
chartCtl *charttesting.Controller
d *driver
}
@ -39,12 +37,9 @@ type DriverTestSuite struct {
func (suite *DriverTestSuite) SetupTest() {
suite.artifactCtl = &artifacttesting.Controller{}
suite.blobCtl = &blobtesting.Controller{}
suite.chartCtl = &charttesting.Controller{}
suite.d = &driver{
artifactCtl: suite.artifactCtl,
blobCtl: suite.blobCtl,
chartCtl: suite.chartCtl,
blobCtl: suite.blobCtl,
}
}

View File

@ -17,7 +17,6 @@ package quota
import (
"github.com/goharbor/harbor/src/controller/artifact"
"github.com/goharbor/harbor/src/controller/blob"
"github.com/goharbor/harbor/src/controller/chartmuseum"
"github.com/goharbor/harbor/src/controller/project"
"github.com/goharbor/harbor/src/controller/quota"
)
@ -25,7 +24,6 @@ import (
var (
artifactController = artifact.Ctl
blobController = blob.Ctl
chartController = chartmuseum.Ctl
projectController = project.Ctl
quotaController = quota.Ctl
)

View File

@ -58,7 +58,6 @@ func Test_projectReferenceObject(t *testing.T) {
{"/api/v2.0/projects/library/repositories", args{req("/api/v2.0/projects/library/repositories")}, "project", "1", false},
{"/api/v2.0/projects/demo", args{req("/api/v2.0/projects/demo")}, "", "", true},
{"/api/v2.0/library", args{req("/api/v2.0/library")}, "", "", true},
{"/api/chartrepo/library/charts", args{req("/api/chartrepo/library/charts")}, "project", "1", false},
{"/v2/library/photon/manifests/2.0", args{req("/v2/library/photon/manifests/2.0")}, "project", "1", false},
{"/v2", args{req("/v2")}, "", "", true},
}

View File

@ -32,8 +32,6 @@ func ParseProjectName(r *http.Request) string {
prefixes := []string{
fmt.Sprintf("/api/%s/projects/", api.APIVersion), // v2.0 management APIs
"/api/chartrepo/", // chartmuseum APIs
fmt.Sprintf("/api/%s/chartrepo/", api.APIVersion), // chartmuseum Label APIs
}
for _, prefix := range prefixes {