-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement policy-level tag prefix matching
Tag prefix matching allows the user to filter tags based on include/exclude criteria and offers the possibility to enable prefix trimming prior to policy evaluation. Signed-off-by: Aurel Canciu <[email protected]>
- Loading branch information
Showing
11 changed files
with
321 additions
and
24 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,46 @@ | ||
/* | ||
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 policy | ||
|
||
import "strings" | ||
|
||
// PrefixMatchFilter applies filtering based on the provided lists of included and | ||
// excluded prefixes | ||
func PrefixMatchFilter(list []string, include []string, exclude []string) []string { | ||
var filtered []string | ||
for _, item := range list { | ||
// Keep by default if no include prefixes specified | ||
var keep bool = len(include) == 0 | ||
for _, in := range include { | ||
if strings.HasPrefix(item, in) { | ||
keep = true | ||
} | ||
} | ||
|
||
for _, out := range exclude { | ||
if strings.HasPrefix(item, out) { | ||
keep = false | ||
} | ||
} | ||
|
||
if keep { | ||
filtered = append(filtered, item) | ||
} | ||
} | ||
|
||
return filtered | ||
} |
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,64 @@ | ||
/* | ||
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 policy | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestPrefixMatchFilter(t *testing.T) { | ||
cases := []struct { | ||
label string | ||
tags []string | ||
include []string | ||
exclude []string | ||
expected []string | ||
}{ | ||
{ | ||
label: "none", | ||
tags: []string{"a"}, | ||
expected: []string{"a"}, | ||
}, | ||
{ | ||
label: "include", | ||
tags: []string{"ver1", "ver2", "ver3", "rel1"}, | ||
include: []string{"ver"}, | ||
expected: []string{"ver1", "ver2", "ver3"}, | ||
}, | ||
{ | ||
label: "include", | ||
tags: []string{"ver1", "ver2", "ver3", "rel1"}, | ||
exclude: []string{"rel"}, | ||
expected: []string{"ver1", "ver2", "ver3"}, | ||
}, | ||
{ | ||
label: "include and exclude", | ||
tags: []string{"ver1", "ver2", "rel1", "rel2", "patch1", "patch2"}, | ||
exclude: []string{"rel", "patch"}, | ||
expected: []string{"ver1", "ver2"}, | ||
}, | ||
} | ||
for _, tt := range cases { | ||
t.Run(tt.label, func(t *testing.T) { | ||
r := PrefixMatchFilter(tt.tags, tt.include, tt.exclude) | ||
if !reflect.DeepEqual(r, tt.expected) { | ||
t.Errorf("incorrect value returned, got '%s', expected '%s'", r, tt.expected) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.