Secret/Keepers: Return already exists error from DB when creating keeper (#109782)

This commit is contained in:
Matheus Macabu
2025-08-18 14:42:55 +02:00
committed by GitHub
parent 3857faf7b3
commit 10508d0614
2 changed files with 7 additions and 11 deletions
+2 -1
View File
@@ -10,7 +10,8 @@ import (
)
var (
ErrKeeperNotFound = errors.New("keeper not found")
ErrKeeperNotFound = errors.New("keeper not found")
ErrKeeperAlreadyExists = errors.New("keeper already exists")
)
// KeeperMetadataStorage is the interface for wiring and dependency injection.
+5 -10
View File
@@ -16,6 +16,7 @@ import (
"github.com/grafana/grafana/pkg/registry/apis/secret/contracts"
"github.com/grafana/grafana/pkg/registry/apis/secret/xkube"
"github.com/grafana/grafana/pkg/storage/secret/metadata/metrics"
"github.com/grafana/grafana/pkg/storage/unified/sql"
"github.com/grafana/grafana/pkg/storage/unified/sql/sqltemplate"
)
@@ -91,13 +92,12 @@ func (s *keeperMetadataStorage) Create(ctx context.Context, keeper *secretv1beta
return err
}
// Validate before inserting that any `secureValues` referenced exist and do not reference other third-party keepers.
if err := s.validateSecureValueReferences(ctx, keeper); err != nil {
return err
}
result, err := s.db.ExecContext(ctx, query, req.GetArgs()...)
if err != nil {
if sql.IsRowAlreadyExistsError(err) {
return fmt.Errorf("namespace=%s name=%s: %w", keeper.Namespace, keeper.Name, contracts.ErrKeeperAlreadyExists)
}
return fmt.Errorf("inserting row: %w", err)
}
@@ -240,11 +240,6 @@ func (s *keeperMetadataStorage) Update(ctx context.Context, newKeeper *secretv1b
return err
}
// Validate before updating that any `secureValues` referenced exists and does not reference other third-party keepers.
if err := s.validateSecureValueReferences(ctx, newKeeper); err != nil {
return err
}
// Read old value first.
oldKeeperRow, err := s.read(ctx, newKeeper.Namespace, newKeeper.Name, contracts.ReadOpts{ForUpdate: true})
if err != nil {