Skip to content

Commit

Permalink
refactor: move store types in their own package
Browse files Browse the repository at this point in the history
this prevent import cycle
  • Loading branch information
guilhem committed Nov 15, 2024
1 parent 25b1960 commit c68bd31
Show file tree
Hide file tree
Showing 27 changed files with 216 additions and 173 deletions.
7 changes: 4 additions & 3 deletions cmd/switcher/switcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
switchconfig "github.com/danielfoehrkn/kubeswitch/pkg/config"
"github.com/danielfoehrkn/kubeswitch/pkg/config/validation"
"github.com/danielfoehrkn/kubeswitch/pkg/store"
storetypes "github.com/danielfoehrkn/kubeswitch/pkg/store/types"
"github.com/danielfoehrkn/kubeswitch/types"
)

Expand Down Expand Up @@ -160,7 +161,7 @@ func setCommonFlags(command *cobra.Command) {
"path to the local directory used for storing internal state.")
}

func initialize() ([]store.KubeconfigStore, *types.Config, error) {
func initialize() ([]storetypes.KubeconfigStore, *types.Config, error) {
if showDebugLogs {
logrus.SetLevel(logrus.DebugLevel)
}
Expand Down Expand Up @@ -194,11 +195,11 @@ func initialize() ([]store.KubeconfigStore, *types.Config, error) {
}

var (
stores []store.KubeconfigStore
stores []storetypes.KubeconfigStore
digitalOceanStoreAddedViaConfig bool
)
for _, kubeconfigStoreFromConfig := range config.KubeconfigStores {
var s store.KubeconfigStore
var s storetypes.KubeconfigStore

if kubeconfigStoreFromConfig.KubeconfigName != nil && *kubeconfigStoreFromConfig.KubeconfigName != "" {
kubeconfigName = *kubeconfigStoreFromConfig.KubeconfigName
Expand Down
6 changes: 3 additions & 3 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"fmt"
"sync"

"github.com/danielfoehrkn/kubeswitch/pkg/store"
storetypes "github.com/danielfoehrkn/kubeswitch/pkg/store/types"
"github.com/danielfoehrkn/kubeswitch/types"
)

Expand All @@ -27,15 +27,15 @@ var (
caches = make(map[string]CacheFactory)
)

type CacheFactory func(store store.KubeconfigStore, cfg *types.Cache) (store.KubeconfigStore, error)
type CacheFactory func(store storetypes.KubeconfigStore, cfg *types.Cache) (storetypes.KubeconfigStore, error)

func Register(kind string, creator CacheFactory) {
cachesMu.Lock()
defer cachesMu.Unlock()
caches[kind] = creator
}

func New(kind string, store store.KubeconfigStore, cfg *types.Cache) (store.KubeconfigStore, error) {
func New(kind string, store storetypes.KubeconfigStore, cfg *types.Cache) (storetypes.KubeconfigStore, error) {
cachesMu.RLock()
create, ok := caches[kind]
cachesMu.RUnlock()
Expand Down
10 changes: 5 additions & 5 deletions pkg/cache/file/fileCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"strings"

"github.com/danielfoehrkn/kubeswitch/pkg/cache"
"github.com/danielfoehrkn/kubeswitch/pkg/store"
storetypes "github.com/danielfoehrkn/kubeswitch/pkg/store/types"
"github.com/danielfoehrkn/kubeswitch/pkg/util"
kubeconfigutil "github.com/danielfoehrkn/kubeswitch/pkg/util/kubectx_copied"
"github.com/danielfoehrkn/kubeswitch/types"
Expand All @@ -37,7 +37,7 @@ func init() {
cache.Register(cacheKey, New)
}

func New(upstream store.KubeconfigStore, ccfg *types.Cache) (store.KubeconfigStore, error) {
func New(upstream storetypes.KubeconfigStore, ccfg *types.Cache) (storetypes.KubeconfigStore, error) {
if ccfg == nil {
return nil, fmt.Errorf("cache config must be provided for file cache")
}
Expand Down Expand Up @@ -72,7 +72,7 @@ func New(upstream store.KubeconfigStore, ccfg *types.Cache) (store.KubeconfigSto
}

type fileCache struct {
upstream store.KubeconfigStore
upstream storetypes.KubeconfigStore
cfg fileCacheCfg
logger *logrus.Entry
}
Expand Down Expand Up @@ -182,7 +182,7 @@ func (c *fileCache) VerifyKubeconfigPaths() error {
return c.upstream.VerifyKubeconfigPaths()
}

func (c *fileCache) StartSearch(channel chan store.SearchResult) {
func (c *fileCache) StartSearch(channel chan storetypes.SearchResult) {
c.upstream.StartSearch(channel)
}

Expand All @@ -194,7 +194,7 @@ func (c *fileCache) GetStoreConfig() types.KubeconfigStore {
}

func (c *fileCache) GetSearchPreview(path string, optionalTags map[string]string) (string, error) {
previewer, ok := c.upstream.(store.Previewer)
previewer, ok := c.upstream.(storetypes.Previewer)
if !ok {
// if the wrapped store is not a previewer, simply return an empty string, hence causing no visual distortion
return "", nil
Expand Down
10 changes: 5 additions & 5 deletions pkg/cache/memory/memoryCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package memory

import (
"github.com/danielfoehrkn/kubeswitch/pkg/cache"
"github.com/danielfoehrkn/kubeswitch/pkg/store"
storetypes "github.com/danielfoehrkn/kubeswitch/pkg/store/types"
"github.com/danielfoehrkn/kubeswitch/types"
"github.com/sirupsen/logrus"
)
Expand All @@ -25,15 +25,15 @@ func init() {
cache.Register("memory", New)
}

func New(upstream store.KubeconfigStore, _ *types.Cache) (store.KubeconfigStore, error) {
func New(upstream storetypes.KubeconfigStore, _ *types.Cache) (storetypes.KubeconfigStore, error) {
return &memoryCache{
upstream: upstream,
cache: make(map[string][]byte),
}, nil
}

type memoryCache struct {
upstream store.KubeconfigStore
upstream storetypes.KubeconfigStore
cache map[string][]byte
}

Expand Down Expand Up @@ -70,7 +70,7 @@ func (c *memoryCache) VerifyKubeconfigPaths() error {
return c.upstream.VerifyKubeconfigPaths()
}

func (c *memoryCache) StartSearch(channel chan store.SearchResult) {
func (c *memoryCache) StartSearch(channel chan storetypes.SearchResult) {
c.upstream.StartSearch(channel)
}

Expand All @@ -83,7 +83,7 @@ func (c *memoryCache) GetStoreConfig() types.KubeconfigStore {
}

func (c *memoryCache) GetSearchPreview(path string, optionalTags map[string]string) (string, error) {
previewer, ok := c.upstream.(store.Previewer)
previewer, ok := c.upstream.(storetypes.Previewer)
if !ok {
// if the wrapped store is not a previewer, simply return an empty string, hence causing no visual distortion
return "", nil
Expand Down
16 changes: 8 additions & 8 deletions pkg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"gopkg.in/yaml.v2"

"github.com/danielfoehrkn/kubeswitch/pkg/index"
"github.com/danielfoehrkn/kubeswitch/pkg/store"
storetypes "github.com/danielfoehrkn/kubeswitch/pkg/store/types"
aliasutil "github.com/danielfoehrkn/kubeswitch/pkg/subcommands/alias/util"
"github.com/danielfoehrkn/kubeswitch/pkg/util"
kubeconfigutil "github.com/danielfoehrkn/kubeswitch/pkg/util/kubectx_copied"
Expand Down Expand Up @@ -64,7 +64,7 @@ var (
logger = logrus.New()
)

func Switcher(stores []store.KubeconfigStore, config *types.Config, stateDir string, noIndex, showPreview bool) (*string, *string, error) {
func Switcher(stores []storetypes.KubeconfigStore, config *types.Config, stateDir string, noIndex, showPreview bool) (*string, *string, error) {
c, err := DoSearch(stores, config, stateDir, noIndex)
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -108,7 +108,7 @@ func Switcher(stores []store.KubeconfigStore, config *types.Config, stateDir str
}(*c)

// remember the store for later kubeconfig retrieval
var kindToStore = map[string]store.KubeconfigStore{}
var kindToStore = map[string]storetypes.KubeconfigStore{}
for _, s := range stores {
kindToStore[s.GetID()] = s
}
Expand Down Expand Up @@ -180,7 +180,7 @@ func Switcher(stores []store.KubeconfigStore, config *types.Config, stateDir str

// writeIndex tries to write the Index file for the kubeconfig store
// if it fails to do so, it logs a warning, but does not panic
func writeIndex(store store.KubeconfigStore, searchIndex *index.SearchIndex, ctxToPathMapping map[string]string, ctxToTagsMapping map[string]map[string]string) {
func writeIndex(store storetypes.KubeconfigStore, searchIndex *index.SearchIndex, ctxToPathMapping map[string]string, ctxToTagsMapping map[string]map[string]string) {
index := types.Index{
Kind: store.GetKind(),
ContextToPathMapping: ctxToPathMapping,
Expand All @@ -202,7 +202,7 @@ func writeIndex(store store.KubeconfigStore, searchIndex *index.SearchIndex, ctx
}
}

func showFuzzySearch(storeIDToStore map[string]store.KubeconfigStore, showPreview bool) (string, string, error) {
func showFuzzySearch(storeIDToStore map[string]storetypes.KubeconfigStore, showPreview bool) (string, string, error) {
// display selection dialog for all kubeconfig context names
idx, err := fuzzyfinder.Find(
&allKubeconfigContextNames,
Expand All @@ -224,7 +224,7 @@ func showFuzzySearch(storeIDToStore map[string]store.KubeconfigStore, showPrevie
}

// getFuzzyFinderOptions returns a list of fuzzy finder options
func getFuzzyFinderOptions(storeIDToStore map[string]store.KubeconfigStore, showPreview bool) []fuzzyfinder.Option {
func getFuzzyFinderOptions(storeIDToStore map[string]storetypes.KubeconfigStore, showPreview bool) []fuzzyfinder.Option {
options := []fuzzyfinder.Option{fuzzyfinder.WithHotReloadLock(hotReloadLock.RLocker())}

if showPreview {
Expand All @@ -245,7 +245,7 @@ func getFuzzyFinderOptions(storeIDToStore map[string]store.KubeconfigStore, show
kubeconfigStore := storeIDToStore[storeID]

var storeSpecificPreview *string
previewer, ok := kubeconfigStore.(store.Previewer)
previewer, ok := kubeconfigStore.(storetypes.Previewer)
if ok {
pr, err := previewer.GetSearchPreview(path, tags)
if err != nil {
Expand Down Expand Up @@ -278,7 +278,7 @@ func getFuzzyFinderOptions(storeIDToStore map[string]store.KubeconfigStore, show
return options
}

func getSanitizedKubeconfigForKubeconfigPath(kubeconfigStore store.KubeconfigStore, path string, tags map[string]string) (string, error) {
func getSanitizedKubeconfigForKubeconfigPath(kubeconfigStore storetypes.KubeconfigStore, path string, tags map[string]string) (string, error) {
// during first run without index, the files are already read in the getContextsForKubeconfigPath and saved in-memory
kubeconfig := readFromPathToKubeconfig(path)
if len(kubeconfig) > 0 {
Expand Down
19 changes: 10 additions & 9 deletions pkg/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ import (
"os"
"sync"

"github.com/sirupsen/logrus"

"github.com/danielfoehrkn/kubeswitch/pkg/index"
"github.com/danielfoehrkn/kubeswitch/pkg/store"
storetypes "github.com/danielfoehrkn/kubeswitch/pkg/store/types"
aliasstate "github.com/danielfoehrkn/kubeswitch/pkg/subcommands/alias/state"
aliasutil "github.com/danielfoehrkn/kubeswitch/pkg/subcommands/alias/util"
"github.com/danielfoehrkn/kubeswitch/pkg/util"
"github.com/danielfoehrkn/kubeswitch/types"
"github.com/sirupsen/logrus"
)

type DiscoveredContext struct {
Expand All @@ -39,14 +40,14 @@ type DiscoveredContext struct {
// This metadata is later handed over in the getKubeconfigForPath() function when retrieving the kubeconfig bytes for the path
Tags map[string]string
// Store is a reference to the backing store that contains the kubeconfig
Store *store.KubeconfigStore
Store *storetypes.KubeconfigStore
// Error is an error that occured during the search
Error error
}

// DoSearch executes a concurrent search over the given kubeconfig stores
// returns results from all stores on the return channel
func DoSearch(stores []store.KubeconfigStore, config *types.Config, stateDir string, noIndex bool) (*chan DiscoveredContext, error) {
func DoSearch(stores []storetypes.KubeconfigStore, config *types.Config, stateDir string, noIndex bool) (*chan DiscoveredContext, error) {
// Silence STDOUT during search to not interfere with the search selection screen
// restore after search is over
originalSTDOUT := os.Stdout
Expand Down Expand Up @@ -102,7 +103,7 @@ func DoSearch(stores []store.KubeconfigStore, config *types.Config, stateDir str
if readFromIndex {
logrus.Debugf("Reading from index for store %s with kind %s", kubeconfigStore.GetID(), kubeconfigStore.GetKind())

go func(store store.KubeconfigStore, index index.SearchIndex) {
go func(store storetypes.KubeconfigStore, index index.SearchIndex) {
// reading from this store is finished, decrease wait counter
defer wgResultChannel.Done()

Expand All @@ -129,15 +130,15 @@ func DoSearch(stores []store.KubeconfigStore, config *types.Config, stateDir str
}

// otherwise, we need to query the backing store for the kubeconfig files
c := make(chan store.SearchResult)
go func(store store.KubeconfigStore, channel chan store.SearchResult) {
c := make(chan storetypes.SearchResult)
go func(store storetypes.KubeconfigStore, channel chan storetypes.SearchResult) {
// only close when directory search is over, otherwise send on closed resultChannel
defer close(channel)
store.GetLogger().Debugf("Starting search for store: %s", store.GetKind())
store.StartSearch(channel)
}(kubeconfigStore, c)

go func(store store.KubeconfigStore, storeSearchChannel chan store.SearchResult, index index.SearchIndex) {
go func(store storetypes.KubeconfigStore, storeSearchChannel chan storetypes.SearchResult, index index.SearchIndex) {
// remember the context to kubeconfig path mapping for this store
// to write it to the index. Do not use the global "ContextToPathMapping"
// as this contains contexts names from all stores combined
Expand Down Expand Up @@ -217,7 +218,7 @@ func DoSearch(stores []store.KubeconfigStore, config *types.Config, stateDir str
return &resultChannel, nil
}

func shouldReadFromIndex(searchIndex *index.SearchIndex, kubeconfigStore store.KubeconfigStore, config *types.Config) (bool, error) {
func shouldReadFromIndex(searchIndex *index.SearchIndex, kubeconfigStore storetypes.KubeconfigStore, config *types.Config) (bool, error) {
// never write an index for the store from env variables and --kubeconfig-path command line falg
if kubeconfigStore.GetID() == fmt.Sprintf("%s.%s", types.StoreKindFilesystem, "env-and-flag") {
return false, nil
Expand Down
12 changes: 7 additions & 5 deletions pkg/store/kubeconfig_store_akamai.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ import (
"golang.org/x/oauth2"
"gopkg.in/yaml.v3"

"github.com/danielfoehrkn/kubeswitch/types"
"github.com/linode/linodego"
"github.com/sirupsen/logrus"

storetypes "github.com/danielfoehrkn/kubeswitch/pkg/store/types"
"github.com/danielfoehrkn/kubeswitch/types"
)

func NewAkamaiStore(store types.KubeconfigStore) (*AkamaiStore, error) {
Expand Down Expand Up @@ -105,14 +107,14 @@ func (s *AkamaiStore) GetLogger() *logrus.Entry {
return s.Logger
}

func (s *AkamaiStore) StartSearch(channel chan SearchResult) {
func (s *AkamaiStore) StartSearch(channel chan storetypes.SearchResult) {
s.Logger.Debug("Akamai: start search")

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

if err := s.InitializeAkamaiStore(); err != nil {
channel <- SearchResult{
channel <- storetypes.SearchResult{
KubeconfigPath: "",
Error: err,
}
Expand All @@ -122,15 +124,15 @@ func (s *AkamaiStore) StartSearch(channel chan SearchResult) {
// list linode instances
instances, err := s.Client.ListLKEClusters(ctx, nil)
if err != nil {
channel <- SearchResult{
channel <- storetypes.SearchResult{
KubeconfigPath: "",
Error: err,
}
return
}

for _, instance := range instances {
channel <- SearchResult{
channel <- storetypes.SearchResult{
KubeconfigPath: instance.Label,
Tags: map[string]string{
"clusterID": strconv.Itoa(instance.ID),
Expand Down
Loading

0 comments on commit c68bd31

Please sign in to comment.