-
Notifications
You must be signed in to change notification settings - Fork 135
/
store.go
150 lines (122 loc) · 4.35 KB
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Code generated by pg-bindings generator. DO NOT EDIT.
package postgres
import (
"context"
"strings"
"time"
"github.com/jackc/pgx/v5"
"github.com/pkg/errors"
"github.com/stackrox/rox/central/metrics"
v1 "github.com/stackrox/rox/generated/api/v1"
"github.com/stackrox/rox/generated/storage"
"github.com/stackrox/rox/pkg/logging"
ops "github.com/stackrox/rox/pkg/metrics"
"github.com/stackrox/rox/pkg/postgres"
"github.com/stackrox/rox/pkg/postgres/pgutils"
pkgSchema "github.com/stackrox/rox/pkg/postgres/schema"
"github.com/stackrox/rox/pkg/sac"
"github.com/stackrox/rox/pkg/sac/resources"
pgSearch "github.com/stackrox/rox/pkg/search/postgres"
"gorm.io/gorm"
)
const (
baseTable = "clusters"
storeName = "Cluster"
// using copyFrom, we may not even want to batch. It would probably be simpler
// to deal with failures if we just sent it all. Something to think about as we
// proceed and move into more e2e and larger performance testing
batchSize = 10000
)
var (
log = logging.LoggerForModule()
schema = pkgSchema.ClustersSchema
targetResource = resources.Cluster
)
type storeType = storage.Cluster
// Store is the interface to interact with the storage for storage.Cluster
type Store interface {
Upsert(ctx context.Context, obj *storeType) error
UpsertMany(ctx context.Context, objs []*storeType) error
Delete(ctx context.Context, id string) error
DeleteByQuery(ctx context.Context, q *v1.Query) error
DeleteMany(ctx context.Context, identifiers []string) error
Count(ctx context.Context) (int, error)
Exists(ctx context.Context, id string) (bool, error)
Get(ctx context.Context, id string) (*storeType, bool, error)
GetByQuery(ctx context.Context, query *v1.Query) ([]*storeType, error)
GetMany(ctx context.Context, identifiers []string) ([]*storeType, []int, error)
GetIDs(ctx context.Context) ([]string, error)
Walk(ctx context.Context, fn func(obj *storeType) error) error
}
// New returns a new Store instance using the provided sql instance.
func New(db postgres.DB) Store {
return pgSearch.NewGenericStore[storeType, *storeType](
db,
schema,
pkGetter,
insertIntoClusters,
nil,
metricsSetAcquireDBConnDuration,
metricsSetPostgresOperationDurationTime,
isUpsertAllowed,
targetResource,
)
}
// region Helper functions
func pkGetter(obj *storeType) string {
return obj.GetId()
}
func metricsSetPostgresOperationDurationTime(start time.Time, op ops.Op) {
metrics.SetPostgresOperationDurationTime(start, op, storeName)
}
func metricsSetAcquireDBConnDuration(start time.Time, op ops.Op) {
metrics.SetAcquireDBConnDuration(start, op, storeName)
}
func isUpsertAllowed(ctx context.Context, objs ...*storeType) error {
scopeChecker := sac.GlobalAccessScopeChecker(ctx).AccessMode(storage.Access_READ_WRITE_ACCESS).Resource(targetResource)
if scopeChecker.IsAllowed() {
return nil
}
var deniedIDs []string
for _, obj := range objs {
subScopeChecker := scopeChecker.ClusterID(obj.GetId())
if !subScopeChecker.IsAllowed() {
deniedIDs = append(deniedIDs, obj.GetId())
}
}
if len(deniedIDs) != 0 {
return errors.Wrapf(sac.ErrResourceAccessDenied, "modifying clusters with IDs [%s] was denied", strings.Join(deniedIDs, ", "))
}
return nil
}
func insertIntoClusters(batch *pgx.Batch, obj *storage.Cluster) error {
serialized, marshalErr := obj.Marshal()
if marshalErr != nil {
return marshalErr
}
values := []interface{}{
// parent primary keys start
pgutils.NilOrUUID(obj.GetId()),
obj.GetName(),
pgutils.EmptyOrMap(obj.GetLabels()),
serialized,
}
finalStr := "INSERT INTO clusters (Id, Name, Labels, serialized) VALUES($1, $2, $3, $4) ON CONFLICT(Id) DO UPDATE SET Id = EXCLUDED.Id, Name = EXCLUDED.Name, Labels = EXCLUDED.Labels, serialized = EXCLUDED.serialized"
batch.Queue(finalStr, values...)
return nil
}
// endregion Helper functions
// region Used for testing
// CreateTableAndNewStore returns a new Store instance for testing.
func CreateTableAndNewStore(ctx context.Context, db postgres.DB, gormDB *gorm.DB) Store {
pkgSchema.ApplySchemaForTable(ctx, gormDB, baseTable)
return New(db)
}
// Destroy drops the tables associated with the target object type.
func Destroy(ctx context.Context, db postgres.DB) {
dropTableClusters(ctx, db)
}
func dropTableClusters(ctx context.Context, db postgres.DB) {
_, _ = db.Exec(ctx, "DROP TABLE IF EXISTS clusters CASCADE")
}
// endregion Used for testing