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
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,16 @@ publish-azure-npm-image:
.PHONY: cni-archive
cni-archive:
cp cni/azure-$(GOOS).conflist $(CNI_BUILD_DIR)/10-azure.conflist
cp telemetry/azure-vnet-telemetry.config $(CNI_BUILD_DIR)/azure-vnet-telemetry.config
chmod 0755 $(CNI_BUILD_DIR)/azure-vnet$(EXE_EXT) $(CNI_BUILD_DIR)/azure-vnet-ipam$(EXE_EXT) $(CNI_BUILD_DIR)/azure-vnet-telemetry$(EXE_EXT)
cd $(CNI_BUILD_DIR) && $(ARCHIVE_CMD) $(CNI_ARCHIVE_NAME) azure-vnet$(EXE_EXT) azure-vnet-ipam$(EXE_EXT) azure-vnet-telemetry$(EXE_EXT) 10-azure.conflist
cd $(CNI_BUILD_DIR) && $(ARCHIVE_CMD) $(CNI_ARCHIVE_NAME) azure-vnet$(EXE_EXT) azure-vnet-ipam$(EXE_EXT) azure-vnet-telemetry$(EXE_EXT) 10-azure.conflist azure-vnet-telemetry.config
chown $(BUILD_USER):$(BUILD_USER) $(CNI_BUILD_DIR)/$(CNI_ARCHIVE_NAME)
mkdir -p $(CNI_MULTITENANCY_BUILD_DIR)
cp cni/azure-$(GOOS)-multitenancy.conflist $(CNI_MULTITENANCY_BUILD_DIR)/10-azure.conflist
cp $(CNI_BUILD_DIR)/azure-vnet$(EXE_EXT) $(CNI_BUILD_DIR)/azure-vnet-ipam$(EXE_EXT) $(CNI_MULTITENANCY_BUILD_DIR)
cp telemetry/azure-vnet-telemetry.config $(CNI_MULTITENANCY_BUILD_DIR)/azure-vnet-telemetry.config
cp $(CNI_BUILD_DIR)/azure-vnet$(EXE_EXT) $(CNI_BUILD_DIR)/azure-vnet-ipam$(EXE_EXT) $(CNI_BUILD_DIR)/azure-vnet-telemetry$(EXE_EXT) $(CNI_MULTITENANCY_BUILD_DIR)
chmod 0755 $(CNI_MULTITENANCY_BUILD_DIR)/azure-vnet$(EXE_EXT) $(CNI_MULTITENANCY_BUILD_DIR)/azure-vnet-ipam$(EXE_EXT)
cd $(CNI_MULTITENANCY_BUILD_DIR) && $(ARCHIVE_CMD) $(CNI_MULTITENANCY_ARCHIVE_NAME) azure-vnet$(EXE_EXT) azure-vnet-ipam$(EXE_EXT) 10-azure.conflist
cd $(CNI_MULTITENANCY_BUILD_DIR) && $(ARCHIVE_CMD) $(CNI_MULTITENANCY_ARCHIVE_NAME) azure-vnet$(EXE_EXT) azure-vnet-ipam$(EXE_EXT) azure-vnet-telemetry$(EXE_EXT) 10-azure.conflist azure-vnet-telemetry.config
chown $(BUILD_USER):$(BUILD_USER) $(CNI_MULTITENANCY_BUILD_DIR)/$(CNI_MULTITENANCY_ARCHIVE_NAME)

# Create a CNM archive for the target platform.
Expand Down
36 changes: 8 additions & 28 deletions cni/network/plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ import (
)

const (
hostNetAgentURL = "http://168.63.129.16/machine/plugins?comp=netagent&type=cnireport"
ipamQueryURL = "http://168.63.129.16/machine/plugins?comp=nmagent&type=getinterfaceinfov1"
pluginName = "CNI"
hostNetAgentURL = "http://168.63.129.16/machine/plugins?comp=netagent&type=cnireport"
ipamQueryURL = "http://168.63.129.16/machine/plugins?comp=nmagent&type=getinterfaceinfov1"
pluginName = "CNI"
telemetryNumRetries = 5
telemetryWaitTimeInMilliseconds = 200
)

// Version is populated by make during build.
Expand Down Expand Up @@ -133,32 +135,9 @@ func handleIfCniUpdate(update func(*skel.CmdArgs) error) (bool, error) {
return isupdate, nil
}

// startTelemetryService - Kills if any telemetry service runs and start new telemetry service
func startTelemetryService(path string) error {
platform.KillProcessByName(telemetry.TelemetryServiceProcessName)

log.Printf("[cni] Starting telemetry service process")

if err := common.StartProcess(path); err != nil {
log.Printf("[Telemetry] Failed to start telemetry service process :%v", err)
return err
}

log.Printf("[cni] Telemetry service started")

for attempt := 0; attempt < 5; attempt++ {
if telemetry.SockExists() {
break
}

time.Sleep(200 * time.Millisecond)
}

return nil
}

func connectToTelemetryService(tb *telemetry.TelemetryBuffer) {
path := fmt.Sprintf("%v/%v", telemetry.CniInstallDir, telemetry.TelemetryServiceProcessName)
args := []string{"-d", telemetry.CniInstallDir}

for attempt := 0; attempt < 2; attempt++ {
if err := tb.Connect(); err != nil {
Expand All @@ -170,7 +149,8 @@ func connectToTelemetryService(tb *telemetry.TelemetryBuffer) {
return
}

startTelemetryService(path)
telemetry.StartTelemetryService(path, args)
telemetry.WaitForTelemetrySocket(telemetryNumRetries, telemetryWaitTimeInMilliseconds)
} else {
tb.Connected = true
log.Printf("Connected to telemetry service")
Expand Down
121 changes: 118 additions & 3 deletions cni/telemetry/service/telemetrymain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,145 @@ package main
// Entry point of the telemetry service if started by CNI

import (
"fmt"
"os"
"runtime"
"time"

acn "github.com/Azure/azure-container-networking/common"
"github.com/Azure/azure-container-networking/log"
"github.com/Azure/azure-container-networking/telemetry"
)

const (
reportToHostIntervalInSeconds = 60 * time.Second
azurecnitelemetry = "azure-vnet-telemetry"
reportToHostIntervalInSeconds = 30
azureVnetTelemetry = "azure-vnet-telemetry"
configExtension = ".config"
)

var version string

var args = acn.ArgumentList{
{
Name: acn.OptLogLevel,
Shorthand: acn.OptLogLevelAlias,
Description: "Set the logging level",
Type: "int",
DefaultValue: acn.OptLogLevelInfo,
ValueMap: map[string]interface{}{
acn.OptLogLevelInfo: log.LevelInfo,
acn.OptLogLevelDebug: log.LevelDebug,
},
},
{
Name: acn.OptLogTarget,
Shorthand: acn.OptLogTargetAlias,
Description: "Set the logging target",
Type: "int",
DefaultValue: acn.OptLogTargetFile,
ValueMap: map[string]interface{}{
acn.OptLogTargetSyslog: log.TargetSyslog,
acn.OptLogTargetStderr: log.TargetStderr,
acn.OptLogTargetFile: log.TargetLogfile,
acn.OptLogStdout: log.TargetStdout,
acn.OptLogMultiWrite: log.TargetStdOutAndLogFile,
},
},
{
Name: acn.OptLogLocation,
Shorthand: acn.OptLogLocationAlias,
Description: "Set the directory location where logs will be saved",
Type: "string",
DefaultValue: "",
},
{
Name: acn.OptVersion,
Shorthand: acn.OptVersionAlias,
Description: "Print version information",
Type: "bool",
DefaultValue: false,
},
{
Name: acn.OptTelemetryConfigDir,
Shorthand: acn.OptTelemetryConfigDirAlias,
Description: "Set the telmetry config directory",
Type: "string",
DefaultValue: telemetry.CniInstallDir,
},
}

// Prints description and version information.
func printVersion() {
fmt.Printf("Azure Container Telemetry Service\n")
fmt.Printf("Version %v\n", version)
}

func main() {
var tb *telemetry.TelemetryBuffer
var config telemetry.TelemetryConfig
var configPath string
var err error

acn.ParseArgs(&args, printVersion)
logTarget := acn.GetArg(acn.OptLogTarget).(int)
logDirectory := acn.GetArg(acn.OptLogLocation).(string)
logLevel := acn.GetArg(acn.OptLogLevel).(int)
configDirectory := acn.GetArg(acn.OptTelemetryConfigDir).(string)
vers := acn.GetArg(acn.OptVersion).(bool)

if vers {
printVersion()
os.Exit(0)
}

log.SetName(azureVnetTelemetry)
log.SetLevel(logLevel)
if logDirectory != "" {
log.SetLogDirectory(logDirectory)
}

err = log.SetTarget(logTarget)
if err != nil {
fmt.Printf("Failed to configure logging: %v\n", err)
return
}

log.Printf("args %+v", os.Args)

if runtime.GOOS == "linux" {
configPath = fmt.Sprintf("%s/%s%s", configDirectory, azureVnetTelemetry, configExtension)
} else {
configPath = fmt.Sprintf("%s\\%s%s", configDirectory, azureVnetTelemetry, configExtension)
}

log.Printf("[Telemetry] Config path: %s", configPath)

config, err = telemetry.ReadConfigFile(configPath)
if err != nil {
log.Printf("[Telemetry] Error reading telemetry config: %v", err)
}

log.Printf("read config returned %+v", config)

for {
tb = telemetry.NewTelemetryBuffer("")

log.Printf("[Telemetry] Starting telemetry server")
err = tb.StartServer()
if err == nil || tb.FdExists {
break
}

log.Printf("[Telemetry] Telemetry service starting failed: %v", err)
tb.Cleanup(telemetry.FdName)
time.Sleep(time.Millisecond * 200)
}

tb.BufferAndPushData(reportToHostIntervalInSeconds)
if config.ReportToHostIntervalInSeconds == 0 {
config.ReportToHostIntervalInSeconds = reportToHostIntervalInSeconds
}

log.Printf("[Telemetry] Report to host for an interval of %d seconds", config.ReportToHostIntervalInSeconds)
tb.BufferAndPushData(config.ReportToHostIntervalInSeconds * time.Second)
log.Close()
}
4 changes: 4 additions & 0 deletions common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,8 @@ const (
// CNI binary location
OptCNIConfigFile = "cni-config-file"
OptCNIConfigFileAlias = "cniconfig"

// Telemetry config Location
OptTelemetryConfigDir = "telemetry-config-file"
OptTelemetryConfigDirAlias = "d"
)
6 changes: 3 additions & 3 deletions common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func GetInterfaceSubnetWithSpecificIp(ipAddr string) *net.IPNet {
return nil
}

func StartProcess(path string) error {
func StartProcess(path string, args []string) error {
var attr = os.ProcAttr{
Env: os.Environ(),
Files: []*os.File{
Expand All @@ -114,8 +114,8 @@ func StartProcess(path string) error {
},
}

args := []string{path}
process, err := os.StartProcess(path, args, &attr)
processArgs := append([]string{path}, args...)
process, err := os.StartProcess(path, processArgs, &attr)
if err == nil {
// Release detaches the process
return process.Release()
Expand Down
3 changes: 3 additions & 0 deletions telemetry/azure-vnet-telemetry.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"reportToHostIntervalInSeconds": 30
}
1 change: 1 addition & 0 deletions telemetry/metadata_test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"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":""}
2 changes: 1 addition & 1 deletion telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func (report *CNIReport) GetInterfaceDetails(queryUrl string) {
if resp.StatusCode != http.StatusOK {
errMsg := fmt.Sprintf("Error while getting interface details. http code :%d", resp.StatusCode)
report.InterfaceDetails.ErrorMessage = errMsg
telemetryLogger.Printf(errMsg)
log.Printf(errMsg)
return
}

Expand Down
55 changes: 51 additions & 4 deletions telemetry/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ import (
"net/http"
"net/url"
"os"
"runtime"
"testing"
"time"

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

const (
telemetryConfig = "azure-vnet-telemetry.config"
)

var reportManager *ReportManager
Expand Down Expand Up @@ -89,15 +95,17 @@ func TestMain(m *testing.M) {
return
}

if runtime.GOOS == "linux" {
platform.ExecuteCommand("cp metadata_test.json /tmp/azuremetadata.json")
} else {
platform.ExecuteCommand("copy metadata_test.json azuremetadata.json")
}

reportManager = &ReportManager{}
reportManager.HostNetAgentURL = "http://" + hostAgentUrl
reportManager.ContentType = "application/json"
reportManager.Report = &CNIReport{}

if err := InitTelemetryLogger(); err == nil {
defer CloseTelemetryLogger()
}

tb = NewTelemetryBuffer(hostAgentUrl)
err = tb.StartServer()
if err == nil {
Expand All @@ -109,6 +117,13 @@ func TestMain(m *testing.M) {
}

exitCode := m.Run()

if runtime.GOOS == "linux" {
platform.ExecuteCommand("rm /tmp/azuremetadata.json")
} else {
platform.ExecuteCommand("del azuremetadata.json")
}

tb.Cleanup(FdName)
os.Exit(exitCode)
}
Expand Down Expand Up @@ -248,6 +263,38 @@ func TestClientCloseTelemetryConnection(t *testing.T) {
tb.Cancel()
}

func TestReadConfigFile(t *testing.T) {
config, err := ReadConfigFile(telemetryConfig)
if err != nil {
t.Errorf("Read telemetry config failed with error %v", err)
}

if config.ReportToHostIntervalInSeconds != 30 {
t.Errorf("ReportToHostIntervalInSeconds not expected value. Got %d", config.ReportToHostIntervalInSeconds)
}

config, err = ReadConfigFile("a.config")
if err == nil {
t.Errorf("[Telemetry] Didn't throw not found error: %v", err)
}

config, err = ReadConfigFile("telemetry.go")
if err == nil {
t.Errorf("[Telemetry] Didn't report invalid telemetry config: %v", err)
}
}

func TestStartTelemetryService(t *testing.T) {
err := StartTelemetryService("", nil)
if err == nil {
t.Errorf("StartTelemetryService didnt return error for incorrect service name %v", err)
}
}

func TestWaitForTelemetrySocket(t *testing.T) {
WaitForTelemetrySocket(1, 10)
}

func TestSetReportState(t *testing.T) {
err := reportManager.SetReportState("a.json")
if err != nil {
Expand Down
Loading