Skip to content

Commit d28b594

Browse files
authored
Moving dnc specific material out. (#233)
1 parent 45914be commit d28b594

File tree

3 files changed

+18
-109
lines changed

3 files changed

+18
-109
lines changed

cni/network/plugin/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var version string
2525

2626
// If report write succeeded, mark the report flag state to false.
2727
func markSendReport(reportManager *telemetry.ReportManager) {
28-
if err := reportManager.SetReportState(); err != nil {
28+
if err := reportManager.SetReportState(telemetry.CNITelemetryFile); err != nil {
2929
log.Printf("SetReportState failed due to %v", err)
3030
reflect.ValueOf(reportManager.Report).Elem().FieldByName("ErrorMessage").SetString(err.Error())
3131

@@ -67,7 +67,7 @@ func main() {
6767
reportManager.GetHostMetadata()
6868
reportManager.Report.(*telemetry.CNIReport).GetReport(pluginName, config.Version, ipamQueryURL)
6969

70-
if !reportManager.GetReportState() {
70+
if !reportManager.GetReportState(telemetry.CNITelemetryFile) {
7171
log.Printf("GetReport state file didn't exist. Setting flag to true")
7272

7373
err = reportManager.SendReport()

telemetry/telemetry.go

Lines changed: 12 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ import (
2323
)
2424

2525
const (
26+
// NPMTelemetryFile Path.
27+
NPMTelemetryFile = platform.NPMRuntimePath + "AzureNPMTelemetry.json"
28+
// CNITelemetryFile Path.
29+
CNITelemetryFile = platform.CNIRuntimePath + "AzureCNITelemetry.json"
30+
2631
metadataURL = "http://169.254.169.254/metadata/instance?api-version=2017-08-01&format=json"
2732
ContentType = "application/json"
2833
)
@@ -73,24 +78,6 @@ type OrchestratorInfo struct {
7378
ErrorMessage string
7479
}
7580

76-
const (
77-
// CNITelemetryFile Path.
78-
CNITelemetryFile = platform.CNIRuntimePath + "AzureCNITelemetry.json"
79-
// NPMTelemetryFile Path.
80-
NPMTelemetryFile = platform.NPMRuntimePath + "AzureNPMTelemetry.json"
81-
// DNCTelemetryFile Path.
82-
DNCTelemetryFile = platform.DNCRuntimePath + "AzureDNCTelemetry.json"
83-
)
84-
85-
const (
86-
// CNI report type
87-
CNIReportType = "CNI"
88-
// NPM report type
89-
NPMReportType = "NPM"
90-
// DNC report type
91-
DNCReportType = "DNC"
92-
)
93-
9481
// Metadata retrieved from wireserver
9582
type Metadata struct {
9683
Location string `json:"location"`
@@ -155,18 +142,6 @@ type NPMReport struct {
155142
Metadata Metadata `json:"compute"`
156143
}
157144

158-
// DNCReport structure.
159-
type DNCReport struct {
160-
IsNewInstance bool
161-
CPUUsage string
162-
MemoryUsage string
163-
Processes string
164-
EventMessage string
165-
PartitionKey string
166-
Allocations string
167-
Metadata Metadata `json:"compute"`
168-
}
169-
170145
// ReportManager structure.
171146
type ReportManager struct {
172147
HostNetAgentURL string
@@ -230,24 +205,18 @@ func (report *NPMReport) GetReport(clusterID, nodeName, npmVersion, kubernetesVe
230205
func (reportMgr *ReportManager) SendReport() error {
231206
log.Printf("[Telemetry] Going to send Telemetry report to hostnetagent %v", reportMgr.HostNetAgentURL)
232207

233-
var body bytes.Buffer
234-
httpc := &http.Client{}
235-
json.NewEncoder(&body).Encode(reportMgr.Report)
236-
237208
switch reportMgr.Report.(type) {
238209
case *CNIReport:
239210
log.Printf("[Telemetry] %+v", reportMgr.Report.(*CNIReport))
240-
241211
case *NPMReport:
242212
log.Printf("[Telemetry] %+v", reportMgr.Report.(*NPMReport))
243-
244-
case *DNCReport:
245-
log.Printf("[Telemetry] %+v", reportMgr.Report.(*DNCReport))
246-
247213
default:
248-
return fmt.Errorf("[Telemetry] failed to resolve report type")
214+
log.Printf("[Telemetry] %+v", reportMgr.Report)
249215
}
250216

217+
httpc := &http.Client{}
218+
var body bytes.Buffer
219+
json.NewEncoder(&body).Encode(reportMgr.Report)
251220
resp, err := httpc.Post(reportMgr.HostNetAgentURL, reportMgr.ContentType, &body)
252221
if err != nil {
253222
return fmt.Errorf("[Telemetry] HTTP Post returned error %v", err)
@@ -271,29 +240,11 @@ func (reportMgr *ReportManager) SendReport() error {
271240
}
272241

273242
// SetReportState will save the state in file if telemetry report sent successfully.
274-
func (reportMgr *ReportManager) SetReportState() error {
275-
var telemetryFile string
243+
func (reportMgr *ReportManager) SetReportState(telemetryFile string) error {
276244
var reportBytes []byte
277245
var err error
278246

279-
// get bytes from associated report type
280-
switch reportMgr.Report.(type) {
281-
case *CNIReport:
282-
telemetryFile = CNITelemetryFile
283-
reportBytes, err = json.Marshal(reportMgr.Report.(*CNIReport))
284-
285-
case *NPMReport:
286-
telemetryFile = NPMTelemetryFile
287-
reportBytes, err = json.Marshal(reportMgr.Report.(*NPMReport))
288-
289-
case *DNCReport:
290-
telemetryFile = DNCTelemetryFile
291-
reportBytes, err = json.Marshal(reportMgr.Report.(*DNCReport))
292-
293-
default:
294-
return fmt.Errorf("[Telemetry] Invalid report type")
295-
}
296-
247+
reportBytes, err = json.Marshal(reportMgr.Report)
297248
if err != nil {
298249
return fmt.Errorf("[Telemetry] report write failed with err %+v", err)
299250
}
@@ -319,24 +270,7 @@ func (reportMgr *ReportManager) SetReportState() error {
319270
}
320271

321272
// GetReportState will check if report is sent at least once by checking telemetry file.
322-
func (reportMgr *ReportManager) GetReportState() bool {
323-
var telemetryFile string
324-
325-
switch reportMgr.Report.(type) {
326-
case *CNIReport:
327-
telemetryFile = CNITelemetryFile
328-
329-
case *NPMReport:
330-
telemetryFile = NPMTelemetryFile
331-
332-
case *DNCReport:
333-
telemetryFile = DNCTelemetryFile
334-
335-
default:
336-
log.Printf("[Telemetry] Invalid report type")
337-
return false
338-
}
339-
273+
func (reportMgr *ReportManager) GetReportState(telemetryFile string) bool {
340274
// try to set IsNewInstance in report
341275
if _, err := os.Stat(telemetryFile); os.IsNotExist(err) {
342276
log.Printf("[Telemetry] File not exist %v", telemetryFile)
@@ -518,28 +452,3 @@ func (reportMgr *ReportManager) GetHostMetadata() error {
518452

519453
return err
520454
}
521-
522-
// GetSystemDetails - retrieve system details like cpu usage, mem usage, and number or running processes
523-
func (reportMgr *ReportManager) GetSystemDetails() error {
524-
// hostStat, err := host.Info()
525-
// if err != nil {
526-
// return err
527-
// }
528-
529-
// reflect.ValueOf(reportMgr.Report).Elem().FieldByName("Processes").SetString(strconv.FormatUint(hostStat.Procs, 10))
530-
531-
// cpuStat, err := cpu.Percent(0, false)
532-
// if err != nil {
533-
// return err
534-
// }
535-
536-
// reflect.ValueOf(reportMgr.Report).Elem().FieldByName("CPUUsage").SetString(strconv.FormatFloat(cpuStat[0], 'f', 2, 64))
537-
538-
// memStat, err := mem.VirtualMemory()
539-
// if err != nil {
540-
// return err
541-
// }
542-
543-
// reflect.ValueOf(reportMgr.Report).Elem().FieldByName("MemoryUsage").SetString(strconv.FormatFloat(memStat.UsedPercent, 'f', 2, 64))
544-
return nil
545-
}

telemetry/telemetry_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,20 @@ func TestGetOSDetails(t *testing.T) {
101101
}
102102
}
103103
func TestGetSystemDetails(t *testing.T) {
104-
reportManager.GetSystemDetails()
104+
reportManager.Report.(*CNIReport).GetSystemDetails()
105105
if reportManager.Report.(*CNIReport).ErrorMessage != "" {
106106
t.Errorf("GetSystemDetails failed due to %v", reportManager.Report.(*CNIReport).ErrorMessage)
107107
}
108108
}
109109
func TestGetInterfaceDetails(t *testing.T) {
110-
reportManager.GetSystemDetails()
110+
reportManager.Report.(*CNIReport).GetSystemDetails()
111111
if reportManager.Report.(*CNIReport).ErrorMessage != "" {
112112
t.Errorf("GetInterfaceDetails failed due to %v", reportManager.Report.(*CNIReport).ErrorMessage)
113113
}
114114
}
115115

116116
func TestGetReportState(t *testing.T) {
117-
state := reportManager.GetReportState()
117+
state := reportManager.GetReportState(CNITelemetryFile)
118118
if state != false {
119119
t.Errorf("Wrong state in getreport state")
120120
}
@@ -128,7 +128,7 @@ func TestSendTelemetry(t *testing.T) {
128128
}
129129

130130
func TestSetReportState(t *testing.T) {
131-
err := reportManager.SetReportState()
131+
err := reportManager.SetReportState(CNITelemetryFile)
132132
if err != nil {
133133
t.Errorf("SetReportState failed due to %v", err)
134134
}

0 commit comments

Comments
 (0)