Skip to content

Commit 70ae01d

Browse files
Return associated pod data for deployments, etc., as part of the payload (#1017)
* Generate assocation blocks for deployments with their related pods. Also: - Add `metadata.state.{error,message,transitioning`} to `_v1_Pod_fields` table (We already index `metadata.state.name` for all GVKs - Return an array called `metadata.associations` for each GVK we care about. - Right now there will be at most one entry: `{ GVK: podGVK, data: <array of info on each pod's metadata.state associated with the deployment> }` - The integration tests rely on the core rancher deployments, which all have associated pods. - No need to add "fake" deployments. - Write a more generic db-client routine to read rows containing n string values. * We need to also show jobs associated with cron jobs. - One problem is that pods are related to their parent objects via `metadata.relationships[toType=pod].selector` but jobs are related to parent `batch.cronjobs` via `metadata.relationships[toType=batch.job].toId` I wrote separate selection functions for each form. Also: Drop ReadStrings2 in favor of 'ReadStringsN(..., 2)' * Misc changes: - If a yaml load fails modify the error to give the input file name. - s/podname/childname/ and add integration tests for deployment/pod relationships * Add the includeAssociatedData query param. * Ensure user has access to the associated resource. - Fix a unit-test to ensure the user has access to the child schema. - Demote access denial to associated data to a trace log. * This test often fails in CI, so add debug statements to help diagnose why.
1 parent 5a529fb commit 70ae01d

26 files changed

+1387
-76
lines changed

pkg/sqlcache/db/client.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type Client interface {
5555
QueryForRows(ctx context.Context, stmt Stmt, params ...any) (Rows, error)
5656
ReadObjects(rows Rows, typ reflect.Type) ([]any, error)
5757
ReadStrings(rows Rows) ([]string, error)
58-
ReadStrings2(rows Rows) ([][]string, error)
58+
ReadStringsN(rows Rows, numColumns int) ([][]string, error)
5959
ReadInt(rows Rows) (int, error)
6060
ReadStringIntString(rows Rows) ([][]string, error)
6161
Upsert(tx TxClient, stmt Stmt, key string, obj SerializedObject) error
@@ -323,20 +323,22 @@ func (c *client) ReadStrings(rows Rows) ([]string, error) {
323323
return result, nil
324324
}
325325

326-
// ReadStrings2 scans the given rows into pairs of strings, and then returns the strings as a slice.
327-
func (c *client) ReadStrings2(rows Rows) ([][]string, error) {
326+
// ReadStringIntString scans the given rows into (string, int, string) tuples, and then returns a slice of them.
327+
func (c *client) ReadStringIntString(rows Rows) ([][]string, error) {
328328
c.connLock.RLock()
329329
defer c.connLock.RUnlock()
330330

331331
var result [][]string
332332
for rows.Next() {
333-
var key1, key2 string
334-
err := rows.Scan(&key1, &key2)
333+
var val1 string
334+
var val2 int
335+
var val3 string
336+
err := rows.Scan(&val1, &val2, &val3)
335337
if err != nil {
336338
return nil, closeRowsOnError(rows, err)
337339
}
338340

339-
result = append(result, []string{key1, key2})
341+
result = append(result, []string{val1, strconv.Itoa(val2), val3})
340342
}
341343
err := rows.Err()
342344
if err != nil {
@@ -351,22 +353,25 @@ func (c *client) ReadStrings2(rows Rows) ([][]string, error) {
351353
return result, nil
352354
}
353355

354-
// ReadStringIntString scans the given rows into (string, string, string) tuples, and then returns a slice of them.
355-
func (c *client) ReadStringIntString(rows Rows) ([][]string, error) {
356+
// ReadStringsN scans the given rows into string-slices of the specified number of columns
357+
func (c *client) ReadStringsN(rows Rows, numColumns int) ([][]string, error) {
356358
c.connLock.RLock()
357359
defer c.connLock.RUnlock()
358360

359361
var result [][]string
362+
stringPointers := make([]any, numColumns)
360363
for rows.Next() {
361-
var val1 string
362-
var val2 int
363-
var val3 string
364-
err := rows.Scan(&val1, &val2, &val3)
364+
// stringList needs be reinitialized on each iteration or all the final data in
365+
// the `result` array will be the last row!
366+
stringList := make([]string, numColumns)
367+
for i := range stringList {
368+
stringPointers[i] = &stringList[i]
369+
}
370+
err := rows.Scan(stringPointers...)
365371
if err != nil {
366372
return nil, closeRowsOnError(rows, err)
367373
}
368-
369-
result = append(result, []string{val1, strconv.Itoa(val2), val3})
374+
result = append(result, stringList)
370375
}
371376
err := rows.Err()
372377
if err != nil {

pkg/sqlcache/informer/db_mocks_test.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/sqlcache/informer/factory/db_mocks_test.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/sqlcache/informer/factory/sql_informer_mocks_test.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/sqlcache/informer/informer.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/rancher/apiserver/pkg/types"
15+
"github.com/rancher/steve/pkg/accesscontrol"
1516
"github.com/rancher/steve/pkg/sqlcache/db"
1617
"github.com/rancher/steve/pkg/sqlcache/partition"
1718
"github.com/rancher/steve/pkg/sqlcache/sqltypes"
@@ -66,6 +67,7 @@ func (f *WatchFilter) matches(obj metav1.Object) bool {
6667
}
6768

6869
type ByOptionsLister interface {
70+
AugmentList(ctx context.Context, list *unstructured.UnstructuredList, childGVK schema.GroupVersionKind, childSchemaName string, useSelectors bool, accessList accesscontrol.AccessListByVerb) error
6971
ListByOptions(ctx context.Context, lo *sqltypes.ListOptions, partitions []partition.Partition, namespace string) (*unstructured.UnstructuredList, int, *types.APISummary, string, error)
7072
Watch(ctx context.Context, options WatchOptions, eventsCh chan<- watch.Event) error
7173
GetLatestResourceVersion() []string
@@ -179,6 +181,10 @@ func (i *Informer) RunWithContext(ctx context.Context) {
179181
wg.Wait()
180182
}
181183

184+
func (i *Informer) AugmentList(ctx context.Context, list *unstructured.UnstructuredList, childGVK schema.GroupVersionKind, childSchemaName string, useSelectors bool, accessList accesscontrol.AccessListByVerb) error {
185+
return i.ByOptionsLister.AugmentList(ctx, list, childGVK, childSchemaName, useSelectors, accessList)
186+
}
187+
182188
// ListByOptions returns objects according to the specified list options and partitions.
183189
// Specifically:
184190
// - an unstructured list of resources belonging to any of the specified partitions

pkg/sqlcache/informer/informer_mocks_test.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)