From 4e2c7bf3532002bfbcc97610fee1f965c0404067 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Wed, 6 Aug 2025 17:37:37 +0800 Subject: [PATCH] fix(region): support lb health check sync status (#23028) --- .../compute/loadbalancer_health_check.go | 1 + pkg/apis/compute/loadbalancerbackendgroup.go | 2 + .../models/loadbalancer_health_checks.go | 6 +- .../models/loadbalancerbackendgroups.go | 11 +++ ...adbalancer_health_check_syncstatus_task.go | 73 +++++++++++++++++++ 5 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 pkg/compute/tasks/loadbalancer/loadbalancer_health_check_syncstatus_task.go diff --git a/cmd/climc/shell/compute/loadbalancer_health_check.go b/cmd/climc/shell/compute/loadbalancer_health_check.go index 24c58665b0..17676d1d08 100644 --- a/cmd/climc/shell/compute/loadbalancer_health_check.go +++ b/cmd/climc/shell/compute/loadbalancer_health_check.go @@ -27,4 +27,5 @@ func init() { cmd.Create(&options.LoadbalancerHealthCheckCreateOptions{}) cmd.Delete(&options.LoadbalancerHealthCheckIdOptions{}) cmd.Update(&options.LoadbalancerHealthCheckUpdateOptions{}) + cmd.Perform("syncstatus", &options.LoadbalancerHealthCheckIdOptions{}) } diff --git a/pkg/apis/compute/loadbalancerbackendgroup.go b/pkg/apis/compute/loadbalancerbackendgroup.go index f7fd6375e3..3222a55632 100644 --- a/pkg/apis/compute/loadbalancerbackendgroup.go +++ b/pkg/apis/compute/loadbalancerbackendgroup.go @@ -29,6 +29,8 @@ type LoadbalancerBackendGroupDetails struct { SLoadbalancerBackendGroup + LoadbalancerHealthCheck string `json:"loadbalancer_health_check"` + LbListenerCount int `json:"lb_listener_count"` IsDefault bool `json:"is_default"` diff --git a/pkg/compute/models/loadbalancer_health_checks.go b/pkg/compute/models/loadbalancer_health_checks.go index aa738f1f39..81adeb62a1 100644 --- a/pkg/compute/models/loadbalancer_health_checks.go +++ b/pkg/compute/models/loadbalancer_health_checks.go @@ -175,7 +175,7 @@ func (hc *SLoadbalancerHealthCheck) ValidateUpdateData( if err != nil { return nil, errors.Wrap(err, "SVirtualResourceBase.ValidateUpdateData") } - return nil, cloudprovider.ErrNotImplemented + return input, nil } func (hc *SLoadbalancerHealthCheck) PostUpdate(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) { @@ -417,6 +417,10 @@ func (hc *SLoadbalancerHealthCheck) syncRemove(ctx context.Context, userCred mcc return hc.RealDelete(ctx, userCred) } +func (hc *SLoadbalancerHealthCheck) PerformSyncstatus(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) { + return nil, StartResourceSyncStatusTask(ctx, userCred, hc, "LoadbalancerHealthCheckSyncstatusTask", "") +} + func (manager *SLoadbalancerHealthCheckManager) ListItemExportKeys(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, diff --git a/pkg/compute/models/loadbalancerbackendgroups.go b/pkg/compute/models/loadbalancerbackendgroups.go index cfce1528ee..3830afbb40 100644 --- a/pkg/compute/models/loadbalancerbackendgroups.go +++ b/pkg/compute/models/loadbalancerbackendgroups.go @@ -450,6 +450,7 @@ func (man *SLoadbalancerBackendGroupManager) FetchCustomizeColumns( lbIds := make([]string, len(objs)) lbbgIds := make([]string, len(objs)) + hcIds := make([]string, len(objs)) for i := range rows { rows[i] = api.LoadbalancerBackendGroupDetails{ StatusStandaloneResourceDetails: stdRows[i], @@ -458,6 +459,7 @@ func (man *SLoadbalancerBackendGroupManager) FetchCustomizeColumns( lbbg := objs[i].(*SLoadbalancerBackendGroup) lbIds[i] = lbbg.LoadbalancerId lbbgIds[i] = lbbg.Id + hcIds[i] = lbbg.LoadbalancerHealthCheckId } lbs := map[string]SLoadbalancer{} @@ -477,8 +479,17 @@ func (man *SLoadbalancerBackendGroupManager) FetchCustomizeColumns( } } } + + hcMap, err := db.FetchIdNameMap2(LoadbalancerHealthCheckManager, hcIds) + if err != nil { + return rows + } + for i := range rows { rows[i].IsDefault = utils.IsInStringArray(lbbgIds[i], defaultLbgIds) + if hcId, ok := hcMap[hcIds[i]]; ok { + rows[i].LoadbalancerHealthCheck = hcId + } } for i := range objs { diff --git a/pkg/compute/tasks/loadbalancer/loadbalancer_health_check_syncstatus_task.go b/pkg/compute/tasks/loadbalancer/loadbalancer_health_check_syncstatus_task.go new file mode 100644 index 0000000000..9fa5723359 --- /dev/null +++ b/pkg/compute/tasks/loadbalancer/loadbalancer_health_check_syncstatus_task.go @@ -0,0 +1,73 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package loadbalancer + +import ( + "context" + + "yunion.io/x/cloudmux/pkg/apis" + "yunion.io/x/cloudmux/pkg/cloudprovider" + "yunion.io/x/jsonutils" + "yunion.io/x/pkg/errors" + + "yunion.io/x/onecloud/pkg/cloudcommon/db" + "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman" + "yunion.io/x/onecloud/pkg/compute/models" +) + +type LoadbalancerHealthCheckSyncstatusTask struct { + taskman.STask +} + +func init() { + taskman.RegisterTask(LoadbalancerHealthCheckSyncstatusTask{}) +} + +func (self *LoadbalancerHealthCheckSyncstatusTask) taskFail(ctx context.Context, hc *models.SLoadbalancerHealthCheck, err error) { + hc.SetStatus(ctx, self.GetUserCred(), apis.STATUS_UNKNOWN, err.Error()) + self.SetStageFailed(ctx, jsonutils.NewString(err.Error())) +} + +func (self *LoadbalancerHealthCheckSyncstatusTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { + hc := obj.(*models.SLoadbalancerHealthCheck) + iRegion, err := hc.GetIRegion(ctx) + if err != nil { + self.taskFail(ctx, hc, errors.Wrapf(err, "GetIRegion")) + return + } + hcs, err := iRegion.GetILoadBalancerHealthChecks() + if err != nil { + self.taskFail(ctx, hc, errors.Wrapf(err, "GetLoadbalancerHealthChecks")) + return + } + for i := range hcs { + if hcs[i].GetGlobalId() == hc.ExternalId { + err := hc.SyncWithCloudLoadbalancerHealthCheck(ctx, self.GetUserCred(), hcs[i], hc.GetCloudprovider()) + if err != nil { + self.taskFail(ctx, hc, errors.Wrapf(err, "SyncWithCloudLoadbalancerHealthCheck")) + return + } + self.taskComplete(ctx, hc) + return + } + } + + self.taskFail(ctx, hc, errors.Wrapf(cloudprovider.ErrNotFound, "LoadbalancerHealthCheck not found")) +} + +func (self *LoadbalancerHealthCheckSyncstatusTask) taskComplete(ctx context.Context, hc *models.SLoadbalancerHealthCheck) { + hc.SetStatus(ctx, self.GetUserCred(), apis.STATUS_AVAILABLE, "") + self.SetStageComplete(ctx, nil) +}