add istio gateway ingress annotation support to gateway source

This commit is contained in:
David Pait 2023-08-08 08:05:42 -04:00
parent 362b233833
commit cb2772c6ae

View File

@ -166,7 +166,7 @@ func (sc *gatewaySource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, e
continue
}
gwEndpoints, err := sc.endpointsFromGateway(gwHostnames, gateway)
gwEndpoints, err := sc.endpointsFromGateway(ctx, gwHostnames, gateway)
if err != nil {
return nil, err
}
@ -232,12 +232,43 @@ func (sc *gatewaySource) setResourceLabel(gateway *networkingv1alpha3.Gateway, e
}
}
func (sc *gatewaySource) targetsFromGateway(gateway *networkingv1alpha3.Gateway) (targets endpoint.Targets, err error) {
func (sc *gatewaySource) targetsFromIngress(ctx context.Context, ingressStr string, gateway *networkingv1alpha3.Gateway) (targets endpoint.Targets, err error) {
namespace, name, err := parseIngress(ingressStr)
if err != nil {
log.Debugf("Failed parsing ingressStr %s of Gateway %s/%s", ingressStr, gateway.Namespace, gateway.Name)
return nil, err
}
if namespace == "" {
namespace = gateway.Namespace
}
ingress, err := sc.kubeClient.NetworkingV1().Ingresses(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
log.Error(err)
return
}
for _, lb := range ingress.Status.LoadBalancer.Ingress {
if lb.IP != "" {
targets = append(targets, lb.IP)
} else if lb.Hostname != "" {
targets = append(targets, lb.Hostname)
}
}
return
}
func (sc *gatewaySource) targetsFromGateway(ctx context.Context, gateway *networkingv1alpha3.Gateway) (targets endpoint.Targets, err error) {
targets = getTargetsFromTargetAnnotation(gateway.Annotations)
if len(targets) > 0 {
return
}
ingressStr, found := gateway.Annotations[IstioIngressBackedGateway]
if found && ingressStr != "" {
targets, err = sc.targetsFromIngress(ctx, ingressStr, gateway)
return
}
services, err := sc.serviceInformer.Lister().Services(sc.namespace).List(labels.Everything())
if err != nil {
log.Error(err)
@ -262,7 +293,7 @@ func (sc *gatewaySource) targetsFromGateway(gateway *networkingv1alpha3.Gateway)
}
// endpointsFromGatewayConfig extracts the endpoints from an Istio Gateway Config object
func (sc *gatewaySource) endpointsFromGateway(hostnames []string, gateway *networkingv1alpha3.Gateway) ([]*endpoint.Endpoint, error) {
func (sc *gatewaySource) endpointsFromGateway(ctx context.Context, hostnames []string, gateway *networkingv1alpha3.Gateway) ([]*endpoint.Endpoint, error) {
var endpoints []*endpoint.Endpoint
annotations := gateway.Annotations
@ -274,7 +305,7 @@ func (sc *gatewaySource) endpointsFromGateway(hostnames []string, gateway *netwo
targets := getTargetsFromTargetAnnotation(annotations)
if len(targets) == 0 {
targets, err = sc.targetsFromGateway(gateway)
targets, err = sc.targetsFromGateway(ctx, gateway)
if err != nil {
return nil, err
}