Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for admission controller based on namespace node selectors. #24980

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/kube-apiserver/app/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
_ "k8s.io/kubernetes/plugin/pkg/admission/namespace/exists"
_ "k8s.io/kubernetes/plugin/pkg/admission/namespace/lifecycle"
_ "k8s.io/kubernetes/plugin/pkg/admission/persistentvolume/label"
_ "k8s.io/kubernetes/plugin/pkg/admission/podnodeselector"
_ "k8s.io/kubernetes/plugin/pkg/admission/resourcequota"
_ "k8s.io/kubernetes/plugin/pkg/admission/security/podsecuritypolicy"
_ "k8s.io/kubernetes/plugin/pkg/admission/securitycontext/scdeny"
Expand Down
101 changes: 101 additions & 0 deletions pkg/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package labels

import (
"fmt"
"sort"
"strings"
)
Expand Down Expand Up @@ -78,3 +79,103 @@ func FormatLabels(labelMap map[string]string) string {
}
return l
}

// Conflicts takes 2 maps and returns true if there a key match between
// the maps but the value doesn't match, and returns false in other cases
func Conflicts(labels1, labels2 Set) bool {
small := labels1
big := labels2
if len(labels2) < len(labels1) {
small = labels2
big = labels1
}

for k, v := range small {
if val, match := big[k]; match {
if val != v {
return true
}
}
}

return false
}

// Merge combines given maps, and does not check for any conflicts
// between the maps. In case of conflicts, second map (labels2) wins
func Merge(labels1, labels2 Set) Set {
mergedMap := Set{}

for k, v := range labels1 {
mergedMap[k] = v
}
for k, v := range labels2 {
mergedMap[k] = v
}
return mergedMap
}

// Equals returns true if the given maps are equal
func Equals(labels1, labels2 Set) bool {
if len(labels1) != len(labels2) {
return false
}

for k, v := range labels1 {
value, ok := labels2[k]
if !ok {
return false
}
if value != v {
return false
}
}
return true
}

// AreLabelsInWhiteList verifies if the provided label list
// is in the provided whitelist and returns true, otherwise false.
func AreLabelsInWhiteList(labels, whitelist Set) bool {
if len(whitelist) == 0 {
return true
}

for k, v := range labels {
value, ok := whitelist[k]
if !ok {
return false
}
if value != v {
return false
}
}
return true
}

// ConvertSelectorToLabelsMap converts selector string to labels map
// and validates keys and values
func ConvertSelectorToLabelsMap(selector string) (Set, error) {
labelsMap := Set{}

if len(selector) == 0 {
return labelsMap, nil
}

labels := strings.Split(selector, ",")
for _, label := range labels {
l := strings.Split(label, "=")
if len(l) != 2 {
return labelsMap, fmt.Errorf("invalid selector: %s", l)
}
key := strings.TrimSpace(l[0])
if err := validateLabelKey(key); err != nil {
return labelsMap, err
}
value := strings.TrimSpace(l[1])
if err := validateLabelValue(value); err != nil {
return labelsMap, err
}
labelsMap[key] = value
}
return labelsMap, nil
}
171 changes: 171 additions & 0 deletions pkg/labels/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,174 @@ func TestLabelGet(t *testing.T) {
t.Errorf("Set.Get is broken")
}
}

func TestLabelConflict(t *testing.T) {
tests := []struct {
labels1 map[string]string
labels2 map[string]string
conflict bool
}{
{
labels1: map[string]string{},
labels2: map[string]string{},
conflict: false,
},
{
labels1: map[string]string{"env": "test"},
labels2: map[string]string{"infra": "true"},
conflict: false,
},
{
labels1: map[string]string{"env": "test"},
labels2: map[string]string{"infra": "true", "env": "test"},
conflict: false,
},
{
labels1: map[string]string{"env": "test"},
labels2: map[string]string{"env": "dev"},
conflict: true,
},
{
labels1: map[string]string{"env": "test", "infra": "false"},
labels2: map[string]string{"infra": "true", "color": "blue"},
conflict: true,
},
}
for _, test := range tests {
conflict := Conflicts(Set(test.labels1), Set(test.labels2))
if conflict != test.conflict {
t.Errorf("expected: %v but got: %v", test.conflict, conflict)
}
}
}

func TestLabelMerge(t *testing.T) {
tests := []struct {
labels1 map[string]string
labels2 map[string]string
mergedLabels map[string]string
}{
{
labels1: map[string]string{},
labels2: map[string]string{},
mergedLabels: map[string]string{},
},
{
labels1: map[string]string{"infra": "true"},
labels2: map[string]string{},
mergedLabels: map[string]string{"infra": "true"},
},
{
labels1: map[string]string{"infra": "true"},
labels2: map[string]string{"env": "test", "color": "blue"},
mergedLabels: map[string]string{"infra": "true", "env": "test", "color": "blue"},
},
}
for _, test := range tests {
mergedLabels := Merge(Set(test.labels1), Set(test.labels2))
if !Equals(mergedLabels, test.mergedLabels) {
t.Errorf("expected: %v but got: %v", test.mergedLabels, mergedLabels)
}
}
}

func TestLabelSelectorParse(t *testing.T) {
tests := []struct {
selector string
labels map[string]string
valid bool
}{
{
selector: "",
labels: map[string]string{},
valid: true,
},
{
selector: "x=a",
labels: map[string]string{"x": "a"},
valid: true,
},
{
selector: "x=a,y=b,z=c",
labels: map[string]string{"x": "a", "y": "b", "z": "c"},
valid: true,
},
{
selector: " x = a , y = b , z = c ",
labels: map[string]string{"x": "a", "y": "b", "z": "c"},
valid: true,
},
{
selector: "color=green,env=test,service=front",
labels: map[string]string{"color": "green", "env": "test", "service": "front"},
valid: true,
},
{
selector: "color=green, env=test, service=front",
labels: map[string]string{"color": "green", "env": "test", "service": "front"},
valid: true,
},
{
selector: ",",
labels: map[string]string{},
valid: false,
},
{
selector: "x",
labels: map[string]string{},
valid: false,
},
{
selector: "x,y",
labels: map[string]string{},
valid: false,
},
{
selector: "x=$y",
labels: map[string]string{},
valid: false,
},
{
selector: "x!=y",
labels: map[string]string{},
valid: false,
},
{
selector: "x==y",
labels: map[string]string{},
valid: false,
},
{
selector: "x=a||y=b",
labels: map[string]string{},
valid: false,
},
{
selector: "x in (y)",
labels: map[string]string{},
valid: false,
},
{
selector: "x notin (y)",
labels: map[string]string{},
valid: false,
},
{
selector: "x y",
labels: map[string]string{},
valid: false,
},
}
for _, test := range tests {
labels, err := ConvertSelectorToLabelsMap(test.selector)
if test.valid && err != nil {
t.Errorf("selector: %s, expected no error but got: %s", test.selector, err)
} else if !test.valid && err == nil {
t.Errorf("selector: %s, expected an error", test.selector)
}

if !Equals(Set(labels), test.labels) {
t.Errorf("expected: %s but got: %s", test.labels, labels)
}
}
}