func()

in pkg/gateway/model_build_targetgroup.go [287:359]


func (t *backendRefTargetGroupModelBuildTask) buildTargetGroupSpec(ctx context.Context) (model.TargetGroupSpec, error) {
	// note we only build target groups for backendRefs on non-deleted routes
	backendKind := string(*t.backendRef.Kind())
	t.log.Debugf(ctx, "buildTargetGroupSpec, kind %s", backendKind)

	vpc := config.VpcID
	eksCluster := config.ClusterName
	backendRefNsName := getBackendRefNsName(t.route, t.backendRef)
	svc := &corev1.Service{}
	if err := t.client.Get(ctx, backendRefNsName, svc); err != nil {
		if apierrors.IsNotFound(err) {
			return model.TargetGroupSpec{}, &InvalidBackendRefError{
				BackendRef: t.backendRef,
				Reason:     fmt.Sprintf("service %s on route %s not found, backendRef invalid", backendRefNsName.Name, t.route.Name()),
			}
		} else {
			return model.TargetGroupSpec{},
				fmt.Errorf("error finding backend service %s due to %s", backendRefNsName, err)
		}
	}

	var err error
	ipAddressType, err := buildTargetGroupIpAddressType(svc)
	if err != nil {
		return model.TargetGroupSpec{}, err
	}

	tgp, err := t.tgp.ObjResolvedPolicy(ctx, svc)
	if err != nil {
		return model.TargetGroupSpec{}, err
	}

	protocol, protocolVersion, healthCheckConfig, err := parseTargetGroupConfig(tgp)
	if err != nil {
		return model.TargetGroupSpec{}, err
	}

	var parentRefType model.K8SSourceType
	switch t.route.(type) {
	case *core.HTTPRoute:
		parentRefType = model.SourceTypeHTTPRoute
	case *core.GRPCRoute:
		// protocolVersion:GRPC takes precedence over other protocolVersions for k8s svc backendref by GRPCRoutes
		protocolVersion = vpclattice.TargetGroupProtocolVersionGrpc
		parentRefType = model.SourceTypeGRPCRoute
	case *core.TLSRoute:
		// protocol:TCP takes precedence over other protocol for k8s svc backendref by TLSRoutes
		protocol = vpclattice.TargetGroupProtocolTcp
		protocolVersion = ""
		parentRefType = model.SourceTypeTLSRoute
	default:
		return model.TargetGroupSpec{}, fmt.Errorf("unsupported route type %T", t.route)
	}

	spec := model.TargetGroupSpec{
		Type:              model.TargetGroupTypeIP,
		Port:              80,
		Protocol:          protocol,
		ProtocolVersion:   protocolVersion,
		IpAddressType:     ipAddressType,
		HealthCheckConfig: healthCheckConfig,
	}
	spec.VpcId = vpc
	spec.K8SSourceType = parentRefType
	spec.K8SClusterName = eksCluster
	spec.K8SServiceName = backendRefNsName.Name
	spec.K8SServiceNamespace = backendRefNsName.Namespace
	spec.K8SRouteName = t.route.Name()
	spec.K8SRouteNamespace = t.route.Namespace()
	spec.K8SProtocolVersion = protocolVersion

	return spec, nil
}