Skip to content

Commit

Permalink
refactor, resourcetopo informer
Browse files Browse the repository at this point in the history
  • Loading branch information
shaofan-hs committed Apr 15, 2024
1 parent 249890d commit eb8b0b8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions resourcetopo/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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")
}
Expand Down
3 changes: 1 addition & 2 deletions resourcetopo/node_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
Expand Down
12 changes: 5 additions & 7 deletions resourcetopo/resourcetopo_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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():
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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},
Expand All @@ -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{
Expand Down
9 changes: 7 additions & 2 deletions resourcetopo/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit eb8b0b8

Please sign in to comment.