This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1184 from justinbarrick/master
Add --k8s-namespace-whitelist setting that specifies namespaces to watch.
- Loading branch information
Showing
4 changed files
with
113 additions
and
13 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,62 @@ | ||
package kubernetes | ||
|
||
import ( | ||
apiv1 "k8s.io/api/core/v1" | ||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
fakekubernetes "k8s.io/client-go/kubernetes/fake" | ||
"testing" | ||
"reflect" | ||
) | ||
|
||
func newNamespace(name string) *apiv1.Namespace { | ||
return &apiv1.Namespace{ | ||
ObjectMeta: meta_v1.ObjectMeta{ | ||
Name: name, | ||
}, | ||
TypeMeta: meta_v1.TypeMeta{ | ||
APIVersion: "v1", | ||
Kind: "Namespace", | ||
}, | ||
} | ||
} | ||
|
||
func testGetAllowedNamespaces(t *testing.T, namespace []string, expected []string) { | ||
clientset := fakekubernetes.NewSimpleClientset(newNamespace("default"), | ||
newNamespace("kube-system")) | ||
|
||
c := NewCluster(clientset, nil, nil, nil, nil, namespace) | ||
|
||
namespaces, err := c.getAllowedNamespaces() | ||
if err != nil { | ||
t.Errorf("The error should be nil, not: %s", err) | ||
} | ||
|
||
result := []string{} | ||
for _, namespace := range namespaces { | ||
result = append(result, namespace.ObjectMeta.Name) | ||
} | ||
|
||
if reflect.DeepEqual(result, expected) != true { | ||
t.Errorf("Unexpected namespaces: %v != %v.", result, expected) | ||
} | ||
} | ||
|
||
func TestGetAllowedNamespacesDefault(t *testing.T) { | ||
testGetAllowedNamespaces(t, []string{}, []string{"default","kube-system",}) | ||
} | ||
|
||
func TestGetAllowedNamespacesNamespacesIsNil(t *testing.T) { | ||
testGetAllowedNamespaces(t, nil, []string{"default","kube-system",}) | ||
} | ||
|
||
func TestGetAllowedNamespacesNamespacesSet(t *testing.T) { | ||
testGetAllowedNamespaces(t, []string{"default"}, []string{"default",}) | ||
} | ||
|
||
func TestGetAllowedNamespacesNamespacesSetDoesNotExist(t *testing.T) { | ||
testGetAllowedNamespaces(t, []string{"hello"}, []string{}) | ||
} | ||
|
||
func TestGetAllowedNamespacesNamespacesMultiple(t *testing.T) { | ||
testGetAllowedNamespaces(t, []string{"default","hello","kube-system"}, []string{"default","kube-system"}) | ||
} |
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