diff --git a/resourcetopo/manager.go b/resourcetopo/manager.go index fa60769..93f87c8 100644 --- a/resourcetopo/manager.go +++ b/resourcetopo/manager.go @@ -22,8 +22,8 @@ import ( "sync" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" - "k8s.io/client-go/tools/cache" "k8s.io/klog/v2" ) @@ -151,7 +151,7 @@ func (m *manager) GetNode(meta metav1.TypeMeta, name types.NamespacedName) (Node return s.GetNode(name) } -func (m *manager) getOrCreateStorage(typeMeta metav1.TypeMeta, getInformer func(meta metav1.TypeMeta) cache.SharedInformer) (*nodeStorage, error) { +func (m *manager) getOrCreateStorage(typeMeta metav1.TypeMeta, getInformer func(meta metav1.TypeMeta) Informer) (*nodeStorage, error) { if getInformer == nil { return nil, fmt.Errorf("unexpected nil getInformer func") } diff --git a/resourcetopo/node_storage.go b/resourcetopo/node_storage.go index b91bbe2..a490034 100644 --- a/resourcetopo/node_storage.go +++ b/resourcetopo/node_storage.go @@ -23,7 +23,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/types" - "k8s.io/client-go/tools/cache" "k8s.io/klog/v2" ) @@ -53,7 +52,7 @@ type nodeStorage struct { namespacedInfo map[string]map[string]*nodeInfo // namespace => name => object info } -func newNodeStorage(manager *manager, informer cache.SharedInformer, meta metav1.TypeMeta) *nodeStorage { +func newNodeStorage(manager *manager, informer Informer, meta metav1.TypeMeta) *nodeStorage { s := &nodeStorage{ manager: manager, meta: meta, diff --git a/resourcetopo/resourcetopo_utils_test.go b/resourcetopo/resourcetopo_utils_test.go index 67e4e79..a0d4b8f 100644 --- a/resourcetopo/resourcetopo_utils_test.go +++ b/resourcetopo/resourcetopo_utils_test.go @@ -27,7 +27,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/informers" - "k8s.io/client-go/tools/cache" "k8s.io/klog/v2" ) @@ -45,7 +44,7 @@ var ( ServiceAccountMeta = metav1.TypeMeta{Kind: "ServiceAccount", APIVersion: "core/v1"} ) -func GetInformer(meta metav1.TypeMeta, k8sInformerFactory informers.SharedInformerFactory) cache.SharedIndexInformer { +func GetInformer(meta metav1.TypeMeta, k8sInformerFactory informers.SharedInformerFactory) Informer { gvk := meta.String() switch gvk { case PodMeta.String(): @@ -78,7 +77,7 @@ func buildManagerConfig(config *TopologyConfig) *ManagerConfig { func buildInspectTopoConfig(k8sInformerFactory informers.SharedInformerFactory) *TopologyConfig { return &TopologyConfig{ - GetInformer: func(meta metav1.TypeMeta) cache.SharedInformer { + GetInformer: func(meta metav1.TypeMeta) Informer { return GetInformer(meta, k8sInformerFactory) }, Resolvers: []RelationResolver{ @@ -121,7 +120,7 @@ func buildInspectTopoConfig(k8sInformerFactory informers.SharedInformerFactory) func buildClusterTest(k8sInformerFactory informers.SharedInformerFactory) *TopologyConfig { return &TopologyConfig{ - GetInformer: func(meta metav1.TypeMeta) cache.SharedInformer { + GetInformer: func(meta metav1.TypeMeta) Informer { return GetInformer(meta, k8sInformerFactory) }, Resolvers: []RelationResolver{ @@ -163,12 +162,11 @@ func buildClusterTest(k8sInformerFactory informers.SharedInformerFactory) *Topol func buildSvcPodTest(k8sInformerFactory informers.SharedInformerFactory) *TopologyConfig { return &TopologyConfig{ - GetInformer: func(meta metav1.TypeMeta) cache.SharedInformer { + GetInformer: func(meta metav1.TypeMeta) Informer { return GetInformer(meta, k8sInformerFactory) }, Resolvers: []RelationResolver{ { - PreMeta: ServiceMeta, PostMetas: []metav1.TypeMeta{PodMeta}, ReverseNotice: []metav1.TypeMeta{PodMeta}, @@ -194,7 +192,7 @@ func buildSvcPodTest(k8sInformerFactory informers.SharedInformerFactory) *Topolo func buildDeployTopoConfig(k8sInformerFactory informers.SharedInformerFactory) *TopologyConfig { return &TopologyConfig{ - GetInformer: func(meta metav1.TypeMeta) cache.SharedInformer { + GetInformer: func(meta metav1.TypeMeta) Informer { return GetInformer(meta, k8sInformerFactory) }, Resolvers: []RelationResolver{ diff --git a/resourcetopo/types.go b/resourcetopo/types.go index 2f91fb6..580fdfd 100644 --- a/resourcetopo/types.go +++ b/resourcetopo/types.go @@ -19,13 +19,18 @@ package resourcetopo import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" - "k8s.io/client-go/tools/cache" + toolscache "k8s.io/client-go/tools/cache" ) +type Informer interface { + // AddEventHandler adds an event handler which will process related resource toppologies + AddEventHandler(handler toolscache.ResourceEventHandler) +} + // TopologyConfig offers a way to describe the relation among kubernetes or kubernetes-likely resource. type TopologyConfig struct { // GetInformer return a working SharedInformer for objects of meta type - GetInformer func(meta metav1.TypeMeta) cache.SharedInformer + GetInformer func(meta metav1.TypeMeta) Informer Resolvers []RelationResolver Discoverers []VirtualResourceDiscoverer }