Skip to content

Commit 6cec80a

Browse files
committed
added ai id and formated tables to be printed after telemetry is sent
1 parent ed71c9a commit 6cec80a

File tree

3 files changed

+64
-32
lines changed

3 files changed

+64
-32
lines changed

tools/azure-npm-to-cilium-validator/azure-npm-to-cilium-validator.go

Lines changed: 61 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"os"
99

1010
"github.com/Azure/azure-container-networking/npm/metrics"
11-
"github.com/Azure/azure-container-networking/npm/util"
1211
"github.com/olekukonko/tablewriter"
1312
corev1 "k8s.io/api/core/v1"
1413
networkingv1 "k8s.io/api/networking/v1"
@@ -95,11 +94,15 @@ func main() {
9594
klog.Infof("initializing metrics")
9695
metrics.InitializeAll()
9796

97+
// Create telemetry handle
98+
err = metrics.CreateTelemetryHandle(0, "", "014c22bd-4107-459e-8475-67909e96edcb")
99+
100+
if err != nil {
101+
klog.Infof("CreateTelemetryHandle failed with error %v. AITelemetry is not initialized.", err)
102+
}
103+
98104
// Print the migration summary
99105
printMigrationSummary(detailedMigrationSummary, namespaces, policiesByNamespace, servicesByNamespace, podsByNamespace)
100-
101-
// Close the metrics
102-
metrics.Close()
103106
}
104107

105108
func printMigrationSummary(
@@ -111,40 +114,80 @@ func printMigrationSummary(
111114
) {
112115
// Get the network policies with endports
113116
ingressEndportNetworkPolicy, egressEndportNetworkPolicy := getEndportNetworkPolicies(policiesByNamespace)
114-
metrics.SendLog(util.PodID, fmt.Sprintf("Found %d network policies with endPort", len(ingressEndportNetworkPolicy)+len(egressEndportNetworkPolicy)), metrics.DonotPrint)
117+
118+
// Send endPort telemetry
119+
metrics.SendLog(0, fmt.Sprintf("Found %d network policies with endPort", len(ingressEndportNetworkPolicy)+len(egressEndportNetworkPolicy)), metrics.DonotPrint)
115120

116121
// Get the network policies with cidr
117122
ingressPoliciesWithCIDR, egressPoliciesWithCIDR := getCIDRNetworkPolicies(policiesByNamespace)
118-
metrics.SendLog(util.PodID, fmt.Sprintf("Found %d network policies with CIDR", len(ingressPoliciesWithCIDR)+len(egressPoliciesWithCIDR)), metrics.DonotPrint)
123+
124+
// Send cidr telemetry
125+
metrics.SendLog(0, fmt.Sprintf("Found %d network policies with CIDR", len(ingressPoliciesWithCIDR)+len(egressPoliciesWithCIDR)), metrics.DonotPrint)
119126

120127
// Get the named port
121128
ingressPoliciesWithNamedPort, egressPoliciesWithNamedPort := getNamedPortPolicies(policiesByNamespace)
122-
metrics.SendLog(util.PodID, fmt.Sprintf("Found %d network policies with named port", len(ingressPoliciesWithNamedPort)+len(egressPoliciesWithNamedPort)), metrics.DonotPrint)
129+
130+
// Send named port telemetry
131+
metrics.SendLog(0, fmt.Sprintf("Found %d network policies with named port", len(ingressPoliciesWithNamedPort)+len(egressPoliciesWithNamedPort)), metrics.DonotPrint)
123132

124133
// Get the network policies with egress (except not egress allow all)
125134
egressPolicies := getEgressPolicies(policiesByNamespace)
126-
metrics.SendLog(util.PodID, fmt.Sprintf("Found %d network policies with egress", len(egressPolicies)), metrics.DonotPrint)
135+
136+
// Send egress telemetry
137+
metrics.SendLog(0, fmt.Sprintf("Found %d network policies with egress", len(egressPolicies)), metrics.DonotPrint)
127138

128139
// Get services that have externalTrafficPolicy!=Local that are unsafe (might have traffic disruption)
129140
unsafeServices := getUnsafeExternalTrafficPolicyClusterServices(namespaces, servicesByNamespace, policiesByNamespace)
130-
metrics.SendLog(util.PodID, fmt.Sprintf("Found %d services with externalTrafficPolicy=Cluster", len(unsafeServices)), metrics.DonotPrint)
141+
142+
// Send unsafe services telemetry
143+
metrics.SendLog(0, fmt.Sprintf("Found %d services with externalTrafficPolicy=Cluster", len(unsafeServices)), metrics.DonotPrint)
144+
145+
unsafeNetworkPolicesInCluster := false
146+
unsafeServicesInCluster := false
147+
if len(ingressEndportNetworkPolicy) > 0 || len(egressEndportNetworkPolicy) > 0 ||
148+
len(ingressPoliciesWithCIDR) > 0 || len(egressPoliciesWithCIDR) > 0 ||
149+
len(ingressPoliciesWithNamedPort) > 0 || len(egressPoliciesWithNamedPort) > 0 ||
150+
len(egressPolicies) > 0 {
151+
unsafeNetworkPolicesInCluster = true
152+
}
153+
if len(unsafeServices) > 0 {
154+
unsafeServicesInCluster = true
155+
}
156+
157+
if unsafeNetworkPolicesInCluster || unsafeServicesInCluster {
158+
// Send cluster unsafe telemetry
159+
metrics.SendLog(0, "Fails some checks. Unsafe to migrate this cluster", metrics.DonotPrint)
160+
} else {
161+
// Send cluster safe telemetry
162+
metrics.SendLog(0, "Passes all checks. Safe to migrate this cluster", metrics.DonotPrint)
163+
}
164+
165+
// Close the metrics before table is rendered to prevent formatting issues
166+
metrics.Close()
131167

132168
// Print the migration summary table
133169
renderMigrationSummaryTable(ingressEndportNetworkPolicy, egressEndportNetworkPolicy, ingressPoliciesWithCIDR, egressPoliciesWithCIDR, ingressPoliciesWithNamedPort, egressPoliciesWithNamedPort, egressPolicies, unsafeServices)
134170

135171
// Print the flagged resource table and cluster resource table if the detailed-report flag is set
136172
if *detailedMigrationSummary {
137-
if len(ingressEndportNetworkPolicy) > 0 || len(egressEndportNetworkPolicy) > 0 ||
138-
len(ingressPoliciesWithCIDR) > 0 || len(egressPoliciesWithCIDR) > 0 ||
139-
len(ingressPoliciesWithNamedPort) > 0 || len(egressPoliciesWithNamedPort) > 0 ||
140-
len(egressPolicies) > 0 {
173+
if unsafeNetworkPolicesInCluster {
141174
renderFlaggedNetworkPolicyTable(ingressEndportNetworkPolicy, egressEndportNetworkPolicy, ingressPoliciesWithCIDR, egressPoliciesWithCIDR, ingressPoliciesWithNamedPort, egressPoliciesWithNamedPort, egressPolicies)
142175
}
143-
if len(unsafeServices) > 0 {
176+
if unsafeServicesInCluster {
144177
renderFlaggedServiceTable(unsafeServices)
145178
}
146179
renderClusterResourceTable(policiesByNamespace, servicesByNamespace, podsByNamespace)
147180
}
181+
182+
// Print if the cluster is safe to migrate
183+
if unsafeNetworkPolicesInCluster || unsafeServicesInCluster {
184+
fmt.Println("\n\033[31m✘ Review above issues before migration.\033[0m")
185+
fmt.Println("Please see \033[32maka.ms/azurenpmtocilium\033[0m for instructions on how to evaluate/assess the above warnings marked by ❌.")
186+
fmt.Println("NOTE: rerun this script if any modifications (create/update/delete) are made to services or policies.")
187+
} else {
188+
fmt.Println("\n\033[32m✔ Safe to migrate this cluster.\033[0m")
189+
fmt.Println("For more details please see \033[32maka.ms/azurenpmtocilium\033[0m.")
190+
}
148191
}
149192

150193
func renderMigrationSummaryTable(
@@ -157,7 +200,6 @@ func renderMigrationSummaryTable(
157200
egressPolicies,
158201
unsafeServices []string,
159202
) {
160-
fmt.Println("Migration Summary:")
161203
migrationSummarytable := tablewriter.NewWriter(os.Stdout)
162204
migrationSummarytable.SetHeader([]string{"Breaking Change", "Upgrade compatibility", "Count"})
163205
migrationSummarytable.SetRowLine(true)
@@ -186,21 +228,9 @@ func renderMigrationSummaryTable(
186228
} else {
187229
migrationSummarytable.Append([]string{"Disruption for some Services with externalTrafficPolicy=Cluster", "❌", fmt.Sprintf("%d", len(unsafeServices))})
188230
}
231+
232+
fmt.Println("\nMigration Summary:")
189233
migrationSummarytable.Render()
190-
if len(ingressEndportNetworkPolicy) > 0 || len(egressEndportNetworkPolicy) > 0 ||
191-
len(ingressPoliciesWithCIDR) > 0 || len(egressPoliciesWithCIDR) > 0 ||
192-
len(ingressPoliciesWithNamedPort) > 0 || len(egressPoliciesWithNamedPort) > 0 ||
193-
len(egressPolicies) > 0 ||
194-
len(unsafeServices) > 0 {
195-
metrics.SendLog(util.PodID, "Fails some checks. Unsafe to migrate this cluster", metrics.DonotPrint)
196-
fmt.Println("\n\033[31m✘ Review above issues before migration.\033[0m")
197-
fmt.Println("Please see \033[32maka.ms/azurenpmtocilium\033[0m for instructions on how to evaluate/assess the above warnings marked by ❌.")
198-
fmt.Println("NOTE: rerun this script if any modifications (create/update/delete) are made to services or policies.")
199-
} else {
200-
metrics.SendLog(util.PodID, "Passes all checks. Safe to migrate this cluster", metrics.DonotPrint)
201-
fmt.Println("\n\033[32m✔ Safe to migrate this cluster.\033[0m")
202-
fmt.Println("For more details please see \033[32maka.ms/azurenpmtocilium\033[0m.")
203-
}
204234
}
205235

206236
func renderFlaggedNetworkPolicyTable(
@@ -212,7 +242,6 @@ func renderFlaggedNetworkPolicyTable(
212242
egressPoliciesWithNamedPort,
213243
egressPolicies []string,
214244
) {
215-
fmt.Println("\nFlagged Network Policies:")
216245
flaggedResourceTable := tablewriter.NewWriter(os.Stdout)
217246
flaggedResourceTable.SetHeader([]string{"Network Policy", "NetworkPolicy with endPort", "NetworkPolicy with CIDR", "NetworkPolicy with Named Port", "NetworkPolicy with Egress (Not Allow All Egress)"})
218247
flaggedResourceTable.SetRowLine(true)
@@ -271,6 +300,7 @@ func renderFlaggedNetworkPolicyTable(
271300
flaggedResourceTable.Append([]string{policy, flags[0], flags[1], flags[2], flags[3]})
272301
}
273302

303+
fmt.Println("\nFlagged Network Policies:")
274304
flaggedResourceTable.Render()
275305
}
276306

@@ -286,8 +316,6 @@ func renderFlaggedServiceTable(unsafeServices []string) {
286316
}
287317

288318
func renderClusterResourceTable(policiesByNamespace map[string][]*networkingv1.NetworkPolicy, servicesByNamespace map[string][]*corev1.Service, podsByNamespace map[string][]*corev1.Pod) {
289-
fmt.Println("\nCluster Resources:")
290-
291319
resourceTable := tablewriter.NewWriter(os.Stdout)
292320
resourceTable.SetHeader([]string{"Resource", "Count"})
293321
resourceTable.SetRowLine(true)
@@ -313,6 +341,7 @@ func renderClusterResourceTable(policiesByNamespace map[string][]*networkingv1.N
313341
}
314342
resourceTable.Append([]string{"Pod", fmt.Sprintf("%d", totalPods)})
315343

344+
fmt.Println("\nCluster Resources:")
316345
resourceTable.Render()
317346
}
318347

tools/azure-npm-to-cilium-validator/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ require (
2626
github.com/containerd/typeurl/v2 v2.2.0 // indirect
2727
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
2828
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
29+
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
2930
github.com/go-logr/logr v1.4.2 // indirect
3031
github.com/go-openapi/jsonpointer v0.20.0 // indirect
3132
github.com/go-openapi/jsonreference v0.20.2 // indirect

tools/azure-npm-to-cilium-validator/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF
3737
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
3838
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
3939
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
40+
github.com/evanphx/json-patch v5.6.0+incompatible h1:jBYDEEiFBPxA0v50tFdvOzQQTCvpL6mnFh5mB2/l16U=
41+
github.com/evanphx/json-patch v5.6.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
4042
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
4143
github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas=
4244
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=

0 commit comments

Comments
 (0)