harbor/src/jobservice/job/impl/gc/util.go
MinerYang ebac530b46
add goheader linter settings (#18503)
fix files for goheader linter

fix copyright 2018/2019

Signed-off-by: yminer <yminer@vmware.com>
2023-04-25 11:18:42 +08:00

96 lines
2.8 KiB
Go

// 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 gc
import (
"fmt"
"time"
"github.com/gomodule/redigo/redis"
"github.com/goharbor/harbor/src/jobservice/logger"
"github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/retry"
"github.com/goharbor/harbor/src/pkg/registry"
"github.com/goharbor/harbor/src/pkg/registry/interceptor/readonly"
)
// delKeys ...
func delKeys(con redis.Conn, pattern string) error {
iter := 0
keys := make([]string, 0)
for {
arr, err := redis.Values(con.Do("SCAN", iter, "MATCH", pattern))
if err != nil {
return fmt.Errorf("error retrieving '%s' keys: %s", pattern, err)
}
iter, err = redis.Int(arr[0], nil)
if err != nil {
return fmt.Errorf("unexpected type for Int, got type %T", err)
}
k, err := redis.Strings(arr[1], nil)
if err != nil {
return fmt.Errorf("converts an array command reply to a []string %v", err)
}
keys = append(keys, k...)
if iter == 0 {
break
}
}
for _, key := range keys {
_, err := con.Do("DEL", key)
if err != nil {
return fmt.Errorf("failed to clean registry cache %v", err)
}
}
return nil
}
// v2DeleteManifest calls the registry API to remove manifest
func v2DeleteManifest(logger logger.Interface, repository, digest string) error {
exist, _, err := registry.Cli.ManifestExist(repository, digest)
if err != nil {
return err
}
// it could be happened at remove manifest success but fail to delete harbor DB.
// when the GC job executes again, the manifest should not exist.
if !exist {
return nil
}
return retry.Retry(func() error {
err := registry.Cli.DeleteManifest(repository, digest)
// if the system is in read-only mode, return an Abort error to skip retrying
if err == readonly.Err {
return retry.Abort(err)
}
// If delete returned an err because the manifest is unknown, consider success
if errors.IsNotFoundErr(err) {
return nil
}
return err
}, retry.Callback(func(err error, sleep time.Duration) {
logger.Infof("failed to exec v2DeleteManifest, error: %v, will retry again after: %s", err, sleep)
}))
}
// ignoreNotFound ignores the NotFoundErr error
func ignoreNotFound(f func() error) error {
if err := f(); err != nil && !errors.IsNotFoundErr(err) {
return err
}
return nil
}