GitOps for k8s
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

namespacer_test.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package kubernetes
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  7. corefake "k8s.io/client-go/kubernetes/fake"
  8. kresource "github.com/fluxcd/flux/cluster/kubernetes/resource"
  9. )
  10. var getAndList = metav1.Verbs([]string{"get", "list"})
  11. func makeFakeClient() *corefake.Clientset {
  12. apiResources := []*metav1.APIResourceList{
  13. {
  14. GroupVersion: "apps/v1",
  15. APIResources: []metav1.APIResource{
  16. {Name: "deployments", SingularName: "deployment", Namespaced: true, Kind: "Deployment", Verbs: getAndList},
  17. },
  18. },
  19. {
  20. GroupVersion: "v1",
  21. APIResources: []metav1.APIResource{
  22. {Name: "namespaces", SingularName: "namespace", Namespaced: false, Kind: "Namespace", Verbs: getAndList},
  23. },
  24. },
  25. {
  26. GroupVersion: "apiextensions.k8s.io/v1beta1",
  27. APIResources: []metav1.APIResource{
  28. {Name: "customresourcedefinitions", SingularName: "customresourcedefinition", Namespaced: false, Kind: "CustomResourceDefinition", Verbs: getAndList},
  29. },
  30. },
  31. }
  32. coreClient := corefake.NewSimpleClientset()
  33. coreClient.Fake.Resources = apiResources
  34. return coreClient
  35. }
  36. func TestNamespaceDefaulting(t *testing.T) {
  37. testKubeconfig := `apiVersion: v1
  38. clusters: []
  39. contexts:
  40. - context:
  41. cluster: cluster
  42. namespace: namespace
  43. user: user
  44. name: context
  45. current-context: context
  46. kind: Config
  47. preferences: {}
  48. users: []
  49. `
  50. err := ioutil.WriteFile("testkubeconfig", []byte(testKubeconfig), 0600)
  51. if err != nil {
  52. t.Fatal("cannot create test kubeconfig file")
  53. }
  54. defer os.Remove("testkubeconfig")
  55. os.Setenv("KUBECONFIG", "testkubeconfig")
  56. defer os.Unsetenv("KUBECONFIG")
  57. coreClient := makeFakeClient()
  58. ns, err := getDefaultNamespace()
  59. if err != nil {
  60. t.Fatal("cannot get default namespace")
  61. }
  62. if ns != "namespace" {
  63. t.Fatal("unexpected default namespace", ns)
  64. }
  65. nser, err := NewNamespacer(coreClient.Discovery())
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. const defs = `---
  70. apiVersion: apps/v1
  71. kind: Deployment
  72. metadata:
  73. name: hasNamespace
  74. namespace: foo-ns
  75. ---
  76. apiVersion: apps/v1
  77. kind: Deployment
  78. metadata:
  79. name: noNamespace
  80. ---
  81. apiVersion: v1
  82. kind: Namespace
  83. metadata:
  84. name: notNamespaced
  85. namespace: spurious
  86. `
  87. manifests, err := kresource.ParseMultidoc([]byte(defs), "<string>")
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. assertEffectiveNamespace := func(id, expected string) {
  92. res, ok := manifests[id]
  93. if !ok {
  94. t.Errorf("manifest for %q not found", id)
  95. return
  96. }
  97. got, err := nser.EffectiveNamespace(res, nil)
  98. if err != nil {
  99. t.Errorf("error getting effective namespace for %q: %s", id, err.Error())
  100. return
  101. }
  102. if got != expected {
  103. t.Errorf("expected effective namespace of %q, got %q", expected, got)
  104. }
  105. }
  106. assertEffectiveNamespace("foo-ns:deployment/hasNamespace", "foo-ns")
  107. assertEffectiveNamespace("<cluster>:deployment/noNamespace", "namespace")
  108. assertEffectiveNamespace("spurious:namespace/notNamespaced", "")
  109. }