@@ -744,129 +744,113 @@ func (nsc *NetworkServicesController) Collect(ch chan<- prometheus.Metric) {
744
744
}
745
745
}()
746
746
747
+ serviceMap := map [string ]* serviceInfo {}
748
+ for _ , svc := range nsc .getServiceMap () {
749
+ uPort , err := safecast .ToUint16 (svc .port )
750
+ if err != nil {
751
+ klog .Errorf ("failed to convert port %d to uint16: %v" , svc .port , err )
752
+ continue
753
+ }
754
+ protocol := convertSvcProtoToSysCallProto (svc .protocol )
755
+
756
+ serviceMap [fmt .Sprintf ("%s:%d/%d" , svc .clusterIP .String (), uPort , protocol )] = svc
757
+ serviceMap [fmt .Sprintf ("%s:%d/%d" , nsc .krNode .GetPrimaryNodeIP ().String (), uPort , protocol )] = svc
758
+ }
759
+
747
760
ipvsSvcs , err := nsc .ln .ipvsGetServices ()
748
761
if err != nil {
749
762
klog .Errorf ("failed to list IPVS services: %v" , err )
750
763
return
751
764
}
752
765
753
766
klog .V (1 ).Info ("Publishing IPVS metrics" )
754
- for _ , svc := range nsc .getServiceMap () {
755
- var protocol uint16
756
- var pushMetric bool
757
- var svcVip string
758
-
759
- protocol = convertSvcProtoToSysCallProto (svc .protocol )
760
- for _ , ipvsSvc := range ipvsSvcs {
761
-
762
- uPort , err := safecast .ToUint16 (svc .port )
763
- if err != nil {
764
- klog .Errorf ("failed to convert port %d to uint16: %v" , svc .port , err )
765
- }
766
- switch svcAddress := ipvsSvc .Address .String (); svcAddress {
767
- case svc .clusterIP .String ():
768
- if protocol == ipvsSvc .Protocol && uPort == ipvsSvc .Port {
769
- pushMetric = true
770
- svcVip = svc .clusterIP .String ()
771
- } else {
772
- pushMetric = false
773
- }
774
- case nsc .krNode .GetPrimaryNodeIP ().String ():
775
- if protocol == ipvsSvc .Protocol && uPort == ipvsSvc .Port {
776
- pushMetric = true
777
- svcVip = nsc .krNode .GetPrimaryNodeIP ().String ()
778
- } else {
779
- pushMetric = false
780
- }
781
- default :
782
- svcVip = ""
783
- pushMetric = false
784
- }
785
-
786
- if pushMetric {
787
-
788
- klog .V (3 ).Infof ("Publishing metrics for %s/%s (%s:%d/%s)" ,
789
- svc .namespace , svc .name , svcVip , svc .port , svc .protocol )
790
-
791
- labelValues := []string {
792
- svc .namespace ,
793
- svc .name ,
794
- svcVip ,
795
- svc .protocol ,
796
- strconv .Itoa (svc .port ),
797
- }
798
-
799
- ch <- prometheus .MustNewConstMetric (
800
- metrics .ServiceBpsIn ,
801
- prometheus .GaugeValue ,
802
- float64 (ipvsSvc .Stats .BPSIn ),
803
- labelValues ... ,
804
- )
805
-
806
- ch <- prometheus .MustNewConstMetric (
807
- metrics .ServiceBpsOut ,
808
- prometheus .GaugeValue ,
809
- float64 (ipvsSvc .Stats .BPSOut ),
810
- labelValues ... ,
811
- )
812
-
813
- ch <- prometheus .MustNewConstMetric (
814
- metrics .ServiceBytesIn ,
815
- prometheus .CounterValue ,
816
- float64 (ipvsSvc .Stats .BytesIn ),
817
- labelValues ... ,
818
- )
819
-
820
- ch <- prometheus .MustNewConstMetric (
821
- metrics .ServiceBytesOut ,
822
- prometheus .CounterValue ,
823
- float64 (ipvsSvc .Stats .BytesOut ),
824
- labelValues ... ,
825
- )
826
-
827
- ch <- prometheus .MustNewConstMetric (
828
- metrics .ServiceCPS ,
829
- prometheus .GaugeValue ,
830
- float64 (ipvsSvc .Stats .CPS ),
831
- labelValues ... ,
832
- )
833
-
834
- ch <- prometheus .MustNewConstMetric (
835
- metrics .ServicePacketsIn ,
836
- prometheus .CounterValue ,
837
- float64 (ipvsSvc .Stats .PacketsIn ),
838
- labelValues ... ,
839
- )
840
-
841
- ch <- prometheus .MustNewConstMetric (
842
- metrics .ServicePacketsOut ,
843
- prometheus .CounterValue ,
844
- float64 (ipvsSvc .Stats .PacketsOut ),
845
- labelValues ... ,
846
- )
847
-
848
- ch <- prometheus .MustNewConstMetric (
849
- metrics .ServicePpsIn ,
850
- prometheus .GaugeValue ,
851
- float64 (ipvsSvc .Stats .PPSIn ),
852
- labelValues ... ,
853
- )
854
-
855
- ch <- prometheus .MustNewConstMetric (
856
- metrics .ServicePpsOut ,
857
- prometheus .GaugeValue ,
858
- float64 (ipvsSvc .Stats .PPSOut ),
859
- labelValues ... ,
860
- )
861
-
862
- ch <- prometheus .MustNewConstMetric (
863
- metrics .ServiceTotalConn ,
864
- prometheus .CounterValue ,
865
- float64 (ipvsSvc .Stats .Connections ),
866
- labelValues ... ,
867
- )
868
- }
767
+ for _ , ipvsSvc := range ipvsSvcs {
768
+ svcVip := ipvsSvc .Address .String ()
769
+ svc , ok := serviceMap [fmt .Sprintf ("%s:%d/%d" , svcVip , ipvsSvc .Port , ipvsSvc .Protocol )]
770
+ if ! ok {
771
+ continue
869
772
}
773
+
774
+ klog .V (3 ).Infof ("Publishing metrics for %s/%s (%s:%d/%s)" ,
775
+ svc .namespace , svc .name , svcVip , svc .port , svc .protocol )
776
+
777
+ labelValues := []string {
778
+ svc .namespace ,
779
+ svc .name ,
780
+ svcVip ,
781
+ svc .protocol ,
782
+ strconv .Itoa (svc .port ),
783
+ }
784
+
785
+ ch <- prometheus .MustNewConstMetric (
786
+ metrics .ServiceBpsIn ,
787
+ prometheus .GaugeValue ,
788
+ float64 (ipvsSvc .Stats .BPSIn ),
789
+ labelValues ... ,
790
+ )
791
+
792
+ ch <- prometheus .MustNewConstMetric (
793
+ metrics .ServiceBpsOut ,
794
+ prometheus .GaugeValue ,
795
+ float64 (ipvsSvc .Stats .BPSOut ),
796
+ labelValues ... ,
797
+ )
798
+
799
+ ch <- prometheus .MustNewConstMetric (
800
+ metrics .ServiceBytesIn ,
801
+ prometheus .CounterValue ,
802
+ float64 (ipvsSvc .Stats .BytesIn ),
803
+ labelValues ... ,
804
+ )
805
+
806
+ ch <- prometheus .MustNewConstMetric (
807
+ metrics .ServiceBytesOut ,
808
+ prometheus .CounterValue ,
809
+ float64 (ipvsSvc .Stats .BytesOut ),
810
+ labelValues ... ,
811
+ )
812
+
813
+ ch <- prometheus .MustNewConstMetric (
814
+ metrics .ServiceCPS ,
815
+ prometheus .GaugeValue ,
816
+ float64 (ipvsSvc .Stats .CPS ),
817
+ labelValues ... ,
818
+ )
819
+
820
+ ch <- prometheus .MustNewConstMetric (
821
+ metrics .ServicePacketsIn ,
822
+ prometheus .CounterValue ,
823
+ float64 (ipvsSvc .Stats .PacketsIn ),
824
+ labelValues ... ,
825
+ )
826
+
827
+ ch <- prometheus .MustNewConstMetric (
828
+ metrics .ServicePacketsOut ,
829
+ prometheus .CounterValue ,
830
+ float64 (ipvsSvc .Stats .PacketsOut ),
831
+ labelValues ... ,
832
+ )
833
+
834
+ ch <- prometheus .MustNewConstMetric (
835
+ metrics .ServicePpsIn ,
836
+ prometheus .GaugeValue ,
837
+ float64 (ipvsSvc .Stats .PPSIn ),
838
+ labelValues ... ,
839
+ )
840
+
841
+ ch <- prometheus .MustNewConstMetric (
842
+ metrics .ServicePpsOut ,
843
+ prometheus .GaugeValue ,
844
+ float64 (ipvsSvc .Stats .PPSOut ),
845
+ labelValues ... ,
846
+ )
847
+
848
+ ch <- prometheus .MustNewConstMetric (
849
+ metrics .ServiceTotalConn ,
850
+ prometheus .CounterValue ,
851
+ float64 (ipvsSvc .Stats .Connections ),
852
+ labelValues ... ,
853
+ )
870
854
}
871
855
872
856
ch <- prometheus .MustNewConstMetric (
0 commit comments