-
Notifications
You must be signed in to change notification settings - Fork 589
/
reports.go
114 lines (98 loc) · 3.88 KB
/
reports.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
package telemetry
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
"k8s.io/client-go/rest"
"github.com/kong/kubernetes-ingress-controller/v2/internal/adminapi"
"github.com/kong/kubernetes-ingress-controller/v2/internal/manager/metadata"
)
// GatewayClientsProvider is an interface that provides clients for the currently discovered Gateway instances.
type GatewayClientsProvider interface {
GatewayClients() []*adminapi.Client
GatewayClientsCount() int
}
type InstanceIDProvider interface {
GetID() uuid.UUID
}
const (
splunkEndpoint = "kong-hf.konghq.com:61833"
splunkEndpointInsecureSkipVerify = false
telemetryPeriod = time.Hour
)
type ReportConfig struct {
SplunkEndpoint string
SplunkEndpointInsecureSkipVerify bool
TelemetryPeriod time.Duration
ReportValues ReportValues
}
// SetupAnonymousReports sets up and starts the anonymous reporting and returns
// a cleanup function and an error.
// The caller is responsible to call the returned function - when the returned
// error is not nil - to stop the reports sending.
func SetupAnonymousReports(
ctx context.Context,
kubeCfg *rest.Config,
clientsProvider GatewayClientsProvider,
reportCfg ReportConfig,
instanceIDProvider InstanceIDProvider,
) (func(), error) {
// if anonymous reports are enabled this helps provide Kong with insights about usage of the ingress controller
// which is non-sensitive and predominantly informs us of the controller and cluster versions in use.
// This data helps inform us what versions, features, e.t.c. end-users are actively using which helps to inform
// our prioritization of work and we appreciate when our end-users provide them, however if you do feel
// uncomfortable and would rather turn them off run the controller with the "--anonymous-reports false" flag.
// This now only uses the first instance for telemetry reporting.
// That's fine because we allow for now only 1 set of version and db setting
// throughout all Kong instances that 1 KIC instance configures.
//
// When we change that and decide to allow heterogeneous Kong instances to be
// configured by 1 KIC instance then this will have to change.
//
// https://github.com/Kong/kubernetes-ingress-controller/issues/3589
root, err := clientsProvider.GatewayClients()[0].AdminAPIClient().Root(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get Kong root config data: %w", err)
}
// gather versioning information from the kong client
kongVersion, ok := root["version"].(string)
if !ok {
return nil, fmt.Errorf("malformed Kong version found in Kong client root")
}
cfg, ok := root["configuration"].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("malformed Kong configuration found in Kong client root")
}
kongDB, ok := cfg["database"].(string)
if !ok {
return nil, fmt.Errorf("malformed database configuration found in Kong client root")
}
fixedPayload := Payload{
"v": metadata.Release,
"kv": kongVersion,
"db": kongDB,
"id": instanceIDProvider.GetID(), // universal unique identifier for this system
}
// Use defaults when not specified.
if reportCfg.SplunkEndpoint == "" {
reportCfg.SplunkEndpoint = splunkEndpoint
}
if !reportCfg.SplunkEndpointInsecureSkipVerify {
reportCfg.SplunkEndpointInsecureSkipVerify = splunkEndpointInsecureSkipVerify
}
if reportCfg.TelemetryPeriod == 0 {
reportCfg.TelemetryPeriod = telemetryPeriod
}
tMgr, err := CreateManager(kubeCfg, clientsProvider, fixedPayload, reportCfg)
if err != nil {
return nil, fmt.Errorf("failed to create anonymous reports manager: %w", err)
}
if err := tMgr.Start(); err != nil {
return nil, fmt.Errorf("anonymous reports failed to start: %w", err)
}
if err := tMgr.TriggerExecute(ctx, SignalStart); err != nil {
return tMgr.Stop, fmt.Errorf("failed to trigger telemetry report during start: %w", err)
}
return tMgr.Stop, nil
}