chore(source): code cleanup

Signed-off-by: ivan katliarchuk <ivan.katliarchuk@gmail.com>
This commit is contained in:
ivan katliarchuk 2025-05-09 08:21:31 +01:00
parent 8d8f81369f
commit 9f8f30882b
No known key found for this signature in database
GPG Key ID: 90C9B4748A999097
18 changed files with 62 additions and 105 deletions

View File

@ -254,7 +254,7 @@ func (z zoneTags) filterZonesByTags(p *AWSProvider, zones map[string]*profiledZo
// append adds tags to the ZoneTags for a given zoneID.
func (z zoneTags) append(id string, tags []route53types.Tag) {
zoneId := fmt.Sprintf("/hostedzone/%s", id)
if _, exists := z[zoneId]; !exists {
if _, ok := z[zoneId]; !ok {
z[zoneId] = make(map[string]string)
}
for _, tag := range tags {

View File

@ -312,7 +312,7 @@ func (c *inMemoryClient) validateChangeBatch(zone string, changes *plan.Changes)
}
mesh := sets.New[endpoint.EndpointKey]()
for _, newEndpoint := range changes.Create {
if _, exists := curZone[newEndpoint.Key()]; exists {
if _, ok := curZone[newEndpoint.Key()]; ok {
return ErrRecordAlreadyExists
}
if err := c.updateMesh(mesh, newEndpoint); err != nil {
@ -320,7 +320,7 @@ func (c *inMemoryClient) validateChangeBatch(zone string, changes *plan.Changes)
}
}
for _, updateEndpoint := range changes.UpdateNew {
if _, exists := curZone[updateEndpoint.Key()]; !exists {
if _, ok := curZone[updateEndpoint.Key()]; !ok {
return ErrRecordNotFound
}
if err := c.updateMesh(mesh, updateEndpoint); err != nil {
@ -328,12 +328,12 @@ func (c *inMemoryClient) validateChangeBatch(zone string, changes *plan.Changes)
}
}
for _, updateOldEndpoint := range changes.UpdateOld {
if rec, exists := curZone[updateOldEndpoint.Key()]; !exists || rec.Targets[0] != updateOldEndpoint.Targets[0] {
if rec, ok := curZone[updateOldEndpoint.Key()]; !ok || rec.Targets[0] != updateOldEndpoint.Targets[0] {
return ErrRecordNotFound
}
}
for _, deleteEndpoint := range changes.Delete {
if rec, exists := curZone[deleteEndpoint.Key()]; !exists || rec.Targets[0] != deleteEndpoint.Targets[0] {
if rec, ok := curZone[deleteEndpoint.Key()]; !ok || rec.Targets[0] != deleteEndpoint.Targets[0] {
return ErrRecordNotFound
}
if err := c.updateMesh(mesh, deleteEndpoint); err != nil {

View File

@ -1248,7 +1248,7 @@ func (r *DynamoDBStub) BatchExecuteStatement(context context.Context, input *dyn
var key string
assert.Nil(r.t, attributevalue.Unmarshal(statement.Parameters[0], &key))
if code, exists := r.stubConfig.ExpectInsertError[key]; exists {
if code, ok := r.stubConfig.ExpectInsertError[key]; ok {
delete(r.stubConfig.ExpectInsertError, key)
responses = append(responses, dynamodbtypes.BatchStatementResponse{
Error: &dynamodbtypes.BatchStatementError{

View File

@ -119,7 +119,7 @@ func (sc *ambassadorHostSource) Endpoints(ctx context.Context) ([]*endpoint.Endp
}
// Get a list of Ambassador Host resources
ambassadorHosts := []*ambassador.Host{}
var ambassadorHosts []*ambassador.Host
for _, hostObj := range hosts {
unstructuredHost, ok := hostObj.(*unstructured.Unstructured)
if !ok {
@ -185,11 +185,10 @@ func (sc *ambassadorHostSource) Endpoints(ctx context.Context) ([]*endpoint.Endp
// endpointsFromHost extracts the endpoints from a Host object
func (sc *ambassadorHostSource) endpointsFromHost(host *ambassador.Host, targets endpoint.Targets) ([]*endpoint.Endpoint, error) {
var endpoints []*endpoint.Endpoint
annotations := host.Annotations
resource := fmt.Sprintf("host/%s/%s", host.Namespace, host.Name)
providerSpecific, setIdentifier := getProviderSpecificAnnotations(annotations)
ttl := getTTLFromAnnotations(annotations, resource)
providerSpecific, setIdentifier := getProviderSpecificAnnotations(host.Annotations)
ttl := getTTLFromAnnotations(host.Annotations, resource)
if host.Spec != nil {
hostname := host.Spec.Hostname
@ -311,9 +310,8 @@ func (sc *ambassadorHostSource) filterByAnnotations(ambassadorHosts []*ambassado
// Return a filtered list of Ambassador Hosts
filteredList := []*ambassador.Host{}
for _, host := range ambassadorHosts {
annotations := labels.Set(host.Annotations)
// include Ambassador Host if its annotations match the annotation filter
if selector.Matches(annotations) {
if selector.Matches(labels.Set(host.Annotations)) {
filteredList = append(filteredList, host)
}
}

View File

@ -84,8 +84,8 @@ func legacyEndpointsFromMoleculeService(svc *v1.Service) []*endpoint.Endpoint {
}
// Get the desired hostname of the service from the annotation.
hostnameAnnotation, exists := svc.Annotations[moleculeAnnotationKey]
if !exists {
hostnameAnnotation, ok := svc.Annotations[moleculeAnnotationKey]
if !ok {
return nil
}

View File

@ -224,14 +224,11 @@ func (sc *httpProxySource) filterByAnnotations(httpProxies []*projectcontour.HTT
return httpProxies, nil
}
filteredList := []*projectcontour.HTTPProxy{}
var filteredList []*projectcontour.HTTPProxy
for _, httpProxy := range httpProxies {
// convert the HTTPProxy's annotations to an equivalent label selector
annotations := labels.Set(httpProxy.Annotations)
// include HTTPProxy if its annotations match the selector
if selector.Matches(annotations) {
if selector.Matches(labels.Set(httpProxy.Annotations)) {
filteredList = append(filteredList, httpProxy)
}
}

View File

@ -287,11 +287,8 @@ func (cs *crdSource) filterByAnnotations(dnsendpoints *endpoint.DNSEndpointList)
filteredList := endpoint.DNSEndpointList{}
for _, dnsendpoint := range dnsendpoints.Items {
// convert the dnsendpoint' annotations to an equivalent label selector
annotations := labels.Set(dnsendpoint.Annotations)
// include dnsendpoint if its annotations match the selector
if selector.Matches(annotations) {
if selector.Matches(labels.Set(dnsendpoint.Annotations)) {
filteredList.Items = append(filteredList.Items, dnsendpoint)
}
}

View File

@ -201,11 +201,8 @@ func (ts *f5TransportServerSource) filterByAnnotations(transportServers []*f5.Tr
filteredList := []*f5.TransportServer{}
for _, ts := range transportServers {
// convert the TransportServer's annotations to an equivalent label selector
annotations := labels.Set(ts.Annotations)
// include TransportServer if its annotations match the selector
if selector.Matches(annotations) {
if selector.Matches(labels.Set(ts.Annotations)) {
filteredList = append(filteredList, ts)
}
}

View File

@ -208,11 +208,8 @@ func (vs *f5VirtualServerSource) filterByAnnotations(virtualServers []*f5.Virtua
filteredList := []*f5.VirtualServer{}
for _, vs := range virtualServers {
// convert the VirtualServer's annotations to an equivalent label selector
annotations := labels.Set(vs.Annotations)
// include VirtualServer if its annotations match the selector
if selector.Matches(annotations) {
if selector.Matches(labels.Set(vs.Annotations)) {
filteredList = append(filteredList, vs)
}
}

View File

@ -161,12 +161,12 @@ func (gs *glooSource) generateEndpointsFromProxy(ctx context.Context, proxy *pro
for _, listener := range proxy.Spec.Listeners {
for _, virtualHost := range listener.HTTPListener.VirtualHosts {
annotations, err := gs.annotationsFromProxySource(ctx, virtualHost)
ants, err := gs.annotationsFromProxySource(ctx, virtualHost)
if err != nil {
return nil, err
}
ttl := getTTLFromAnnotations(annotations, resource)
providerSpecific, setIdentifier := getProviderSpecificAnnotations(annotations)
ttl := getTTLFromAnnotations(ants, resource)
providerSpecific, setIdentifier := getProviderSpecificAnnotations(ants)
for _, domain := range virtualHost.Domains {
endpoints = append(endpoints, endpointsForHostname(strings.TrimSuffix(domain, "."), targets, ttl, providerSpecific, setIdentifier, "")...)
}
@ -176,7 +176,7 @@ func (gs *glooSource) generateEndpointsFromProxy(ctx context.Context, proxy *pro
}
func (gs *glooSource) annotationsFromProxySource(ctx context.Context, virtualHost proxyVirtualHost) (map[string]string, error) {
annotations := map[string]string{}
ants := map[string]string{}
for _, src := range virtualHost.Metadata.Source {
kind := sourceKind(src.Kind)
if kind != nil {
@ -185,7 +185,7 @@ func (gs *glooSource) annotationsFromProxySource(ctx context.Context, virtualHos
return nil, err
}
for key, value := range source.GetAnnotations() {
annotations[key] = value
ants[key] = value
}
}
}
@ -197,11 +197,11 @@ func (gs *glooSource) annotationsFromProxySource(ctx context.Context, virtualHos
return nil, err
}
for key, value := range source.GetAnnotations() {
annotations[key] = value
ants[key] = value
}
}
}
return annotations, nil
return ants, nil
}
func (gs *glooSource) proxyTargets(ctx context.Context, name string, namespace string) (endpoint.Targets, error) {

View File

@ -217,11 +217,8 @@ func (sc *gatewaySource) filterByAnnotations(gateways []*networkingv1alpha3.Gate
var filteredList []*networkingv1alpha3.Gateway
for _, gw := range gateways {
// convert the annotations to an equivalent label selector
annotations := labels.Set(gw.Annotations)
// include if the annotations match the selector
if selector.Matches(annotations) {
if selector.Matches(labels.Set(gw.Annotations)) {
filteredList = append(filteredList, gw)
}
}
@ -313,10 +310,9 @@ func (sc *gatewaySource) endpointsFromGateway(ctx context.Context, hostnames []s
resource := fmt.Sprintf("gateway/%s/%s", gateway.Namespace, gateway.Name)
annotations := gateway.Annotations
ttl := getTTLFromAnnotations(annotations, resource)
ttl := getTTLFromAnnotations(gateway.Annotations, resource)
targets := getTargetsFromTargetAnnotation(annotations)
targets := getTargetsFromTargetAnnotation(gateway.Annotations)
if len(targets) == 0 {
targets, err = sc.targetsFromGateway(ctx, gateway)
if err != nil {
@ -324,7 +320,7 @@ func (sc *gatewaySource) endpointsFromGateway(ctx context.Context, hostnames []s
}
}
providerSpecific, setIdentifier := getProviderSpecificAnnotations(annotations)
providerSpecific, setIdentifier := getProviderSpecificAnnotations(gateway.Annotations)
for _, host := range hostnames {
endpoints = append(endpoints, endpointsForHostname(host, targets, ttl, providerSpecific, setIdentifier, resource)...)

View File

@ -268,13 +268,10 @@ func (sc *virtualServiceSource) filterByAnnotations(virtualservices []*networkin
var filteredList []*networkingv1alpha3.VirtualService
for _, virtualservice := range virtualservices {
// convert the annotations to an equivalent label selector
annotations := labels.Set(virtualservice.Annotations)
for _, vs := range virtualservices {
// include if the annotations match the selector
if selector.Matches(annotations) {
filteredList = append(filteredList, virtualservice)
if selector.Matches(labels.Set(vs.Annotations)) {
filteredList = append(filteredList, vs)
}
}

View File

@ -179,11 +179,8 @@ func (sc *kongTCPIngressSource) filterByAnnotations(tcpIngresses []*TCPIngress)
filteredList := []*TCPIngress{}
for _, tcpIngress := range tcpIngresses {
// convert the TCPIngress's annotations to an equivalent label selector
annotations := labels.Set(tcpIngress.Annotations)
// include TCPIngress if its annotations match the selector
if selector.Matches(annotations) {
if selector.Matches(labels.Set(tcpIngress.Annotations)) {
filteredList = append(filteredList, tcpIngress)
}
}

View File

@ -222,14 +222,11 @@ func (ns *nodeSource) filterByAnnotations(nodes []*v1.Node) ([]*v1.Node, error)
return nodes, nil
}
filteredList := []*v1.Node{}
var filteredList []*v1.Node
for _, node := range nodes {
// convert the node's annotations to an equivalent label selector
annotations := labels.Set(node.Annotations)
// include node if its annotations match the selector
if selector.Matches(annotations) {
if selector.Matches(labels.Set(node.Annotations)) {
filteredList = append(filteredList, node)
}
}

View File

@ -24,7 +24,7 @@ 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"
@ -208,14 +208,11 @@ func (ors *ocpRouteSource) filterByAnnotations(ocpRoutes []*routev1.Route) ([]*r
return ocpRoutes, nil
}
filteredList := []*routev1.Route{}
var filteredList []*routev1.Route
for _, ocpRoute := range ocpRoutes {
// convert the Route's annotations to an equivalent label selector
annotations := labels.Set(ocpRoute.Annotations)
// include ocpRoute if its annotations match the selector
if selector.Matches(annotations) {
if selector.Matches(labels.Set(ocpRoute.Annotations)) {
filteredList = append(filteredList, ocpRoute)
}
}

View File

@ -434,14 +434,11 @@ func (sc *serviceSource) filterByAnnotations(services []*v1.Service) ([]*v1.Serv
return services, nil
}
filteredList := []*v1.Service{}
var filteredList []*v1.Service
for _, service := range services {
// convert the service's annotations to an equivalent label selector
annotations := labels.Set(service.Annotations)
// include service if its annotations match the selector
if selector.Matches(annotations) {
if selector.Matches(labels.Set(service.Annotations)) {
filteredList = append(filteredList, service)
}
}
@ -504,9 +501,9 @@ func (sc *serviceSource) generateEndpoints(svc *v1.Service, hostname string, pro
targets = extractServiceExternalName(svc)
}
for _, endpoint := range endpoints {
endpoint.ProviderSpecific = providerSpecific
endpoint.SetIdentifier = setIdentifier
for _, en := range endpoints {
en.ProviderSpecific = providerSpecific
en.SetIdentifier = setIdentifier
}
}

View File

@ -89,10 +89,10 @@ type Source interface {
AddEventHandler(context.Context, func())
}
func getTTLFromAnnotations(annotations map[string]string, resource string) endpoint.TTL {
func getTTLFromAnnotations(ants map[string]string, resource string) endpoint.TTL {
ttlNotConfigured := endpoint.TTL(0)
ttlAnnotation, exists := annotations[ttlAnnotationKey]
if !exists {
ttlAnnotation, ok := ants[ttlAnnotationKey]
if !ok {
return ttlNotConfigured
}
ttlValue, err := parseTTL(ttlAnnotation)
@ -171,9 +171,9 @@ func getEndpointsTypeFromAnnotations(annotations map[string]string) string {
return annotations[endpointsTypeAnnotationKey]
}
func getInternalHostnamesFromAnnotations(annotations map[string]string) []string {
internalHostnameAnnotation, exists := annotations[internalHostnameAnnotationKey]
if !exists {
func getInternalHostnamesFromAnnotations(ants map[string]string) []string {
internalHostnameAnnotation, ok := ants[internalHostnameAnnotationKey]
if !ok {
return nil
}
return splitHostnameAnnotation(internalHostnameAnnotation)
@ -183,40 +183,40 @@ func splitHostnameAnnotation(annotation string) []string {
return strings.Split(strings.ReplaceAll(annotation, " ", ""), ",")
}
func getAliasFromAnnotations(annotations map[string]string) bool {
aliasAnnotation, exists := annotations[aliasAnnotationKey]
func getAliasFromAnnotations(ants map[string]string) bool {
aliasAnnotation, exists := ants[aliasAnnotationKey]
return exists && aliasAnnotation == "true"
}
func getProviderSpecificAnnotations(annotations map[string]string) (endpoint.ProviderSpecific, string) {
func getProviderSpecificAnnotations(ants map[string]string) (endpoint.ProviderSpecific, string) {
providerSpecificAnnotations := endpoint.ProviderSpecific{}
if v, exists := annotations[CloudflareProxiedKey]; exists {
if v, exists := ants[CloudflareProxiedKey]; exists {
providerSpecificAnnotations = append(providerSpecificAnnotations, endpoint.ProviderSpecificProperty{
Name: CloudflareProxiedKey,
Value: v,
})
}
if v, exists := annotations[CloudflareCustomHostnameKey]; exists {
if v, exists := ants[CloudflareCustomHostnameKey]; exists {
providerSpecificAnnotations = append(providerSpecificAnnotations, endpoint.ProviderSpecificProperty{
Name: CloudflareCustomHostnameKey,
Value: v,
})
}
if v, exists := annotations[CloudflareRegionKey]; exists {
if v, exists := ants[CloudflareRegionKey]; exists {
providerSpecificAnnotations = append(providerSpecificAnnotations, endpoint.ProviderSpecificProperty{
Name: CloudflareRegionKey,
Value: v,
})
}
if getAliasFromAnnotations(annotations) {
if getAliasFromAnnotations(ants) {
providerSpecificAnnotations = append(providerSpecificAnnotations, endpoint.ProviderSpecificProperty{
Name: "alias",
Value: "true",
})
}
setIdentifier := ""
for k, v := range annotations {
for k, v := range ants {
if k == SetIdentifierKey {
setIdentifier = v
} else if strings.HasPrefix(k, "external-dns.alpha.kubernetes.io/aws-") {
@ -346,8 +346,7 @@ func getLabelSelector(annotationFilter string) (labels.Selector, error) {
}
func matchLabelSelector(selector labels.Selector, srcAnnotations map[string]string) bool {
annotations := labels.Set(srcAnnotations)
return selector.Matches(annotations)
return selector.Matches(labels.Set(srcAnnotations))
}
type eventHandlerFunc func()

View File

@ -554,11 +554,8 @@ func (ts *traefikSource) filterIngressRouteByAnnotation(ingressRoutes []*Ingress
filteredList := []*IngressRoute{}
for _, ingressRoute := range ingressRoutes {
// convert the IngressRoute's annotations to an equivalent label selector
annotations := labels.Set(ingressRoute.Annotations)
// include IngressRoute if its annotations match the selector
if selector.Matches(annotations) {
if selector.Matches(labels.Set(ingressRoute.Annotations)) {
filteredList = append(filteredList, ingressRoute)
}
}
@ -582,14 +579,11 @@ func (ts *traefikSource) filterIngressRouteTcpByAnnotations(ingressRoutes []*Ing
return ingressRoutes, nil
}
filteredList := []*IngressRouteTCP{}
var filteredList []*IngressRouteTCP
for _, ingressRoute := range ingressRoutes {
// convert the IngressRoute's annotations to an equivalent label selector
annotations := labels.Set(ingressRoute.Annotations)
// include IngressRoute if its annotations match the selector
if selector.Matches(annotations) {
if selector.Matches(labels.Set(ingressRoute.Annotations)) {
filteredList = append(filteredList, ingressRoute)
}
}
@ -613,14 +607,11 @@ func (ts *traefikSource) filterIngressRouteUdpByAnnotations(ingressRoutes []*Ing
return ingressRoutes, nil
}
filteredList := []*IngressRouteUDP{}
var filteredList []*IngressRouteUDP
for _, ingressRoute := range ingressRoutes {
// convert the IngressRoute's annotations to an equivalent label selector
annotations := labels.Set(ingressRoute.Annotations)
// include IngressRoute if its annotations match the selector
if selector.Matches(annotations) {
if selector.Matches(labels.Set(ingressRoute.Annotations)) {
filteredList = append(filteredList, ingressRoute)
}
}