func()

in pkg/provider/alibaba/alb/server_group.go [132:246]


func (m *ALBProvider) updateServerGroupAttribute(ctx context.Context, resSGP *alb.ServerGroup, sdkSGP *albsdk.ServerGroup) (*albsdk.UpdateServerGroupAttributeResponse, error) {
	traceID := ctx.Value(util.TraceID)

	var (
		isHealthCheckConfigNeedUpdate,
		isStickySessionConfigNeedUpdate,
		isServerGroupNameNeedUpdate,
		isSchedulerNeedUpdate bool
	)
	if resSGP.Spec.ServerGroupName != sdkSGP.ServerGroupName {
		m.logger.V(util.MgrLogLevel).Info("ServerGroupName update:",
			"res", resSGP.Spec.ServerGroupName,
			"sdk", sdkSGP.ServerGroupName,
			"serverGroupID", sdkSGP.ServerGroupId,
			"traceID", traceID)
		isServerGroupNameNeedUpdate = true
	}
	if !isServerGroupSchedulerValid(resSGP.Spec.Scheduler) {
		return nil, fmt.Errorf("invalid server group scheduler: %s", resSGP.Spec.Scheduler)
	}
	if !strings.EqualFold(resSGP.Spec.Scheduler, sdkSGP.Scheduler) {
		m.logger.V(util.MgrLogLevel).Info("Scheduler update:",
			"res", resSGP.Spec.Scheduler,
			"sdk", sdkSGP.Scheduler,
			"serverGroupID", sdkSGP.ServerGroupId,
			"traceID", traceID)
		isSchedulerNeedUpdate = true
	}

	if err := checkHealthCheckConfigValid(resSGP.Spec.HealthCheckConfig); err != nil {
		return nil, err
	}
	if resSGP.Spec.HealthCheckConfig.HealthCheckEnabled {
		if !reflect.DeepEqual(resSGP.Spec.HealthCheckConfig, sdkSGP.HealthCheckConfig) {
			m.logger.V(util.MgrLogLevel).Info("HealthCheckConfig update:",
				"res", resSGP.Spec.HealthCheckConfig,
				"sdk", sdkSGP.HealthCheckConfig,
				"serverGroupID", sdkSGP.ServerGroupId,
				"traceID", traceID)
			isHealthCheckConfigNeedUpdate = true
		}
	} else if !resSGP.Spec.HealthCheckConfig.HealthCheckEnabled && sdkSGP.HealthCheckConfig.HealthCheckEnabled {
		m.logger.V(util.MgrLogLevel).Info("HealthCheckConfig update:",
			"res", resSGP.Spec.HealthCheckConfig,
			"sdk", sdkSGP.HealthCheckConfig,
			"serverGroupID", sdkSGP.ServerGroupId,
			"traceID", traceID)
		isHealthCheckConfigNeedUpdate = true
	}

	if err := checkStickySessionConfigValid(resSGP.Spec.StickySessionConfig); err != nil {
		return nil, err
	}
	if resSGP.Spec.StickySessionConfig.StickySessionEnabled {
		if !reflect.DeepEqual(resSGP.Spec.StickySessionConfig, sdkSGP.StickySessionConfig) {
			m.logger.V(util.MgrLogLevel).Info("StickySessionConfig update:",
				"res", resSGP.Spec.StickySessionConfig,
				"sdk", sdkSGP.StickySessionConfig,
				"serverGroupID", sdkSGP.ServerGroupId,
				"traceID", traceID)
			isStickySessionConfigNeedUpdate = true
		}
	} else if !resSGP.Spec.StickySessionConfig.StickySessionEnabled && sdkSGP.StickySessionConfig.StickySessionEnabled {
		m.logger.V(util.MgrLogLevel).Info("StickySessionConfig update:",
			"res", resSGP.Spec.StickySessionConfig,
			"sdk", sdkSGP.StickySessionConfig,
			"serverGroupID", sdkSGP.ServerGroupId,
			"traceID", traceID)
		isStickySessionConfigNeedUpdate = true
	}

	if !isServerGroupNameNeedUpdate && !isSchedulerNeedUpdate &&
		!isHealthCheckConfigNeedUpdate && !isStickySessionConfigNeedUpdate {
		return nil, nil
	}

	updateSgpReq := albsdk.CreateUpdateServerGroupAttributeRequest()
	updateSgpReq.ServerGroupId = sdkSGP.ServerGroupId

	if isServerGroupNameNeedUpdate {
		updateSgpReq.ServerGroupName = resSGP.Spec.ServerGroupName
	}
	if isSchedulerNeedUpdate {
		updateSgpReq.Scheduler = resSGP.Spec.Scheduler
	}
	if isHealthCheckConfigNeedUpdate {
		updateSgpReq.HealthCheckConfig = *transSDKHealthCheckConfigToUpdateSGP(resSGP.Spec.HealthCheckConfig)
	}
	if isStickySessionConfigNeedUpdate {
		updateSgpReq.StickySessionConfig = *transSDKStickySessionConfigToUpdateSGP(resSGP.Spec.StickySessionConfig)
	}

	startTime := time.Now()
	m.logger.V(util.MgrLogLevel).Info("updating server group attribute",
		"stackID", resSGP.Stack().StackID(),
		"resourceID", resSGP.ID(),
		"traceID", traceID,
		"serverGroupID", sdkSGP.ServerGroupId,
		"startTime", startTime,
		util.Action, util.UpdateALBServerGroupAttribute)
	updateSgpResp, err := m.auth.ALB.UpdateServerGroupAttribute(updateSgpReq)
	if err != nil {
		return nil, err
	}
	m.logger.V(util.MgrLogLevel).Info("updated server group attribute",
		"stackID", resSGP.Stack().StackID(),
		"resourceID", resSGP.ID(),
		"traceID", traceID,
		"serverGroupID", sdkSGP.ServerGroupId,
		"requestID", updateSgpResp.RequestId,
		"elapsedTime", time.Since(startTime).Milliseconds(),
		util.Action, util.UpdateALBServerGroupAttribute)

	return updateSgpResp, nil
}