forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
telemetry.go
145 lines (132 loc) · 3.33 KB
/
telemetry.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package telemetry
import (
"context"
"fmt"
"os"
"os/exec"
"sync"
"time"
"github.com/pkg/errors"
"github.com/rancher/rancher/pkg/settings"
"github.com/rancher/rancher/pkg/ticker"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/labels"
)
var adminRole = "Default Admin"
type process struct {
cmd *exec.Cmd
running bool
lock sync.RWMutex
}
func (p *process) kill() {
p.lock.Lock()
defer p.lock.Unlock()
if p.cmd.Process != nil {
p.cmd.Process.Kill()
}
}
func (p *process) getRunningState() bool {
p.lock.RLock()
defer p.lock.RUnlock()
return p.running
}
func (p *process) setRunningState(state bool) {
p.lock.Lock()
defer p.lock.Unlock()
p.running = state
}
func isLeader(management *config.ScaledContext) bool {
if management.PeerManager == nil {
return true
}
return management.PeerManager.IsLeader()
}
func Start(ctx context.Context, httpsPort int, management *config.ScaledContext) error {
p := process{
lock: sync.RWMutex{},
}
// have two go routines running. One is to run telemetry if setting is true, one is to kill telemetry if setting is false
go func() {
t := time.NewTicker(time.Second * 5)
go func() {
<-ctx.Done()
t.Stop()
}()
defer t.Stop()
for range t.C {
if settings.TelemetryOpt.Get() == "in" && isLeader(management) {
if !p.running {
var token string
var e error
for i := 0; i < 3; i++ {
token, e = createToken(management)
if e != nil {
logrus.Info(e)
time.Sleep(time.Second * 5)
continue
} else {
break
}
}
if token == "" {
logrus.Infof("Unable to obtain token for telemetry service. Telemetry will not be launched.")
return
}
cmd := exec.Command("telemetry", "client", "--url", fmt.Sprintf("https://localhost:%d/v3", httpsPort), "--token-key", token)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
p.cmd = cmd
if err := cmd.Start(); err != nil {
logrus.Error(err)
continue
}
p.setRunningState(true)
// in here we don't care the error, it will block until the telemetry process exited
cmd.Wait()
p.setRunningState(false)
}
}
}
}()
go func() {
for range ticker.Context(ctx, time.Second*5) {
if settings.TelemetryOpt.Get() != "in" || !isLeader(management) {
if p.getRunningState() {
p.kill()
p.setRunningState(false)
}
}
}
}()
go func() {
select {
case <-ctx.Done():
if p.getRunningState() {
p.kill()
p.setRunningState(false)
}
}
}()
return nil
}
func createToken(management *config.ScaledContext) (string, error) {
users, err := management.Management.Users("").Controller().Lister().List("", labels.SelectorFromSet(
map[string]string{
"authz.management.cattle.io/bootstrapping": "admin-user",
},
))
if err != nil {
return "", errors.Wrapf(err, "Can't list admin-user for telemetry. Err: %v. Retry after 5 seconds", err)
}
for _, user := range users {
if user.DisplayName == adminRole {
token, err := management.UserManager.EnsureToken("telemetry", "telemetry token", user.Name)
if err != nil {
return "", errors.Wrapf(err, "Can't create token for telemetry. Err: %v. Retry after 5 seconds", err)
}
return token, nil
}
}
return "", errors.Errorf("user %s doesn't exist for telemetry. Retry after 5 seconds", adminRole)
}