Skip to content

Commit c927857

Browse files
committed
fix postgres impl
Signed-off-by: Davanum Srinivas <[email protected]>
1 parent a55619f commit c927857

File tree

6 files changed

+66
-33
lines changed

6 files changed

+66
-33
lines changed

distros/kubernetes/nvsentinel/templates/configmap.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ data:
2424
"enableK8sPlatformConnector": "{{ .Values.platformConnector.k8sConnector.enabled }}",
2525
"K8sConnectorQps": {{ printf "%.2f" .Values.platformConnector.k8sConnector.qps }},
2626
"K8sConnectorBurst": {{ .Values.platformConnector.k8sConnector.burst }},
27-
"enableMongoDBStorePlatformConnector": "{{ .Values.global.mongodbStore.enabled }}"
27+
"enableMongoDBStorePlatformConnector": "{{ .Values.global.mongodbStore.enabled }}",
28+
"enablePostgresDBStorePlatformConnector": "{{ eq .Values.global.datastore.provider \"postgresql\" }}"
2829
{{- if .Values.platformConnector.nodeMetadata }}
2930
,"nodeMetadataAugmentationEnabled": "{{ .Values.platformConnector.nodeMetadata.enabled }}"
3031
,"nodeMetadataCacheSize": {{ .Values.platformConnector.nodeMetadata.cacheSize }}

platform-connectors/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func initializeConnectors(
212212
}
213213

214214
// Keep the legacy config key name for backward compatibility with existing ConfigMaps
215-
if config["enableMongoDBStorePlatformConnector"] == True {
215+
if config["enableMongoDBStorePlatformConnector"] == True || config["enablePostgresDBStorePlatformConnector"] == True {
216216
storeConnector, err = initializeDatabaseStoreConnector(ctx, databaseClientCertMountPath)
217217
if err != nil {
218218
return nil, nil, nil, fmt.Errorf("failed to initialize database store connector: %w", err)

store-client/pkg/datastore/providers/postgresql/database_client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ func (c *PostgreSQLDatabaseClient) FindOne(
254254
}
255255

256256
//nolint:gosec // G201: table name is controlled internally, not from user input
257-
query := fmt.Sprintf("SELECT data FROM %s WHERE %s LIMIT 1", c.tableName, whereClause)
257+
query := fmt.Sprintf("SELECT document FROM %s WHERE %s LIMIT 1", c.tableName, whereClause)
258258

259259
var jsonData []byte
260260

@@ -293,7 +293,7 @@ func (c *PostgreSQLDatabaseClient) Find(
293293
}
294294

295295
//nolint:gosec // G201: table name is controlled internally, not from user input
296-
query := fmt.Sprintf("SELECT data FROM %s WHERE %s", c.tableName, whereClause)
296+
query := fmt.Sprintf("SELECT document FROM %s WHERE %s", c.tableName, whereClause)
297297

298298
// Apply options
299299
if options != nil {

store-client/pkg/datastore/providers/postgresql/health_events.go

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"log/slog"
2323
"strings"
24-
"time"
2524

2625
"github.com/nvidia/nvsentinel/store-client/pkg/datastore"
2726
)
@@ -268,6 +267,14 @@ func (p *PostgreSQLHealthEventStore) FindHealthEventsByNode(
268267
return nil, fmt.Errorf("failed to unmarshal health event: %w", err)
269268
}
270269

270+
// Populate RawEvent for cold-start support
271+
var rawEvent map[string]interface{}
272+
if err := json.Unmarshal(documentJSON, &rawEvent); err != nil {
273+
return nil, fmt.Errorf("failed to unmarshal raw event: %w", err)
274+
}
275+
276+
event.RawEvent = rawEvent
277+
271278
events = append(events, event)
272279
}
273280

@@ -368,6 +375,14 @@ func (p *PostgreSQLHealthEventStore) executeFilterQuery(
368375
return nil, fmt.Errorf("failed to unmarshal health event: %w", err)
369376
}
370377

378+
// Populate RawEvent for cold-start support
379+
var rawEvent map[string]interface{}
380+
if err := json.Unmarshal(documentJSON, &rawEvent); err != nil {
381+
return nil, fmt.Errorf("failed to unmarshal raw event: %w", err)
382+
}
383+
384+
event.RawEvent = rawEvent
385+
371386
events = append(events, event)
372387
}
373388

@@ -407,6 +422,14 @@ func (p *PostgreSQLHealthEventStore) FindHealthEventsByStatus(
407422
return nil, fmt.Errorf("failed to unmarshal health event: %w", err)
408423
}
409424

425+
// Populate RawEvent for cold-start support
426+
var rawEvent map[string]interface{}
427+
if err := json.Unmarshal(documentJSON, &rawEvent); err != nil {
428+
return nil, fmt.Errorf("failed to unmarshal raw event: %w", err)
429+
}
430+
431+
event.RawEvent = rawEvent
432+
410433
events = append(events, event)
411434
}
412435

@@ -563,6 +586,14 @@ func (p *PostgreSQLHealthEventStore) FindLatestEventForNode(
563586
return nil, fmt.Errorf("failed to unmarshal health event: %w", err)
564587
}
565588

589+
// Populate RawEvent for cold-start support
590+
var rawEvent map[string]interface{}
591+
if err := json.Unmarshal(documentJSON, &rawEvent); err != nil {
592+
return nil, fmt.Errorf("failed to unmarshal raw event: %w", err)
593+
}
594+
595+
event.RawEvent = rawEvent
596+
566597
return &event, nil
567598
}
568599

@@ -576,10 +607,9 @@ func (p *PostgreSQLHealthEventStore) FindHealthEventsByQuery(ctx context.Context
576607
// Build the full query
577608
//nolint:gosec // G202 false positive - using parameterized query with placeholders
578609
query := `
579-
SELECT id, data, createdAt, updatedAt
610+
SELECT document
580611
FROM health_events
581612
WHERE ` + whereClause + `
582-
ORDER BY createdAt DESC
583613
`
584614

585615
rows, err := p.db.QueryContext(ctx, query, args...)
@@ -591,13 +621,9 @@ func (p *PostgreSQLHealthEventStore) FindHealthEventsByQuery(ctx context.Context
591621
var events []datastore.HealthEventWithStatus
592622

593623
for rows.Next() {
594-
var id string
595-
596624
var documentJSON []byte
597625

598-
var createdAt, updatedAt time.Time
599-
600-
if err := rows.Scan(&id, &documentJSON, &createdAt, &updatedAt); err != nil {
626+
if err := rows.Scan(&documentJSON); err != nil {
601627
return nil, fmt.Errorf("failed to scan health event row: %w", err)
602628
}
603629

@@ -606,7 +632,13 @@ func (p *PostgreSQLHealthEventStore) FindHealthEventsByQuery(ctx context.Context
606632
return nil, fmt.Errorf("failed to unmarshal health event: %w", err)
607633
}
608634

609-
event.CreatedAt = createdAt
635+
// Populate RawEvent for cold-start support
636+
var rawEvent map[string]interface{}
637+
if err := json.Unmarshal(documentJSON, &rawEvent); err != nil {
638+
return nil, fmt.Errorf("failed to unmarshal raw event: %w", err)
639+
}
640+
641+
event.RawEvent = rawEvent
610642
events = append(events, event)
611643
}
612644

store-client/pkg/query/builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ func mongoFieldToJSONB(fieldPath string) string {
407407
return fieldPath
408408
}
409409

410-
return fmt.Sprintf("data->>'%s'", fieldPath)
410+
return fmt.Sprintf("document->>'%s'", fieldPath)
411411
}
412412

413413
// Split the path into parts
@@ -417,7 +417,7 @@ func mongoFieldToJSONB(fieldPath string) string {
417417
// All intermediate parts use -> operator
418418
// Final part uses ->> operator to extract as text
419419
var jsonbPath strings.Builder
420-
jsonbPath.WriteString("data")
420+
jsonbPath.WriteString("document")
421421

422422
for i, part := range parts {
423423
if i < len(parts)-1 {

store-client/pkg/query/builder_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestBuilder_Eq(t *testing.T) {
3737
expectedMongo: map[string]interface{}{
3838
"status": "active",
3939
},
40-
expectedSQL: "data->>'status' = $1",
40+
expectedSQL: "document->>'status' = $1",
4141
expectedArgs: []interface{}{"active"},
4242
},
4343
{
@@ -47,7 +47,7 @@ func TestBuilder_Eq(t *testing.T) {
4747
expectedMongo: map[string]interface{}{
4848
"healtheventstatus.nodequarantined": "Quarantined",
4949
},
50-
expectedSQL: "data->'healtheventstatus'->>'nodequarantined' = $1",
50+
expectedSQL: "document->'healtheventstatus'->>'nodequarantined' = $1",
5151
expectedArgs: []interface{}{"Quarantined"},
5252
},
5353
{
@@ -92,7 +92,7 @@ func TestBuilder_Ne(t *testing.T) {
9292

9393
// Test SQL output
9494
sql, args := builder.ToSQL()
95-
assert.Equal(t, "data->>'agent' != $1", sql)
95+
assert.Equal(t, "document->>'agent' != $1", sql)
9696
assert.Equal(t, []interface{}{"health-events-analyzer"}, args)
9797
}
9898

@@ -110,7 +110,7 @@ func TestBuilder_In(t *testing.T) {
110110

111111
// Test SQL output
112112
sql, args := builder.ToSQL()
113-
assert.Equal(t, "data->>'status' IN ($1, $2)", sql)
113+
assert.Equal(t, "document->>'status' IN ($1, $2)", sql)
114114
assert.Equal(t, []interface{}{"active", "pending"}, args)
115115
}
116116

@@ -128,7 +128,7 @@ func TestBuilder_Gt(t *testing.T) {
128128

129129
// Test SQL output
130130
sql, args := builder.ToSQL()
131-
assert.Equal(t, "data->>'count' > $1", sql)
131+
assert.Equal(t, "document->>'count' > $1", sql)
132132
assert.Equal(t, []interface{}{10}, args)
133133
}
134134

@@ -146,7 +146,7 @@ func TestBuilder_Gte(t *testing.T) {
146146

147147
// Test SQL output
148148
sql, args := builder.ToSQL()
149-
assert.Equal(t, "data->>'count' >= $1", sql)
149+
assert.Equal(t, "document->>'count' >= $1", sql)
150150
assert.Equal(t, []interface{}{10}, args)
151151
}
152152

@@ -164,7 +164,7 @@ func TestBuilder_Lt(t *testing.T) {
164164

165165
// Test SQL output
166166
sql, args := builder.ToSQL()
167-
assert.Equal(t, "data->>'count' < $1", sql)
167+
assert.Equal(t, "document->>'count' < $1", sql)
168168
assert.Equal(t, []interface{}{100}, args)
169169
}
170170

@@ -182,7 +182,7 @@ func TestBuilder_Lte(t *testing.T) {
182182

183183
// Test SQL output
184184
sql, args := builder.ToSQL()
185-
assert.Equal(t, "data->>'count' <= $1", sql)
185+
assert.Equal(t, "document->>'count' <= $1", sql)
186186
assert.Equal(t, []interface{}{100}, args)
187187
}
188188

@@ -204,7 +204,7 @@ func TestBuilder_And(t *testing.T) {
204204

205205
// Test SQL output
206206
sql, args := builder.ToSQL()
207-
assert.Equal(t, "(data->>'status' = $1) AND (data->>'type' = $2)", sql)
207+
assert.Equal(t, "(document->>'status' = $1) AND (document->>'type' = $2)", sql)
208208
assert.Equal(t, []interface{}{"active", "critical"}, args)
209209
}
210210

@@ -233,7 +233,7 @@ func TestBuilder_And_WithConflictingFields(t *testing.T) {
233233

234234
// Test SQL output
235235
sql, args := builder.ToSQL()
236-
assert.Equal(t, "(data->>'count' > $1) AND (data->>'count' < $2)", sql)
236+
assert.Equal(t, "(document->>'count' > $1) AND (document->>'count' < $2)", sql)
237237
assert.Equal(t, []interface{}{10, 100}, args)
238238
}
239239

@@ -257,7 +257,7 @@ func TestBuilder_Or(t *testing.T) {
257257

258258
// Test SQL output
259259
sql, args := builder.ToSQL()
260-
assert.Equal(t, "(data->>'status' = $1) OR (data->>'status' = $2)", sql)
260+
assert.Equal(t, "(document->>'status' = $1) OR (document->>'status' = $2)", sql)
261261
assert.Equal(t, []interface{}{"active", "pending"}, args)
262262
}
263263

@@ -303,17 +303,17 @@ func TestBuilder_NestedFieldPaths(t *testing.T) {
303303
{
304304
name: "single level",
305305
field: "status",
306-
expectedPath: "data->>'status'",
306+
expectedPath: "document->>'status'",
307307
},
308308
{
309309
name: "two levels",
310310
field: "healthevent.isfatal",
311-
expectedPath: "data->'healthevent'->>'isfatal'",
311+
expectedPath: "document->'healthevent'->>'isfatal'",
312312
},
313313
{
314314
name: "three levels",
315315
field: "healtheventstatus.userpodsevictionstatus.status",
316-
expectedPath: "data->'healtheventstatus'->'userpodsevictionstatus'->>'status'",
316+
expectedPath: "document->'healtheventstatus'->'userpodsevictionstatus'->>'status'",
317317
},
318318
}
319319

@@ -361,7 +361,7 @@ func TestMongoFieldToJSONB(t *testing.T) {
361361
{
362362
name: "simple field",
363363
mongoField: "status",
364-
expectedPath: "data->>'status'",
364+
expectedPath: "document->>'status'",
365365
},
366366
{
367367
name: "column field (id)",
@@ -376,17 +376,17 @@ func TestMongoFieldToJSONB(t *testing.T) {
376376
{
377377
name: "nested two levels",
378378
mongoField: "healthevent.isfatal",
379-
expectedPath: "data->'healthevent'->>'isfatal'",
379+
expectedPath: "document->'healthevent'->>'isfatal'",
380380
},
381381
{
382382
name: "nested three levels",
383383
mongoField: "healtheventstatus.nodequarantined",
384-
expectedPath: "data->'healtheventstatus'->>'nodequarantined'",
384+
expectedPath: "document->'healtheventstatus'->>'nodequarantined'",
385385
},
386386
{
387387
name: "deeply nested",
388388
mongoField: "healtheventstatus.userpodsevictionstatus.status",
389-
expectedPath: "data->'healtheventstatus'->'userpodsevictionstatus'->>'status'",
389+
expectedPath: "document->'healtheventstatus'->'userpodsevictionstatus'->>'status'",
390390
},
391391
}
392392

0 commit comments

Comments
 (0)