-
Notifications
You must be signed in to change notification settings - Fork 187
/
sources.go
230 lines (202 loc) · 6.46 KB
/
sources.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 endpoints
import (
"github.com/gin-gonic/gin"
"github.com/keyval-dev/odigos/api/odigos/v1alpha1"
"github.com/keyval-dev/odigos/common/consts"
"github.com/keyval-dev/odigos/common/utils"
"github.com/keyval-dev/odigos/frontend/kube"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type SourceLanguage struct {
ContainerName string `json:"container_name"`
Language string `json:"language"`
}
// this object contains only part of the source fields. It is used to display the sources in the frontend
type ThinSource struct {
// combination of namespace, kind and name is unique
Name string `json:"name"`
Kind string `json:"kind"`
Namespace string `json:"namespace"`
Languages []SourceLanguage `json:"languages"`
}
type Source struct {
ThinSource
ReportedName string `json:"reported_name,omitempty"`
}
type PatchSourceRequest struct {
ReportedName *string `json:"reported_name"`
}
func GetSources(c *gin.Context) {
instrumentedApplications, err := kube.DefaultClient.OdigosClient.InstrumentedApplications("").List(c, metav1.ListOptions{})
if err != nil {
returnError(c, err)
return
}
sources := []ThinSource{}
for _, app := range instrumentedApplications.Items {
sources = append(sources, k8sInstrumentedAppToThinSource(&app))
}
c.JSON(200, sources)
}
func GetSource(c *gin.Context) {
ns := c.Param("namespace")
kind := c.Param("kind")
name := c.Param("name")
k8sObjectName := utils.GetRuntimeObjectName(name, kind)
instrumentedApplication, err := kube.DefaultClient.OdigosClient.InstrumentedApplications(ns).Get(c, k8sObjectName, metav1.GetOptions{})
if err != nil {
returnError(c, err)
return
}
owner := getK8sObject(c, ns, kind, name)
if owner == nil {
c.JSON(500, gin.H{
"message": "could not find owner of instrumented application",
})
return
}
ownerAnnotations := owner.GetAnnotations()
var reportedName string
if ownerAnnotations != nil {
reportedName = ownerAnnotations[consts.OdigosReportedNameAnnotation]
}
c.JSON(200, Source{
ThinSource: k8sInstrumentedAppToThinSource(instrumentedApplication),
ReportedName: reportedName,
})
}
func PatchSource(c *gin.Context) {
ns := c.Param("namespace")
kind := c.Param("kind")
name := c.Param("name")
request := PatchSourceRequest{}
if err := c.ShouldBindJSON(&request); err != nil {
returnError(c, err)
return
}
if request.ReportedName != nil {
newReportedName := *request.ReportedName
switch kind {
case "Deployment":
deployment, err := kube.DefaultClient.AppsV1().Deployments(ns).Get(c, name, metav1.GetOptions{})
if err != nil {
c.JSON(404, gin.H{"error": "could not find a deployment with the given name in the given namespace"})
return
}
deployment.SetAnnotations(updateReportedName(deployment.GetAnnotations(), newReportedName))
_, err = kube.DefaultClient.AppsV1().Deployments(ns).Update(c, deployment, metav1.UpdateOptions{})
if err != nil {
returnError(c, err)
return
}
case "StatefulSet":
statefulset, err := kube.DefaultClient.AppsV1().StatefulSets(ns).Get(c, name, metav1.GetOptions{})
if err != nil {
c.JSON(404, gin.H{"error": "could not find a statefulset with the given name in the given namespace"})
return
}
statefulset.SetAnnotations(updateReportedName(statefulset.GetAnnotations(), newReportedName))
_, err = kube.DefaultClient.AppsV1().StatefulSets(ns).Update(c, statefulset, metav1.UpdateOptions{})
if err != nil {
returnError(c, err)
return
}
case "DaemonSet":
daemonset, err := kube.DefaultClient.AppsV1().DaemonSets(ns).Get(c, name, metav1.GetOptions{})
if err != nil {
c.JSON(404, gin.H{"error": "could not find a daemonset with the given name in the given namespace"})
return
}
daemonset.SetAnnotations(updateReportedName(daemonset.GetAnnotations(), newReportedName))
_, err = kube.DefaultClient.AppsV1().DaemonSets(ns).Update(c, daemonset, metav1.UpdateOptions{})
if err != nil {
returnError(c, err)
return
}
default:
c.JSON(400, gin.H{"error": "kind must be one of Deployment, StatefulSet or DaemonSet"})
return
}
}
c.Status(200)
}
func DeleteSource(c *gin.Context) {
// to delete a source, we need to set it's instrumentation label to disable
// afterwards, odigos will detect the change and remove the instrumented application object from k8s
ns := c.Param("namespace")
kind := c.Param("kind")
name := c.Param("name")
var kindAsEnum WorkloadKind
switch kind {
case "Deployment":
kindAsEnum = WorkloadKindDeployment
case "StatefulSet":
kindAsEnum = WorkloadKindStatefulSet
case "DaemonSet":
kindAsEnum = WorkloadKindDaemonSet
default:
c.JSON(400, gin.H{"error": "kind must be one of Deployment, StatefulSet or DaemonSet"})
return
}
instrumented := false
err := setWorkloadInstrumentationLabel(c, ns, name, kindAsEnum, &instrumented)
if err != nil {
returnError(c, err)
return
}
c.JSON(200, gin.H{"message": "ok"})
}
func k8sInstrumentedAppToThinSource(app *v1alpha1.InstrumentedApplication) ThinSource {
var source ThinSource
source.Name = app.OwnerReferences[0].Name
source.Kind = app.OwnerReferences[0].Kind
source.Namespace = app.Namespace
for _, language := range app.Spec.RuntimeDetails {
source.Languages = append(source.Languages, SourceLanguage{
ContainerName: language.ContainerName,
Language: string(language.Language),
})
}
return source
}
func getK8sObject(c *gin.Context, ns string, kind string, name string) metav1.Object {
switch kind {
case "Deployment":
deployment, err := kube.DefaultClient.AppsV1().Deployments(ns).Get(c, name, metav1.GetOptions{})
if err != nil {
return nil
}
return deployment
case "StatefulSet":
statefulSet, err := kube.DefaultClient.AppsV1().StatefulSets(ns).Get(c, name, metav1.GetOptions{})
if err != nil {
return nil
}
return statefulSet
case "DaemonSet":
daemonSet, err := kube.DefaultClient.AppsV1().DaemonSets(ns).Get(c, name, metav1.GetOptions{})
if err != nil {
return nil
}
return daemonSet
default:
return nil
}
}
func updateReportedName(annotations map[string]string, reportedName string) map[string]string {
if reportedName == "" {
// delete the reported name if it is empty, so we pick up the name from the k8s object
if annotations == nil {
return nil
} else {
delete(annotations, consts.OdigosReportedNameAnnotation)
return annotations
}
} else {
if annotations == nil {
annotations = make(map[string]string)
}
annotations[consts.OdigosReportedNameAnnotation] = reportedName
return annotations
}
}