Skip to content

Commit

Permalink
🧹 share k8s discovery cache within a provider instance (#2063)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Milchev <[email protected]>
  • Loading branch information
imilchev authored Oct 4, 2023
1 parent f2d8b29 commit 5d810ef
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
4 changes: 2 additions & 2 deletions providers/k8s/connection/api/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Connection struct {
currentClusterName string
}

func NewConnection(id uint32, asset *inventory.Asset) (shared.Connection, error) {
func NewConnection(id uint32, asset *inventory.Asset, discoveryCache *resources.DiscoveryCache) (shared.Connection, error) {
// check if the user .kube/config file exists
// NOTE: BuildConfigFromFlags falls back to cluster loading when .kube/config string is empty
// therefore we want to only change the kubeconfig string when the file really exists
Expand Down Expand Up @@ -72,7 +72,7 @@ func NewConnection(id uint32, asset *inventory.Asset) (shared.Connection, error)
config.Burst = 1000

// initialize api
d, err := resources.NewDiscoveryCache().Get(config)
d, err := discoveryCache.Get(config)
if err != nil {
return nil, err
}
Expand Down
5 changes: 4 additions & 1 deletion providers/k8s/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"go.mondoo.com/cnquery/providers/k8s/connection/api"
"go.mondoo.com/cnquery/providers/k8s/connection/manifest"
"go.mondoo.com/cnquery/providers/k8s/connection/shared"
connectionResources "go.mondoo.com/cnquery/providers/k8s/connection/shared/resources"
"go.mondoo.com/cnquery/providers/k8s/resources"
)

Expand All @@ -24,12 +25,14 @@ const ConnectionType = "k8s"
type Service struct {
runtimes map[uint32]*plugin.Runtime
lastConnectionID uint32
discoveryCache *connectionResources.DiscoveryCache
}

func Init() *Service {
return &Service{
runtimes: map[uint32]*plugin.Runtime{},
lastConnectionID: 0,
discoveryCache: connectionResources.NewDiscoveryCache(),
}
}

Expand Down Expand Up @@ -165,7 +168,7 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba
}
} else {
s.lastConnectionID++
conn, err = api.NewConnection(s.lastConnectionID, asset)
conn, err = api.NewConnection(s.lastConnectionID, asset, s.discoveryCache)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 5d810ef

Please sign in to comment.