Skip to content

Commit

Permalink
Fixed labels in intents overwritten after initial query (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
evyatarmeged authored May 24, 2023
1 parent 9fb5633 commit 2cfb879
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
47 changes: 39 additions & 8 deletions src/mapper/pkg/intentsstore/holder.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand All @@ -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 {
Expand Down
19 changes: 15 additions & 4 deletions src/mapper/pkg/resolvers/schema.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
})
Expand Down

0 comments on commit 2cfb879

Please sign in to comment.