Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/tutorials/istio.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,34 @@ transfer-encoding: chunked

**Note:** The `-H` flag in the original Istio tutorial is no longer necessary in the `curl` commands.

### Optional Gateway Annotation

To support setups where an Ingress resource is used provision an external LB you can add the following annotation to your Gateway

**Note:** The Ingress namespace can be omitted if its in the same namespace as the gateway

```bash
$ cat <<EOF | kubectl apply -f -
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: httpbin-gateway
namespace: istio-system
annotations:
"external-dns.alpha.kubernetes.io/ingress": "$ingressNamespace/$ingressName"
spec:
selector:
istio: ingressgateway # use Istio default gateway implementation
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "*"
EOF
```

### Debug ExternalDNS

* Look for the deployment pod to see the status
Expand Down
55 changes: 51 additions & 4 deletions source/istio_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ import (
"sigs.k8s.io/external-dns/endpoint"
)

// IstioGatewayIngressSource is the annotation used to determine if the gateway is implemented by an Ingress object
// instead of a standard LoadBalancer service type
const IstioGatewayIngressSource = "external-dns.alpha.kubernetes.io/ingress"

// gatewaySource is an implementation of Source for Istio Gateway objects.
// The gateway implementation uses the spec.servers.hosts values for the hostnames.
// Use targetAnnotationKey to explicitly set Endpoint.
Expand Down Expand Up @@ -166,7 +170,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
}
Expand Down Expand Up @@ -232,12 +236,55 @@ func (sc *gatewaySource) setResourceLabel(gateway *networkingv1alpha3.Gateway, e
}
}

func (sc *gatewaySource) targetsFromGateway(gateway *networkingv1alpha3.Gateway) (targets endpoint.Targets, err error) {
func parseIngress(ingress string) (namespace, name string, err error) {
parts := strings.Split(ingress, "/")
if len(parts) == 2 {
namespace, name = parts[0], parts[1]
} else if len(parts) == 1 {
name = parts[0]
} else {
err = fmt.Errorf("invalid ingress name (name or namespace/name) found %q", ingress)
}

return
}

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 {
return nil, fmt.Errorf("failed to parse Ingress annotation on Gateway (%s/%s): %w", gateway.Namespace, gateway.Name, 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, ok := gateway.Annotations[IstioGatewayIngressSource]
if ok && 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)
Expand All @@ -262,7 +309,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
Expand All @@ -274,7 +321,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
}
Expand Down
Loading