-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathtools.go
57 lines (49 loc) · 1.24 KB
/
tools.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
package apis
import (
"github.com/gin-gonic/gin"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"kubegems.io/kubegems/pkg/utils/pagination"
)
const (
allNamespace = "_all"
)
func getLabelSelector(c *gin.Context) labels.Selector {
labelsMap := c.QueryMap("labels")
sel := labels.SelectorFromSet(labelsMap)
return sel
}
func getDefaultHeader(c *gin.Context, key, defaultV string) string {
value := c.Request.Header.Get(key)
if len(value) == 0 {
return defaultV
}
return value
}
var NewPageDataFromContext = pagination.NewPageDataFromContext
type SortAndSearchAble = pagination.SortAndSearchAble
func getFieldSelector(c *gin.Context) (fields.Selector, bool) {
fieldSelectorStr := c.Query("fieldSelector")
if len(fieldSelectorStr) == 0 {
return nil, false
}
sel, err := fields.ParseSelector(fieldSelectorStr)
if err != nil {
return nil, false
}
return sel, true
}
func getFieldSelectorMatch(c *gin.Context) (map[string]string, bool) {
ret := map[string]string{}
sel, exist := getFieldSelector(c)
if !exist {
return ret, false
}
for _, req := range sel.Requirements() {
if req.Operator == selection.Equals {
ret[req.Field] = req.Value
}
}
return ret, len(ret) > 0
}