-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
types.go
251 lines (236 loc) · 6.38 KB
/
types.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package orchestrator
import (
"strings"
"github.com/patrickmn/go-cache"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
// NodeType represents a kind of resource used by a container orchestrator.
type NodeType int
// CheckName is the cluster check name of the orchestrator check
const CheckName = "orchestrator"
// ExtraLogContext is used to add check name into log context
var ExtraLogContext = []interface{}{"check", CheckName}
// NoExpiration maps to go-cache corresponding value
const NoExpiration = cache.NoExpiration
// The values in these enfms should match the values defined in the agent payload schema, defined here:
// https://github.com/DataDog/agent-payload/blob/master/proto/process/agent.proto (within enum K8sResource)
// we do not utilize iota as these types are used in external systems, not just within the agent instance.
const (
// K8sUnsetType represents a Kubernetes unset type
K8sUnsetType NodeType = 0
// K8sPod represents a Kubernetes Pod
K8sPod = 1
// K8sReplicaSet represents a Kubernetes ReplicaSet
K8sReplicaSet = 2
// K8sService represents a Kubernetes Service
K8sService = 3
// K8sNode represents a Kubernetes Node
K8sNode = 4
// K8sCluster represents a Kubernetes Cluster
K8sCluster = 5
// K8sJob represents a Kubernetes Job
K8sJob = 6
// K8sCronJob represents a Kubernetes CronJob
K8sCronJob = 7
// K8sDaemonSet represents a Kubernetes DaemonSet
K8sDaemonSet = 8
// K8sStatefulSet represents a Kubernetes StatefulSet
K8sStatefulSet = 9
// K8sPersistentVolume represents a Kubernetes PersistentVolume
K8sPersistentVolume = 10
// K8sPersistentVolumeClaim represents a Kubernetes PersistentVolumeClaim
K8sPersistentVolumeClaim = 11
// K8sRole represents a Kubernetes Role
K8sRole = 12
// K8sRoleBinding represents a Kubernetes RoleBinding
K8sRoleBinding = 13
// K8sClusterRole represents a Kubernetes ClusterRole
K8sClusterRole = 14
// K8sClusterRoleBinding represents a Kubernetes ClusterRoleBinding
K8sClusterRoleBinding = 15
// K8sServiceAccount represents a Kubernetes ServiceAccount
K8sServiceAccount = 16
// K8sIngress represents a Kubernetes Ingress
K8sIngress = 17
// K8sDeployment represents a Kubernetes Deployment
K8sDeployment = 18
// K8sNamespace represents a Kubernetes Namespace
K8sNamespace = 19
// K8sCRD represents a Kubernetes CRD
K8sCRD = 20
// K8sCR represents a Kubernetes CR
K8sCR = 21
// K8sVerticalPodAutoscaler represents a Kubernetes VerticalPod Autoscaler
K8sVerticalPodAutoscaler = 22
// K8sHorizontalPodAutoscaler represents a Kubernetes Horizontal Pod Autoscaler
K8sHorizontalPodAutoscaler = 23
// K8sNetworkPolicy represents a Kubernetes NetworkPolicy
K8sNetworkPolicy = 24
// K8sLimitRange represents a Kubernetes LimitRange
K8sLimitRange = 25
// K8sStorageClass represents a Kubernetes StorageClass
K8sStorageClass = 26
// ECSTask represents an ECS Task
ECSTask = 150
)
// NodeTypes returns the current existing NodesTypes as a slice to iterate over.
func NodeTypes() []NodeType {
return []NodeType{
K8sCluster,
K8sCronJob,
K8sDeployment,
K8sDaemonSet,
K8sJob,
K8sNode,
K8sPod,
K8sReplicaSet,
K8sService,
K8sStatefulSet,
K8sPersistentVolumeClaim,
K8sPersistentVolume,
K8sRole,
K8sRoleBinding,
K8sClusterRole,
K8sClusterRoleBinding,
K8sServiceAccount,
K8sIngress,
K8sNamespace,
K8sCR,
K8sCRD,
K8sVerticalPodAutoscaler,
K8sHorizontalPodAutoscaler,
K8sNetworkPolicy,
K8sLimitRange,
K8sStorageClass,
ECSTask,
}
}
func (n NodeType) String() string {
switch n {
case K8sCluster:
return "Cluster"
case K8sCronJob:
return "CronJob"
case K8sDeployment:
return "Deployment"
case K8sDaemonSet:
return "DaemonSet"
case K8sJob:
return "Job"
case K8sNode:
return "Node"
case K8sPod:
return "Pod"
case K8sReplicaSet:
return "ReplicaSet"
case K8sService:
return "Service"
case K8sStatefulSet:
return "StatefulSet"
case K8sPersistentVolume:
return "PersistentVolume"
case K8sPersistentVolumeClaim:
return "PersistentVolumeClaim"
case K8sRole:
return "Role"
case K8sRoleBinding:
return "RoleBinding"
case K8sClusterRole:
return "ClusterRole"
case K8sClusterRoleBinding:
return "ClusterRoleBinding"
case K8sServiceAccount:
return "ServiceAccount"
case K8sIngress:
return "Ingress"
case K8sNamespace:
return "Namespace"
case K8sCRD:
return "CustomResourceDefinition"
case K8sCR:
return "CustomResource"
case K8sVerticalPodAutoscaler:
return "VerticalPodAutoscaler"
case K8sHorizontalPodAutoscaler:
return "HorizontalPodAutoscaler"
case K8sNetworkPolicy:
return "NetworkPolicy"
case K8sLimitRange:
return "LimitRange"
case K8sStorageClass:
return "StorageClass"
case K8sUnsetType:
return "UnsetType"
case ECSTask:
return "ECSTask"
default:
_ = log.Errorf("Trying to convert unknown NodeType iota: %d", n)
return "Unknown"
}
}
// Orchestrator returns the orchestrator name for a node type.
func (n NodeType) Orchestrator() string {
switch n {
case K8sCluster,
K8sCronJob,
K8sDeployment,
K8sDaemonSet,
K8sJob,
K8sNode,
K8sPod,
K8sReplicaSet,
K8sService,
K8sStatefulSet,
K8sPersistentVolume,
K8sPersistentVolumeClaim,
K8sRole,
K8sRoleBinding,
K8sClusterRole,
K8sClusterRoleBinding,
K8sServiceAccount,
K8sIngress,
K8sCRD,
K8sCR,
K8sNamespace,
K8sVerticalPodAutoscaler,
K8sHorizontalPodAutoscaler,
K8sNetworkPolicy,
K8sLimitRange,
K8sStorageClass,
K8sUnsetType:
return "k8s"
case ECSTask:
return "ecs"
default:
log.Errorf("Unknown NodeType %v", n)
return ""
}
}
// TelemetryTags return tags used for telemetry.
func (n NodeType) TelemetryTags() []string {
if n.String() == "" {
log.Errorf("Unknown NodeType %v", n)
return []string{"unknown", "unknown"}
}
tags := getTelemetryTags(n)
return tags
}
func getTelemetryTags(n NodeType) []string {
return []string{
n.Orchestrator(),
strings.ToLower(n.String()),
}
}
// SetCacheStats sets the cache stats for each resource
func SetCacheStats(resourceListLen int, resourceMsgLen int, nodeType NodeType, ca *cache.Cache) {
stats := CheckStats{
CacheHits: resourceListLen - resourceMsgLen,
CacheMiss: resourceMsgLen,
NodeType: nodeType,
}
ca.Set(BuildStatsKey(nodeType), stats, NoExpiration)
}