Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(source): code cleanup #5189

Open
wants to merge 39 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
141e87b
Merge branch 'kubernetes-sigs:master' into master
ivankatliarchuk Mar 6, 2025
a3395cc
Merge branch 'kubernetes-sigs:master' into master
ivankatliarchuk Mar 7, 2025
fc24c55
Merge branch 'kubernetes-sigs:master' into master
ivankatliarchuk Mar 11, 2025
6e691be
Merge branch 'kubernetes-sigs:master' into master
ivankatliarchuk Mar 13, 2025
32f0eeb
Merge branch 'kubernetes-sigs:master' into master
ivankatliarchuk Mar 14, 2025
34b469a
Merge branch 'kubernetes-sigs:master' into master
ivankatliarchuk Mar 16, 2025
37a6621
chore(source): code cleanup
ivankatliarchuk Mar 17, 2025
eba1dc6
chore(source): code cleanup
ivankatliarchuk Mar 17, 2025
ac3cb68
chore(source): code cleanup
ivankatliarchuk Mar 17, 2025
f9d399e
chore(source): code cleanup
ivankatliarchuk Mar 17, 2025
85902d0
chore(source): code cleanup
ivankatliarchuk Mar 17, 2025
7e7f7ff
chore(source): code cleanup
ivankatliarchuk Mar 17, 2025
361631d
chore(source): code cleanup
ivankatliarchuk Mar 18, 2025
fcda79f
chore(source): code cleanup
ivankatliarchuk Mar 18, 2025
b2795e0
chore(source): code cleanup
ivankatliarchuk Mar 19, 2025
ba698e6
Merge branch 'kubernetes-sigs:master' into feat-code-cleanup-0
ivankatliarchuk Mar 24, 2025
f285cb1
chore(source): code cleanup
ivankatliarchuk Mar 28, 2025
4910d30
Merge remote-tracking branch 'refs/remotes/origin/feat-code-cleanup-0…
ivankatliarchuk Mar 28, 2025
d2dc8c6
chore(source): code cleanup
ivankatliarchuk Mar 28, 2025
b3ce318
chore(source): code cleanup
ivankatliarchuk Mar 28, 2025
62554cb
chore(source): code cleanup
ivankatliarchuk Mar 28, 2025
50bf165
chore(source): code cleanup
ivankatliarchuk Mar 28, 2025
9bd6e1d
chore(source): code cleanup
ivankatliarchuk Mar 28, 2025
b16775d
chore(source): code cleanup
ivankatliarchuk Mar 28, 2025
505557a
Merge branch 'master' into feat-code-cleanup-0
ivankatliarchuk Apr 1, 2025
bcb2dd8
chore(source): code cleanup
ivankatliarchuk Apr 1, 2025
631c789
chore(source): code cleanup
ivankatliarchuk Apr 2, 2025
3c09340
chore(source): code cleanup
ivankatliarchuk Apr 2, 2025
d88e9b2
chore(source): code cleanup
ivankatliarchuk Apr 2, 2025
1d1ac2e
chore(source): code cleanup
ivankatliarchuk Apr 2, 2025
87a66f9
chore(source): code cleanup
ivankatliarchuk Apr 2, 2025
7bbc566
chore(source): code cleanup
ivankatliarchuk Apr 2, 2025
6fcc697
chore(source): code cleanup
ivankatliarchuk Apr 2, 2025
e7435c7
chore(source): code cleanup
ivankatliarchuk Apr 6, 2025
7e1147c
chore(source): code cleanup
ivankatliarchuk Apr 6, 2025
2b703e5
chore(source): code cleanup
ivankatliarchuk Apr 6, 2025
87f34fd
chore(source): code cleanup
ivankatliarchuk Apr 6, 2025
807f2e8
chore(source): code cleanup
ivankatliarchuk Apr 6, 2025
86b92e1
Merge branch 'master' into feat-code-cleanup-0
ivankatliarchuk Apr 9, 2025
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
58 changes: 11 additions & 47 deletions source/istio_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"k8s.io/client-go/tools/cache"

"sigs.k8s.io/external-dns/endpoint"
"sigs.k8s.io/external-dns/source/utils"
)

// IstioGatewayIngressSource is the annotation used to determine if the gateway is implemented by an Ingress object
Expand Down Expand Up @@ -200,11 +201,7 @@ func (sc *gatewaySource) AddEventHandler(ctx context.Context, handler func()) {

// filterByAnnotations filters a list of configs by a given annotation selector.
func (sc *gatewaySource) filterByAnnotations(gateways []*networkingv1alpha3.Gateway) ([]*networkingv1alpha3.Gateway, error) {
labelSelector, err := metav1.ParseToLabelSelector(sc.annotationFilter)
if err != nil {
return nil, err
}
selector, err := metav1.LabelSelectorAsSelector(labelSelector)
selector, err := utils.ParseAnnotationFilter(sc.annotationFilter)
if err != nil {
return nil, err
}
Expand All @@ -229,21 +226,8 @@ func (sc *gatewaySource) filterByAnnotations(gateways []*networkingv1alpha3.Gate
return filteredList, nil
}

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)
namespace, name, err := utils.ParseIngress(ingressStr)
if err != nil {
return nil, fmt.Errorf("failed to parse Ingress annotation on Gateway (%s/%s): %w", gateway.Namespace, gateway.Name, err)
}
Expand All @@ -266,44 +250,24 @@ func (sc *gatewaySource) targetsFromIngress(ctx context.Context, ingressStr stri
return
}

func (sc *gatewaySource) targetsFromGateway(ctx context.Context, gateway *networkingv1alpha3.Gateway) (targets endpoint.Targets, err error) {
targets = getTargetsFromTargetAnnotation(gateway.Annotations)
func (sc *gatewaySource) targetsFromGateway(ctx context.Context, gateway *networkingv1alpha3.Gateway) (endpoint.Targets, error) {
targets := getTargetsFromTargetAnnotation(gateway.Annotations)
if len(targets) > 0 {
return
return targets, nil
}

ingressStr, ok := gateway.Annotations[IstioGatewayIngressSource]
if ok && ingressStr != "" {
targets, err = sc.targetsFromIngress(ctx, ingressStr, gateway)
return
return sc.targetsFromIngress(ctx, ingressStr, gateway)
}

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

for _, service := range services {
if !gatewaySelectorMatchesServiceSelector(gateway.Spec.Selector, service.Spec.Selector) {
continue
}

if len(service.Spec.ExternalIPs) > 0 {
targets = append(targets, service.Spec.ExternalIPs...)
continue
}
targets, err := utils.EndpointTargetsFromServices(sc.serviceInformer, sc.namespace, gateway.Spec.Selector)

for _, lb := range service.Status.LoadBalancer.Ingress {
if lb.IP != "" {
targets = append(targets, lb.IP)
} else if lb.Hostname != "" {
targets = append(targets, lb.Hostname)
}
}
if err != nil {
return nil, err
}

return
return targets, nil
}

// endpointsFromGatewayConfig extracts the endpoints from an Istio Gateway Config object
Expand Down
10 changes: 4 additions & 6 deletions source/istio_virtualservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"

"sigs.k8s.io/external-dns/source/utils"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would drop the whitespaces here and let gofmt/goimports group the imports

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"sigs.k8s.io/external-dns/endpoint"
)

Expand Down Expand Up @@ -252,11 +254,7 @@ func (sc *virtualServiceSource) endpointsFromTemplate(ctx context.Context, virtu

// filterByAnnotations filters a list of configs by a given annotation selector.
func (sc *virtualServiceSource) filterByAnnotations(virtualservices []*networkingv1alpha3.VirtualService) ([]*networkingv1alpha3.VirtualService, error) {
labelSelector, err := metav1.ParseToLabelSelector(sc.annotationFilter)
if err != nil {
return nil, err
}
selector, err := metav1.LabelSelectorAsSelector(labelSelector)
selector, err := utils.ParseAnnotationFilter(sc.annotationFilter)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -435,7 +433,7 @@ func parseGateway(gateway string) (namespace, name string, err error) {
}

func (sc *virtualServiceSource) targetsFromIngress(ctx context.Context, ingressStr string, gateway *networkingv1alpha3.Gateway) (targets endpoint.Targets, err error) {
namespace, name, err := parseIngress(ingressStr)
namespace, name, err := utils.ParseIngress(ingressStr)
if err != nil {
return nil, fmt.Errorf("failed to parse Ingress annotation on Gateway (%s/%s): %w", gateway.Namespace, gateway.Name, err)
}
Expand Down
7 changes: 2 additions & 5 deletions source/kong_tcpingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"k8s.io/client-go/tools/cache"

"sigs.k8s.io/external-dns/endpoint"
"sigs.k8s.io/external-dns/source/utils"
)

var kongGroupdVersionResource = schema.GroupVersionResource{
Expand Down Expand Up @@ -162,11 +163,7 @@ func (sc *kongTCPIngressSource) Endpoints(ctx context.Context) ([]*endpoint.Endp

// filterByAnnotations filters a list of TCPIngresses by a given annotation selector.
func (sc *kongTCPIngressSource) filterByAnnotations(tcpIngresses []*TCPIngress) ([]*TCPIngress, error) {
labelSelector, err := metav1.ParseToLabelSelector(sc.annotationFilter)
if err != nil {
return nil, err
}
selector, err := metav1.LabelSelectorAsSelector(labelSelector)
selector, err := utils.ParseAnnotationFilter(sc.annotationFilter)
if err != nil {
return nil, err
}
Expand Down
8 changes: 2 additions & 6 deletions source/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import (

log "github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
kubeinformers "k8s.io/client-go/informers"
coreinformers "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"

"sigs.k8s.io/external-dns/endpoint"
"sigs.k8s.io/external-dns/source/utils"
)

type nodeSource struct {
Expand Down Expand Up @@ -197,11 +197,7 @@ func (ns *nodeSource) nodeAddresses(node *v1.Node) ([]string, error) {

// filterByAnnotations filters a list of nodes by a given annotation selector.
func (ns *nodeSource) filterByAnnotations(nodes []*v1.Node) ([]*v1.Node, error) {
labelSelector, err := metav1.ParseToLabelSelector(ns.annotationFilter)
if err != nil {
return nil, err
}
selector, err := metav1.LabelSelectorAsSelector(labelSelector)
selector, err := utils.ParseAnnotationFilter(ns.annotationFilter)
if err != nil {
return nil, err
}
Expand Down
10 changes: 3 additions & 7 deletions source/openshift_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ import (
"time"

routev1 "github.com/openshift/api/route/v1"
versioned "github.com/openshift/client-go/route/clientset/versioned"
"github.com/openshift/client-go/route/clientset/versioned"
extInformers "github.com/openshift/client-go/route/informers/externalversions"
routeInformer "github.com/openshift/client-go/route/informers/externalversions/route/v1"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/tools/cache"

"sigs.k8s.io/external-dns/endpoint"
"sigs.k8s.io/external-dns/source/utils"
)

// ocpRouteSource is an implementation of Source for OpenShift Route objects.
Expand Down Expand Up @@ -194,11 +194,7 @@ func (ors *ocpRouteSource) endpointsFromTemplate(ocpRoute *routev1.Route) ([]*en
}

func (ors *ocpRouteSource) filterByAnnotations(ocpRoutes []*routev1.Route) ([]*routev1.Route, error) {
labelSelector, err := metav1.ParseToLabelSelector(ors.annotationFilter)
if err != nil {
return nil, err
}
selector, err := metav1.LabelSelectorAsSelector(labelSelector)
selector, err := utils.ParseAnnotationFilter(ors.annotationFilter)
if err != nil {
return nil, err
}
Expand Down
8 changes: 3 additions & 5 deletions source/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"

"sigs.k8s.io/external-dns/source/utils"

"sigs.k8s.io/external-dns/endpoint"
)

Expand Down Expand Up @@ -420,11 +422,7 @@ func (sc *serviceSource) endpoints(svc *v1.Service) []*endpoint.Endpoint {

// filterByAnnotations filters a list of services by a given annotation selector.
func (sc *serviceSource) filterByAnnotations(services []*v1.Service) ([]*v1.Service, error) {
labelSelector, err := metav1.ParseToLabelSelector(sc.annotationFilter)
if err != nil {
return nil, err
}
selector, err := metav1.LabelSelectorAsSelector(labelSelector)
selector, err := utils.ParseAnnotationFilter(sc.annotationFilter)
if err != nil {
return nil, err
}
Expand Down
Loading
Loading