Skip to content
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
28 changes: 15 additions & 13 deletions src/jobservice/job/known_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package job

import "github.com/goharbor/harbor/src/lib"

// Define the register name constants of known jobs

const (
Expand Down Expand Up @@ -53,19 +55,19 @@ const (
var (
// executionSweeperCount stores the count for execution retained
executionSweeperCount = map[string]int64{
ImageScanJobVendorType: 1,
SBOMJobVendorType: 1,
ScanAllVendorType: 1,
PurgeAuditVendorType: 10,
ExecSweepVendorType: 10,
GarbageCollectionVendorType: 50,
SlackJobVendorType: 50,
WebhookJobVendorType: 50,
ReplicationVendorType: 50,
ScanDataExportVendorType: 50,
SystemArtifactCleanupVendorType: 50,
P2PPreheatVendorType: 50,
RetentionVendorType: 50,
ImageScanJobVendorType: lib.GetEnvInt64("IMAGE_SCAN_EXECUTION_RETENTION_COUNT", 1),
SBOMJobVendorType: lib.GetEnvInt64("SBOM_EXECUTION_RETENTION_COUNT", 1),
ScanAllVendorType: lib.GetEnvInt64("SCAN_ALL_EXECUTION_RETENTION_COUNT", 1),
PurgeAuditVendorType: lib.GetEnvInt64("PURGE_AUDIT_EXECUTION_RETENTION_COUNT", 10),
ExecSweepVendorType: lib.GetEnvInt64("EXECUTION_SWEEP_EXECUTION_RETENTION_COUNT", 10),
GarbageCollectionVendorType: lib.GetEnvInt64("GARBAGE_COLLECTION_EXECUTION_RETENTION_COUNT", 50),
SlackJobVendorType: lib.GetEnvInt64("SLACK_EXECUTION_RETENTION_COUNT", 50),
WebhookJobVendorType: lib.GetEnvInt64("WEBHOOK_EXECUTION_RETENTION_COUNT", 50),
ReplicationVendorType: lib.GetEnvInt64("REPLICATION_EXECUTION_RETENTION_COUNT", 50),
ScanDataExportVendorType: lib.GetEnvInt64("SCAN_DATA_EXPORT_EXECUTION_RETENTION_COUNT", 50),
SystemArtifactCleanupVendorType: lib.GetEnvInt64("SYSTEM_ARTIFACT_CLEANUP_EXECUTION_RETENTION_COUNT", 50),
P2PPreheatVendorType: lib.GetEnvInt64("P2P_PREHEAT_EXECUTION_RETENTION_COUNT", 50),
RetentionVendorType: lib.GetEnvInt64("RETENTION_EXECUTION_RETENTION_COUNT", 50),
}
)

Expand Down
35 changes: 35 additions & 0 deletions src/lib/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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 lib

import (
"os"
"strconv"
)

// GetEnvInt64 reads an environment variable and converts it to an int64, returning the default value if not set or invalid.
func GetEnvInt64(envKey string, defaultValue int64) int64 {
value := os.Getenv(envKey)
if value == "" {
return defaultValue
}

intValue, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return defaultValue
}

return intValue
}
83 changes: 83 additions & 0 deletions src/lib/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package lib

import (
"os"
"testing"
)

func TestGetEnvInt64(t *testing.T) {
tests := []struct {
name string
envKey string
envValue string
defaultValue int64
setEnv bool
expectedValue int64
}{
{
name: "env set with valid value",
envKey: "TEST_ENV",
envValue: "100",
defaultValue: 50,
setEnv: true,
expectedValue: 100,
},
{
name: "env not set",
envKey: "UNSET_ENV",
envValue: "",
defaultValue: 50,
setEnv: false,
expectedValue: 50,
},
{
name: "env set with invalid value",
envKey: "INVALID_ENV",
envValue: "not_a_number",
defaultValue: 50,
setEnv: true,
expectedValue: 50,
},
{
name: "env set with zero",
envKey: "ZERO_ENV",
envValue: "0",
defaultValue: 50,
setEnv: true,
expectedValue: 0,
},
{
name: "env set with negative value",
envKey: "NEGATIVE_ENV",
envValue: "-10",
defaultValue: 50,
setEnv: true,
expectedValue: -10,
},
{
name: "env set with large value",
envKey: "LARGE_ENV",
envValue: "9223372036854775807",
defaultValue: 50,
setEnv: true,
expectedValue: 9223372036854775807,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.setEnv {
os.Setenv(tt.envKey, tt.envValue)
} else {
os.Unsetenv(tt.envKey)
}

result := GetEnvInt64(tt.envKey, tt.defaultValue)
if result != tt.expectedValue {
t.Errorf("GetEnvInt64(%q, %d) = %d; want %d", tt.envKey, tt.defaultValue, result, tt.expectedValue)
}

os.Unsetenv(tt.envKey)
})
}
}
Loading