Skip to content

Commit

Permalink
Add istio-virtualservice source
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredkrohmer committed Jan 9, 2020
1 parent 2ec94f8 commit 7613f95
Show file tree
Hide file tree
Showing 6 changed files with 1,674 additions and 32 deletions.
2 changes: 1 addition & 1 deletion pkg/apis/externaldns/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func (cfg *Config) ParseFlags(args []string) error {
app.Flag("contour-load-balancer", "The fully-qualified name of the Contour load balancer service. (default: heptio-contour/contour)").Default("heptio-contour/contour").StringVar(&cfg.ContourLoadBalancerService)

// Flags related to processing sources
app.Flag("source", "The resource types that are queried for endpoints; specify multiple times for multiple sources (required, options: service, ingress, node, fake, connector, istio-gateway, cloudfoundry, contour-ingressroute, crd, empty)").Required().PlaceHolder("source").EnumsVar(&cfg.Sources, "service", "ingress", "node", "istio-gateway", "cloudfoundry", "contour-ingressroute", "fake", "connector", "crd", "empty")
app.Flag("source", "The resource types that are queried for endpoints; specify multiple times for multiple sources (required, options: service, ingress, node, fake, connector, istio-gateway, istio-virtualservice, cloudfoundry, contour-ingressroute, crd, empty)").Required().PlaceHolder("source").EnumsVar(&cfg.Sources, "service", "ingress", "node", "istio-gateway", "istio-virtualservice", "cloudfoundry", "contour-ingressroute", "fake", "connector", "crd", "empty")
app.Flag("namespace", "Limit sources of endpoints to a specific namespace (default: all namespaces)").Default(defaultConfig.Namespace).StringVar(&cfg.Namespace)
app.Flag("annotation-filter", "Filter sources managed by external-dns via annotation using label selector semantics (default: all sources)").Default(defaultConfig.AnnotationFilter).StringVar(&cfg.AnnotationFilter)
app.Flag("fqdn-template", "A templated string that's used to generate DNS names from sources that don't define a hostname themselves, or to add a hostname suffix when paired with the fake source (optional). Accepts comma separated list for multiple global FQDN.").Default(defaultConfig.FQDNTemplate).StringVar(&cfg.FQDNTemplate)
Expand Down
35 changes: 16 additions & 19 deletions source/gateway.go → source/istio_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ import (
"sigs.k8s.io/external-dns/endpoint"
)

// gatewaySource is an implementation of Source for Istio Gateway objects.
// istioGatewaySource 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.
type gatewaySource struct {
type istioGatewaySource struct {
kubeClient kubernetes.Interface
istioClient istiomodel.ConfigStore
namespace string
Expand All @@ -52,7 +52,7 @@ type gatewaySource struct {
serviceInformer coreinformers.ServiceInformer
}

// NewIstioGatewaySource creates a new gatewaySource with the given config.
// NewIstioGatewaySource creates a new istioGatewaySource with the given config.
func NewIstioGatewaySource(
kubeClient kubernetes.Interface,
istioClient istiomodel.ConfigStore,
Expand Down Expand Up @@ -101,7 +101,7 @@ func NewIstioGatewaySource(
return nil, fmt.Errorf("failed to sync cache: %v", err)
}

return &gatewaySource{
return &istioGatewaySource{
kubeClient: kubeClient,
istioClient: istioClient,
namespace: namespace,
Expand All @@ -115,7 +115,7 @@ func NewIstioGatewaySource(

// Endpoints returns endpoint objects for each host-target combination that should be processed.
// Retrieves all gateway resources in the source's namespace(s).
func (sc *gatewaySource) Endpoints() ([]*endpoint.Endpoint, error) {
func (sc *istioGatewaySource) Endpoints() ([]*endpoint.Endpoint, error) {
configs, err := sc.istioClient.List(istiomodel.Gateway.Type, sc.namespace)
if err != nil {
return nil, err
Expand Down Expand Up @@ -173,7 +173,7 @@ func (sc *gatewaySource) Endpoints() ([]*endpoint.Endpoint, error) {
return endpoints, nil
}

func (sc *gatewaySource) endpointsFromTemplate(config *istiomodel.Config) ([]*endpoint.Endpoint, error) {
func (sc *istioGatewaySource) endpointsFromTemplate(config *istiomodel.Config) ([]*endpoint.Endpoint, error) {
// Process the whole template string
var buf bytes.Buffer
err := sc.fqdnTemplate.Execute(&buf, config)
Expand Down Expand Up @@ -210,7 +210,7 @@ func (sc *gatewaySource) endpointsFromTemplate(config *istiomodel.Config) ([]*en
}

// filterByAnnotations filters a list of configs by a given annotation selector.
func (sc *gatewaySource) filterByAnnotations(configs []istiomodel.Config) ([]istiomodel.Config, error) {
func (sc *istioGatewaySource) filterByAnnotations(configs []istiomodel.Config) ([]istiomodel.Config, error) {
labelSelector, err := metav1.ParseToLabelSelector(sc.annotationFilter)
if err != nil {
return nil, err
Expand Down Expand Up @@ -240,30 +240,27 @@ func (sc *gatewaySource) filterByAnnotations(configs []istiomodel.Config) ([]ist
return filteredList, nil
}

func (sc *gatewaySource) setResourceLabel(config istiomodel.Config, endpoints []*endpoint.Endpoint) {
func (sc *istioGatewaySource) setResourceLabel(config istiomodel.Config, endpoints []*endpoint.Endpoint) {
for _, ep := range endpoints {
ep.Labels[endpoint.ResourceLabelKey] = fmt.Sprintf("gateway/%s/%s", config.Namespace, config.Name)
}
}

func (sc *gatewaySource) targetsFromGatewayConfig(config *istiomodel.Config) (targets endpoint.Targets, err error) {
func (sc *istioGatewaySource) targetsFromGatewayConfig(config *istiomodel.Config) (targets endpoint.Targets, err error) {
gateway := config.Spec.(*istionetworking.Gateway)
labelSelector, err := metav1.ParseToLabelSelector(labels.Set(gateway.Selector).String())
if err != nil {
return nil, err
}
selector, err := metav1.LabelSelectorAsSelector(labelSelector)
if err != nil {
return nil, err
}

services, err := sc.serviceInformer.Lister().Services(sc.namespace).List(selector)
services, err := sc.serviceInformer.Lister().Services(config.Namespace).List(labels.Everything())
if err != nil {
log.Error(err)
return
}

for _, service := range services {
if !labels.Equals(service.Spec.Selector, gateway.Selector) {
// Only consider services which have the same selector as the Gateway CR.
// Use the target annotation to override if this picks the wrong service.
continue
}
for _, lb := range service.Status.LoadBalancer.Ingress {
if lb.IP != "" {
targets = append(targets, lb.IP)
Expand All @@ -278,7 +275,7 @@ func (sc *gatewaySource) targetsFromGatewayConfig(config *istiomodel.Config) (ta
}

// endpointsFromGatewayConfig extracts the endpoints from an Istio Gateway Config object
func (sc *gatewaySource) endpointsFromGatewayConfig(config istiomodel.Config) ([]*endpoint.Endpoint, error) {
func (sc *istioGatewaySource) endpointsFromGatewayConfig(config istiomodel.Config) ([]*endpoint.Endpoint, error) {
var endpoints []*endpoint.Endpoint

ttl, err := getTTLFromAnnotations(config.Annotations)
Expand Down
Loading

0 comments on commit 7613f95

Please sign in to comment.