@@ -2,13 +2,18 @@ package util
2
2
3
3
import (
4
4
"encoding/base64"
5
+ "encoding/json"
5
6
"fmt"
7
+ "log"
6
8
"net/http"
9
+ "net/url"
7
10
"strings"
8
11
"sync"
9
12
"time"
10
13
11
14
"github.com/gorilla/websocket"
15
+ "github.com/tidwall/gjson"
16
+ "golang.org/x/exp/slices"
12
17
)
13
18
14
19
var HeaderBlacklist = []string {"Cookie" , "X-CSRFToken" }
@@ -54,7 +59,7 @@ func DecodeSubprotocol(encodedProtocol string) (string, error) {
54
59
return string (decodedProtocol ), err
55
60
}
56
61
57
- func CopyMsgs (writeMutex * sync.Mutex , dest , src * websocket.Conn ) error {
62
+ func CopyMsgs (writeMutex * sync.Mutex , dest * websocket. Conn , src * websocket.Conn ) error {
58
63
for {
59
64
messageType , msg , err := src .ReadMessage ()
60
65
if err != nil {
@@ -155,3 +160,99 @@ func CreateProxyHeaders(w http.ResponseWriter, r *http.Request) (http.Header, st
155
160
156
161
return proxiedHeader , subProtocol , nil
157
162
}
163
+
164
+ func labelsIncludes (labels map [string ]interface {}, label string ) bool {
165
+ splitLabel := strings .Split (label , "=" )
166
+ return labels [splitLabel [0 ]] == splitLabel [1 ]
167
+ }
168
+
169
+ func isMigratable (statuses []interface {}, search string ) bool {
170
+ indexOfItem := slices .IndexFunc (statuses , func (status interface {}) bool {
171
+ return status .(map [string ]interface {})["type" ] == "LiveMigratable" && status .(map [string ]interface {})["status" ] == "True"
172
+ })
173
+ isMigrate := indexOfItem != - 1
174
+ if search == "notMigratable" && isMigrate {
175
+ return false
176
+ }
177
+
178
+ if search == "migratable" && ! isMigrate {
179
+ return false
180
+ }
181
+
182
+ return true
183
+ }
184
+
185
+ func FilterResponseQuery (bodyBytes []byte , query url.Values ) map [string ]interface {} {
186
+ items := gjson .ParseBytes (bodyBytes ).Get ("items" ).Array ()
187
+ filteredJson := []interface {}{}
188
+ isFilters := len (query ) != 0
189
+ if isFilters {
190
+ nextItem:
191
+ for _ , item := range items {
192
+ for key , val := range query {
193
+ for _ , match := range val {
194
+ itemValue := item .Get (key )
195
+ matches := strings .Split (match , "," )
196
+ isMatch := false
197
+ for index , search := range matches {
198
+ switch typeResult := itemValue .Type .String (); typeResult {
199
+ case "JSON" :
200
+ {
201
+ // case of json and all conditions (and) must apply (labels by input)
202
+ if key == "status.conditions" {
203
+ isMigrate := isMigratable (itemValue .Value ().([]interface {}), search )
204
+ if ! isMigrate {
205
+ continue nextItem
206
+ }
207
+ continue
208
+ }
209
+ okInclude := labelsIncludes (itemValue .Value ().(map [string ]interface {}), search )
210
+ if ! okInclude {
211
+ continue nextItem
212
+ }
213
+ }
214
+
215
+ case "String" :
216
+ {
217
+ //case of string and at least one must match (or) apply (name, template, status, os)
218
+ okString := strings .Contains (strings .ToLower (itemValue .Str ), strings .ToLower (search ))
219
+ if okString {
220
+ isMatch = true
221
+ break
222
+ }
223
+ if index == len (matches )- 1 && ! isMatch {
224
+ continue nextItem
225
+ }
226
+ }
227
+ case "Null" :
228
+ {
229
+ if strings .ToLower (search ) == "null" {
230
+ break
231
+ }
232
+ continue nextItem
233
+ }
234
+ default :
235
+ continue nextItem
236
+ }
237
+ }
238
+ }
239
+ }
240
+ valueJson := map [string ]interface {}{}
241
+ err := json .Unmarshal ([]byte (item .Raw ), & valueJson )
242
+ if err != nil {
243
+ log .Println ("error creating json of item: " , err .Error ())
244
+ } else {
245
+ filteredJson = append (filteredJson , valueJson )
246
+ }
247
+ }
248
+ }
249
+
250
+ returnJson := map [string ]interface {}{}
251
+ json .Unmarshal (bodyBytes , & returnJson )
252
+ returnJson ["totalItems" ] = len (items )
253
+ if isFilters {
254
+ returnJson ["items" ] = filteredJson
255
+ }
256
+
257
+ return returnJson
258
+ }
0 commit comments