Skip to content

Commit e6ba085

Browse files
swordqiuQiu Jian
andauthored
fix: create redirect lb listener fail (#24726)
Co-authored-by: Qiu Jian <qiujian@yunionyun.com>
1 parent 49afcfe commit e6ba085

3 files changed

Lines changed: 33 additions & 15 deletions

File tree

pkg/apis/compute/loadbalancer_const.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ var LB_LISTENER_TYPES = []string{
160160
LB_LISTENER_TYPE_HTTPS,
161161
}
162162

163+
var LB_APP_LISTENER_TYPES = []string{
164+
LB_LISTENER_TYPE_HTTP,
165+
LB_LISTENER_TYPE_HTTPS,
166+
}
167+
163168
// aws_network_lb_listener
164169
var AWS_NETWORK_LB_LISTENER_TYPES = choices.NewChoices(
165170
LB_LISTENER_TYPE_TCP,

pkg/apis/compute/loadbalancerlistener.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ type LoadbalancerListenerCreateInput struct {
181181
AclType string `json:"acl_type"`
182182
}
183183

184+
func (input LoadbalancerListenerCreateInput) IsRedirect() bool {
185+
return len(input.Redirect) > 0 && input.Redirect != LB_REDIRECT_OFF
186+
}
187+
184188
func (self *LoadbalancerListenerCreateInput) Validate() error {
185189
if len(self.Status) == 0 {
186190
self.Status = LB_STATUS_ENABLED
@@ -191,7 +195,13 @@ func (self *LoadbalancerListenerCreateInput) Validate() error {
191195
if !utils.IsInStringArray(self.SendProxy, LB_SENDPROXY_CHOICES) {
192196
return httperrors.NewInputParameterError("invalid send_proxy %s", self.SendProxy)
193197
}
194-
if !utils.IsInStringArray(self.Scheduler, LB_SCHEDULER_TYPES) {
198+
if len(self.Redirect) == 0 {
199+
self.Redirect = LB_REDIRECT_OFF
200+
}
201+
if !utils.IsInStringArray(self.Redirect, []string{LB_REDIRECT_OFF, LB_REDIRECT_RAW}) {
202+
return httperrors.NewInputParameterError("invalid redirect %s", self.Redirect)
203+
}
204+
if !self.IsRedirect() && !utils.IsInStringArray(self.Scheduler, LB_SCHEDULER_TYPES) {
195205
return httperrors.NewInputParameterError("invalid scheduler %s", self.Scheduler)
196206
}
197207
if len(self.StickySession) == 0 {
@@ -223,6 +233,9 @@ func (self *LoadbalancerListenerCreateInput) Validate() error {
223233
if !utils.IsInStringArray(self.ListenerType, LB_LISTENER_TYPES) {
224234
return httperrors.NewInputParameterError("invalid listener_type %s", self.ListenerType)
225235
}
236+
if self.IsRedirect() && !utils.IsInStringArray(self.ListenerType, LB_APP_LISTENER_TYPES) {
237+
return httperrors.NewInputParameterError("redirect is only supported for http/https listeners")
238+
}
226239
if self.ListenerPort < 1 || self.ListenerPort > 65535 {
227240
return httperrors.NewOutOfRangeError("listener_port out of range 1-65535")
228241
}
@@ -272,12 +285,7 @@ func (self *LoadbalancerListenerCreateInput) Validate() error {
272285
self.HealthCheckInterval = 30
273286
}
274287
}
275-
if len(self.Redirect) == 0 {
276-
self.Redirect = LB_REDIRECT_OFF
277-
}
278-
if !utils.IsInStringArray(self.Redirect, []string{LB_REDIRECT_OFF, LB_REDIRECT_RAW}) {
279-
return httperrors.NewInputParameterError("invalid redirect %s", self.Redirect)
280-
}
288+
281289
return nil
282290
}
283291

pkg/compute/models/loadbalancerlisteners.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -359,14 +359,6 @@ func (man *SLoadbalancerListenerManager) ValidateCreateData(ctx context.Context,
359359
return nil, err
360360
}
361361
lb := lbObj.(*SLoadbalancer)
362-
lbbgObj, err := validators.ValidateModel(ctx, userCred, LoadbalancerBackendGroupManager, &input.BackendGroupId)
363-
if err != nil {
364-
return nil, err
365-
}
366-
lbbg := lbbgObj.(*SLoadbalancerBackendGroup)
367-
if lbbg.LoadbalancerId != lb.Id {
368-
return nil, httperrors.NewConflictError("backendgroup_id not same with listener's loadbalancer")
369-
}
370362
region, err := lb.GetRegion()
371363
if err != nil {
372364
return nil, errors.Wrapf(err, "GetRegion")
@@ -378,6 +370,19 @@ func (man *SLoadbalancerListenerManager) ValidateCreateData(ctx context.Context,
378370
if err != nil {
379371
return nil, err
380372
}
373+
var lbbg *SLoadbalancerBackendGroup
374+
if input.IsRedirect() {
375+
input.BackendGroupId = ""
376+
} else {
377+
lbbgObj, err := validators.ValidateModel(ctx, userCred, LoadbalancerBackendGroupManager, &input.BackendGroupId)
378+
if err != nil {
379+
return nil, err
380+
}
381+
lbbg = lbbgObj.(*SLoadbalancerBackendGroup)
382+
if lbbg.LoadbalancerId != lb.Id {
383+
return nil, httperrors.NewConflictError("backendgroup_id not same with listener's loadbalancer")
384+
}
385+
}
381386
if utils.IsInStringArray(input.ListenerType, []string{api.LB_LISTENER_TYPE_TCP, api.LB_LISTENER_TYPE_UDP}) {
382387

383388
}

0 commit comments

Comments
 (0)