Skip to content

Commit 64fd8c4

Browse files
committed
avoid iterating over annotations for each svc port
Signed-off-by: Ziv Nevo <[email protected]>
1 parent b22e261 commit 64fd8c4

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

pkg/analyzer/info_to_resource.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,10 @@ func k8sServiceFromInfo(info *resource.Info) (*Service, error) {
106106
serviceCtx.Resource.Selectors = matchLabelSelectorToStrLabels(svcObj.Spec.Selector)
107107
serviceCtx.Resource.ExposeExternally = (svcObj.Spec.Type == v1.ServiceTypeLoadBalancer || svcObj.Spec.Type == v1.ServiceTypeNodePort)
108108

109+
prometheusPort, prometheusPortValid := exposedPrometheusScrapePort(svcObj.Annotations)
109110
for _, p := range svcObj.Spec.Ports {
110111
n := SvcNetworkAttr{Port: int(p.Port), TargetPort: p.TargetPort, Protocol: p.Protocol, name: p.Name}
111-
n.exposeToCluster = exposesPrometheusScrapePort(&n, svcObj.Annotations)
112+
n.exposeToCluster = prometheusPortValid && n.equals(prometheusPort)
112113
serviceCtx.Resource.Network = append(serviceCtx.Resource.Network, n)
113114
}
114115

@@ -117,7 +118,7 @@ func k8sServiceFromInfo(info *resource.Info) (*Service, error) {
117118

118119
const defaultPrometheusScrapePort = 9090
119120

120-
func exposesPrometheusScrapePort(port *SvcNetworkAttr, annotations map[string]string) bool {
121+
func exposedPrometheusScrapePort(annotations map[string]string) (*intstr.IntOrString, bool) {
121122
scrapeOn := false
122123
scrapePort := intstr.FromInt(defaultPrometheusScrapePort)
123124
for k, v := range annotations {
@@ -130,7 +131,7 @@ func exposesPrometheusScrapePort(port *SvcNetworkAttr, annotations map[string]st
130131
}
131132
}
132133

133-
return scrapeOn && port.equals(&scrapePort)
134+
return &scrapePort, scrapeOn
134135
}
135136

136137
func (port *SvcNetworkAttr) equals(intStrPort *intstr.IntOrString) bool {

0 commit comments

Comments
 (0)