-
Notifications
You must be signed in to change notification settings - Fork 350
/
ingress.go
101 lines (83 loc) · 1.79 KB
/
ingress.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
package definitions
import (
"encoding/json"
"errors"
"fmt"
"strconv"
)
var errInvalidPortType = errors.New("invalid port type")
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 ""
}
}