Skip to content

Commit

Permalink
fix: incorrect number of topology (#423)
Browse files Browse the repository at this point in the history
## What type of PR is this?
/kind bug

## What this PR does / why we need it:
fix incorrect number of topology
  • Loading branch information
panshuai111 authored May 14, 2024
1 parent cb1bd3c commit 767e631
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
24 changes: 19 additions & 5 deletions pkg/infra/search/storage/elasticsearch/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,26 +163,40 @@ func (s *Storage) AggregateByTerms(ctx context.Context, keys []string) (*storage
}

// ConvertResourceGroup2Map converts a ResourceGroup to a map[string]any.
func ConvertResourceGroup2Map(rg *entity.ResourceGroup) map[string]any {
func ConvertResourceGroup2Map(rg *entity.ResourceGroup) (map[string]any, error) {
result := make(map[string]interface{})
v := reflect.ValueOf(rg).Elem()

// Iterate through the fields of the struct.
for i := 0; i < v.NumField(); i++ {
field := v.Type().Field(i)
value := v.Field(i).Interface()
s, ok := field.Tag.Lookup("json")
if !ok {
return nil, fmt.Errorf("the JSON tag for the %s field in the ResourceGroup does not exist", field.Name)
}
ss := strings.Split(s, ",")
if len(ss) == 0 {
return nil, fmt.Errorf("invalid json tag: %s", s)
}
tag := strings.TrimSpace(ss[0])

switch fieldValue := value.(type) {
case map[string]string:
// Handle the map field by iterating its keys and values.
for key, val := range fieldValue {
result[field.Name+"."+key] = val
result[tag+"."+key] = val
}
default:
case string:
// For non-map fields, directly add them to the result map.
result[field.Name] = value
// TODO: use pointers instead of null values to determine if a field exists
if fieldValue != "" {
result[tag] = value
}
default:
return nil, fmt.Errorf("type %T not supported", fieldValue)
}
}

return result
return result, nil
}
8 changes: 6 additions & 2 deletions pkg/infra/topology/relationship.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ package topology

import (
"context"
"errors"
"fmt"
"os"
"reflect"

"github.com/pkg/errors"

"github.com/KusionStack/karpor/pkg/core/entity"
"github.com/KusionStack/karpor/pkg/infra/search/storage"
"github.com/KusionStack/karpor/pkg/infra/search/storage/elasticsearch"
Expand Down Expand Up @@ -292,7 +293,10 @@ func (rg *RelationshipGraph) CountRelationshipGraphByCustomResourceGroup(ctx con
return nil, errors.New("apiVersion should be empty")
}
for _, node := range rg.RelationshipNodes {
kvs := elasticsearch.ConvertResourceGroup2Map(resourceGroup)
kvs, err := elasticsearch.ConvertResourceGroup2Map(resourceGroup)
if err != nil {
return nil, errors.Wrap(err, "failed to convert resource group to map")
}
kvs["apiVersion"] = schema.GroupVersion{Group: node.Group, Version: node.Version}.String()
kvs["kind"] = node.Kind
kvs["cluster"] = name
Expand Down

0 comments on commit 767e631

Please sign in to comment.