-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
75 additions
and
54 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
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,20 @@ | ||
package admission | ||
|
||
import ( | ||
kapi "k8s.io/kubernetes/pkg/api" | ||
) | ||
|
||
// ByRestrictions is a helper to sort SCCs based on priority. If priorities are equal | ||
// a string compare of the name is used. | ||
type ByPriority []*kapi.SecurityContextConstraints | ||
|
||
func (s ByPriority) Len() int { | ||
return len(s) | ||
} | ||
func (s ByPriority) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | ||
func (s ByPriority) Less(i, j int) bool { | ||
if s[i].SortPriority == s[j].SortPriority { | ||
return s[i].Name < s[j].Name | ||
} | ||
return s[i].SortPriority > s[j].SortPriority | ||
} |
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,43 @@ | ||
package admission | ||
|
||
import ( | ||
"sort" | ||
"testing" | ||
|
||
kapi "k8s.io/kubernetes/pkg/api" | ||
) | ||
|
||
func TestByPriority(t *testing.T) { | ||
tests := map[string]struct { | ||
sccs []*kapi.SecurityContextConstraints | ||
expected []string | ||
}{ | ||
"sort with priorities": { | ||
sccs: []*kapi.SecurityContextConstraints{testSCC("one", 1), testSCC("two", 2), testSCC("three", 3), testSCC("negative", -1), testSCC("super", 100)}, | ||
expected: []string{"super", "three", "two", "one", "negative"}, | ||
}, | ||
"sort with equal priorities": { | ||
sccs: []*kapi.SecurityContextConstraints{testSCC("one", 1), testSCC("foo", 2), testSCC("bar", 2), testSCC("baz", 2), testSCC("barbaz", 2)}, | ||
expected: []string{"bar", "barbaz", "baz", "foo", "one"}, | ||
}, | ||
} | ||
|
||
for testCaseName, testCase := range tests { | ||
sort.Sort(ByPriority(testCase.sccs)) | ||
|
||
for i, scc := range testCase.sccs { | ||
if scc.Name != testCase.expected[i] { | ||
t.Errorf("%s found %s at element %d but expected %s", testCaseName, scc.Name, i, testCase.expected[i]) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func testSCC(name string, priority int) *kapi.SecurityContextConstraints { | ||
return &kapi.SecurityContextConstraints{ | ||
ObjectMeta: kapi.ObjectMeta{ | ||
Name: name, | ||
}, | ||
SortPriority: priority, | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.