Skip to content

Commit

Permalink
fix error
Browse files Browse the repository at this point in the history
  • Loading branch information
britaniar committed Mar 11, 2024
1 parent 6f7e619 commit 0834f2e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
2 changes: 1 addition & 1 deletion pkg/utils/informer/informermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package informer
import (
"context"
"fmt"
"k8s.io/klog/v2"

"sync"
"time"
Expand All @@ -17,6 +16,7 @@ import (
"k8s.io/client-go/dynamic"
"k8s.io/client-go/dynamic/dynamicinformer"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
)

// InformerManager manages dynamic shared informer for all resources, include Kubernetes resource and
Expand Down
38 changes: 14 additions & 24 deletions pkg/utils/informer/informermanager_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package informer

import (
"fmt"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/tools/cache"
)
Expand Down Expand Up @@ -38,24 +38,19 @@ var _ = Describe("Informer Manager Suite", func() {
// Make sure that the add method returns with no errors
//Check that the dynamic resource was added to the informer manager
impl.AddDynamicResources([]APIResourceMeta{dynResource}, handler, true)
if _, ok := impl.apiResources[dynResource.GroupVersionKind]; !ok {
fmt.Errorf("Expected dynamic resource %v to be added to informer manager", dynResource)
}
addedResource := impl.apiResources[dynResource.GroupVersionKind]
addedResource, ok := impl.apiResources[dynResource.GroupVersionKind]
Expect(ok).Should(BeTrue(), "Expected dynamic resource %v to be added to informer manager", dynResource)

// Check that the informer was created for the dynamic resource
informer := impl.informerFactory.ForResource(addedResource.GroupVersionResource).Informer()
if informer == nil {
fmt.Errorf("Expected informer to be created for resource %v", addedResource)
}
Expect(informer).ShouldNot(BeNil())

// Remove the dynamic resource from the informer manager
impl.AddDynamicResources([]APIResourceMeta{}, handler, true)

// Verify the map. Check that the dynamic resource was removed from the informer manager
if _, ok := impl.apiResources[dynResource.GroupVersionKind]; ok {
fmt.Errorf("Expected dynamic resource %v to be removed from informer manager", dynResource)
}
_, ok = impl.apiResources[dynResource.GroupVersionKind]
Expect(ok).ShouldNot(BeTrue(), "Expected dynamic resource %v to be removed from informer manager", dynResource)
})
It("multiple dynamic resources", func() {
handler := &cache.ResourceEventHandlerFuncs{
Expand Down Expand Up @@ -103,33 +98,28 @@ var _ = Describe("Informer Manager Suite", func() {
impl.AddDynamicResources([]APIResourceMeta{dynResources[0]}, handler, false)
impl.AddDynamicResources(dynResources, handler, true)
for _, dynResource := range dynResources {
if _, ok := impl.apiResources[dynResource.GroupVersionKind]; !ok {
fmt.Errorf("Expected dynamic resource %v to be added to informer manager", dynResource)
}
_, ok := impl.apiResources[dynResource.GroupVersionKind]
Expect(ok).Should(BeTrue(), "Expected dynamic resource %v to be added to informer manager", dynResource)
}

// Check that the informer was created for the dynamic resources
for _, dynResource := range dynResources {
informer := impl.informerFactory.ForResource(dynResource.GroupVersionResource).Informer()
if informer == nil {
fmt.Errorf("Expected informer to be created for resource %v", dynResource)
}
Expect(informer).ShouldNot(BeNil())
}

// Remove the dynamic resource from the informer manager
impl.AddDynamicResources([]APIResourceMeta{dynResources[0]}, handler, true)

// verify the map. Check that the dynamic resource was removed from the informer manager
if _, ok := impl.apiResources[dynResources[1].GroupVersionKind]; ok {
fmt.Errorf("Expected dynamic resource %v to be removed from informer manager", dynResources[1])
}
_, ok := impl.apiResources[dynResources[1].GroupVersionKind]
Expect(ok).ShouldNot(BeTrue(), "Expected dynamic resource %v to be removed from informer manager", dynResources[1])
// Remove the dynamic resource from the informer manager
impl.AddDynamicResources([]APIResourceMeta{}, handler, true)

// verify the map. Check that the dynamic resource was removed from the informer manager
if _, ok := impl.apiResources[dynResources[0].GroupVersionKind]; ok {
fmt.Errorf("Expected dynamic resource %v to be removed from informer manager", dynResources[0])
}
_, ok = impl.apiResources[dynResources[0].GroupVersionKind]
Expect(ok).ShouldNot(BeTrue(), "Expected dynamic resource %v to be removed from informer manager", dynResources[0])
})
})
})

0 comments on commit 0834f2e

Please sign in to comment.