Skip to content

Commit 40985ca

Browse files
authored
listener options merge what translator generates (#10578)
1 parent 5875e84 commit 40985ca

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
changelog:
2+
- type: NON_USER_FACING
3+
issueLink: https://github.com/solo-io/solo-projects/issues/7300
4+
resolvesIssue: true
5+
description: >-
6+
If we generate any ListenerOptions from a Gateway to Proxy
7+
translator, they will no longer be overridden by user
8+
ListenerOptions.

projects/gateway2/translator/plugins/listeneroptions/listener_options_plugin.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/solo-io/gloo/projects/gateway2/translator/plugins"
88
lisquery "github.com/solo-io/gloo/projects/gateway2/translator/plugins/listeneroptions/query"
99
v1 "github.com/solo-io/gloo/projects/gloo/pkg/api/v1"
10+
"github.com/solo-io/gloo/projects/gloo/pkg/utils"
1011

1112
"sigs.k8s.io/controller-runtime/pkg/client"
1213
)
@@ -47,7 +48,11 @@ func (p *plugin) ApplyListenerPlugin(
4748
// use the first option (highest in priority)
4849
// see for more context: https://github.com/solo-io/solo-projects/issues/6313
4950
optToUse := attachedOptions[0]
50-
outListener.Options = optToUse.Spec.GetOptions()
51+
if outListener.GetOptions() != nil {
52+
outListener.Options, _ = utils.ShallowMergeListenerOptions(outListener.GetOptions(), optToUse.Spec.GetOptions())
53+
} else {
54+
outListener.Options = optToUse.Spec.GetOptions()
55+
}
5156

5257
return nil
5358
}

projects/gateway2/translator/plugins/listeneroptions/listener_options_plugin_test.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/solo-io/gloo/projects/gateway2/translator/testutils"
1717
"github.com/solo-io/gloo/projects/gateway2/wellknown"
1818
v1 "github.com/solo-io/gloo/projects/gloo/pkg/api/v1"
19+
"github.com/solo-io/gloo/projects/gloo/pkg/api/v1/options/proxy_protocol"
1920
corev1 "github.com/solo-io/skv2/pkg/api/core.skv2.solo.io/v1"
2021
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2122
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -48,12 +49,19 @@ var _ = Describe("ListenerOptions Plugin", func() {
4849
},
4950
}
5051

51-
outputListener = &v1.Listener{}
52+
outputListener = &v1.Listener{
53+
Options: &v1.ListenerOptions{
54+
ProxyProtocol: &proxy_protocol.ProxyProtocol{},
55+
},
56+
}
5257

5358
expectedOptions = &v1.ListenerOptions{
59+
// from config
5460
PerConnectionBufferLimitBytes: &wrapperspb.UInt32Value{
5561
Value: uint32(419),
5662
},
63+
// base
64+
ProxyProtocol: &proxy_protocol.ProxyProtocol{},
5765
}
5866
})
5967
JustBeforeEach(func() {
@@ -102,7 +110,7 @@ var _ = Describe("ListenerOptions Plugin", func() {
102110
It("does not add buffer limit", func() {
103111
err := plugin.ApplyListenerPlugin(ctx, listenerCtx, outputListener)
104112
Expect(err).ToNot(HaveOccurred())
105-
Expect(outputListener.GetOptions()).To(BeNil())
113+
Expect(outputListener.GetOptions().GetPerConnectionBufferLimitBytes()).To(BeNil())
106114
})
107115
})
108116

@@ -114,11 +122,10 @@ var _ = Describe("ListenerOptions Plugin", func() {
114122
It("does not add buffer limit", func() {
115123
err := plugin.ApplyListenerPlugin(ctx, listenerCtx, outputListener)
116124
Expect(err).ToNot(HaveOccurred())
117-
Expect(outputListener.GetOptions()).To(BeNil())
125+
Expect(outputListener.GetOptions().GetPerConnectionBufferLimitBytes()).To(BeNil())
118126
})
119127
})
120128
})
121-
122129
})
123130

124131
func attachedListenerOption() *solokubev1.ListenerOption {
@@ -144,6 +151,7 @@ func attachedListenerOption() *solokubev1.ListenerOption {
144151
},
145152
}
146153
}
154+
147155
func attachedListenerOptionWithSectionName() *solokubev1.ListenerOption {
148156
listOpt := attachedListenerOption()
149157
listOpt.Spec.TargetRefs[0].SectionName = &wrapperspb.StringValue{

projects/gloo/pkg/utils/merge.go

+26
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,32 @@ func isEmptyValue(v reflect.Value) bool {
8080
return false
8181
}
8282

83+
// ShallowMergeListenerOptions merges the top-level fields of src into dst.
84+
// The fields in dst that have non-zero values will not be overwritten.
85+
// It performs a shallow merge of top-level fields only.
86+
// It returns a boolean indicating whether any fields in src overwrote dst.
87+
func ShallowMergeListenerOptions(dst, src *v1.ListenerOptions) (*v1.ListenerOptions, bool) {
88+
if src == nil {
89+
return dst, false
90+
}
91+
92+
if dst == nil {
93+
return src.Clone().(*v1.ListenerOptions), true
94+
}
95+
96+
dstValue, srcValue := reflect.ValueOf(dst).Elem(), reflect.ValueOf(src).Elem()
97+
98+
overwrote := false
99+
for i := range dstValue.NumField() {
100+
dstField, srcField := dstValue.Field(i), srcValue.Field(i)
101+
if srcOverride := ShallowMerge(dstField, srcField); srcOverride {
102+
overwrote = true
103+
}
104+
}
105+
106+
return dst, overwrote
107+
}
108+
83109
// ShallowMergeRouteOptions merges the top-level fields of src into dst.
84110
// The fields in dst that have non-zero values will not be overwritten.
85111
// It performs a shallow merge of top-level fields only.

0 commit comments

Comments
 (0)