func()

in pkg/skoop/network/aliyun/assertion.go [663:727]


func (a *slbAssertion) assertServerGroup(sgID string, backends []network.LoadBalancerBackend) ([]model.Suspicion, error) {
	sg, err := a.cloudManager.GetSLBVserverGroup(sgID)
	if err != nil {
		return nil, err
	}
	if sg == nil {
		return nil, fmt.Errorf("cannot find vserver group %q", sgID)
	}

	var sus []model.Suspicion
	if len(sg.BackendServers.BackendServer) == 0 {
		sus = append(sus, model.Suspicion{
			Level:   model.SuspicionLevelFatal,
			Message: fmt.Sprintf("server group %q has no backend", sgID),
		})
	}

	if len(sg.BackendServers.BackendServer) != len(backends) {
		sus = append(sus, model.Suspicion{
			Level: model.SuspicionLevelWarning,
			Message: fmt.Sprintf("backend count %d is not equal to expected backend count %d",
				len(sg.BackendServers.BackendServer), len(backends)),
		})
	}

	for _, s := range sg.BackendServers.BackendServer {
		contains := lo.ContainsBy(backends, func(b network.LoadBalancerBackend) bool {
			if *s.Port != int32(b.Port) {
				return false
			}
			if s.ServerIp != nil && b.IP != "" {
				return *s.ServerIp == b.IP
			}
			if s.ServerId != nil && b.ID != "" {
				return *s.ServerId == b.ID
			}
			// If we're unable to find backend by the given information,
			// return true to prevent suspicions.
			return true
		})

		if !contains {
			backend := ""
			if s.ServerIp != nil {
				backend = fmt.Sprintf("%s:%d", *s.ServerIp, *s.Port)
			} else if s.ServerId != nil {
				backend = fmt.Sprintf("%s:%d", *s.ServerId, *s.Port)
			} else {
				backend = fmt.Sprintf("(unknown):%d", *s.Port)
			}

			sus = append(sus, model.Suspicion{
				Level: model.SuspicionLevelWarning,
				Message: fmt.Sprintf("backend %q in server group %q is unexpected",
					backend, sgID),
			})
		}
		// limit suspicions count to not exceed 5
		if len(sus) > 5 {
			break
		}
	}

	return sus, nil
}