-
Notifications
You must be signed in to change notification settings - Fork 345
/
component.go
98 lines (85 loc) · 2.1 KB
/
component.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package telemetry
import (
"time"
config "github.com/k0sproject/k0s/pkg/apis/v1beta1"
"github.com/k0sproject/k0s/pkg/constant"
kubeutil "github.com/k0sproject/k0s/pkg/kubernetes"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
)
// Component is a telemetry component for k0s component manager
type Component struct {
ClusterConfig *config.ClusterConfig
K0sVars constant.CfgVars
Version string
KubeClientFactory kubeutil.ClientFactory
kubernetesClient kubernetes.Interface
analyticsClient analyticsClient
log *logrus.Entry
stopCh chan struct{}
interval time.Duration
}
// Init set up for external service clients (segment, k8s api)
func (c *Component) Init() error {
c.log = logrus.WithField("component", "telemetry")
if segmentToken == "" {
c.log.Info("no token, telemetry is disabled")
return nil
}
c.interval = c.ClusterConfig.Spec.Telemetry.Interval
c.stopCh = make(chan struct{})
c.log.Info("kube client has been init")
c.analyticsClient = newSegmentClient(segmentToken)
c.log.Info("segment client has been init")
return nil
}
func (c *Component) retrieveKubeClient(ch chan struct{}) {
client, err := c.KubeClientFactory.GetClient()
if err != nil {
c.log.WithError(err).Warning("can't init kube client")
return
}
c.kubernetesClient = client
close(ch)
}
// Run runs work cycle
func (c *Component) Run() error {
if segmentToken == "" {
c.log.Info("no token, telemetry is disabled")
return nil
}
initedCh := make(chan struct{})
wait.Until(func() {
c.retrieveKubeClient(initedCh)
}, time.Second, initedCh)
go c.run()
return nil
}
// Run does nothing
func (c *Component) Stop() error {
if segmentToken == "" {
c.log.Info("no token, telemetry is disabled")
return nil
}
close(c.stopCh)
if c.analyticsClient != nil {
_ = c.analyticsClient.Close()
}
return nil
}
// Healthy checks health
func (c *Component) Healthy() error {
return nil
}
func (c Component) run() {
ticker := time.NewTicker(c.interval)
for {
select {
case <-ticker.C:
c.sendTelemetry()
case <-c.stopCh:
return
}
}
}