Skip to content

Commit 7142aa4

Browse files
ybelMekktommytroen
andcommitted
add: image sync table
* add to updater Co-authored-by: Tommy Trøen <[email protected]>
1 parent d3e0889 commit 7142aa4

File tree

6 files changed

+93
-20
lines changed

6 files changed

+93
-20
lines changed

Diff for: internal/database/migrations/0001_schema.sql

+14
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ CREATE TABLE images
4949
)
5050
;
5151

52+
CREATE TABLE image_sync_status
53+
(
54+
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
55+
image_name TEXT NOT NULL,
56+
image_tag TEXT NOT NULL,
57+
status_code TEXT NOT NULL,
58+
reason TEXT NOT NULL,
59+
source TEXT NOT NULL,
60+
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
61+
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
62+
CONSTRAINT image_name_tag UNIQUE (image_name, image_tag)
63+
)
64+
;
65+
5266
CREATE TABLE vulnerability_summary
5367
(
5468
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),

Diff for: internal/database/queries/image.sql

+11-3
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,20 @@ SET
3131
WHERE state = ANY(@included_states::image_state[])
3232
;
3333

34-
3534
-- name: MarkImagesForResync :exec
3635
UPDATE images
37-
SET
38-
state = 'resync',
36+
SET state = 'resync',
3937
updated_at = NOW()
4038
WHERE updated_at < @threshold_time
4139
AND state != 'resync'
4240
AND state != ANY(@excluded_states::image_state[]);
41+
42+
-- name: UpdateImageSyncStatus :exec
43+
INSERT INTO image_sync_status (image_name, image_tag, status_code, reason, source)
44+
VALUES (@image_name, @image_tag, @status_code, @reason, @source) ON CONFLICT (image_name, image_tag) DO
45+
UPDATE
46+
SET
47+
status_code = @status_code,
48+
reason = @reason,
49+
updated_at = NOW()
50+
;

Diff for: internal/database/sql/image.sql.go

+30-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: internal/database/sql/querier.go

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: internal/updater/updater.go

+35-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ func (u *Updater) QueueImage(ctx context.Context, imageName, imageTag string) {
9292
}); dbErr != nil {
9393
log.Errorf("failed to update image state to failed: %v", dbErr)
9494
}
95+
err := u.db.UpdateImageSyncStatus(ctx, sql.UpdateImageSyncStatusParams{
96+
ImageName: imageName,
97+
ImageTag: imageTag,
98+
StatusCode: "GenericError",
99+
Reason: err.Error(),
100+
Source: "dependencytrack",
101+
})
102+
if err != nil {
103+
log.Errorf("failed to update image sync status: %v", err)
104+
}
95105
}
96106
}()
97107
}
@@ -141,7 +151,31 @@ func (u *Updater) updateForImage(ctx context.Context, imageName, imageTag string
141151
return fmt.Errorf("getting project: %w", err)
142152
}
143153

144-
if p == nil || p.Metrics == nil {
154+
if p == nil {
155+
err := u.db.UpdateImageSyncStatus(ctx, sql.UpdateImageSyncStatusParams{
156+
ImageName: imageName,
157+
ImageTag: imageTag,
158+
StatusCode: "NotFound",
159+
Reason: "project not found",
160+
Source: "dependencytrack",
161+
})
162+
if err != nil {
163+
log.Errorf("failed to update image sync status: %v", err)
164+
}
165+
return nil
166+
}
167+
168+
if p.Metrics == nil {
169+
err := u.db.UpdateImageSyncStatus(ctx, sql.UpdateImageSyncStatusParams{
170+
ImageName: imageName,
171+
ImageTag: imageTag,
172+
StatusCode: "NotFound",
173+
Reason: "metrics not found",
174+
Source: "dependencytrack",
175+
})
176+
if err != nil {
177+
log.Errorf("failed to update image sync status: %v", err)
178+
}
145179
return nil
146180
}
147181

Diff for: internal/updater/updater_integration_test.go

+2-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package updater_test
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
76
"github.com/google/uuid"
87
"github.com/nais/v13s/internal/collections"
@@ -35,13 +34,7 @@ func TestUpdater(t *testing.T) {
3534
defer cancel()
3635

3736
insertWorkloads(ctx, t, db, projectNames)
38-
39-
err = u.Run(updaterCtx)
40-
if err != nil {
41-
if !errors.Is(err, context.DeadlineExceeded) {
42-
t.Fatalf("unexpected error: %s", err)
43-
}
44-
}
37+
u.Run(updaterCtx)
4538

4639
imageName := projectNames[0]
4740
imageTag := "v1"
@@ -94,12 +87,7 @@ func TestUpdater(t *testing.T) {
9487
defer cancel()
9588

9689
// Should update projectName[0] since it is older than updater.DefaultResyncImagesOlderThanMinutes
97-
err = u.Run(updaterCtx)
98-
if err != nil {
99-
if !errors.Is(err, context.DeadlineExceeded) {
100-
t.Fatalf("unexpected error: %s", err)
101-
}
102-
}
90+
u.Run(updaterCtx)
10391

10492
vulns, err := db.ListVulnerabilities(
10593
ctx,

0 commit comments

Comments
 (0)