From b100cdfaff93026a955ca2f756cb7255553b0383 Mon Sep 17 00:00:00 2001 From: Zetao Zhuang Date: Wed, 8 May 2024 23:29:19 -0700 Subject: [PATCH] resolve import cycle --- cns/{logger => internal}/heartbeat.go | 15 ++++++++------- cns/{monitor => restserver}/homeazmonitor.go | 7 +++---- cns/{monitor => restserver}/homeazmonitor_test.go | 2 +- cns/restserver/restserver.go | 5 ++--- cns/service/main.go | 6 +++--- 5 files changed, 17 insertions(+), 18 deletions(-) rename cns/{logger => internal}/heartbeat.go (62%) rename cns/{monitor => restserver}/homeazmonitor.go (96%) rename cns/{monitor => restserver}/homeazmonitor_test.go (99%) diff --git a/cns/logger/heartbeat.go b/cns/internal/heartbeat.go similarity index 62% rename from cns/logger/heartbeat.go rename to cns/internal/heartbeat.go index ab9fff784a..a2d12ee351 100644 --- a/cns/logger/heartbeat.go +++ b/cns/internal/heartbeat.go @@ -1,11 +1,12 @@ // Copyright 2018 Microsoft. All rights reserved. // MIT License -package logger +package internal import ( "context" - "github.com/Azure/azure-container-networking/cns/monitor" + "github.com/Azure/azure-container-networking/cns/logger" + "github.com/Azure/azure-container-networking/cns/restserver" "github.com/Azure/azure-container-networking/cns/types" "strconv" "time" @@ -13,11 +14,11 @@ import ( "github.com/Azure/azure-container-networking/aitelemetry" ) -func SendHeartBeat(ctx context.Context, heartbeatIntervalInMins int, homeAzMonitor *monitor.HomeAzMonitor) { +func SendHeartBeat(ctx context.Context, heartbeatIntervalInMins int, homeAzMonitor *restserver.HomeAzMonitor) { ticker := time.NewTicker(time.Minute * time.Duration(heartbeatIntervalInMins)) defer ticker.Stop() metric := aitelemetry.Metric{ - Name: HeartBeatMetricStr, + Name: logger.HeartBeatMetricStr, // This signifies 1 heartbeat is sent. Sum of this metric will give us number of heartbeats received Value: 1.0, CustomDimensions: make(map[string]string), @@ -29,11 +30,11 @@ func SendHeartBeat(ctx context.Context, heartbeatIntervalInMins int, homeAzMonit case <-ticker.C: getHomeAzResp := homeAzMonitor.GetHomeAz(ctx) if getHomeAzResp.Response.ReturnCode == types.Success { - metric.CustomDimensions[IsAZRSupportedStr] = strconv.FormatBool(getHomeAzResp.HomeAzResponse.IsSupported) - metric.CustomDimensions[HomeAZStr] = strconv.FormatUint(uint64(getHomeAzResp.HomeAzResponse.HomeAz), 10) + metric.CustomDimensions[logger.IsAZRSupportedStr] = strconv.FormatBool(getHomeAzResp.HomeAzResponse.IsSupported) + metric.CustomDimensions[logger.HomeAZStr] = strconv.FormatUint(uint64(getHomeAzResp.HomeAzResponse.HomeAz), 10) } - SendMetric(metric) + logger.SendMetric(metric) } } } diff --git a/cns/monitor/homeazmonitor.go b/cns/restserver/homeazmonitor.go similarity index 96% rename from cns/monitor/homeazmonitor.go rename to cns/restserver/homeazmonitor.go index 4fce645bef..3fb21e590f 100644 --- a/cns/monitor/homeazmonitor.go +++ b/cns/restserver/homeazmonitor.go @@ -1,9 +1,8 @@ -package monitor +package restserver import ( "context" "fmt" - "github.com/Azure/azure-container-networking/cns/restserver" "net/http" "time" @@ -22,7 +21,7 @@ const ( ) type HomeAzMonitor struct { - restserver.nmagentClient + nmagentClient values *cache.Cache // channel used as signal to end of the goroutine for populating home az cache closing chan struct{} @@ -30,7 +29,7 @@ type HomeAzMonitor struct { } // NewHomeAzMonitor creates a new HomeAzMonitor object -func NewHomeAzMonitor(client restserver.nmagentClient, cacheRefreshIntervalSecs time.Duration) *HomeAzMonitor { +func NewHomeAzMonitor(client nmagentClient, cacheRefreshIntervalSecs time.Duration) *HomeAzMonitor { return &HomeAzMonitor{ nmagentClient: client, cacheRefreshIntervalSecs: cacheRefreshIntervalSecs, diff --git a/cns/monitor/homeazmonitor_test.go b/cns/restserver/homeazmonitor_test.go similarity index 99% rename from cns/monitor/homeazmonitor_test.go rename to cns/restserver/homeazmonitor_test.go index 9d9915f97d..b435f3cde1 100644 --- a/cns/monitor/homeazmonitor_test.go +++ b/cns/restserver/homeazmonitor_test.go @@ -1,4 +1,4 @@ -package monitor +package restserver import ( "context" diff --git a/cns/restserver/restserver.go b/cns/restserver/restserver.go index 30d46f42ad..268c97ef14 100644 --- a/cns/restserver/restserver.go +++ b/cns/restserver/restserver.go @@ -2,7 +2,6 @@ package restserver import ( "context" - "github.com/Azure/azure-container-networking/cns/monitor" "net" "net/http" "net/http/pprof" @@ -58,7 +57,7 @@ type HTTPRestService struct { ipamClient *ipamclient.IpamClient nma nmagentClient wsproxy wireserverProxy - homeAzMonitor *monitor.HomeAzMonitor + homeAzMonitor *HomeAzMonitor networkContainer *networkcontainers.NetworkContainers PodIPIDByPodInterfaceKey map[string][]string // PodInterfaceId is key and value is slice of Pod IP (SecondaryIP) uuids. PodIPConfigState map[string]cns.IPConfigurationStatus // Secondary IP ID(uuid) is key @@ -159,7 +158,7 @@ type networkInfo struct { // NewHTTPRestService creates a new HTTP Service object. func NewHTTPRestService(config *common.ServiceConfig, wscli interfaceGetter, wsproxy wireserverProxy, nmagentClient nmagentClient, - endpointStateStore store.KeyValueStore, gen CNIConflistGenerator, homeAzMonitor *monitor.HomeAzMonitor, + endpointStateStore store.KeyValueStore, gen CNIConflistGenerator, homeAzMonitor *HomeAzMonitor, ) (*HTTPRestService, error) { service, err := cns.NewService(config.Name, config.Version, config.ChannelMode, config.Store) if err != nil { diff --git a/cns/service/main.go b/cns/service/main.go index 9ddb90180e..c979ce5318 100644 --- a/cns/service/main.go +++ b/cns/service/main.go @@ -8,7 +8,7 @@ import ( "context" "encoding/json" "fmt" - "github.com/Azure/azure-container-networking/cns/monitor" + "github.com/Azure/azure-container-networking/cns/internal" "io/fs" "net/http" "os" @@ -675,7 +675,7 @@ func main() { config.ChannelMode = cns.Managed } - homeAzMonitor := monitor.NewHomeAzMonitor(nmaClient, time.Duration(cnsconfig.AZRSettings.PopulateHomeAzCacheRetryIntervalSecs)*time.Second) + homeAzMonitor := restserver.NewHomeAzMonitor(nmaClient, time.Duration(cnsconfig.AZRSettings.PopulateHomeAzCacheRetryIntervalSecs)*time.Second) // homeAz monitor is only required when there is a direct channel between DNC and CNS. // This will prevent the monitor from unnecessarily calling NMA APIs for other scenarios such as AKS-swift, swiftv2 if cnsconfig.ChannelMode == cns.Direct { @@ -933,7 +933,7 @@ func main() { } if !disableTelemetry { - go logger.SendHeartBeat(rootCtx, cnsconfig.TelemetrySettings.HeartBeatIntervalInMins, homeAzMonitor) + go internal.SendHeartBeat(rootCtx, cnsconfig.TelemetrySettings.HeartBeatIntervalInMins, homeAzMonitor) go httpRemoteRestService.SendNCSnapShotPeriodically(rootCtx, cnsconfig.TelemetrySettings.SnapshotIntervalInMins) }