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.

cached_disco_test.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package kubernetes
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. crdv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
  7. crdfake "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
  8. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  9. )
  10. func TestCachedDiscovery(t *testing.T) {
  11. coreClient := makeFakeClient()
  12. myCRD := &crdv1beta1.CustomResourceDefinition{
  13. ObjectMeta: metav1.ObjectMeta{
  14. Name: "custom",
  15. },
  16. }
  17. crdClient := crdfake.NewSimpleClientset(myCRD)
  18. // Here's my fake API resource
  19. myAPI := &metav1.APIResourceList{
  20. GroupVersion: "foo/v1",
  21. APIResources: []metav1.APIResource{
  22. {Name: "customs", SingularName: "custom", Namespaced: true, Kind: "Custom", Verbs: getAndList},
  23. },
  24. }
  25. apiResources := coreClient.Fake.Resources
  26. coreClient.Fake.Resources = append(apiResources, myAPI)
  27. shutdown := make(chan struct{})
  28. defer close(shutdown)
  29. cachedDisco := MakeCachedDiscovery(coreClient.Discovery(), crdClient, shutdown)
  30. saved := getDefaultNamespace
  31. getDefaultNamespace = func() (string, error) { return "bar-ns", nil }
  32. defer func() { getDefaultNamespace = saved }()
  33. namespacer, err := NewNamespacer(cachedDisco)
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. namespaced, err := namespacer.lookupNamespaced("foo/v1", "Custom", nil)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. if !namespaced {
  42. t.Error("got false from lookupNamespaced, expecting true")
  43. }
  44. // In a cluster, we'd rely on the apiextensions server to reflect
  45. // changes to CRDs to changes in the API resources. Here I will be
  46. // more narrow, and just test that the API resources are reloaded
  47. // when a CRD is updated or deleted.
  48. // This is delicate: we can't just change the value in-place,
  49. // since that will update everyone's record of it, and the test
  50. // below will trivially succeed.
  51. updatedAPI := &metav1.APIResourceList{
  52. GroupVersion: "foo/v1",
  53. APIResources: []metav1.APIResource{
  54. {Name: "customs", SingularName: "custom", Namespaced: false /* <-- changed */, Kind: "Custom", Verbs: getAndList},
  55. },
  56. }
  57. coreClient.Fake.Resources = append(apiResources, updatedAPI)
  58. // Provoke the cached discovery client into invalidating
  59. _, err = crdClient.ApiextensionsV1beta1().CustomResourceDefinitions().Update(myCRD)
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. // Wait for the update to "go through"
  64. c := time.After(time.Second)
  65. loop:
  66. for {
  67. select {
  68. default:
  69. namespaced, err = namespacer.lookupNamespaced("foo/v1", "Custom", nil)
  70. assert.NoError(t, err)
  71. if !namespaced {
  72. break loop
  73. }
  74. time.Sleep(10 * time.Millisecond)
  75. case <-c:
  76. t.Fatal("timed out waiting for Update to happen")
  77. }
  78. }
  79. }