This repository has been archived by the owner on Nov 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
helpers.go
168 lines (148 loc) · 3.96 KB
/
helpers.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
package app
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func requestIsHTTPS(r *http.Request) bool {
switch {
case r.URL.Scheme == "https":
return true
case r.TLS != nil:
return true
case strings.HasPrefix(r.Proto, "HTTPS"):
return true
case r.Header.Get("X-Forwarded-Proto") == "https":
return true
default:
return false
}
}
// Used to get the namespace from a query param
// Returns an error if more than one namespace in query param
func parseNamespace(c echo.Context) (string, error) {
values := c.QueryParams()
namespaces := values["namespace"]
if len(namespaces) > 1 {
return "", newStatusError(http.StatusBadRequest, errors.Errorf("Invalid number of namespace query params"))
}
if len(namespaces) == 0 {
return "", nil
}
return namespaces[0], nil
}
func parseListOptions(c echo.Context) (*client.ListOptions, error) {
ns, err := parseNamespace(c)
if err != nil {
return nil, err
}
opts := &client.ListOptions{
Namespace: ns,
Continue: c.QueryParam("continue"),
Raw: &metav1.ListOptions{
ResourceVersion: c.QueryParam("resourceVersion"),
},
}
if s := c.QueryParam("labelSelector"); s != "" {
sel, err := labels.Parse(s)
if err != nil {
return nil, err
}
opts.LabelSelector = sel
}
if s := c.QueryParam("fieldSelector"); s != "" {
sel, err := fields.ParseSelector(s)
if err != nil {
return nil, err
}
opts.FieldSelector = sel
}
if s := c.QueryParam("limit"); s != "" {
n, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return nil, err
}
opts.Limit = n
}
return opts, nil
}
// parseDeleteOptions parses metav1.DeleteOptions from a echo.Context and returns any DeleteOptions along with an error
// preconditions: will NOT be implemented at this time as it is not clear how best to implement it
// orphanDependents: has been deprecated, so it will not be implemented here
func parseDeleteOptions(c echo.Context) (metav1.DeleteOptions, error) {
myDeleteOptions := DefaultDeleteOptions
// gracePeriodSeconds
if values := c.QueryParam("gracePeriodSeconds"); values != "" {
convertValues, err := strconv.ParseInt(values, 10, 64)
if err != nil {
return metav1.DeleteOptions{}, fmt.Errorf("failed to ParseInt %v", err)
}
myDeleteOptions.GracePeriodSeconds = &convertValues
}
// propagationPolicy
if values := c.QueryParam("deletionPropagation"); values != "" {
valuesTypeConversion := metav1.DeletionPropagation(values)
myDeleteOptions.PropagationPolicy = &valuesTypeConversion
}
// dryRun
if values := c.QueryParams()["dryRun"]; len(values) > 0 {
myDeleteOptions.DryRun = values
}
return myDeleteOptions, nil
}
func parseGetOptions(c echo.Context) metav1.GetOptions {
return metav1.GetOptions{
ResourceVersion: c.QueryParam("resourceVersion"),
}
}
type Map = map[string]interface{}
type resourceVersioner interface {
GetResourceVersion() string
}
type unstructuredContenter interface {
UnstructuredContent() map[string]interface{}
}
//nolint:unparam
func result(rv resourceVersioner, extra ...Map) Map {
ctx := Map{
"result": rv,
"resourceVersion": rv.GetResourceVersion(),
}
if u, ok := rv.(unstructuredContenter); ok {
ctx["result"] = u.UnstructuredContent()
}
for _, m := range extra {
for k, v := range m {
ctx[k] = v
}
}
return ctx
}
// sendJSONSuccess is a wrapper for the basic api endpoint result, returning status OK
func (x *Controller) sendJSONSuccess(c echo.Context, params ...Map) error {
ctx := Map{
"resourceVersion": "",
"result": nil,
}
for _, m := range params {
for k, v := range m {
ctx[k] = v
}
}
h := Map{
"version": Version,
"apiVersion": APIVersion,
"context": ctx,
}
if p := c.QueryParam("pretty"); p != "" {
return c.JSONPretty(http.StatusOK, h, " ")
}
return c.JSON(http.StatusOK, h)
}