From 5140d4f6e245ea673bd6ac191c8943bff39650b2 Mon Sep 17 00:00:00 2001 From: zc Date: Wed, 28 Feb 2024 20:29:01 +0800 Subject: [PATCH] fix label select db session --- .github/workflows/main.yaml | 8 ++++---- core/service/common/label.go | 14 ++++++++------ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index d87b0e8..249917f 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -13,7 +13,7 @@ jobs: - name: Install Go uses: actions/setup-go@v4 with: - go-version: 1.21 + go-version: 1.21.0 - name: Checkout Code uses: actions/checkout@v3 @@ -22,9 +22,9 @@ jobs: run: go get -v -t -d ./... - name: Lint - uses: golangci/golangci-lint-action@v3 + uses: golangci/golangci-lint-action@v4 with: - version: latest + version: v1.54.2 - name: Test run: make tests @@ -35,7 +35,7 @@ jobs: - name: Install Go uses: actions/setup-go@v4 with: - go-version: 1.21 + go-version: 1.21.0 - name: Checkout Code uses: actions/checkout@v3 diff --git a/core/service/common/label.go b/core/service/common/label.go index f1788be..ed97b69 100644 --- a/core/service/common/label.go +++ b/core/service/common/label.go @@ -19,6 +19,8 @@ import ( "fmt" "strings" + "gorm.io/gorm" + "github.com/99nil/gopkg/sets" storageV1 "github.com/zc2638/ink/pkg/api/storage/v1" @@ -31,7 +33,9 @@ func SelectNamesByLabels(ctx context.Context, kind, namespace string, labels map } db := database.FromContext(ctx) - db = db.Model(&storageV1.Label{}).Where(&storageV1.Label{Namespace: namespace, Kind: kind}) + db = db.Model(&storageV1.Label{}). + Where(&storageV1.Label{Namespace: namespace, Kind: kind}). + Session(&gorm.Session{}) var start bool nameSet := sets.New[string]() @@ -46,11 +50,9 @@ func SelectNamesByLabels(ctx context.Context, kind, namespace string, labels map nameSet.Add(selectNames...) continue } - for _, sn := range selectNames { - if !nameSet.Has(sn) { - nameSet.Remove(sn) - } - } + + selectNameSet := sets.New(selectNames...) + nameSet = nameSet.Intersection(selectNameSet) if nameSet.Len() == 0 { return nil, nil }