use constructor methods to create sources (#82)

* ref(source): use constructor methods to create sources

* fix: import and use missing package

* fix(source): fix merge conflict with master branch
This commit is contained in:
Martin Linkhorst 2017-03-13 14:18:20 +01:00 committed by GitHub
parent cf9c5e39ed
commit c3378f7f67
5 changed files with 30 additions and 31 deletions

View File

@ -74,10 +74,7 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
source := &source.ServiceSource{ source := source.NewServiceSource(client, cfg.Namespace)
Client: client,
Namespace: cfg.Namespace,
}
dnsProvider, err := dnsprovider.NewGoogleProvider(cfg.GoogleProject, cfg.DryRun) dnsProvider, err := dnsprovider.NewGoogleProvider(cfg.GoogleProject, cfg.DryRun)
if err != nil { if err != nil {

View File

@ -24,18 +24,23 @@ import (
"github.com/kubernetes-incubator/external-dns/endpoint" "github.com/kubernetes-incubator/external-dns/endpoint"
) )
// IngressSource is an implementation of Source for Kubernetes ingress objects. // ingressSource is an implementation of Source for Kubernetes ingress objects.
// Ingress implementation will use the spec.rules.host value for the hostname // Ingress implementation will use the spec.rules.host value for the hostname
// Ingress annotations are ignored // Ingress annotations are ignored
type IngressSource struct { type ingressSource struct {
Client kubernetes.Interface client kubernetes.Interface
Namespace string namespace string
}
// NewIngressSource creates a new ingressSource with the given client and namespace scope.
func NewIngressSource(client kubernetes.Interface, namespace string) Source {
return &ingressSource{client: client, namespace: namespace}
} }
// Endpoints returns endpoint objects for each host-target combination that should be processed. // Endpoints returns endpoint objects for each host-target combination that should be processed.
// Retrieves all ingress resources on all namespaces // Retrieves all ingress resources on all namespaces
func (sc *IngressSource) Endpoints() ([]endpoint.Endpoint, error) { func (sc *ingressSource) Endpoints() ([]endpoint.Endpoint, error) {
ingresses, err := sc.Client.Extensions().Ingresses(sc.Namespace).List(v1.ListOptions{}) ingresses, err := sc.client.Extensions().Ingresses(sc.namespace).List(v1.ListOptions{})
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -25,8 +25,8 @@ import (
"k8s.io/client-go/pkg/apis/extensions/v1beta1" "k8s.io/client-go/pkg/apis/extensions/v1beta1"
) )
// Validates that IngressSource is a Source // Validates that ingressSource is a Source
var _ Source = &IngressSource{} var _ Source = &ingressSource{}
func TestIngress(t *testing.T) { func TestIngress(t *testing.T) {
t.Run("endpointsFromIngress", testEndpointsFromIngress) t.Run("endpointsFromIngress", testEndpointsFromIngress)
@ -223,10 +223,7 @@ func testIngressEndpoints(t *testing.T) {
} }
fakeClient := fake.NewSimpleClientset() fakeClient := fake.NewSimpleClientset()
ingressSource := &IngressSource{ ingressSource := NewIngressSource(fakeClient, ti.targetNamespace)
Client: fakeClient,
Namespace: ti.targetNamespace,
}
for _, ingress := range ingresses { for _, ingress := range ingresses {
_, err := fakeClient.Extensions().Ingresses(ingress.Namespace).Create(ingress) _, err := fakeClient.Extensions().Ingresses(ingress.Namespace).Create(ingress)
if err != nil { if err != nil {

View File

@ -32,19 +32,24 @@ const (
controllerAnnotationValue = "dns-controller" controllerAnnotationValue = "dns-controller"
) )
// ServiceSource is an implementation of Source for Kubernetes service objects. // serviceSource is an implementation of Source for Kubernetes service objects.
// It will find all services that are under our jurisdiction, i.e. annotated // It will find all services that are under our jurisdiction, i.e. annotated
// desired hostname and matching or no controller annotation. For each of the // desired hostname and matching or no controller annotation. For each of the
// matched services' external entrypoints it will return a corresponding // matched services' external entrypoints it will return a corresponding
// Endpoint object. // Endpoint object.
type ServiceSource struct { type serviceSource struct {
Client kubernetes.Interface client kubernetes.Interface
Namespace string namespace string
}
// NewServiceSource creates a new serviceSource with the given client and namespace scope.
func NewServiceSource(client kubernetes.Interface, namespace string) Source {
return &serviceSource{client: client, namespace: namespace}
} }
// Endpoints returns endpoint objects for each service that should be processed. // Endpoints returns endpoint objects for each service that should be processed.
func (sc *ServiceSource) Endpoints() ([]endpoint.Endpoint, error) { func (sc *serviceSource) Endpoints() ([]endpoint.Endpoint, error) {
services, err := sc.Client.CoreV1().Services(sc.Namespace).List(v1.ListOptions{}) services, err := sc.client.CoreV1().Services(sc.namespace).List(v1.ListOptions{})
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -26,8 +26,8 @@ import (
"github.com/kubernetes-incubator/external-dns/endpoint" "github.com/kubernetes-incubator/external-dns/endpoint"
) )
// Validates that ServiceSource is a Source // Validates that serviceSource is a Source
var _ Source = &ServiceSource{} var _ Source = &serviceSource{}
func TestService(t *testing.T) { func TestService(t *testing.T) {
t.Run("Endpoints", testServiceEndpoints) t.Run("Endpoints", testServiceEndpoints)
@ -201,10 +201,7 @@ func testServiceEndpoints(t *testing.T) {
} }
// Create our object under test and get the endpoints. // Create our object under test and get the endpoints.
client := &ServiceSource{ client := NewServiceSource(kubernetes, tc.targetNamespace)
Client: kubernetes,
Namespace: tc.targetNamespace,
}
endpoints, err := client.Endpoints() endpoints, err := client.Endpoints()
if err != nil { if err != nil {
@ -243,9 +240,7 @@ func BenchmarkServiceEndpoints(b *testing.B) {
b.Fatal(err) b.Fatal(err)
} }
client := &ServiceSource{ client := NewServiceSource(kubernetes, v1.NamespaceAll)
Client: kubernetes,
}
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_, err := client.Endpoints() _, err := client.Endpoints()