diff --git a/src/.golangci.yaml b/src/.golangci.yaml index 0cac18da7..afaf6a395 100644 --- a/src/.golangci.yaml +++ b/src/.golangci.yaml @@ -22,7 +22,7 @@ linters: # - gocyclo # - goimports # - goprintffuncname - # - ineffassign + - ineffassign # - nakedret # - nolintlint # - revive diff --git a/src/controller/replication/policy.go b/src/controller/replication/policy.go index d951f70d4..fcf787c99 100644 --- a/src/controller/replication/policy.go +++ b/src/controller/replication/policy.go @@ -74,7 +74,7 @@ func (c *controller) populateRegistry(ctx context.Context, p *pkgmodel.Policy) ( if err != nil { return nil, err } - var srcRegistryID, destRegistryID int64 = 0, 0 + var srcRegistryID, destRegistryID int64 if policy.SrcRegistry != nil && policy.SrcRegistry.ID != 0 { srcRegistryID = policy.SrcRegistry.ID destRegistryID = 0 diff --git a/src/jobservice/job/impl/replication/replication.go b/src/jobservice/job/impl/replication/replication.go index 24c260197..14c890d73 100644 --- a/src/jobservice/job/impl/replication/replication.go +++ b/src/jobservice/job/impl/replication/replication.go @@ -93,7 +93,7 @@ func parseParams(params map[string]interface{}) (*model.Resource, *model.Resourc if err := parseParam(params, "dst_resource", dst); err != nil { return nil, nil, 0, err } - var speed int32 = 0 + var speed int32 value, exist := params["speed"] if !exist { speed = 0 diff --git a/src/jobservice/migration/migrator_v180.go b/src/jobservice/migration/migrator_v180.go index 5b9ea23a4..7fc37f0d7 100644 --- a/src/jobservice/migration/migrator_v180.go +++ b/src/jobservice/migration/migrator_v180.go @@ -121,6 +121,10 @@ func (pm *PolicyMigrator) Migrate() error { // Transaction err = conn.Send("MULTI") + if err != nil { + logger.Errorf("send command MULTI failed with error: %s", err) + continue + } setArgs := []interface{}{ fullID, "status", @@ -135,6 +139,10 @@ func (pm *PolicyMigrator) Migrate() error { } // Set fields err = conn.Send("HMSET", setArgs...) + if err != nil { + logger.Errorf("send command HMSET failed with error: %s", err) + continue + } // Remove useless fields rmArgs := []interface{}{ @@ -160,6 +168,10 @@ func (pm *PolicyMigrator) Migrate() error { if rawJSON, er := policy.Serialize(); er == nil { // Remove the old one first err = conn.Send("ZREMRANGEBYSCORE", rds.KeyPeriodicPolicy(pm.namespace), numbericPolicyID, numbericPolicyID) + if err != nil { + logger.Errorf("send command ZREMRANGEBYSCORE failed with error: %s", err) + continue + } // Save back to the rdb err = conn.Send("ZADD", rds.KeyPeriodicPolicy(pm.namespace), numbericPolicyID, rawJSON) } else { diff --git a/src/lib/redis/helper.go b/src/lib/redis/helper.go deleted file mode 100644 index a93b2b84c..000000000 --- a/src/lib/redis/helper.go +++ /dev/null @@ -1,72 +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 redis - -import ( - "os" - "strconv" - "sync" - "time" - - "github.com/gomodule/redigo/redis" -) - -var ( - pool *redis.Pool - poolOnce sync.Once - - poolMaxIdle = 200 - poolMaxActive = 1000 - poolIdleTimeout int64 = 180 - dialConnectionTimeout = 30 * time.Second - dialReadTimeout = 10 * time.Second - dialWriteTimeout = 10 * time.Second -) - -// DefaultPool return default redis pool -func DefaultPool() *redis.Pool { - poolOnce.Do(func() { - maxIdle, err := strconv.Atoi(os.Getenv("REDIS_POOL_MAX_IDLE")) - if err != nil || maxIdle < 0 { - maxIdle = poolMaxIdle - } - - maxActive, err := strconv.Atoi(os.Getenv("REDIS_POOL_MAX_ACTIVE")) - if err != nil || maxActive < 0 { - maxActive = poolMaxActive - } - - idleTimeout, err := strconv.ParseInt(os.Getenv("REDIS_POOL_IDLE_TIMEOUT"), 10, 64) - if err != nil || idleTimeout < 0 { - idleTimeout = poolIdleTimeout - } - - // get _REDIS_URL_REG from environment directly here to avoid cyclic dependency - url := os.Getenv("_REDIS_URL_REG") - if url == "" { - url = "redis://localhost:6379/1" - } - pool, err = GetRedisPool("CommonRedis", url, &PoolParam{ - PoolMaxIdle: maxIdle, - PoolMaxActive: maxActive, - PoolIdleTimeout: time.Duration(idleTimeout) * time.Second, - DialConnectionTimeout: dialConnectionTimeout, - DialReadTimeout: dialReadTimeout, - DialWriteTimeout: dialWriteTimeout, - }) - }) - - return pool -} diff --git a/src/lib/redis/helper_test.go b/src/lib/redis/helper_test.go deleted file mode 100644 index 1527376cd..000000000 --- a/src/lib/redis/helper_test.go +++ /dev/null @@ -1,40 +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 redis - -import ( - "fmt" - "github.com/stretchr/testify/require" - "os" - "testing" -) - -const testingRedisHost = "REDIS_HOST" - -func TestGetRedisPool(t *testing.T) { - pool, err := GetRedisPool("test", fmt.Sprintf("redis://%s:%d", getRedisHost(), 6379), nil) - require.Nil(t, err) - conn := pool.Get() - defer conn.Close() -} - -func getRedisHost() string { - redisHost := os.Getenv(testingRedisHost) - if redisHost == "" { - redisHost = "127.0.0.1" // for local test - } - - return redisHost -} diff --git a/src/pkg/reg/adapter/aliacr/adapter.go b/src/pkg/reg/adapter/aliacr/adapter.go index c9cc9810f..23f97068b 100644 --- a/src/pkg/reg/adapter/aliacr/adapter.go +++ b/src/pkg/reg/adapter/aliacr/adapter.go @@ -167,7 +167,7 @@ func getAdapterInfo() *model.AdapterPattern { func (a *adapter) listNamespaces(c *cr.Client) (namespaces []string, err error) { // list namespaces var nsReq = cr.CreateGetNamespaceListRequest() - var nsResp = cr.CreateGetNamespaceListResponse() + var nsResp *cr.GetNamespaceListResponse nsReq.SetDomain(a.domain) nsResp, err = c.GetNamespaceList(nsReq) if err != nil { diff --git a/src/pkg/reg/adapter/aliacr/auth.go b/src/pkg/reg/adapter/aliacr/auth.go index 7d611c9eb..af74ff725 100644 --- a/src/pkg/reg/adapter/aliacr/auth.go +++ b/src/pkg/reg/adapter/aliacr/auth.go @@ -50,7 +50,7 @@ func (a *aliyunAuthCredential) Modify(r *http.Request) (err error) { } var tokenRequest = cr.CreateGetAuthorizationTokenRequest() - var tokenResponse = cr.CreateGetAuthorizationTokenResponse() + var tokenResponse *cr.GetAuthorizationTokenResponse tokenRequest.SetDomain(fmt.Sprintf(endpointTpl, a.region)) tokenResponse, err = client.GetAuthorizationToken(tokenRequest) if err != nil { diff --git a/src/pkg/reg/adapter/tencentcr/adapter.go b/src/pkg/reg/adapter/tencentcr/adapter.go index e101ef7d5..dbf159d31 100644 --- a/src/pkg/reg/adapter/tencentcr/adapter.go +++ b/src/pkg/reg/adapter/tencentcr/adapter.go @@ -121,7 +121,7 @@ func newAdapter(registry *model.Registry) (a *adapter, err error) { Values: []*string{common.StringPtr(strings.ReplaceAll(registryURL.Host, ".tencentcloudcr.com", ""))}, }, } - var resp = tcr.NewDescribeInstancesResponse() + var resp *tcr.DescribeInstancesResponse resp, err = client.DescribeInstances(req) if err != nil { log.Errorf("DescribeInstances error=%s", err.Error()) diff --git a/src/pkg/reg/adapter/tencentcr/sdk.go b/src/pkg/reg/adapter/tencentcr/sdk.go index bb1067fa4..376ae39af 100644 --- a/src/pkg/reg/adapter/tencentcr/sdk.go +++ b/src/pkg/reg/adapter/tencentcr/sdk.go @@ -54,7 +54,7 @@ func (a *adapter) createRepository(namespace, repository string) (err error) { repoReq.RegistryId = a.registryID repoReq.NamespaceName = &namespace repoReq.RepositoryName = &repository - var repoResp = tcr.NewDescribeRepositoriesResponse() + var repoResp *tcr.DescribeRepositoriesResponse repoResp, err = a.tcrClient.DescribeRepositories(repoReq) if err != nil { return @@ -69,7 +69,7 @@ func (a *adapter) createRepository(namespace, repository string) (err error) { req.NamespaceName = &namespace req.RepositoryName = &repository req.RegistryId = a.registryID - var resp = tcr.NewCreateRepositoryResponse() + var resp *tcr.CreateRepositoryResponse resp, err = a.tcrClient.CreateRepository(req) if err != nil { log.Debugf("[tencent-tcr.PrepareForPush.createRepository] error=%v", err) @@ -124,7 +124,7 @@ func (a *adapter) isNamespaceExist(namespace string) (exist bool, err error) { var req = tcr.NewDescribeNamespacesRequest() req.NamespaceName = &namespace req.RegistryId = a.registryID - var resp = tcr.NewDescribeNamespacesResponse() + var resp *tcr.DescribeNamespacesResponse resp, err = a.tcrClient.DescribeNamespaces(req) if err != nil { return diff --git a/src/pkg/replication/dao/dao.go b/src/pkg/replication/dao/dao.go index 901c3d7d8..6beb2ad56 100644 --- a/src/pkg/replication/dao/dao.go +++ b/src/pkg/replication/dao/dao.go @@ -101,9 +101,13 @@ func (d *dao) Update(ctx context.Context, policy *model.Policy, props ...string) return err } n, err := ormer.Update(policy, props...) - if e := orm.AsConflictError(err, "replication policy %s already exists", policy.Name); e != nil { - err = e + if err != nil { + if e := orm.AsConflictError(err, "replication policy %s already exists", policy.Name); e != nil { + err = e + } + return err } + if n == 0 { return errors.NotFoundError(nil).WithMessage("replication policy %d not found", policy.ID) } diff --git a/src/pkg/task/dao/task.go b/src/pkg/task/dao/task.go index 0edcfbeba..d8e0dd390 100644 --- a/src/pkg/task/dao/task.go +++ b/src/pkg/task/dao/task.go @@ -203,6 +203,9 @@ func (t *taskDAO) GetMaxEndTime(ctx context.Context, executionID int64) (time.Ti var endTime time.Time err = ormer.Raw("select max(end_time) from task where execution_id = ?", executionID). QueryRow(&endTime) + if err != nil { + return time.Time{}, err + } return endTime, nil } diff --git a/src/server/registry/catalog.go b/src/server/registry/catalog.go index 13e3d195b..0e9058fe2 100644 --- a/src/server/registry/catalog.go +++ b/src/server/registry/catalog.go @@ -75,7 +75,7 @@ func (r *repositoryHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) } // handle the pagination - resRepos := repoNames + var resRepos []string repoNamesLen := len(repoNames) // with "last", get items form lastEntryIndex+1 to lastEntryIndex+maxEntries // without "last", get items from 0 to maxEntries' diff --git a/src/server/v2.0/handler/config.go b/src/server/v2.0/handler/config.go index 09c7f04c7..240ece59c 100644 --- a/src/server/v2.0/handler/config.go +++ b/src/server/v2.0/handler/config.go @@ -108,6 +108,9 @@ func (c *configAPI) GetInternalconfig(ctx context.Context, params configure.GetI return c.SendError(ctx, err) } cfg, err := c.controller.AllConfigs(ctx) + if err != nil { + return c.SendError(ctx, err) + } resultCfg, err := c.controller.ConvertForGet(ctx, cfg, true) if err != nil { return c.SendError(ctx, err)