-
Notifications
You must be signed in to change notification settings - Fork 474
/
workloads.go
150 lines (123 loc) · 3.99 KB
/
workloads.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
package handlers
import (
"io/ioutil"
"net/http"
"github.com/gorilla/mux"
"github.com/kiali/kiali/business"
)
// WorkloadList is the API handler to fetch all the workloads to be displayed, related to a single namespace
func WorkloadList(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
namespace := params["namespace"]
criteria := business.WorkloadCriteria{Namespace: namespace, IncludeIstioResources: true}
// Get business layer
business, err := getBusiness(r)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, "Workloads initialization error: "+err.Error())
return
}
// Fetch and build workloads
workloadList, err := business.Workload.GetWorkloadList(criteria)
if err != nil {
handleErrorResponse(w, err)
return
}
RespondWithJSON(w, http.StatusOK, workloadList)
}
// WorkloadDetails is the API handler to fetch all details to be displayed, related to a single workload
func WorkloadDetails(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
query := r.URL.Query()
// Get business layer
business, err := getBusiness(r)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, "Workloads initialization error: "+err.Error())
return
}
namespace := params["namespace"]
workload := params["workload"]
workloadType := query.Get("type")
// Fetch and build workload
workloadDetails, err := business.Workload.GetWorkload(namespace, workload, workloadType, true)
if err != nil {
handleErrorResponse(w, err)
return
}
RespondWithJSON(w, http.StatusOK, workloadDetails)
}
// WorkloadUpdate is the API to perform a patch on a Workload configuration
func WorkloadUpdate(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
query := r.URL.Query()
// Get business layer
business, err := getBusiness(r)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, "Workloads initialization error: "+err.Error())
return
}
namespace := params["namespace"]
workload := params["workload"]
workloadType := query.Get("type")
body, err := ioutil.ReadAll(r.Body)
if err != nil {
RespondWithError(w, http.StatusBadRequest, "Update request with bad update patch: "+err.Error())
}
jsonPatch := string(body)
workloadDetails, err := business.Workload.UpdateWorkload(namespace, workload, workloadType, true, jsonPatch)
if err != nil {
handleErrorResponse(w, err)
return
}
audit(r, "UPDATE on Namespace: "+namespace+" Workload name: "+workload+" Type: "+workloadType+" Patch: "+jsonPatch)
RespondWithJSON(w, http.StatusOK, workloadDetails)
}
// PodDetails is the API handler to fetch all details to be displayed, related to a single pod
func PodDetails(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
// Get business layer
business, err := getBusiness(r)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, "Pods initialization error: "+err.Error())
return
}
namespace := vars["namespace"]
pod := vars["pod"]
// Fetch and build pod
podDetails, err := business.Workload.GetPod(namespace, pod)
if err != nil {
handleErrorResponse(w, err)
return
}
RespondWithJSON(w, http.StatusOK, podDetails)
}
// PodLogs is the API handler to fetch logs for a single pod container
func PodLogs(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
queryParams := r.URL.Query()
// Get business layer
business, err := getBusiness(r)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, "Pod Logs initialization error: "+err.Error())
return
}
namespace := vars["namespace"]
pod := vars["pod"]
// Get log options
opts, err := business.Workload.BuildLogOptionsCriteria(
queryParams.Get("container"),
queryParams.Get("duration"),
queryParams.Get("isProxy"),
queryParams.Get("sinceTime"),
queryParams.Get("tailLines"))
if err != nil {
handleErrorResponse(w, err)
return
}
// Fetch pod logs
podLogs, err := business.Workload.GetPodLogs(namespace, pod, opts)
if err != nil {
handleErrorResponse(w, err)
return
}
RespondWithJSON(w, http.StatusOK, podLogs)
}