-
Notifications
You must be signed in to change notification settings - Fork 343
/
ingressv1.go
187 lines (156 loc) · 4.47 KB
/
ingressv1.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package definitions
import (
"encoding/json"
"fmt"
"strconv"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
type IngressV1List struct {
Items []*IngressV1Item `json:"items"`
}
type IngressV1Item struct {
Metadata *Metadata `json:"metadata"`
Spec *IngressV1Spec `json:"spec"`
}
// IngressSpecV1 https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.22/#ingressspec-v1-networking-k8s-io
type IngressV1Spec struct {
DefaultBackend *BackendV1 `json:"defaultBackend,omitempty"`
IngressClassName string `json:"ingressClassName,omitempty"`
Rules []*RuleV1 `json:"rules"`
IngressTLS []*TLSV1 `json:"tls,omitempty"`
}
// BackendV1 https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.22/#ingressbackend-v1-networking-k8s-io
type BackendV1 struct {
Service Service `json:"service,omitempty"` // can be nil, because of TypedLocalObjectReference
// Resource TypedLocalObjectReference is not supported https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.22/#typedlocalobjectreference-v1-core
// 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
}
// Service https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.22/#ingressservicebackend-v1-networking-k8s-io
type Service struct {
Name string `json:"name"`
Port BackendPortV1 `json:"port"`
}
type BackendPortV1 struct {
Name string `json:"name"`
Number int `json:"number"`
}
func (p BackendPortV1) String() string {
if p.Number != 0 {
return strconv.Itoa(p.Number)
}
return p.Name
}
type RuleV1 struct {
Host string `json:"host"`
Http *HTTPRuleV1 `json:"http"`
}
type HTTPRuleV1 struct {
Paths []*PathRuleV1 `json:"paths"`
}
// PathRuleV1 https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.22/#httpingresspath-v1-networking-k8s-io
type PathRuleV1 struct {
Path string `json:"path"`
PathType string `json:"pathType"`
Backend *BackendV1 `json:"backend"`
}
type TLSV1 struct {
Hosts []string `json:"hosts"`
SecretName string `json:"secretName"`
}
// ParseIngressV1JSON parse JSON into an IngressV1List
func ParseIngressV1JSON(d []byte) (IngressV1List, error) {
var il IngressV1List
err := json.Unmarshal(d, &il)
return il, err
}
// ParseIngressV1YAML parse YAML into an IngressV1List
func ParseIngressV1YAML(d []byte) (IngressV1List, error) {
var il IngressV1List
err := yaml.Unmarshal(d, &il)
return il, err
}
// TODO: implement once IngressItem has a validate method
// ValidateIngressV1 is a no-op
func ValidateIngressV1(_ *IngressV1Item) error {
return nil
}
// ValidateIngresses is a no-op
func ValidateIngressesV1(ingressList IngressV1List) error {
var err error
// discover all errors to avoid the user having to repeatedly validate
for _, i := range ingressList.Items {
nerr := ValidateIngressV1(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
}
func GetHostsFromIngressRulesV1(ing *IngressV1Item) []string {
hostList := make([]string, 0)
for _, i := range ing.Spec.Rules {
hostList = append(hostList, i.Host)
}
return hostList
}
// ResourceID is a stripped down version of Metadata used to identify resources in a cache map
type ResourceID struct {
Namespace string
Name string
}
/* required from v1beta1 */
// BackendPort is used for TargetPort similar to Kubernetes intOrString type
type BackendPort struct {
Value interface{}
}
// String converts BackendPort to string
func (p BackendPort) String() string {
switch v := p.Value.(type) {
case string:
return v
case int:
return strconv.Itoa(v)
default:
return ""
}
}
// Number converts BackendPort to int
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
}
}
var errInvalidPortType = errors.New("invalid port type")