This repository has been archived by the owner on Jan 16, 2023. It is now read-only.
/
argus.go
230 lines (204 loc) · 6.25 KB
/
argus.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package argus
import (
"net/http"
"net/url"
"time"
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
"github.com/logicmonitor/k8s-argus/pkg/config"
"github.com/logicmonitor/k8s-argus/pkg/constants"
"github.com/logicmonitor/k8s-argus/pkg/device"
"github.com/logicmonitor/k8s-argus/pkg/devicegroup"
"github.com/logicmonitor/k8s-argus/pkg/etcd"
"github.com/logicmonitor/k8s-argus/pkg/sync"
"github.com/logicmonitor/k8s-argus/pkg/tree"
"github.com/logicmonitor/k8s-argus/pkg/types"
"github.com/logicmonitor/k8s-argus/pkg/watch/deployment"
"github.com/logicmonitor/k8s-argus/pkg/watch/namespace"
"github.com/logicmonitor/k8s-argus/pkg/watch/node"
"github.com/logicmonitor/k8s-argus/pkg/watch/pod"
"github.com/logicmonitor/k8s-argus/pkg/watch/service"
"github.com/logicmonitor/k8s-collectorset-controller/api"
"github.com/logicmonitor/lm-sdk-go/client"
"github.com/logicmonitor/lm-sdk-go/client/lm"
log "github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
)
// Argus represents the Argus cli.
type Argus struct {
*types.Base
Watchers []types.Watcher
}
func newLMClient(argusConfig *config.Config) (*client.LMSdkGo, error) {
config := client.NewConfig()
config.SetAccessID(&argusConfig.ID)
config.SetAccessKey(&argusConfig.Key)
domain := argusConfig.Account + ".logicmonitor.com"
config.SetAccountDomain(&domain)
//config.UserAgent = constants.UserAgentBase + constants.Version
if argusConfig.ProxyURL == "" {
return client.New(config), nil
}
return newLMClientWithProxy(config, argusConfig)
}
func newLMClientWithProxy(config *client.Config, argusConfig *config.Config) (*client.LMSdkGo, error) {
proxyURL, err := url.Parse(argusConfig.ProxyURL)
if err != nil {
return nil, err
}
if argusConfig.ProxyUser != "" {
if argusConfig.ProxyPass != "" {
proxyURL.User = url.UserPassword(argusConfig.ProxyUser, argusConfig.ProxyPass)
} else {
proxyURL.User = url.User(argusConfig.ProxyUser)
}
}
log.Infof("Using http/s proxy: %s", argusConfig.ProxyURL)
httpClient := http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxyURL),
},
}
transport := httptransport.NewWithClient(config.TransportCfg.Host, config.TransportCfg.BasePath, config.TransportCfg.Schemes, &httpClient)
authInfo := client.LMv1Auth(*config.AccessID, *config.AccessKey)
client := new(client.LMSdkGo)
client.Transport = transport
client.LM = lm.New(transport, strfmt.Default, authInfo)
return client, nil
}
func newK8sClient() (*kubernetes.Clientset, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
config.UserAgent = constants.UserAgentBase + constants.Version
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
return clientset, nil
}
// NewArgus instantiates and returns argus.
func NewArgus(base *types.Base, client api.CollectorSetControllerClient) (*Argus, error) {
argus := &Argus{
Base: base,
}
deviceManager := &device.Manager{
Base: base,
ControllerClient: client,
}
deviceTree := &tree.DeviceTree{
Base: base,
}
// init sync to delete the non-exist resource devices through logicmonitor API
initSyncer := sync.InitSyncer{
DeviceManager: deviceManager,
}
initSyncer.InitSync()
deviceGroups, err := deviceTree.CreateDeviceTree()
if err != nil {
return nil, err
}
if base.Config.EtcdDiscoveryToken != "" {
etcdController := etcd.Controller{
DeviceManager: deviceManager,
}
_, err = etcdController.DiscoverByToken()
if err != nil {
return nil, err
}
}
argus.Watchers = []types.Watcher{
&namespace.Watcher{
Base: base,
DeviceGroups: deviceGroups,
},
&node.Watcher{
DeviceManager: deviceManager,
DeviceGroups: deviceGroups,
LMClient: base.LMClient,
},
&service.Watcher{
DeviceManager: deviceManager,
},
&pod.Watcher{
DeviceManager: deviceManager,
},
&deployment.Watcher{
DeviceManager: deviceManager,
},
}
return argus, nil
}
// NewBase instantiates and returns the base structure used throughout Argus.
func NewBase(config *config.Config) (*types.Base, error) {
// LogicMonitor API client.
lmClient, err := newLMClient(config)
if err != nil {
return nil, err
}
// check and update the params
checkAndUpdateClusterGroup(config, lmClient)
// Kubernetes API client.
k8sClient, err := newK8sClient()
if err != nil {
return nil, err
}
base := &types.Base{
LMClient: lmClient,
K8sClient: k8sClient,
Config: config,
}
return base, nil
}
// Watch watches the API for events.
func (a *Argus) Watch() {
for _, w := range a.Watchers {
if !w.Enabled() {
log.Warnf("Have no permission for resource %s", w.Resource())
continue
}
watchlist := cache.NewListWatchFromClient(getK8sRESTClient(a.K8sClient, w.APIVersion()), w.Resource(), v1.NamespaceAll, fields.Everything())
_, controller := cache.NewInformer(
watchlist,
w.ObjType(),
time.Minute*10,
cache.ResourceEventHandlerFuncs{
AddFunc: w.AddFunc(),
DeleteFunc: w.DeleteFunc(),
UpdateFunc: w.UpdateFunc(),
},
)
stop := make(chan struct{})
go controller.Run(stop)
}
}
// get the K8s RESTClient by apiVersion, use the default V1 version if there is no match
func getK8sRESTClient(clientset *kubernetes.Clientset, apiVersion string) rest.Interface {
switch apiVersion {
case constants.K8sAPIVersionV1:
return clientset.CoreV1().RESTClient()
case constants.K8sAPIVersionAppsV1beta2:
return clientset.AppsV1beta2().RESTClient()
case constants.K8sAPIVersionAppsV1:
return clientset.AppsV1().RESTClient()
default:
return clientset.CoreV1().RESTClient()
}
}
// check the cluster group ID, if the group does not exist, just use the root group
func checkAndUpdateClusterGroup(config *config.Config, lmClient *client.LMSdkGo) {
// do not need to check the root group
if config.ClusterGroupID == constants.RootDeviceGroupID {
return
}
// if the group does not exist anymore, we will add the cluster to the root group
if !devicegroup.ExistsByID(config.ClusterGroupID, lmClient) {
log.Warnf("The device group (id=%v) does not exist, the cluster will be added to the root group", config.ClusterGroupID)
config.ClusterGroupID = constants.RootDeviceGroupID
}
}