/
application.go
182 lines (166 loc) Β· 3.91 KB
/
application.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
package model
import (
"fmt"
"github.com/coroot/coroot/timeseries"
"github.com/coroot/coroot/utils"
"strconv"
"strings"
)
type Application struct {
Id ApplicationId
Instances []*Instance
DesiredInstances timeseries.TimeSeries
Status Status
Reports []*AuditReport
LatencySLIs []*LatencySLI
AvailabilitySLIs []*AvailabilitySLI
}
func NewApplication(id ApplicationId) *Application {
app := &Application{Id: id}
return app
}
func (app *Application) GetInstance(name string) *Instance {
for _, i := range app.Instances {
if i.Name == name {
return i
}
}
return nil
}
func (app *Application) GetOrCreateInstance(name string) *Instance {
instance := app.GetInstance(name)
if instance == nil {
instance = NewInstance(name, app.Id)
app.Instances = append(app.Instances, instance)
}
return instance
}
func (app *Application) Labels() Labels {
res := Labels{}
switch app.Id.Kind {
case ApplicationKindRds:
res["db"] = fmt.Sprintf(`%s (RDS)`, app.Instances[0].Rds.Engine.Value())
case ApplicationKindUnknown:
res["instances"] = strconv.Itoa(len(app.Instances))
case ApplicationKindExternalService:
eps := utils.NewStringSet()
for _, instance := range app.Instances {
for listen := range instance.TcpListens {
eps.Add(listen.IP)
}
}
if eps.Len() > 0 {
name := "external endpoint"
if eps.Len() > 1 {
name += "s"
}
res[name] = strings.Join(eps.Items(), ", ")
}
default:
res["ns"] = app.Id.Namespace
}
for _, i := range app.Instances {
for _, c := range i.Containers {
for t := range c.ApplicationTypes {
if t.IsDatabase() {
res["db"] = string(t)
}
if t.IsQueue() {
res["queue"] = string(t)
}
}
}
}
return res
}
func (app *Application) IsControlPlane() bool {
id := app.Id
if id.Kind == ApplicationKindExternalService && id.Name == "kube-apiserver" {
return true
}
if id.Namespace == "kube-system" || id.Name == "kubelet" {
return true
}
for _, i := range app.Instances {
if i.ApplicationTypes()["k3s"] {
return true
}
if !i.ApplicationTypes()["etcd"] {
continue
}
for _, d := range i.Downstreams {
if d.Instance != nil || d.Instance.ApplicationTypes()["kube-apiserver"] {
return true
}
}
}
if strings.Contains(id.Namespace, "chaos") {
return utils.NewStringSet("chaos-controller-manager", "chaos-daemon", "chaos-dashboard").Has(id.Name)
}
if id.Name == "postgres-operator" {
return true
}
return false
}
func (app *Application) IsMonitoring() bool {
id := app.Id
for _, n := range []string{"monitoring", "prometheus", "grafana", "alertmanager", "coroot"} {
if strings.Contains(id.Namespace, n) || strings.Contains(id.Name, n) {
return true
}
}
return false
}
func (app *Application) IsRedis() bool {
for _, i := range app.Instances {
if i.Redis != nil {
return true
}
}
return false
}
func (app *Application) IsPostgres() bool {
for _, i := range app.Instances {
if i.Postgres != nil {
return true
}
}
return false
}
func (app *Application) IsStandalone() bool {
for _, i := range app.Instances {
for _, u := range i.Upstreams {
if u.RemoteInstance != nil && u.RemoteInstance.OwnerId != app.Id && !u.Obsolete() {
return false
}
}
for _, d := range i.Downstreams {
if d.Instance != nil && d.Instance.OwnerId != app.Id && !d.Obsolete() {
return false
}
}
}
return true
}
func (app *Application) InstrumentationStatus() map[ApplicationType]bool {
res := map[ApplicationType]bool{}
for _, i := range app.Instances {
if i.Pod != nil && i.Pod.IsObsolete() {
continue
}
for t := range i.ApplicationTypes() {
var instanceInstrumented bool
switch t {
case ApplicationTypePostgres:
instanceInstrumented = i.Postgres != nil
case ApplicationTypeRedis:
instanceInstrumented = i.Redis != nil
default:
continue
}
appInstrumented, visited := res[t]
res[t] = (appInstrumented || !visited) && instanceInstrumented
}
}
return res
}