-
Notifications
You must be signed in to change notification settings - Fork 24
/
discovery.go
77 lines (62 loc) · 2 KB
/
discovery.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
72
73
74
75
76
77
package k8s
import (
"context"
"fmt"
"github.com/jetstack/preflight/pkg/datagatherer"
"k8s.io/client-go/discovery"
)
// ConfigDiscovery contains the configuration for the k8s-discovery data-gatherer
type ConfigDiscovery struct {
// KubeConfigPath is the path to the kubeconfig file. If empty, will assume it runs in-cluster.
KubeConfigPath string `yaml:"kubeconfig"`
}
// UnmarshalYAML unmarshals the Config resolving GroupVersionResource.
func (c *ConfigDiscovery) UnmarshalYAML(unmarshal func(interface{}) error) error {
aux := struct {
KubeConfigPath string `yaml:"kubeconfig"`
}{}
err := unmarshal(&aux)
if err != nil {
return err
}
c.KubeConfigPath = aux.KubeConfigPath
return nil
}
// NewDataGatherer constructs a new instance of the generic K8s data-gatherer for the provided
// GroupVersionResource.
func (c *ConfigDiscovery) NewDataGatherer(ctx context.Context) (datagatherer.DataGatherer, error) {
cl, err := NewDiscoveryClient(c.KubeConfigPath)
if err != nil {
return nil, err
}
return &DataGathererDiscovery{cl: cl}, nil
}
// DataGathererDiscovery stores the config for a k8s-discovery datagatherer
type DataGathererDiscovery struct {
// The 'discovery' client used for fetching data.
cl discovery.DiscoveryClient
}
func (g *DataGathererDiscovery) Run(stopCh <-chan struct{}) error {
// no async functionality, see Fetch
return nil
}
func (g *DataGathererDiscovery) WaitForCacheSync(stopCh <-chan struct{}) error {
// no async functionality, see Fetch
return nil
}
func (g *DataGathererDiscovery) Delete() error {
// no async functionality, see Fetch
return nil
}
// Fetch will fetch discovery data from the apiserver, or return an error
func (g *DataGathererDiscovery) Fetch() (interface{}, error) {
data, err := g.cl.ServerVersion()
if err != nil {
return nil, fmt.Errorf("failed to get server version: %v", err)
}
response := map[string]interface{}{
// data has type Info: https://godoc.org/k8s.io/apimachinery/pkg/version#Info
"server_version": data,
}
return response, nil
}