func()

in pkg/controller/ingress/alb_controller.go [167:240]


func (g *albconfigReconciler) syncIngress(obj interface{}) error {
	g.logger.Info("start syncIngress")
	traceID := sdkutils.GetUUID()
	ctx := context.WithValue(context.Background(), util.TraceID, traceID)
	e := obj.(k8s.Element)
	evt := e.Event
	ings := g.store.ListIngresses()

	if len(ings) == 0 {
		g.logger.Info("ingress length: 0, skip")
		return nil
	}
	if evt.Type == helper.EndPointEvent || evt.Type == helper.NodeEvent || evt.Type == helper.ServiceEvent {
		g.syncServersQueue.EnqueueSkippableTask(evt)
		return nil
	}
	for _, ing := range ings {
		g.logger.Info("start ingress: %s", ing.Name)
		var (
			groupIDNew *albconfigmanager.GroupID
		)
		groupIDNew, _ = g.groupLoader.LoadGroupID(ctx, &ing.Ingress)
		var groupInChan = func(groupID *albconfigmanager.GroupID) {
			albconfig := &v1.AlbConfig{}
			if err := g.k8sClient.Get(ctx, types.NamespacedName{
				Namespace: groupID.Namespace,
				Name:      groupID.Name,
			}, albconfig); err != nil {
				if errors.IsNotFound(err) {
					err = g.k8sClient.Create(ctx, g.makeAlbConfig(ctx, groupID.Name, &ing.Ingress), &client.CreateOptions{})
					if err != nil {
						g.logger.Error(err, "Create albconfig failed", "albconfig", ing)
						return
					}
					return
				}
				g.logger.Error(err, "get albconfig failed", "albconfig", ing)
				return
			}
			lss := make([]*v1.ListenerSpec, 0)
			ingListByPort := make(map[int32]albconfigmanager.Protocol, 0)
			ingGroup, _ := g.groupLoader.Load(ctx, *groupID, ings)
			if ingGroup.Members != nil && len(ingGroup.Members) > 0 {
				for _, ingm := range ingGroup.Members {
					portAndProtocol, _ := albconfigmanager.ComputeIngressListenPorts(ingm)
					for port, pro := range portAndProtocol {
						ingListByPort[port] = pro
					}
				}
			}
			for k, v := range ingListByPort {
				ls := &v1.ListenerSpec{
					Port:     intstr.FromInt(int(k)),
					Protocol: string(v),
				}
				lss = append(lss, ls)
			}
			albconfig.Spec.Listeners = lss
			err := g.k8sClient.Update(ctx, albconfig, &client.UpdateOptions{})
			if err != nil {
				g.logger.Error(err, "Update albconfig")
				return
			}
			g.acEventChan <- event.GenericEvent{
				Object: albconfig,
			}
		}

		groupInChan(groupIDNew)

	}

	return nil
}