-
Notifications
You must be signed in to change notification settings - Fork 742
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: updating to use include-label-prefix
fix: remove copy of labels Signed-off-by: Hans Knecht <[email protected]>
- Loading branch information
1 parent
35c8957
commit e7357c4
Showing
3 changed files
with
82 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package router | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
func includeLabelsByPrefix(labels map[string]string, includeLabelPrefixes []string) map[string]string { | ||
filteredLabels := make(map[string]string) | ||
for key, value := range labels { | ||
for _, includeLabelPrefix := range includeLabelPrefixes { | ||
if includeLabelPrefix == "*" || strings.HasPrefix(key, includeLabelPrefix) { | ||
filteredLabels[key] = value | ||
break | ||
} | ||
} | ||
} | ||
|
||
return filteredLabels | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
Copyright 2020 The Flux authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package router | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIncludeLabelsByPrefix(t *testing.T) { | ||
labels := map[string]string{ | ||
"foo": "foo-value", | ||
"bar": "bar-value", | ||
"lorem": "ipsum", | ||
} | ||
includeLabelPrefix := []string{"foo", "lor"} | ||
|
||
filteredLabels := includeLabelsByPrefix(labels, includeLabelPrefix) | ||
|
||
assert.Equal(t, filteredLabels, map[string]string{ | ||
"foo": "foo-value", | ||
"lorem": "ipsum", | ||
// bar excluded | ||
}) | ||
} | ||
|
||
func TestIncludeLabelsByPrefixWithWildcard(t *testing.T) { | ||
labels := map[string]string{ | ||
"foo": "foo-value", | ||
"bar": "bar-value", | ||
"lorem": "ipsum", | ||
} | ||
includeLabelPrefix := []string{"*"} | ||
|
||
filteredLabels := includeLabelsByPrefix(labels, includeLabelPrefix) | ||
|
||
assert.Equal(t, filteredLabels, map[string]string{ | ||
"foo": "foo-value", | ||
"bar": "bar-value", | ||
"lorem": "ipsum", | ||
}) | ||
} |