-
Notifications
You must be signed in to change notification settings - Fork 33
/
fetcher.go
71 lines (61 loc) · 2.02 KB
/
fetcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package fetcher
import (
"context"
"time"
"github.com/bonnefoa/kubectl-fzf/v3/internal/k8s/clusterconfig"
"github.com/bonnefoa/kubectl-fzf/v3/internal/k8s/resources"
"github.com/bonnefoa/kubectl-fzf/v3/internal/util"
)
// Fetcher defines configuration to fetch completion datas
type Fetcher struct {
clusterconfig.ClusterConfig
fetcherCachePath string
httpEndpoint string
fzfNamespace string
minimumCache time.Duration
portForwardLocalPort int // Local port to use for port-forward
fetcherState FetcherState
}
func NewFetcher(fetchConfigCli *FetcherCli) *Fetcher {
f := Fetcher{
ClusterConfig: clusterconfig.NewClusterConfig(fetchConfigCli.ClusterConfigCli),
httpEndpoint: fetchConfigCli.HttpEndpoint,
fzfNamespace: fetchConfigCli.FzfNamespace,
fetcherCachePath: fetchConfigCli.FetcherCachePath,
minimumCache: fetchConfigCli.MinimumCache,
portForwardLocalPort: fetchConfigCli.PortForwardLocalPort,
fetcherState: *newFetcherState(fetchConfigCli.FetcherCachePath),
}
return &f
}
func (f *Fetcher) LoadFetcherState() error {
err := f.LoadClusterConfig()
if err != nil {
return err
}
return f.fetcherState.loadStateFromDisk()
}
func (f *Fetcher) SaveFetcherState() error {
return f.fetcherState.writeToDisk()
}
func loadResourceFromFile(filePath string) (map[string]resources.K8sResource, error) {
resources := map[string]resources.K8sResource{}
err := util.LoadGobFromFile(&resources, filePath)
return resources, err
}
func (f *Fetcher) GetResources(ctx context.Context, r resources.ResourceType) (map[string]resources.K8sResource, error) {
resources, err := f.checkLocalFiles(r)
if resources != nil || err != nil {
return resources, err
}
// Check for recent cache
resources, err = f.checkRecentCache(r)
if resources != nil || err != nil {
return resources, err
}
// Fetch remote
if util.IsAddressReachable(f.httpEndpoint) {
return f.loadResourceFromHttpServer(f.httpEndpoint, r)
}
return f.getResourcesFromPortForward(ctx, r)
}