Add filter by service type feature

This commit is contained in:
Devatoria 2018-07-31 11:05:19 -04:00
parent 59210df9d4
commit 05068e3ad3
5 changed files with 127 additions and 7 deletions

View File

@ -75,6 +75,7 @@ func main() {
PublishInternal: cfg.PublishInternal, PublishInternal: cfg.PublishInternal,
PublishHostIP: cfg.PublishHostIP, PublishHostIP: cfg.PublishHostIP,
ConnectorServer: cfg.ConnectorSourceServer, ConnectorServer: cfg.ConnectorSourceServer,
ServiceTypeFilter: cfg.ServiceTypeFilter,
} }
// Lookup all the selected sources by names and pass them the desired configuration. // Lookup all the selected sources by names and pass them the desired configuration.

View File

@ -91,6 +91,7 @@ type Config struct {
ExoscaleEndpoint string ExoscaleEndpoint string
ExoscaleAPIKey string ExoscaleAPIKey string
ExoscaleAPISecret string ExoscaleAPISecret string
ServiceTypeFilter []string
} }
var defaultConfig = &Config{ var defaultConfig = &Config{
@ -144,6 +145,7 @@ var defaultConfig = &Config{
ExoscaleEndpoint: "https://api.exoscale.ch/dns", ExoscaleEndpoint: "https://api.exoscale.ch/dns",
ExoscaleAPIKey: "", ExoscaleAPIKey: "",
ExoscaleAPISecret: "", ExoscaleAPISecret: "",
ServiceTypeFilter: []string{},
} }
// NewConfig returns new Config object // NewConfig returns new Config object
@ -197,6 +199,7 @@ func (cfg *Config) ParseFlags(args []string) error {
app.Flag("publish-internal-services", "Allow external-dns to publish DNS records for ClusterIP services (optional)").BoolVar(&cfg.PublishInternal) app.Flag("publish-internal-services", "Allow external-dns to publish DNS records for ClusterIP services (optional)").BoolVar(&cfg.PublishInternal)
app.Flag("publish-host-ip", "Allow external-dns to publish host-ip for headless services (optional)").BoolVar(&cfg.PublishHostIP) app.Flag("publish-host-ip", "Allow external-dns to publish host-ip for headless services (optional)").BoolVar(&cfg.PublishHostIP)
app.Flag("connector-source-server", "The server to connect for connector source, valid only when using connector source").Default(defaultConfig.ConnectorSourceServer).StringVar(&cfg.ConnectorSourceServer) app.Flag("connector-source-server", "The server to connect for connector source, valid only when using connector source").Default(defaultConfig.ConnectorSourceServer).StringVar(&cfg.ConnectorSourceServer)
app.Flag("service-type-filter", "The service types to take care about (default: all, expected: ClusterIP, NodePort, LoadBalancer or ExternalName)").StringsVar(&cfg.ServiceTypeFilter)
// Flags related to providers // Flags related to providers
app.Flag("provider", "The DNS provider where the DNS records will be created (required, options: aws, aws-sd, google, azure, cloudflare, digitalocean, dnsimple, infoblox, dyn, designate, coredns, skydns, inmemory, pdns, oci, exoscale, linode)").Required().PlaceHolder("provider").EnumVar(&cfg.Provider, "aws", "aws-sd", "google", "azure", "cloudflare", "digitalocean", "dnsimple", "infoblox", "dyn", "designate", "coredns", "skydns", "inmemory", "pdns", "oci", "exoscale", "linode") app.Flag("provider", "The DNS provider where the DNS records will be created (required, options: aws, aws-sd, google, azure, cloudflare, digitalocean, dnsimple, infoblox, dyn, designate, coredns, skydns, inmemory, pdns, oci, exoscale, linode)").Required().PlaceHolder("provider").EnumVar(&cfg.Provider, "aws", "aws-sd", "google", "azure", "cloudflare", "digitalocean", "dnsimple", "infoblox", "dyn", "designate", "coredns", "skydns", "inmemory", "pdns", "oci", "exoscale", "linode")

View File

@ -52,10 +52,11 @@ type serviceSource struct {
combineFQDNAnnotation bool combineFQDNAnnotation bool
publishInternal bool publishInternal bool
publishHostIP bool publishHostIP bool
serviceTypeFilter map[string]struct{}
} }
// NewServiceSource creates a new serviceSource with the given config. // NewServiceSource creates a new serviceSource with the given config.
func NewServiceSource(kubeClient kubernetes.Interface, namespace, annotationFilter string, fqdnTemplate string, combineFqdnAnnotation bool, compatibility string, publishInternal bool, publishHostIP bool) (Source, error) { func NewServiceSource(kubeClient kubernetes.Interface, namespace, annotationFilter string, fqdnTemplate string, combineFqdnAnnotation bool, compatibility string, publishInternal bool, publishHostIP bool, serviceTypeFilter []string) (Source, error) {
var ( var (
tmpl *template.Template tmpl *template.Template
err error err error
@ -69,6 +70,13 @@ func NewServiceSource(kubeClient kubernetes.Interface, namespace, annotationFilt
} }
} }
// Transform the slice into a map so it will
// be way much easier and fast to filter later
serviceTypes := make(map[string]struct{})
for _, serviceType := range serviceTypeFilter {
serviceTypes[serviceType] = struct{}{}
}
return &serviceSource{ return &serviceSource{
client: kubeClient, client: kubeClient,
namespace: namespace, namespace: namespace,
@ -78,6 +86,7 @@ func NewServiceSource(kubeClient kubernetes.Interface, namespace, annotationFilt
combineFQDNAnnotation: combineFqdnAnnotation, combineFQDNAnnotation: combineFqdnAnnotation,
publishInternal: publishInternal, publishInternal: publishInternal,
publishHostIP: publishHostIP, publishHostIP: publishHostIP,
serviceTypeFilter: serviceTypes,
}, nil }, nil
} }
@ -92,6 +101,11 @@ func (sc *serviceSource) Endpoints() ([]*endpoint.Endpoint, error) {
return nil, err return nil, err
} }
// filter on service types if at least one has been provided
if len(sc.serviceTypeFilter) > 0 {
services.Items = sc.filterByServiceType(services.Items)
}
// get the ip addresses of all the nodes and cache them for this run // get the ip addresses of all the nodes and cache them for this run
nodeTargets, err := sc.extractNodeTargets() nodeTargets, err := sc.extractNodeTargets()
if err != nil { if err != nil {
@ -254,6 +268,19 @@ func (sc *serviceSource) filterByAnnotations(services []v1.Service) ([]v1.Servic
return filteredList, nil return filteredList, nil
} }
// filterByServiceType filters services according their types
func (sc *serviceSource) filterByServiceType(services []v1.Service) []v1.Service {
filteredList := []v1.Service{}
for _, service := range services {
// Check if the service is of the given type or not
if _, ok := sc.serviceTypeFilter[string(service.Spec.Type)]; ok {
filteredList = append(filteredList, service)
}
}
return filteredList
}
func (sc *serviceSource) setResourceLabel(service v1.Service, endpoints []*endpoint.Endpoint) { func (sc *serviceSource) setResourceLabel(service v1.Service, endpoints []*endpoint.Endpoint) {
for _, ep := range endpoints { for _, ep := range endpoints {
ep.Labels[endpoint.ResourceLabelKey] = fmt.Sprintf("service/%s/%s", service.Namespace, service.Name) ep.Labels[endpoint.ResourceLabelKey] = fmt.Sprintf("service/%s/%s", service.Namespace, service.Name)

View File

@ -50,6 +50,7 @@ func (suite *ServiceSuite) SetupTest() {
"", "",
false, false,
false, false,
[]string{},
) )
suite.fooWithTargets = &v1.Service{ suite.fooWithTargets = &v1.Service{
Spec: v1.ServiceSpec{ Spec: v1.ServiceSpec{
@ -99,10 +100,11 @@ func testServiceSourceImplementsSource(t *testing.T) {
// testServiceSourceNewServiceSource tests that NewServiceSource doesn't return an error. // testServiceSourceNewServiceSource tests that NewServiceSource doesn't return an error.
func testServiceSourceNewServiceSource(t *testing.T) { func testServiceSourceNewServiceSource(t *testing.T) {
for _, ti := range []struct { for _, ti := range []struct {
title string title string
annotationFilter string annotationFilter string
fqdnTemplate string fqdnTemplate string
expectError bool serviceTypesFilter []string
expectError bool
}{ }{
{ {
title: "invalid template", title: "invalid template",
@ -123,6 +125,11 @@ func testServiceSourceNewServiceSource(t *testing.T) {
expectError: false, expectError: false,
annotationFilter: "kubernetes.io/ingress.class=nginx", annotationFilter: "kubernetes.io/ingress.class=nginx",
}, },
{
title: "non-empty service types filter",
expectError: false,
serviceTypesFilter: []string{string(v1.ServiceTypeClusterIP)},
},
} { } {
t.Run(ti.title, func(t *testing.T) { t.Run(ti.title, func(t *testing.T) {
_, err := NewServiceSource( _, err := NewServiceSource(
@ -134,6 +141,7 @@ func testServiceSourceNewServiceSource(t *testing.T) {
"", "",
false, false,
false, false,
ti.serviceTypesFilter,
) )
if ti.expectError { if ti.expectError {
@ -161,6 +169,7 @@ func testServiceSourceEndpoints(t *testing.T) {
annotations map[string]string annotations map[string]string
clusterIP string clusterIP string
lbs []string lbs []string
serviceTypesFilter []string
expected []*endpoint.Endpoint expected []*endpoint.Endpoint
expectError bool expectError bool
}{ }{
@ -178,6 +187,7 @@ func testServiceSourceEndpoints(t *testing.T) {
map[string]string{}, map[string]string{},
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{}, []*endpoint.Endpoint{},
false, false,
}, },
@ -197,6 +207,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{ []*endpoint.Endpoint{
{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
}, },
@ -218,6 +229,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"1.2.3.4", "1.2.3.4",
[]string{}, []string{},
[]string{},
[]*endpoint.Endpoint{}, []*endpoint.Endpoint{},
false, false,
}, },
@ -235,6 +247,7 @@ func testServiceSourceEndpoints(t *testing.T) {
map[string]string{}, map[string]string{},
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{ []*endpoint.Endpoint{
{DNSName: "foo.fqdn.org", Targets: endpoint.Targets{"1.2.3.4"}}, {DNSName: "foo.fqdn.org", Targets: endpoint.Targets{"1.2.3.4"}},
{DNSName: "foo.fqdn.com", Targets: endpoint.Targets{"1.2.3.4"}}, {DNSName: "foo.fqdn.com", Targets: endpoint.Targets{"1.2.3.4"}},
@ -257,6 +270,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{ []*endpoint.Endpoint{
{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
{DNSName: "bar.example.org", Targets: endpoint.Targets{"1.2.3.4"}}, {DNSName: "bar.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
@ -281,6 +295,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{ []*endpoint.Endpoint{
{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
{DNSName: "bar.example.org", Targets: endpoint.Targets{"1.2.3.4"}}, {DNSName: "bar.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
@ -303,6 +318,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{ []*endpoint.Endpoint{
{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
{DNSName: "bar.example.org", Targets: endpoint.Targets{"1.2.3.4"}}, {DNSName: "bar.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
@ -325,6 +341,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"lb.example.com"}, // Kubernetes omits the trailing dot []string{"lb.example.com"}, // Kubernetes omits the trailing dot
[]string{},
[]*endpoint.Endpoint{ []*endpoint.Endpoint{
{DNSName: "foo.example.org", Targets: endpoint.Targets{"lb.example.com"}}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"lb.example.com"}},
}, },
@ -346,6 +363,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4", "lb.example.com"}, // Kubernetes omits the trailing dot []string{"1.2.3.4", "lb.example.com"}, // Kubernetes omits the trailing dot
[]string{},
[]*endpoint.Endpoint{ []*endpoint.Endpoint{
{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
{DNSName: "foo.example.org", Targets: endpoint.Targets{"lb.example.com"}}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"lb.example.com"}},
@ -369,6 +387,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{ []*endpoint.Endpoint{
{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
}, },
@ -391,6 +410,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{}, []*endpoint.Endpoint{},
false, false,
}, },
@ -410,6 +430,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{ []*endpoint.Endpoint{
{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
}, },
@ -431,6 +452,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{}, []*endpoint.Endpoint{},
false, false,
}, },
@ -450,6 +472,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{ []*endpoint.Endpoint{
{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
}, },
@ -472,6 +495,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{ []*endpoint.Endpoint{
{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
}, },
@ -494,6 +518,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{}, []*endpoint.Endpoint{},
false, false,
}, },
@ -514,6 +539,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{}, []*endpoint.Endpoint{},
true, true,
}, },
@ -534,6 +560,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{ []*endpoint.Endpoint{
{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
}, },
@ -556,6 +583,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{}, []*endpoint.Endpoint{},
false, false,
}, },
@ -575,6 +603,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{}, []string{},
[]string{},
[]*endpoint.Endpoint{}, []*endpoint.Endpoint{},
false, false,
}, },
@ -594,6 +623,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4", "8.8.8.8"}, []string{"1.2.3.4", "8.8.8.8"},
[]string{},
[]*endpoint.Endpoint{ []*endpoint.Endpoint{
{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4", "8.8.8.8"}}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4", "8.8.8.8"}},
}, },
@ -615,6 +645,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{}, []*endpoint.Endpoint{},
false, false,
}, },
@ -634,6 +665,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{ []*endpoint.Endpoint{
{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
}, },
@ -657,6 +689,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{ []*endpoint.Endpoint{
{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
{DNSName: "bar.example.org", Targets: endpoint.Targets{"1.2.3.4"}}, {DNSName: "bar.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
@ -677,6 +710,7 @@ func testServiceSourceEndpoints(t *testing.T) {
map[string]string{}, map[string]string{},
"", "",
[]string{"1.2.3.4", "elb.com"}, []string{"1.2.3.4", "elb.com"},
[]string{},
[]*endpoint.Endpoint{ []*endpoint.Endpoint{
{DNSName: "foo.bar.example.com", Targets: endpoint.Targets{"1.2.3.4"}}, {DNSName: "foo.bar.example.com", Targets: endpoint.Targets{"1.2.3.4"}},
{DNSName: "foo.bar.example.com", Targets: endpoint.Targets{"elb.com"}}, {DNSName: "foo.bar.example.com", Targets: endpoint.Targets{"elb.com"}},
@ -699,6 +733,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4", "elb.com"}, []string{"1.2.3.4", "elb.com"},
[]string{},
[]*endpoint.Endpoint{ []*endpoint.Endpoint{
{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
{DNSName: "foo.example.org", Targets: endpoint.Targets{"elb.com"}}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"elb.com"}},
@ -721,6 +756,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{ []*endpoint.Endpoint{
{DNSName: "mate.example.org", Targets: endpoint.Targets{"1.2.3.4"}}, {DNSName: "mate.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
}, },
@ -740,6 +776,7 @@ func testServiceSourceEndpoints(t *testing.T) {
map[string]string{}, map[string]string{},
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{}, []*endpoint.Endpoint{},
true, true,
}, },
@ -759,6 +796,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{ []*endpoint.Endpoint{
{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}, RecordTTL: endpoint.TTL(0)}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}, RecordTTL: endpoint.TTL(0)},
}, },
@ -781,6 +819,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{ []*endpoint.Endpoint{
{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}, RecordTTL: endpoint.TTL(0)}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}, RecordTTL: endpoint.TTL(0)},
}, },
@ -803,6 +842,7 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{ []*endpoint.Endpoint{
{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}, RecordTTL: endpoint.TTL(10)}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}, RecordTTL: endpoint.TTL(10)},
}, },
@ -825,11 +865,54 @@ func testServiceSourceEndpoints(t *testing.T) {
}, },
"", "",
[]string{"1.2.3.4"}, []string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{ []*endpoint.Endpoint{
{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}, RecordTTL: endpoint.TTL(0)}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}, RecordTTL: endpoint.TTL(0)},
}, },
false, false,
}, },
{
"filter on service types should include matching services",
"",
"",
"testing",
"foo",
v1.ServiceTypeLoadBalancer,
"",
"",
false,
map[string]string{},
map[string]string{
hostnameAnnotationKey: "foo.example.org.",
},
"",
[]string{"1.2.3.4"},
[]string{string(v1.ServiceTypeLoadBalancer)},
[]*endpoint.Endpoint{
{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
},
false,
},
{
"filter on service types should exclude non-matching services",
"",
"",
"testing",
"foo",
v1.ServiceTypeNodePort,
"",
"",
false,
map[string]string{},
map[string]string{
hostnameAnnotationKey: "foo.example.org.",
},
"",
[]string{"1.2.3.4"},
[]string{string(v1.ServiceTypeLoadBalancer)},
[]*endpoint.Endpoint{},
false,
},
} { } {
t.Run(tc.title, func(t *testing.T) { t.Run(tc.title, func(t *testing.T) {
// Create a Kubernetes testing client // Create a Kubernetes testing client
@ -876,6 +959,7 @@ func testServiceSourceEndpoints(t *testing.T) {
tc.compatibility, tc.compatibility,
false, false,
false, false,
tc.serviceTypesFilter,
) )
require.NoError(t, err) require.NoError(t, err)
@ -1010,6 +1094,7 @@ func TestClusterIpServices(t *testing.T) {
tc.compatibility, tc.compatibility,
true, true,
false, false,
[]string{},
) )
require.NoError(t, err) require.NoError(t, err)
@ -1206,6 +1291,7 @@ func TestNodePortServices(t *testing.T) {
tc.compatibility, tc.compatibility,
true, true,
false, false,
[]string{},
) )
require.NoError(t, err) require.NoError(t, err)
@ -1406,6 +1492,7 @@ func TestHeadlessServices(t *testing.T) {
tc.compatibility, tc.compatibility,
true, true,
false, false,
[]string{},
) )
require.NoError(t, err) require.NoError(t, err)
@ -1606,6 +1693,7 @@ func TestHeadlessServicesHostIP(t *testing.T) {
tc.compatibility, tc.compatibility,
true, true,
true, true,
[]string{},
) )
require.NoError(t, err) require.NoError(t, err)
@ -1646,7 +1734,7 @@ func BenchmarkServiceEndpoints(b *testing.B) {
_, err := kubernetes.CoreV1().Services(service.Namespace).Create(service) _, err := kubernetes.CoreV1().Services(service.Namespace).Create(service)
require.NoError(b, err) require.NoError(b, err)
client, err := NewServiceSource(kubernetes, v1.NamespaceAll, "", "", false, "", false, false) client, err := NewServiceSource(kubernetes, v1.NamespaceAll, "", "", false, "", false, false, []string{})
require.NoError(b, err) require.NoError(b, err)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {

View File

@ -44,6 +44,7 @@ type Config struct {
PublishInternal bool PublishInternal bool
PublishHostIP bool PublishHostIP bool
ConnectorServer string ConnectorServer string
ServiceTypeFilter []string
} }
// ClientGenerator provides clients // ClientGenerator provides clients
@ -92,7 +93,7 @@ func BuildWithConfig(source string, p ClientGenerator, cfg *Config) (Source, err
if err != nil { if err != nil {
return nil, err return nil, err
} }
return NewServiceSource(client, cfg.Namespace, cfg.AnnotationFilter, cfg.FQDNTemplate, cfg.CombineFQDNAndAnnotation, cfg.Compatibility, cfg.PublishInternal, cfg.PublishHostIP) return NewServiceSource(client, cfg.Namespace, cfg.AnnotationFilter, cfg.FQDNTemplate, cfg.CombineFQDNAndAnnotation, cfg.Compatibility, cfg.PublishInternal, cfg.PublishHostIP, cfg.ServiceTypeFilter)
case "ingress": case "ingress":
client, err := p.KubeClient() client, err := p.KubeClient()
if err != nil { if err != nil {