@@ -5,6 +5,7 @@ package npm
55import (
66 "fmt"
77 "os"
8+ "reflect"
89 "sync"
910 "time"
1011
@@ -22,7 +23,7 @@ import (
2223
2324var (
2425 hostNetAgentURLForNpm = "http://169.254.169.254/machine/plugins?comp=netagent&type=npmreport"
25- reportType = "application/json"
26+ contentType = "application/json"
2627)
2728
2829// NetworkPolicyManager contains informers for pod, namespace and networkpolicy.
@@ -40,7 +41,7 @@ type NetworkPolicyManager struct {
4041 isAzureNpmChainCreated bool
4142
4243 clusterState telemetry.ClusterState
43- reportManager * telemetry.NPMReportManager
44+ reportManager * telemetry.ReportManager
4445}
4546
4647// GetClusterState returns current cluster state.
@@ -51,11 +52,18 @@ func (npMgr *NetworkPolicyManager) GetClusterState() telemetry.ClusterState {
5152// UpdateAndSendReport updates the npm report then send it.
5253// This function should only be called when npMgr is locked.
5354func (npMgr * NetworkPolicyManager ) UpdateAndSendReport (err error , eventMsg string ) error {
54- npMgr .reportManager .Report .ClusterState = npMgr .GetClusterState ()
55- npMgr .reportManager .Report .EventMessage = eventMsg
55+ clusterState := npMgr .GetClusterState ()
56+ v := reflect .ValueOf (npMgr .reportManager .Report ).Elem ().FieldByName ("ClusterState" )
57+ if v .CanSet () {
58+ v .FieldByName ("PodCount" ).SetInt (int64 (clusterState .PodCount ))
59+ v .FieldByName ("NsCount" ).SetInt (int64 (clusterState .NsCount ))
60+ v .FieldByName ("NwPolicyCount" ).SetInt (int64 (clusterState .NwPolicyCount ))
61+ }
62+
63+ reflect .ValueOf (npMgr .reportManager .Report ).Elem ().FieldByName ("EventMessage" ).SetString (eventMsg )
5664
5765 if err != nil {
58- npMgr .reportManager .Report . ErrorMessage = err .Error ()
66+ reflect . ValueOf ( npMgr .reportManager .Report ). Elem (). FieldByName ( "EventMessage" ). SetString ( err .Error () )
5967 }
6068
6169 return npMgr .reportManager .SendReport ()
@@ -84,8 +92,19 @@ func (npMgr *NetworkPolicyManager) Run(stopCh <-chan struct{}) error {
8492
8593// RunReportManager starts NPMReportManager and send telemetry periodically.
8694func (npMgr * NetworkPolicyManager ) RunReportManager () {
95+ if err := npMgr .reportManager .GetHostMetadata (); err != nil {
96+ reflect .ValueOf (npMgr .reportManager .Report ).Elem ().FieldByName ("ErrorMessage" ).SetString (err .Error ())
97+ }
98+
8799 for {
88- npMgr .reportManager .Report .ClusterState = npMgr .GetClusterState ()
100+ clusterState := npMgr .GetClusterState ()
101+ v := reflect .ValueOf (npMgr .reportManager .Report ).Elem ().FieldByName ("ClusterState" )
102+ if v .CanSet () {
103+ v .FieldByName ("PodCount" ).SetInt (int64 (clusterState .PodCount ))
104+ v .FieldByName ("NsCount" ).SetInt (int64 (clusterState .NsCount ))
105+ v .FieldByName ("NwPolicyCount" ).SetInt (int64 (clusterState .NwPolicyCount ))
106+ }
107+
89108 if err := npMgr .reportManager .SendReport (); err != nil {
90109 log .Printf ("Error sending NPM telemetry report" )
91110 }
@@ -115,12 +134,10 @@ func NewNetworkPolicyManager(clientset *kubernetes.Clientset, informerFactory in
115134 NsCount : 0 ,
116135 NwPolicyCount : 0 ,
117136 },
118- reportManager : & telemetry.NPMReportManager {
119- ReportManager : & telemetry.ReportManager {
120- HostNetAgentURL : hostNetAgentURLForNpm ,
121- ReportType : reportType ,
122- },
123- Report : & telemetry.NPMReport {},
137+ reportManager : & telemetry.ReportManager {
138+ HostNetAgentURL : hostNetAgentURLForNpm ,
139+ ContentType : contentType ,
140+ Report : & telemetry.NPMReport {},
124141 },
125142 }
126143
@@ -132,7 +149,7 @@ func NewNetworkPolicyManager(clientset *kubernetes.Clientset, informerFactory in
132149
133150 clusterID := util .GetClusterID (npMgr .nodeName )
134151 clusterState := npMgr .GetClusterState ()
135- npMgr .reportManager .GetReport (clusterID , npMgr .nodeName , npmVersion , serverVersion .GitVersion , clusterState )
152+ npMgr .reportManager .Report .( * telemetry. NPMReport ). GetReport (clusterID , npMgr .nodeName , npmVersion , serverVersion .GitVersion , clusterState )
136153
137154 allNs , err := newNs (util .KubeAllNamespacesFlag )
138155 if err != nil {
0 commit comments