func()

in internal/pkg/deploy/cloudformation/stack/transformers.go [256:345]


func (s *LoadBalancedWebService) convertNetworkLoadBalancer() (networkLoadBalancerConfig, error) {
	nlbConfig := s.manifest.NLBConfig
	if nlbConfig.IsEmpty() {
		return networkLoadBalancerConfig{}, nil
	}

	// Parse listener port and protocol.
	port, protocol, err := manifest.ParsePortMapping(nlbConfig.Port)
	if err != nil {
		return networkLoadBalancerConfig{}, err
	}
	if protocol == nil {
		protocol = aws.String(defaultNLBProtocol)
	}

	// Configure target container and port.
	targetContainer := s.name
	if nlbConfig.TargetContainer != nil {
		targetContainer = aws.StringValue(nlbConfig.TargetContainer)
	}

	// By default, the target port is the same as listener port.
	targetPort := aws.StringValue(port)
	if targetContainer != s.name {
		// If the target container is a sidecar container, the target port is the exposed sidecar port.
		sideCarPort := s.manifest.Sidecars[targetContainer].Port // We validated that a sidecar container exposes a port if it is a target container.
		port, _, err := manifest.ParsePortMapping(sideCarPort)
		if err != nil {
			return networkLoadBalancerConfig{}, err
		}
		targetPort = aws.StringValue(port)
	}
	// Finally, if a target port is explicitly specified, use that value.
	if nlbConfig.TargetPort != nil {
		targetPort = strconv.Itoa(aws.IntValue(nlbConfig.TargetPort))
	}

	aliases, err := convertAlias(nlbConfig.Aliases)
	if err != nil {
		return networkLoadBalancerConfig{}, fmt.Errorf(`convert "nlb.alias" to string slice: %w`, err)
	}

	hc := template.NLBHealthCheck{
		HealthyThreshold:   nlbConfig.HealthCheck.HealthyThreshold,
		UnhealthyThreshold: nlbConfig.HealthCheck.UnhealthyThreshold,
	}
	if nlbConfig.HealthCheck.Port != nil {
		hc.Port = strconv.Itoa(aws.IntValue(nlbConfig.HealthCheck.Port))
	}
	if nlbConfig.HealthCheck.Timeout != nil {
		hc.Timeout = aws.Int64(int64(nlbConfig.HealthCheck.Timeout.Seconds()))
	}
	if nlbConfig.HealthCheck.Interval != nil {
		hc.Interval = aws.Int64(int64(nlbConfig.HealthCheck.Interval.Seconds()))
	}
	config := networkLoadBalancerConfig{
		settings: &template.NetworkLoadBalancer{
			PublicSubnetCIDRs: s.publicSubnetCIDRBlocks,
			Listener: template.NetworkLoadBalancerListener{
				Port:            aws.StringValue(port),
				Protocol:        strings.ToUpper(aws.StringValue(protocol)),
				TargetContainer: targetContainer,
				TargetPort:      targetPort,
				SSLPolicy:       nlbConfig.SSLPolicy,
				Aliases:         aliases,
				HealthCheck:     hc,
				Stickiness:      nlbConfig.Stickiness,
			},
			MainContainerPort: s.containerPort(),
		},
	}

	if s.dnsDelegationEnabled {
		dnsDelegationRole, dnsName := convertAppInformation(s.appInfo)
		config.appDNSName = dnsName
		config.appDNSDelegationRole = dnsDelegationRole

		nlbCertValidatorLambda, err := s.parser.Read(nlbCertValidatorPath)
		if err != nil {
			return networkLoadBalancerConfig{}, fmt.Errorf("read nlb certificate validator lambda: %w", err)
		}
		nlbCustomDomainLambda, err := s.parser.Read(nlbCustomDomainPath)
		if err != nil {
			return networkLoadBalancerConfig{}, fmt.Errorf("read nlb custom domain lambda: %w", err)
		}
		config.certValidatorLambda = nlbCertValidatorLambda.String()
		config.customDomainLambda = nlbCustomDomainLambda.String()
	}
	return config, nil
}