-
Notifications
You must be signed in to change notification settings - Fork 351
/
ingressv1beta1.go
154 lines (127 loc) · 2.99 KB
/
ingressv1beta1.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
package definitions
import (
"encoding/json"
"fmt"
"strconv"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
var errInvalidPortType = errors.New("invalid port type")
type IngressList struct {
Items []*IngressItem `json:"items"`
}
type IngressItem struct {
Metadata *Metadata `json:"metadata"`
Spec *IngressSpec `json:"spec"`
}
// IngressSpec is the v1beta1
type IngressSpec struct {
DefaultBackend *Backend `json:"backend"`
Rules []*Rule `json:"rules"`
}
type Backend struct {
ServiceName string `json:"serviceName"`
ServicePort BackendPort `json:"servicePort"`
// Traffic field used for custom traffic weights, but not part of the ingress spec.
Traffic float64
// number of True predicates to add to support multi color traffic switching
NoopCount int
}
type Rule struct {
Host string `json:"host"`
Http *HTTPRule `json:"http"`
}
type BackendPort struct {
Value interface{}
}
type HTTPRule struct {
Paths []*PathRule `json:"paths"`
}
type PathRule struct {
Path string `json:"path"`
Backend *Backend `json:"backend"`
}
type ResourceID struct {
Namespace string
Name string
}
func (b Backend) String() string {
return fmt.Sprintf("svc(%s, %s) %0.2f", b.ServiceName, b.ServicePort, b.Traffic)
}
func (p BackendPort) Name() (string, bool) {
s, ok := p.Value.(string)
return s, ok
}
func (p BackendPort) Number() (int, bool) {
i, ok := p.Value.(int)
return i, ok
}
func (p *BackendPort) UnmarshalJSON(value []byte) error {
if value[0] == '"' {
var s string
if err := json.Unmarshal(value, &s); err != nil {
return err
}
p.Value = s
return nil
}
var i int
if err := json.Unmarshal(value, &i); err != nil {
return err
}
p.Value = i
return nil
}
func (p BackendPort) MarshalJSON() ([]byte, error) {
switch p.Value.(type) {
case string, int:
return json.Marshal(p.Value)
default:
return nil, errInvalidPortType
}
}
func (p BackendPort) String() string {
switch v := p.Value.(type) {
case string:
return v
case int:
return strconv.Itoa(v)
default:
return ""
}
}
// ParseIngressJSON parse JSON into an IngressList
func ParseIngressJSON(d []byte) (IngressList, error) {
var il IngressList
err := json.Unmarshal(d, &il)
return il, err
}
// ParseIngressYAML parse YAML into an IngressList
func ParseIngressYAML(d []byte) (IngressList, error) {
var il IngressList
err := yaml.Unmarshal(d, &il)
return il, err
}
// TODO: implement once IngressItem has a validate method
// ValidateIngress is a no-op
func ValidateIngress(_ *IngressItem) error {
return nil
}
// ValidateIngresses is a no-op
func ValidateIngresses(ingressList IngressList) error {
var err error
// discover all errors to avoid the user having to repeatedly validate
for _, i := range ingressList.Items {
nerr := ValidateIngress(i)
if nerr != nil {
name := i.Metadata.Name
namespace := i.Metadata.Namespace
nerr = fmt.Errorf("%s/%s: %w", name, namespace, nerr)
err = errors.Wrap(err, nerr.Error())
}
}
if err != nil {
return err
}
return nil
}