Skip to content

Commit

Permalink
Fix issue when duplicate dimensions are provided
Browse files Browse the repository at this point in the history
Signed-off-by: Frederic Giloux <fgiloux@redhat.com>
  • Loading branch information
fgiloux committed Feb 8, 2023
1 parent 7150903 commit c587d0c
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions pkg/reconciler/topology/partitionset/partitionset_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ func (c *controller) reconcile(ctx context.Context, partitionSet *topologyv1alph
}

var matchLabelsMap map[string]map[string]string
dimensions := removeDuplicates(partitionSet.Spec.Dimensions)
if partitionSet.Spec.ShardSelector != nil {
matchLabelsMap = partition(shards, partitionSet.Spec.Dimensions, partitionSet.Spec.ShardSelector.MatchLabels)
matchLabelsMap = partition(shards, dimensions, partitionSet.Spec.ShardSelector.MatchLabels)
} else {
matchLabelsMap = partition(shards, partitionSet.Spec.Dimensions, nil)
matchLabelsMap = partition(shards, dimensions, nil)
}
partitionSet.Status.Count = uint16(len(matchLabelsMap))
existingMatches := map[string]struct{}{}
Expand Down Expand Up @@ -162,7 +163,7 @@ func (c *controller) reconcile(ctx context.Context, partitionSet *topologyv1alph
// Create partitions when no existing partition for the set has the same selector.
for key, matchLabels := range matchLabelsMap {
if _, ok := existingMatches[key]; !ok {
partition := generatePartition(partitionSet.Name, newMatchExpressions, matchLabels, partitionSet.Spec.Dimensions)
partition := generatePartition(partitionSet.Name, newMatchExpressions, matchLabels, dimensions)
partition.OwnerReferences = []metav1.OwnerReference{
*metav1.NewControllerRef(partitionSet, topologyv1alpha1.SchemeGroupVersion.WithKind("PartitionSet")),
}
Expand Down Expand Up @@ -215,3 +216,16 @@ func partition(shards []*corev1alpha1.Shard, dimensions []string, shardSelectorL
}
return matchLabelsMap
}

// removeDuplicates makes sure that a value is not found more than once in the slice
func removeDuplicates(slice []string) []string {
inResult := make(map[string]struct{})
var result []string
for _, el := range slice {
if _, ok := inResult[el]; !ok {
inResult[el] = struct{}{}
result = append(result, el)
}
}
return result
}

0 comments on commit c587d0c

Please sign in to comment.