-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathstructure_ingress_spec.go
More file actions
220 lines (176 loc) · 4.59 KB
/
structure_ingress_spec.go
File metadata and controls
220 lines (176 loc) · 4.59 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright IBM Corp. 2017, 2025
// SPDX-License-Identifier: MPL-2.0
package kubernetes
import (
"k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/util/intstr"
)
// Flatteners
func flattenIngressRule(in []v1beta1.IngressRule) []interface{} {
att := make([]interface{}, len(in))
for i, r := range in {
m := make(map[string]interface{})
m["host"] = r.Host
m["http"] = flattenIngressRuleHttp(r.HTTP)
att[i] = m
}
return att
}
func flattenIngressRuleHttp(in *v1beta1.HTTPIngressRuleValue) []interface{} {
if in == nil {
return []interface{}{}
}
pathAtts := make([]interface{}, len(in.Paths))
for i, p := range in.Paths {
path := map[string]interface{}{
"path": p.Path,
"backend": flattenIngressBackend(&p.Backend),
}
pathAtts[i] = path
}
httpAtt := map[string]interface{}{
"path": pathAtts,
}
return []interface{}{httpAtt}
}
func flattenIngressBackend(in *v1beta1.IngressBackend) []interface{} {
att := make([]interface{}, 1)
m := make(map[string]interface{})
m["service_name"] = in.ServiceName
m["service_port"] = in.ServicePort.String()
att[0] = m
return att
}
func flattenIngressSpec(in v1beta1.IngressSpec) []interface{} {
att := make(map[string]interface{})
if in.IngressClassName != nil {
att["ingress_class_name"] = in.IngressClassName
}
if in.Backend != nil {
att["backend"] = flattenIngressBackend(in.Backend)
}
if len(in.Rules) > 0 {
att["rule"] = flattenIngressRule(in.Rules)
}
if len(in.TLS) > 0 {
att["tls"] = flattenIngressTLS(in.TLS)
}
return []interface{}{att}
}
func flattenIngressTLS(in []v1beta1.IngressTLS) []interface{} {
att := make([]interface{}, len(in))
for i, v := range in {
m := make(map[string]interface{})
m["hosts"] = v.Hosts
m["secret_name"] = v.SecretName
att[i] = m
}
return att
}
func flattenIngressStatus(in v1beta1.IngressLoadBalancerStatus) []interface{} {
out := make([]interface{}, len(in.Ingress))
for i, ingress := range in.Ingress {
out[i] = map[string]interface{}{
"ip": ingress.IP,
"hostname": ingress.Hostname,
}
}
return []interface{}{
map[string][]interface{}{
"ingress": out,
},
}
}
// Expanders
func expandIngressRule(l []interface{}) []v1beta1.IngressRule {
if len(l) == 0 || l[0] == nil {
return []v1beta1.IngressRule{}
}
obj := make([]v1beta1.IngressRule, len(l))
for i, n := range l {
cfg := n.(map[string]interface{})
var paths []v1beta1.HTTPIngressPath
if httpCfg, ok := cfg["http"]; ok {
httpList := httpCfg.([]interface{})
for _, h := range httpList {
http := h.(map[string]interface{})
if v, ok := http["path"]; ok {
pathList := v.([]interface{})
paths = make([]v1beta1.HTTPIngressPath, len(pathList))
for i, path := range pathList {
p := path.(map[string]interface{})
hip := v1beta1.HTTPIngressPath{
Path: p["path"].(string),
Backend: *expandIngressBackend(p["backend"].([]interface{})),
}
paths[i] = hip
}
}
}
}
obj[i] = v1beta1.IngressRule{
Host: cfg["host"].(string),
IngressRuleValue: v1beta1.IngressRuleValue{
HTTP: &v1beta1.HTTPIngressRuleValue{
Paths: paths,
},
},
}
}
return obj
}
func expandIngressSpec(l []interface{}) v1beta1.IngressSpec {
if len(l) == 0 || l[0] == nil {
return v1beta1.IngressSpec{}
}
in := l[0].(map[string]interface{})
obj := v1beta1.IngressSpec{}
if v, ok := in["ingress_class_name"].(string); ok && len(v) > 0 {
obj.IngressClassName = &v
}
if v, ok := in["backend"].([]interface{}); ok && len(v) > 0 {
obj.Backend = expandIngressBackend(v)
}
if v, ok := in["rule"].([]interface{}); ok && len(v) > 0 {
obj.Rules = expandIngressRule(v)
}
if v, ok := in["tls"].([]interface{}); ok && len(v) > 0 {
obj.TLS = expandIngressTLS(v)
}
return obj
}
func expandIngressBackend(l []interface{}) *v1beta1.IngressBackend {
if len(l) == 0 || l[0] == nil {
return &v1beta1.IngressBackend{}
}
in := l[0].(map[string]interface{})
obj := &v1beta1.IngressBackend{}
if v, ok := in["service_name"].(string); ok {
obj.ServiceName = v
}
if v, ok := in["service_port"].(string); ok {
obj.ServicePort = intstr.Parse(v)
}
return obj
}
func expandIngressTLS(l []interface{}) []v1beta1.IngressTLS {
if len(l) == 0 {
return nil
}
tlsList := make([]v1beta1.IngressTLS, len(l))
for i, t := range l {
if t == nil {
t = map[string]interface{}{}
}
in := t.(map[string]interface{})
obj := v1beta1.IngressTLS{}
if v, ok := in["hosts"]; ok {
obj.Hosts = expandStringSlice(v.([]interface{}))
}
if v, ok := in["secret_name"].(string); ok {
obj.SecretName = v
}
tlsList[i] = obj
}
return tlsList
}