Skip to content

Commit 8c65ae9

Browse files
authored
fix: Resource trees fixes (#506)
* add unique constraint * add tests * fix existing tests * limit component children to 100 * add tests * add wip tests * update on conflict constraint * add get and delete queries * prevent uid conflicts * update tests * fix unit tests * fix unit tests
1 parent 12e5d5f commit 8c65ae9

File tree

3 files changed

+437
-63
lines changed

3 files changed

+437
-63
lines changed

pkg/cache/db/cache.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,60 @@ func (in *ComponentCache) ComponentInsights() (result []client.ClusterInsightCom
126126
return result, err
127127
}
128128

129+
func (in *ComponentCache) GetComponent(group, version, kind, namespace, name string) (result *client.ComponentChildAttributes, err error) {
130+
conn, err := in.pool.Take(context.Background())
131+
if err != nil {
132+
return result, err
133+
}
134+
defer in.pool.Put(conn)
135+
136+
err = sqlitex.ExecuteTransient(conn, getComponent, &sqlitex.ExecOptions{
137+
Args: []interface{}{name, namespace, group, version, kind},
138+
ResultFunc: func(stmt *sqlite.Stmt) error {
139+
result = &client.ComponentChildAttributes{
140+
UID: stmt.ColumnText(0),
141+
Group: lo.EmptyableToPtr(stmt.ColumnText(1)),
142+
Version: stmt.ColumnText(2),
143+
Kind: stmt.ColumnText(3),
144+
Namespace: lo.EmptyableToPtr(stmt.ColumnText(4)),
145+
Name: stmt.ColumnText(5),
146+
State: FromComponentState(ComponentState(stmt.ColumnInt32(6))),
147+
ParentUID: lo.EmptyableToPtr(stmt.ColumnText(7)),
148+
}
149+
return nil
150+
},
151+
})
152+
153+
return result, err
154+
}
155+
156+
func (in *ComponentCache) GetComponentByUID(uid string) (result *client.ComponentChildAttributes, err error) {
157+
conn, err := in.pool.Take(context.Background())
158+
if err != nil {
159+
return result, err
160+
}
161+
defer in.pool.Put(conn)
162+
163+
err = sqlitex.ExecuteTransient(conn, getComponentByUID, &sqlitex.ExecOptions{
164+
Args: []interface{}{uid},
165+
ResultFunc: func(stmt *sqlite.Stmt) error {
166+
result = &client.ComponentChildAttributes{
167+
UID: stmt.ColumnText(0),
168+
Group: lo.EmptyableToPtr(stmt.ColumnText(1)),
169+
Version: stmt.ColumnText(2),
170+
Kind: stmt.ColumnText(3),
171+
Namespace: lo.EmptyableToPtr(stmt.ColumnText(4)),
172+
Name: stmt.ColumnText(5),
173+
State: FromComponentState(ComponentState(stmt.ColumnInt32(6))),
174+
ParentUID: lo.EmptyableToPtr(stmt.ColumnText(7)),
175+
}
176+
return nil
177+
},
178+
})
179+
180+
return result, err
181+
}
182+
129183
// SetComponent stores or updates a component's attributes in the cache.
130184
// It takes a ComponentChildAttributes parameter containing the component's data.
131185
// If a component with the same UID exists, it will be updated; otherwise, a new entry is created.

0 commit comments

Comments
 (0)