diff --git a/src/mapper/pkg/intentsstore/holder.go b/src/mapper/pkg/intentsstore/holder.go index 51cba299..4d696e54 100644 --- a/src/mapper/pkg/intentsstore/holder.go +++ b/src/mapper/pkg/intentsstore/holder.go @@ -1,6 +1,7 @@ package intentsstore import ( + "encoding/json" "github.com/amit7itz/goset" "github.com/otterize/network-mapper/src/mapper/pkg/config" "github.com/otterize/network-mapper/src/mapper/pkg/graph/model" @@ -49,6 +50,15 @@ func (ti *TimestampedIntent) containsExcludedLabels(excludedLabelsMap map[string } } + for _, podLabel := range ti.Intent.Server.Labels { + value, ok := excludedLabelsMap[podLabel.Key] + if ok { + if value == podLabel.Value { + return true + } + } + } + return false } @@ -147,23 +157,27 @@ func (i *IntentsHolder) AddIntent(newTimestamp time.Time, intent model.Intent) { i.addIntentToStore(i.sinceLastGetStore, newTimestamp, intent) } -func (i *IntentsHolder) GetIntents(namespaces []string, includeLabels []string, excludeServiceWithLabels []string, includeAllLabels bool) []TimestampedIntent { +func (i *IntentsHolder) GetIntents(namespaces []string, includeLabels []string, excludeServiceWithLabels []string, includeAllLabels bool) ([]TimestampedIntent, error) { i.lock.Lock() defer i.lock.Unlock() - return i.getIntentsFromStore(i.accumulatingStore, namespaces, includeLabels, excludeServiceWithLabels, includeAllLabels) + result, err := i.getIntentsFromStore(i.accumulatingStore, namespaces, includeLabels, excludeServiceWithLabels, includeAllLabels) + if err != nil { + return []TimestampedIntent{}, err + } + return result, nil } func (i *IntentsHolder) GetNewIntentsSinceLastGet() []TimestampedIntent { i.lock.Lock() defer i.lock.Unlock() - intents := i.getIntentsFromStore(i.sinceLastGetStore, nil, nil, nil, false) + intents, _ := i.getIntentsFromStore(i.sinceLastGetStore, nil, nil, nil, false) i.sinceLastGetStore = make(IntentsStore) return intents } -func (i *IntentsHolder) getIntentsFromStore(store IntentsStore, namespaces, includeLabels, excludeServiceWithLabels []string, includeAllLabels bool) []TimestampedIntent { +func (i *IntentsHolder) getIntentsFromStore(store IntentsStore, namespaces, includeLabels, excludeServiceWithLabels []string, includeAllLabels bool) ([]TimestampedIntent, error) { namespacesSet := goset.FromSlice(namespaces) includeLabelsSet := goset.FromSlice(includeLabels) result := make([]TimestampedIntent, 0) @@ -176,7 +190,12 @@ func (i *IntentsHolder) getIntentsFromStore(store IntentsStore, namespaces, incl }) for pair, intent := range store { - if len(excludeServiceWithLabels) != 0 && intent.containsExcludedLabels(excludedLabelsMap) { + intentCopy, err := getIntentDeepCopy(intent) + if err != nil { + return result, err + } + + if len(excludeServiceWithLabels) != 0 && intentCopy.containsExcludedLabels(excludedLabelsMap) { continue } @@ -190,13 +209,25 @@ func (i *IntentsHolder) getIntentsFromStore(store IntentsStore, namespaces, incl return includeLabelsSet.Contains(label.Key) }) } - intent.Intent.Client.Labels = labelsFilter(intent.Intent.Client.Labels) - intent.Intent.Server.Labels = labelsFilter(intent.Intent.Server.Labels) + intentCopy.Intent.Client.Labels = labelsFilter(intentCopy.Intent.Client.Labels) + intentCopy.Intent.Server.Labels = labelsFilter(intentCopy.Intent.Server.Labels) } result = append(result, intent) } - return result + return result, nil +} + +func getIntentDeepCopy(intent TimestampedIntent) (TimestampedIntent, error) { + intentCopy := TimestampedIntent{} + intentJSON, err := json.Marshal(intent) + if err != nil { + return TimestampedIntent{}, err + } + if err = json.Unmarshal(intentJSON, &intentCopy); err != nil { + return TimestampedIntent{}, err + } + return intentCopy, nil } func dedupeServiceIntentsDests(dests []model.OtterizeServiceIdentity) []model.OtterizeServiceIdentity { diff --git a/src/mapper/pkg/resolvers/schema.resolvers.go b/src/mapper/pkg/resolvers/schema.resolvers.go index e673e519..50666120 100644 --- a/src/mapper/pkg/resolvers/schema.resolvers.go +++ b/src/mapper/pkg/resolvers/schema.resolvers.go @@ -246,7 +246,6 @@ func (r *mutationResolver) ReportIstioConnectionResults(ctx context.Context, res if dstService.OwnerObject != nil { dstSvcIdentity.PodOwnerKind = model.GroupVersionKindFromKubeGVK(dstService.OwnerObject.GetObjectKind().GroupVersionKind()) } - r.intentsHolder.AddIntent(result.LastSeen, model.Intent{ Client: &srcSvcIdentity, Server: &dstSvcIdentity, @@ -264,7 +263,10 @@ func (r *queryResolver) ServiceIntents(ctx context.Context, namespaces []string, if includeAllLabels != nil && *includeAllLabels { shouldIncludeAllLabels = true } - discoveredIntents := r.intentsHolder.GetIntents(namespaces, includeLabels, []string{}, shouldIncludeAllLabels) + discoveredIntents, err := r.intentsHolder.GetIntents(namespaces, includeLabels, []string{}, shouldIncludeAllLabels) + if err != nil { + return []model.ServiceIntents{}, err + } intentsBySource := intentsstore.GroupIntentsBySource(discoveredIntents) // sorting by service name so results are more consistent @@ -281,13 +283,22 @@ func (r *queryResolver) ServiceIntents(ctx context.Context, namespaces []string, return intentsBySource, nil } -func (r *queryResolver) Intents(ctx context.Context, namespaces []string, includeLabels []string, excludeServiceWithLabels []string, includeAllLabels *bool) ([]model.Intent, error) { +func (r *queryResolver) Intents( + ctx context.Context, + namespaces, + includeLabels, + excludeServiceWithLabels []string, + includeAllLabels *bool) ([]model.Intent, error) { shouldIncludeAllLabels := false if includeAllLabels != nil && *includeAllLabels { shouldIncludeAllLabels = true } - timestampedIntents := r.intentsHolder.GetIntents(namespaces, includeLabels, excludeServiceWithLabels, shouldIncludeAllLabels) + timestampedIntents, err := r.intentsHolder.GetIntents(namespaces, includeLabels, excludeServiceWithLabels, shouldIncludeAllLabels) + if err != nil { + return []model.Intent{}, err + } + intents := lo.Map(timestampedIntents, func(timestampedIntent intentsstore.TimestampedIntent, _ int) model.Intent { return timestampedIntent.Intent })