From 8fa03e373906ec2bdb588727a9d291ea66191d6c Mon Sep 17 00:00:00 2001 From: He Weiwei Date: Tue, 22 Dec 2020 11:39:18 +0800 Subject: [PATCH] refactor: remove code of scan all job (#13821) Remove code of scan all job as it's implemented by execution now. Signed-off-by: He Weiwei --- src/jobservice/runtime/bootstrap.go | 8 ++- src/pkg/scan/all/job.go | 83 ----------------------------- src/pkg/scan/all/stats.go | 35 ------------ 3 files changed, 3 insertions(+), 123 deletions(-) delete mode 100644 src/pkg/scan/all/job.go delete mode 100644 src/pkg/scan/all/stats.go diff --git a/src/jobservice/runtime/bootstrap.go b/src/jobservice/runtime/bootstrap.go index 31b01bc25..eb385d7bb 100644 --- a/src/jobservice/runtime/bootstrap.go +++ b/src/jobservice/runtime/bootstrap.go @@ -17,7 +17,6 @@ package runtime import ( "context" "fmt" - redislib "github.com/goharbor/harbor/src/lib/redis" "os" "os/signal" "sync" @@ -44,10 +43,10 @@ import ( "github.com/goharbor/harbor/src/jobservice/worker" "github.com/goharbor/harbor/src/jobservice/worker/cworker" "github.com/goharbor/harbor/src/lib/errors" + redislib "github.com/goharbor/harbor/src/lib/redis" "github.com/goharbor/harbor/src/pkg/p2p/preheat" "github.com/goharbor/harbor/src/pkg/retention" - sc "github.com/goharbor/harbor/src/pkg/scan" - "github.com/goharbor/harbor/src/pkg/scan/all" + "github.com/goharbor/harbor/src/pkg/scan" "github.com/goharbor/harbor/src/pkg/scheduler" "github.com/gomodule/redigo/redis" ) @@ -255,8 +254,7 @@ func (bs *Bootstrap) loadAndRunRedisWorkerPool( // Only for debugging and testing purpose job.SampleJob: (*sample.Job)(nil), // Functional jobs - job.ImageScanJob: (*sc.Job)(nil), - job.ImageScanAllJob: (*all.Job)(nil), + job.ImageScanJob: (*scan.Job)(nil), job.ImageGC: (*gc.GarbageCollector)(nil), job.ImageGCReadOnly: (*gcreadonly.GarbageCollector)(nil), job.Replication: (*replication.Replication)(nil), diff --git a/src/pkg/scan/all/job.go b/src/pkg/scan/all/job.go deleted file mode 100644 index 914393540..000000000 --- a/src/pkg/scan/all/job.go +++ /dev/null @@ -1,83 +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 all - -import ( - "github.com/goharbor/harbor/src/jobservice/job" - "github.com/goharbor/harbor/src/lib/errors" -) - -const ( - // The max number of the goroutines to retrieve the tags - maxProcessors = 25 - // Job parameter key for the admin job ID - jobParamAJID = "admin_job_id" -) - -// Job query the DB and Registry for all image and tags, -// then call Harbor's API to scan each of them. -type Job struct{} - -// MaxFails implements the interface in job/Interface -func (sa *Job) MaxFails() uint { - return 1 -} - -// MaxCurrency is implementation of same method in Interface. -func (sa *Job) MaxCurrency() uint { - return 0 -} - -// ShouldRetry implements the interface in job/Interface -func (sa *Job) ShouldRetry() bool { - return false -} - -// Validate implements the interface in job/Interface -func (sa *Job) Validate(params job.Parameters) error { - _, err := parseAJID(params) - if err != nil { - return errors.Wrap(err, "job validation: scan all job") - } - - return nil -} - -// Run implements the interface in job/Interface -func (sa *Job) Run(ctx job.Context, params job.Parameters) error { - logger := ctx.GetLogger() - logger.Info("Scanning all the images in the registry") - - // No need to check error any more as it has been checked in job validation. - requester, _ := parseAJID(params) - - if err := ctx.Checkin(requester); err != nil { - logger.Error(errors.Wrap(err, "check in data: scan all job")) - } - - return nil -} - -func parseAJID(params job.Parameters) (string, error) { - if len(params) > 0 { - if v, ok := params[jobParamAJID]; ok { - if id, y := v.(string); y { - return id, nil - } - } - } - - return "", errors.Errorf("missing required job parameter: %s", jobParamAJID) -} diff --git a/src/pkg/scan/all/stats.go b/src/pkg/scan/all/stats.go deleted file mode 100644 index e2149a65b..000000000 --- a/src/pkg/scan/all/stats.go +++ /dev/null @@ -1,35 +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 all - -// Stats provides the overall progress of the scan all process. -type Stats struct { - Total uint `json:"total"` - // Status including `Success`, `Error` or `Stopped` will be counted as completed. - // This data may be influenced by job retrying - Completed uint `json:"completed"` - Metrics StatusMetrics `json:"metrics"` - Requester string `json:"requester"` - Ongoing bool `json:"ongoing"` // Indicate if the metrics data is stable now -} - -// StatusMetrics contains the metrics of each status. -// The key should be the following valid status texts: -// - "pending" -// - "running" -// - "success" -// - "error" -// - "stopped" -type StatusMetrics map[string]uint