@@ -40,9 +40,8 @@ type MultiPortOption func(parser *MultiPortReceiver)
40
40
type MultiPortReceiver struct {
41
41
name string
42
42
43
- addrMappings map [string ]string
44
- portMappings map [string ]* corev1.ServicePort
45
- defaultsApplier Defaulter [any ]
43
+ addrMappings map [string ]string
44
+ portMappings map [string ]* corev1.ServicePort
46
45
}
47
46
48
47
func (m * MultiPortReceiver ) Ports (logger logr.Logger , name string , config interface {}) ([]corev1.ServicePort , error ) {
@@ -79,24 +78,29 @@ func (m *MultiPortReceiver) GetDefaultConfig(logger logr.Logger, config interfac
79
78
if err := mapstructure .Decode (config , multiProtoEndpointCfg ); err != nil {
80
79
return nil , err
81
80
}
82
-
83
- defaulter := func (protocol string ) (string , int32 ) {
84
- var port int32
81
+ defaultedConfig := map [string ]interface {}{}
82
+ for protocol , ec := range multiProtoEndpointCfg .Protocols {
85
83
if defaultSvc , ok := m .portMappings [protocol ]; ok {
86
- port = defaultSvc .Port
87
- ec := multiProtoEndpointCfg .Protocols [protocol ]
84
+ port := defaultSvc .Port
88
85
if ec != nil {
89
86
port = ec .GetPortNumOrDefault (logger , port )
90
87
}
88
+ var addr string
89
+ if defaultAddr , ok := m .addrMappings [protocol ]; ok {
90
+ addr = defaultAddr
91
+ }
92
+ conf , err := AddressDefaulter (logger , addr , port , ec )
93
+ if err != nil {
94
+ return nil , err
95
+ }
96
+ defaultedConfig [protocol ] = conf
97
+ } else {
98
+ return nil , fmt .Errorf ("unknown protocol set: %s" , protocol )
91
99
}
92
- var addr string
93
- if defaultAddr , ok := m .addrMappings [protocol ]; ok {
94
- addr = defaultAddr
95
- }
96
- return addr , port
97
100
}
98
-
99
- return m .defaultsApplier (logger , defaulter , multiProtoEndpointCfg )
101
+ return map [string ]interface {}{
102
+ "protocols" : defaultedConfig ,
103
+ }, nil
100
104
}
101
105
102
106
func (m * MultiPortReceiver ) GetLivenessProbe (logger logr.Logger , config interface {}) (* corev1.Probe , error ) {
@@ -118,7 +122,7 @@ func NewMultiPortReceiverBuilder(name string) MultiPortBuilder[*MultiProtocolEnd
118
122
}
119
123
120
124
func NewProtocolBuilder (name string , port int32 ) Builder [* MultiProtocolEndpointConfig ] {
121
- return NewBuilder [* MultiProtocolEndpointConfig ]().WithName (name ).WithPort (port ). WithDefaultsApplier ( MultiAddressDefaulter )
125
+ return NewBuilder [* MultiProtocolEndpointConfig ]().WithName (name ).WithPort (port )
122
126
}
123
127
124
128
func (mp MultiPortBuilder [ComponentConfigType ]) AddPortMapping (builder Builder [ComponentConfigType ]) MultiPortBuilder [ComponentConfigType ] {
@@ -130,21 +134,18 @@ func (mp MultiPortBuilder[ComponentConfigType]) Build() (*MultiPortReceiver, err
130
134
return nil , fmt .Errorf ("must provide at least one port mapping" )
131
135
}
132
136
133
- mp0Defaulter := mp [0 ].MustBuild ().defaultsApplier
134
137
multiReceiver := & MultiPortReceiver {
135
- name : mp [0 ].MustBuild ().name ,
136
- defaultsApplier : createMultiAddressDefaulter (mp0Defaulter ),
137
- addrMappings : map [string ]string {},
138
- portMappings : map [string ]* corev1.ServicePort {},
138
+ name : mp [0 ].MustBuild ().name ,
139
+ addrMappings : map [string ]string {},
140
+ portMappings : map [string ]* corev1.ServicePort {},
139
141
}
140
142
for _ , bu := range mp [1 :] {
141
143
built , err := bu .Build ()
142
144
if err != nil {
143
145
return nil , err
144
146
}
145
- multiReceiver .defaultsApplier = createMultiAddressDefaulter (built .defaultsApplier )
146
- multiReceiver .portMappings [built .name ] = built .settings .GetServicePort ()
147
147
if built .settings != nil {
148
+ multiReceiver .portMappings [built .name ] = built .settings .GetServicePort ()
148
149
multiReceiver .addrMappings [built .name ] = built .settings .defaultRecAddr
149
150
}
150
151
}
@@ -158,42 +159,3 @@ func (mp MultiPortBuilder[ComponentConfigType]) MustBuild() *MultiPortReceiver {
158
159
return p
159
160
}
160
161
}
161
-
162
- func createMultiAddressDefaulter [ComponentConfigType any ](defaultsApplier Defaulter [ComponentConfigType ]) Defaulter [any ] {
163
- return func (logger logr.Logger , addrProv AddressProvider , config any ) (map [string ]interface {}, error ) {
164
- tc , ok := config .(ComponentConfigType )
165
- if ! ok {
166
- return nil , fmt .Errorf ("invalid config type, expected ComponentConfigType" )
167
- }
168
-
169
- result , err := defaultsApplier (logger , addrProv , tc )
170
- if err != nil {
171
- return nil , err
172
- }
173
-
174
- return result , nil
175
- }
176
- }
177
-
178
- func MultiAddressDefaulter (logger logr.Logger , addrProv AddressProvider , config * MultiProtocolEndpointConfig ) (map [string ]interface {}, error ) {
179
- root := make (map [string ]interface {})
180
- if err := mapstructure .Decode (config , & root ); err != nil {
181
- return nil , err
182
- }
183
-
184
- proto , ok := root ["protocols" ].(map [string ]interface {})
185
- if ! ok {
186
- proto = make (map [string ]interface {})
187
- root ["protocols" ] = proto
188
- }
189
-
190
- for protocol , ec := range config .Protocols {
191
- res , err := AddressDefaulter (logger , func (string ) (string , int32 ) { return addrProv (protocol ) }, ec )
192
- if err != nil {
193
- return nil , err
194
- }
195
- proto [protocol ] = res
196
- }
197
-
198
- return root , nil
199
- }
0 commit comments