Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion telemetry/metadata_test.json
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
{"location":"eastus","name":"k8s-agentpool1-42685608-0","offer":"aks","osType":"Linux","placementGroupId":"","platformFaultDomain":"0","platformUpdateDomain":"0","publisher":"microsoft-aks","resourceGroupName":"rgcnideftesttamil","sku":"aks-ubuntu-1604-201902","subscriptionId":"ea821859-912a-4d20-a4dd-e18a3ce5ba2c","tags":"aksEngineVersion:canary;creationSource:aksengine-k8s-agentpool1-42685608-0;orchestrator:Kubernetes:1.10.13;poolName:agentpool1;resourceNameSuffix:42685608","version":"2019.02.12","vmId":"6baf785b-397c-4967-9f75-cdb3d0df66c4","vmSize":"Standard_DS2_v2","KernelVersion":""}
{
"location": "eastus",
"name": "k8s-agentpool1-42685608-0",
"offer": "aks",
"osType": "Linux",
"placementGroupId": "",
"platformFaultDomain": "0",
"platformUpdateDomain": "0",
"publisher": "microsoft-aks",
"resourceGroupName": "rgcnideftesttamil",
"sku": "aks-ubuntu-1604-201902",
"subscriptionId": "ea821859-912a-4d20-a4dd-e18a3ce5ba2c",
"tags": "aksEngineVersion:canary;creationSource:aksengine-k8s-agentpool1-42685608-0;orchestrator:Kubernetes:1.10.13;poolName:agentpool1;resourceNameSuffix:42685608",
"version": "2019.02.12",
"vmId": "6baf785b-397c-4967-9f75-cdb3d0df66c4",
"vmSize": "Standard_DS2_v2",
"KernelVersion": ""
}
222 changes: 20 additions & 202 deletions telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,14 @@ package telemetry

import (
"encoding/json"
"encoding/xml"
"fmt"
"net"
"net/http"
"os"
"os/exec"
"reflect"
"strings"

"github.com/Azure/azure-container-networking/aitelemetry"
"github.com/Azure/azure-container-networking/common"
"github.com/Azure/azure-container-networking/log"
"github.com/Azure/azure-container-networking/platform"
)

const (
// NPMTelemetryFile Path.
NPMTelemetryFile = platform.NPMRuntimePath + "AzureNPMTelemetry.json"
// CNITelemetryFile Path.
CNITelemetryFile = platform.CNIRuntimePath + "AzureCNITelemetry.json"
// ContentType of JSON
Expand Down Expand Up @@ -68,36 +58,28 @@ type BridgeInfo struct {
ErrorMessage string
}

// Orchestrator Details structure.
type OrchestratorInfo struct {
OrchestratorName string
OrchestratorVersion string
ErrorMessage string
}

// Azure CNI Telemetry Report structure.
type CNIReport struct {
IsNewInstance bool
CniSucceeded bool
Name string
Version string
ErrorMessage string
EventMessage string
OperationType string
OperationDuration int
Context string
SubContext string
VMUptime string
Timestamp string
ContainerName string
InfraVnetID string
VnetAddressSpace []string
OrchestratorDetails OrchestratorInfo
OSDetails OSInfo
SystemDetails SystemInfo
InterfaceDetails InterfaceInfo
BridgeDetails BridgeInfo
Metadata common.Metadata `json:"compute"`
IsNewInstance bool
CniSucceeded bool
Name string
Version string
ErrorMessage string
EventMessage string
OperationType string
OperationDuration int
Context string
SubContext string
VMUptime string
Timestamp string
ContainerName string
InfraVnetID string
VnetAddressSpace []string
OSDetails OSInfo
SystemDetails SystemInfo
InterfaceDetails InterfaceInfo
BridgeDetails BridgeInfo
Metadata common.Metadata `json:"compute"`
}

type AIMetric struct {
Expand Down Expand Up @@ -138,170 +120,6 @@ func (reportMgr *ReportManager) SendReport(tb *TelemetryBuffer) error {
return err
}

// SetReportState will save the state in file if telemetry report sent successfully.
func (reportMgr *ReportManager) SetReportState(telemetryFile string) error {
var reportBytes []byte
var err error

reportBytes, err = json.Marshal(reportMgr.Report)
if err != nil {
return fmt.Errorf("[Telemetry] report write failed with err %+v", err)
}

// try to open telemetry file
f, err := os.OpenFile(telemetryFile, os.O_RDWR|os.O_CREATE, 0o666)
if err != nil {
return fmt.Errorf("[Telemetry] Error opening telemetry file %v", err)
}

defer f.Close()

_, err = f.Write(reportBytes)
if err != nil {
fmt.Printf("[Telemetry] Error while writing to file %v", err)
return fmt.Errorf("[Telemetry] Error while writing to file %v", err)
}

// set IsNewInstance in report
reflect.ValueOf(reportMgr.Report).Elem().FieldByName("IsNewInstance").SetBool(false)
return nil
}

// GetReportState will check if report is sent at least once by checking telemetry file.
func (reportMgr *ReportManager) GetReportState(telemetryFile string) bool {
// try to set IsNewInstance in report
if _, err := os.Stat(telemetryFile); os.IsNotExist(err) {
fmt.Printf("[Telemetry] File not exist %v", telemetryFile)
reflect.ValueOf(reportMgr.Report).Elem().FieldByName("IsNewInstance").SetBool(true)
return false
}

return true
}

// GetInterfaceDetails creates a report with interface details(ip, mac, name, secondaryca count).
func (report *CNIReport) GetInterfaceDetails(queryUrl string) {
var (
macAddress string
secondaryCACount int
primaryCA string
subnet string
ifName string
)

if queryUrl == "" {
report.InterfaceDetails.ErrorMessage = "IpamQueryUrl is null"
return
}

interfaces, err := net.Interfaces()
if err != nil {
report.InterfaceDetails.ErrorMessage = "Getting all interfaces failed due to " + err.Error()
return
}

resp, err := http.Get(queryUrl)
if err != nil {
report.InterfaceDetails.ErrorMessage = "Http get failed in getting interface details " + err.Error()
return
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
errMsg := fmt.Sprintf("Error while getting interface details. http code :%d", resp.StatusCode)
report.InterfaceDetails.ErrorMessage = errMsg
log.Logf(errMsg)
return
}

// Decode XML document.
var doc common.XmlDocument
decoder := xml.NewDecoder(resp.Body)
err = decoder.Decode(&doc)
if err != nil {
report.InterfaceDetails.ErrorMessage = "xml decode failed due to " + err.Error()
return
}

// For each interface...
for _, i := range doc.Interface {
i.MacAddress = strings.ToLower(i.MacAddress)

if i.IsPrimary {
// Find the interface with the matching MacAddress.
for _, iface := range interfaces {
macAddr := strings.Replace(iface.HardwareAddr.String(), ":", "", -1)
macAddr = strings.ToLower(macAddr)
if macAddr == i.MacAddress {
ifName = iface.Name
macAddress = iface.HardwareAddr.String()
}
}

for _, s := range i.IPSubnet {
for _, ip := range s.IPAddress {
if ip.IsPrimary {
primaryCA = ip.Address
subnet = s.Prefix
} else {
secondaryCACount++
}
}
}

break
}
}

report.InterfaceDetails = InterfaceInfo{
InterfaceType: "Primary",
MAC: macAddress,
Subnet: subnet,
Name: ifName,
PrimaryCA: primaryCA,
SecondaryCATotalCount: secondaryCACount,
}
}

// GetOrchestratorDetails creates a report with orchestrator details(name, version).
func (report *CNIReport) GetOrchestratorDetails() {
// to-do: GetOrchestratorDetails for all report types and for all k8s environments
// current implementation works for clusters created via acs-engine and on master nodes
report.OrchestratorDetails = OrchestratorInfo{}

// Check for orchestrator tag first
for _, tag := range strings.Split(report.Metadata.Tags, ";") {
if strings.Contains(tag, "orchestrator") {
details := strings.Split(tag, ":")
if len(details) != 2 {
report.OrchestratorDetails.ErrorMessage = "length of orchestrator tag is less than 2"
} else {
report.OrchestratorDetails.OrchestratorName = details[0]
report.OrchestratorDetails.OrchestratorVersion = details[1]
}
} else {
report.OrchestratorDetails.ErrorMessage = "Host metadata unavailable"
}
}

if report.OrchestratorDetails.ErrorMessage != "" {
out, err := exec.Command("kubectl", "version").Output()
if err != nil {
report.OrchestratorDetails.ErrorMessage = "kubectl command failed due to " + err.Error()
return
}

resultArray := strings.Split(strings.TrimLeft(string(out), " "), " ")
if len(resultArray) >= 2 {
report.OrchestratorDetails.OrchestratorName = resultArray[0]
report.OrchestratorDetails.OrchestratorVersion = resultArray[1]
} else {
report.OrchestratorDetails.ErrorMessage = "Length of array is less than 2"
}
}
}

// ReportToBytes - returns the report bytes
func (reportMgr *ReportManager) ReportToBytes() ([]byte, error) {
var err error
Expand Down
12 changes: 0 additions & 12 deletions telemetry/telemetry_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package telemetry
import (
"fmt"
"os/exec"
"reflect"
"runtime"
"strings"
"syscall"
Expand Down Expand Up @@ -114,14 +113,3 @@ func (report *CNIReport) GetOSDetails() {
OSDistribution: osInfoArr["ID"],
}
}

// Get kernel version
func (reportMgr *ReportManager) GetKernelVersion() {
out, err := exec.Command("uname", "-r").Output()
if err == nil {
v := reflect.ValueOf(reportMgr.Report).Elem().FieldByName("Metadata")
if v.CanSet() {
v.FieldByName("KernelVersion").SetString(strings.TrimSuffix(string(out), "\n"))
}
}
}
19 changes: 0 additions & 19 deletions telemetry/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,6 @@ func TestGetInterfaceDetails(t *testing.T) {
}
}

func TestGetReportState(t *testing.T) {
state := reportManager.GetReportState(CNITelemetryFile)
if state != false {
t.Errorf("Wrong state in getreport state")
}
}

func TestSendTelemetry(t *testing.T) {
err := reportManager.SendReport(tb)
if err != nil {
Expand Down Expand Up @@ -276,15 +269,3 @@ func TestStartTelemetryService(t *testing.T) {
func TestWaitForTelemetrySocket(t *testing.T) {
WaitForTelemetrySocket(1, 10)
}

func TestSetReportState(t *testing.T) {
err := reportManager.SetReportState("a.json")
if err != nil {
t.Errorf("SetReportState failed due to %v", err)
}

err = os.Remove("a.json")
if err != nil {
t.Errorf("Error removing telemetry file due to %v", err)
}
}
5 changes: 0 additions & 5 deletions telemetry/telemetry_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,3 @@ func (report *CNIReport) GetOSDetails() {
report.OSDetails.OSVersion = strings.Replace(out, delimiter, "", -1)
}
}

// Get kernel version
func (reportMgr *ReportManager) GetKernelVersion() {
// stub
}
11 changes: 0 additions & 11 deletions telemetry/telemetrybuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,6 @@ func (tb *TelemetryBuffer) ConnectToTelemetryService(telemetryNumRetries, teleme
}
}

// TryToConnectToTelemetryService - Attempt to connect telemetry process without spawning it if it's not already running.
func (tb *TelemetryBuffer) TryToConnectToTelemetryService() {
if err := tb.Connect(); err != nil {
log.Logf("Connection to telemetry socket failed: %v", err)
return
}

tb.Connected = true
log.Logf("Connected to telemetry service")
}

func getTelemetryServiceDirectory() (path string, dir string) {
path = fmt.Sprintf("%v/%v", CniInstallDir, TelemetryServiceProcessName)
if exists, _ := platform.CheckIfFileExists(path); !exists {
Expand Down