forked from joeholley/supergloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalues.go
168 lines (144 loc) · 4.13 KB
/
values.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 options
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/solo-io/go-utils/errors"
gloov1 "github.com/solo-io/gloo/projects/gloo/pkg/api/v1"
"github.com/solo-io/solo-kit/pkg/api/v1/resources/core"
v1 "github.com/solo-io/supergloo/pkg/api/v1"
)
type ResourceRefsValue []core.ResourceRef
func (v *ResourceRefsValue) String() string {
if v == nil {
return "<nil>"
}
var strs []string
for _, r := range *v {
strs = append(strs, fmt.Sprintf("%v.%v", r.Namespace, r.Name))
}
return "[" + strings.Join(strs, ", ") + "]"
}
func (v *ResourceRefsValue) Set(s string) error {
split := strings.SplitN(s, ".", 2)
if len(split) != 2 || strings.Trim(split[0], " ") == "" || strings.Trim(split[1], " ") == "" {
return errors.Errorf("%s invalid: refs must be specified in the format <NAMESPACE>.<NAME>", s)
}
*v = append(*v, core.ResourceRef{Namespace: split[0], Name: split[1]})
return nil
}
func (v *ResourceRefsValue) Type() string {
return "ResourceRefsValue"
}
type MapStringStringValue map[string]string
func (v *MapStringStringValue) String() string {
if v == nil {
return "<nil>"
}
var strs []string
for k, val := range *v {
strs = append(strs, fmt.Sprintf("%v.%v", k, val))
}
return "[" + strings.Join(strs, ", ") + "]"
}
func (v *MapStringStringValue) Set(s string) error {
split := strings.SplitN(s, "=", 2)
if len(split) != 2 {
return errors.Errorf("%s invalid: map entries must be specified in the format KEY=VALUE", s)
}
m := *v
if m == nil {
m = make(MapStringStringValue)
}
m[split[0]] = split[1]
*v = m
return nil
}
func (v *MapStringStringValue) Type() string {
return "MapStringStringValue"
}
type RequestMatchersValue []RequestMatcher
func (v *RequestMatchersValue) String() string {
if v == nil {
return "<nil>"
}
var strs []string
for _, r := range *v {
strs = append(strs, fmt.Sprintf("%#v", r))
}
return "[" + strings.Join(strs, ", ") + "]"
}
func (v *RequestMatchersValue) Set(s string) error {
var match RequestMatcher
err := json.Unmarshal([]byte(s), &match)
if err != nil {
return errors.Wrapf(err, "%s invalid: request matcher must be specified as valid request matcher json", s)
}
*v = append(*v, match)
return nil
}
func (v *RequestMatchersValue) Type() string {
return "RequestMatchersValue"
}
type TrafficShiftingValue v1.TrafficShifting
func (v *TrafficShiftingValue) String() string {
if v == nil || v.Destinations == nil {
return "<nil>"
}
var strs []string
for _, r := range v.Destinations.Destinations {
strs = append(strs, fmt.Sprintf("%v: %v", r.Destination.Upstream, r.Weight))
}
return "[" + strings.Join(strs, ", ") + "]"
}
func (v *TrafficShiftingValue) Set(s string) error {
split := strings.SplitN(s, ".", 2)
if len(split) != 2 {
return errors.Errorf("%s invalid: weighted destinations must be specified in the format <NAMESPACE>.<NAME>:WEIGHT", s)
}
namespace := split[0]
split = strings.SplitN(split[1], ":", 2)
if len(split) != 2 {
return errors.Errorf("%s invalid: weighted destinations must be specified in the format <NAMESPACE>.<NAME>:WEIGHT", s)
}
name := split[0]
weight, err := strconv.Atoi(split[1])
if err != nil {
return err
}
if v.Destinations == nil {
v.Destinations = &gloov1.MultiDestination{}
}
v.Destinations.Destinations = append(v.Destinations.Destinations, &gloov1.WeightedDestination{
Destination: &gloov1.Destination{
Upstream: core.ResourceRef{
Namespace: namespace,
Name: name,
},
},
Weight: uint32(weight),
})
return nil
}
func (v *TrafficShiftingValue) Type() string {
return "TrafficShiftingValue"
}
type ResourceRefValue core.ResourceRef
func (v *ResourceRefValue) String() string {
if v == nil {
return "<nil>"
}
return fmt.Sprintf("%v", *v)
}
func (v *ResourceRefValue) Set(s string) error {
split := strings.SplitN(s, ".", 2)
if len(split) != 2 || strings.Trim(split[0], " ") == "" || strings.Trim(split[1], " ") == "" {
return errors.Errorf("%s invalid: refs must be specified in the format <NAMESPACE>.<NAME>", s)
}
*v = ResourceRefValue(core.ResourceRef{Namespace: split[0], Name: split[1]})
return nil
}
func (v *ResourceRefValue) Type() string {
return "ResourceRefValue"
}