mirror of
https://github.com/kubernetes-sigs/external-dns.git
synced 2025-08-06 09:36:58 +02:00
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:
parent
cf9c5e39ed
commit
c3378f7f67
5
main.go
5
main.go
@ -74,10 +74,7 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
source := &source.ServiceSource{
|
||||
Client: client,
|
||||
Namespace: cfg.Namespace,
|
||||
}
|
||||
source := source.NewServiceSource(client, cfg.Namespace)
|
||||
|
||||
dnsProvider, err := dnsprovider.NewGoogleProvider(cfg.GoogleProject, cfg.DryRun)
|
||||
if err != nil {
|
||||
|
@ -24,18 +24,23 @@ import (
|
||||
"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 annotations are ignored
|
||||
type IngressSource struct {
|
||||
Client kubernetes.Interface
|
||||
Namespace string
|
||||
type ingressSource struct {
|
||||
client kubernetes.Interface
|
||||
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.
|
||||
// Retrieves all ingress resources on all namespaces
|
||||
func (sc *IngressSource) Endpoints() ([]endpoint.Endpoint, error) {
|
||||
ingresses, err := sc.Client.Extensions().Ingresses(sc.Namespace).List(v1.ListOptions{})
|
||||
func (sc *ingressSource) Endpoints() ([]endpoint.Endpoint, error) {
|
||||
ingresses, err := sc.client.Extensions().Ingresses(sc.namespace).List(v1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -25,8 +25,8 @@ import (
|
||||
"k8s.io/client-go/pkg/apis/extensions/v1beta1"
|
||||
)
|
||||
|
||||
// Validates that IngressSource is a Source
|
||||
var _ Source = &IngressSource{}
|
||||
// Validates that ingressSource is a Source
|
||||
var _ Source = &ingressSource{}
|
||||
|
||||
func TestIngress(t *testing.T) {
|
||||
t.Run("endpointsFromIngress", testEndpointsFromIngress)
|
||||
@ -223,10 +223,7 @@ func testIngressEndpoints(t *testing.T) {
|
||||
}
|
||||
|
||||
fakeClient := fake.NewSimpleClientset()
|
||||
ingressSource := &IngressSource{
|
||||
Client: fakeClient,
|
||||
Namespace: ti.targetNamespace,
|
||||
}
|
||||
ingressSource := NewIngressSource(fakeClient, ti.targetNamespace)
|
||||
for _, ingress := range ingresses {
|
||||
_, err := fakeClient.Extensions().Ingresses(ingress.Namespace).Create(ingress)
|
||||
if err != nil {
|
||||
|
@ -32,19 +32,24 @@ const (
|
||||
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
|
||||
// desired hostname and matching or no controller annotation. For each of the
|
||||
// matched services' external entrypoints it will return a corresponding
|
||||
// Endpoint object.
|
||||
type ServiceSource struct {
|
||||
Client kubernetes.Interface
|
||||
Namespace string
|
||||
type serviceSource struct {
|
||||
client kubernetes.Interface
|
||||
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.
|
||||
func (sc *ServiceSource) Endpoints() ([]endpoint.Endpoint, error) {
|
||||
services, err := sc.Client.CoreV1().Services(sc.Namespace).List(v1.ListOptions{})
|
||||
func (sc *serviceSource) Endpoints() ([]endpoint.Endpoint, error) {
|
||||
services, err := sc.client.CoreV1().Services(sc.namespace).List(v1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -26,8 +26,8 @@ import (
|
||||
"github.com/kubernetes-incubator/external-dns/endpoint"
|
||||
)
|
||||
|
||||
// Validates that ServiceSource is a Source
|
||||
var _ Source = &ServiceSource{}
|
||||
// Validates that serviceSource is a Source
|
||||
var _ Source = &serviceSource{}
|
||||
|
||||
func TestService(t *testing.T) {
|
||||
t.Run("Endpoints", testServiceEndpoints)
|
||||
@ -201,10 +201,7 @@ func testServiceEndpoints(t *testing.T) {
|
||||
}
|
||||
|
||||
// Create our object under test and get the endpoints.
|
||||
client := &ServiceSource{
|
||||
Client: kubernetes,
|
||||
Namespace: tc.targetNamespace,
|
||||
}
|
||||
client := NewServiceSource(kubernetes, tc.targetNamespace)
|
||||
|
||||
endpoints, err := client.Endpoints()
|
||||
if err != nil {
|
||||
@ -243,9 +240,7 @@ func BenchmarkServiceEndpoints(b *testing.B) {
|
||||
b.Fatal(err)
|
||||
}
|
||||
|
||||
client := &ServiceSource{
|
||||
Client: kubernetes,
|
||||
}
|
||||
client := NewServiceSource(kubernetes, v1.NamespaceAll)
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := client.Endpoints()
|
||||
|
Loading…
Reference in New Issue
Block a user