Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: component lookups #1707

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 34 additions & 15 deletions pkg/topology/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/flanksource/duty/job"
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/query"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"github.com/flanksource/duty/types"
"github.com/google/uuid"
Expand All @@ -31,30 +32,37 @@ func mergeComponentLookup(ctx *ComponentContext, component *v1.ComponentSpec, sp
if err != nil {
return nil, errors.Wrapf(err, "component lookup failed: %s", component)
}

if len(results) == 1 {
if err := json.Unmarshal([]byte(results[0].(string)), &components); err != nil {
return nil, errors.Wrapf(err, "component lookup returned invalid json: %s", component)
}
} else {
// the property returned a list of new properties
for _, result := range results {
var p pkg.Component
data, err := json.Marshal(result)
if err != nil {
return nil, fmt.Errorf("error marshaling result to json: %w", err)
}
if err := json.Unmarshal(data, &p); err != nil {
return nil, fmt.Errorf("error unmarshaling data from json: %w", err)
switch v := results[0].(type) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this cause an index out of bounds?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a length check above it

case string:
if err := json.Unmarshal([]byte(v), &components); err != nil {
return nil, fmt.Errorf("error unmarshaling data from pkg.Components: %w", err)
}
results = nil
}
}

components = append(components, &p)
// the property returned a list of new properties
for _, result := range results {
var p pkg.Component
data, err := json.Marshal(result)
if err != nil {
return nil, fmt.Errorf("error marshaling result to json: %w", err)
}
if err := json.Unmarshal(data, &p); err != nil {
return nil, fmt.Errorf("error unmarshaling data from json: %w", err)
}

components = append(components, &p)
}

for _, _c := range components {
if err = forEachComponent(ctx, component, _c); err != nil {
return nil, err
}
}

return components, nil
}

Expand Down Expand Up @@ -199,9 +207,19 @@ func lookup(ctx *ComponentContext, name string, spec v1.CanarySpec) ([]interface
if result.Message != "" {
results = append(results, result.Message)
} else if result.Detail != nil {
switch result.Detail.(type) {
switch v := result.Detail.(type) {
case []any:
results = append(results, result.Detail.([]interface{})...)
case checks.ExecDetails:
results = append(results, v.Stdout)
case checks.ConfigDBQueryResult:
for _, item := range v.Results {
results = append(results, any(item))
}
case []unstructured.Unstructured:
for _, item := range v {
results = append(results, any(item))
}
case any:
results = append(results, result.Detail)
default:
Expand All @@ -211,6 +229,7 @@ func lookup(ctx *ComponentContext, name string, spec v1.CanarySpec) ([]interface
results = append(results, "")
}
}

return results, nil
}

Expand Down
Loading