Skip to content

Commit b901e42

Browse files
authored
Fix crash when deferencing nil pointer in v1beta1.IngressRule (#967)
1 parent c9f049b commit b901e42

File tree

2 files changed

+100
-17
lines changed

2 files changed

+100
-17
lines changed

kubernetes/structure_ingress_spec.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,34 @@ import (
1010

1111
func flattenIngressRule(in []v1beta1.IngressRule) []interface{} {
1212
att := make([]interface{}, len(in), len(in))
13-
for i, n := range in {
14-
// rulePrefix := fmt.Sprintf("rule.%d.")
13+
for i, r := range in {
1514
m := make(map[string]interface{})
1615

17-
m["host"] = n.Host
16+
m["host"] = r.Host
17+
m["http"] = flattenIngressRuleHttp(r.HTTP)
18+
att[i] = m
19+
}
20+
return att
21+
}
1822

19-
httpAtt := make(map[string]interface{})
20-
pathAtts := make([]interface{}, len(n.HTTP.Paths), len(n.HTTP.Paths))
21-
for i, p := range n.HTTP.Paths {
22-
path := map[string]interface{}{
23-
"path": p.Path,
24-
"backend": flattenIngressBackend(&p.Backend),
25-
}
26-
pathAtts[i] = path
27-
}
28-
httpAtt["path"] = pathAtts
29-
m["http"] = []interface{}{
30-
httpAtt,
23+
func flattenIngressRuleHttp(in *v1beta1.HTTPIngressRuleValue) []interface{} {
24+
if in == nil {
25+
return []interface{}{}
26+
}
27+
pathAtts := make([]interface{}, len(in.Paths), len(in.Paths))
28+
for i, p := range in.Paths {
29+
path := map[string]interface{}{
30+
"path": p.Path,
31+
"backend": flattenIngressBackend(&p.Backend),
3132
}
33+
pathAtts[i] = path
34+
}
3235

33-
att[i] = m
36+
httpAtt := map[string]interface{}{
37+
"path": pathAtts,
3438
}
35-
return att
39+
40+
return []interface{}{httpAtt}
3641
}
3742

3843
func flattenIngressBackend(in *v1beta1.IngressBackend) []interface{} {
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package kubernetes
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
"k8s.io/api/extensions/v1beta1"
8+
"k8s.io/apimachinery/pkg/util/intstr"
9+
)
10+
11+
// Test Flatteners
12+
func TestFlattenIngressRule(t *testing.T) {
13+
r := v1beta1.HTTPIngressRuleValue{
14+
Paths: []v1beta1.HTTPIngressPath{
15+
{
16+
Path: "/foo/bar",
17+
Backend: v1beta1.IngressBackend{
18+
ServiceName: "foo",
19+
ServicePort: intstr.FromInt(1234),
20+
},
21+
},
22+
},
23+
}
24+
25+
in := []v1beta1.IngressRule{
26+
{
27+
Host: "the-app-name.staging.live.domain-replaced.tld",
28+
IngressRuleValue: v1beta1.IngressRuleValue{
29+
HTTP: (*v1beta1.HTTPIngressRuleValue)(nil),
30+
},
31+
},
32+
{
33+
Host: "",
34+
IngressRuleValue: v1beta1.IngressRuleValue{
35+
HTTP: (*v1beta1.HTTPIngressRuleValue)(&r),
36+
},
37+
},
38+
}
39+
out := []interface{}{
40+
map[string]interface{}{
41+
"host": "the-app-name.staging.live.domain-replaced.tld",
42+
"http": []interface{}{},
43+
},
44+
map[string]interface{}{
45+
"host": "",
46+
"http": []interface{}{
47+
map[string]interface{}{
48+
"path": []interface{}{
49+
map[string]interface{}{
50+
"path": "/foo/bar",
51+
"backend": []interface{}{
52+
map[string]interface{}{
53+
"service_name": "foo",
54+
"service_port": "1234",
55+
},
56+
},
57+
},
58+
},
59+
},
60+
},
61+
},
62+
}
63+
64+
flatRules := flattenIngressRule(in)
65+
66+
if len(flatRules) < len(out) {
67+
t.Error("Failed to flatten ingress rules")
68+
}
69+
70+
for i, v := range flatRules {
71+
control := v.(map[string]interface{})
72+
sample := out[i]
73+
74+
if !reflect.DeepEqual(control, sample) {
75+
t.Errorf("Unexpected result:\n\tWant:%s\n\tGot:%s\n", control, sample)
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)