Skip to content

Commit 4312ba8

Browse files
committed
updated table to use tablewriter
1 parent eac2c66 commit 4312ba8

File tree

3 files changed

+41
-50
lines changed

3 files changed

+41
-50
lines changed

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

Lines changed: 36 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"flag"
66
"fmt"
77
"log"
8+
"os"
89
"strings"
910

11+
"github.com/olekukonko/tablewriter"
1012
corev1 "k8s.io/api/core/v1"
1113
networkingv1 "k8s.io/api/networking/v1"
1214
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -372,41 +374,33 @@ func difference(slice1, slice2 []string) []string {
372374

373375
func printMigrationSummary(namespaces *corev1.NamespaceList, policiesByNamespace map[string][]*networkingv1.NetworkPolicy, servicesByNamespace map[string][]*corev1.Service) {
374376
fmt.Println("Migration Summary:")
375-
fmt.Println("+------------------------------+-------------------------------+")
376-
fmt.Printf("%-30s | %-30s \n", "Breaking Change", "No Policy Changes Needed")
377-
fmt.Println("+------------------------------+-------------------------------+")
377+
378+
table := tablewriter.NewWriter(os.Stdout)
379+
table.SetHeader([]string{"Breaking Change", "No Policy Changes Needed", "Details"})
380+
table.SetRowLine(true)
378381

379382
// Get the endports of the network policies
380383
ingressEndportNetworkPolicy, egressEndportNetworkPolicy := getEndportNetworkPolicies(policiesByNamespace)
381-
382-
// Print the network policies with endport
383-
printPoliciesWithEndport(ingressEndportNetworkPolicy, egressEndportNetworkPolicy)
384-
385-
fmt.Println("+------------------------------+-------------------------------+")
384+
// Add the network policies with endport
385+
addPoliciesWithEndportToTable(table, ingressEndportNetworkPolicy, egressEndportNetworkPolicy)
386386

387387
// Get the cidr of the network policies
388388
ingressPoliciesWithCIDR, egressPoliciesWithCIDR := getCIDRNetworkPolicies(policiesByNamespace)
389-
390-
// Print the network policies with CIDR
391-
printPoliciesWithCIDR(ingressPoliciesWithCIDR, egressPoliciesWithCIDR)
392-
393-
fmt.Println("+------------------------------+-------------------------------+")
389+
// Add the network policies with CIDR
390+
addPoliciesWithCIDRToTable(table, ingressPoliciesWithCIDR, egressPoliciesWithCIDR)
394391

395392
// Get the egress of the network policies
396393
egressPolicies := getEgressPolicies(policiesByNamespace)
397-
398-
// Print the network policies with egress
399-
printEgressPolicies(egressPolicies)
400-
401-
fmt.Println("+------------------------------+-------------------------------+")
394+
// Add the network policies with egress
395+
addEgressPoliciesToTable(table, egressPolicies)
402396

403397
// Get services that have externalTrafficPolicy!=Local
404398
unsafeRiskServices, unsafeNoSelectorServices := getExternalTrafficPolicyClusterServices(namespaces, servicesByNamespace, policiesByNamespace)
399+
// Add the services that are at risk
400+
addUnsafeServicesToTable(table, unsafeRiskServices, unsafeNoSelectorServices)
405401

406-
// Print the services that are at risk
407-
printUnsafeServices(unsafeRiskServices, unsafeNoSelectorServices)
402+
table.Render()
408403

409-
fmt.Println("+------------------------------+-------------------------------+")
410404
if len(ingressEndportNetworkPolicy) > 0 || len(egressEndportNetworkPolicy) > 0 ||
411405
len(ingressPoliciesWithCIDR) > 0 || len(egressPoliciesWithCIDR) > 0 ||
412406
len(egressPolicies) > 0 ||
@@ -420,88 +414,80 @@ func printMigrationSummary(namespaces *corev1.NamespaceList, policiesByNamespace
420414
}
421415
}
422416

423-
func printPoliciesWithEndport(ingressEndportNetworkPolicy, egressEndportNetworkPolicy []string) {
417+
func addPoliciesWithEndportToTable(table *tablewriter.Table, ingressEndportNetworkPolicy, egressEndportNetworkPolicy []string) {
424418
if len(ingressEndportNetworkPolicy) == 0 && len(egressEndportNetworkPolicy) == 0 {
425-
fmt.Printf("%-30s | %-30s \n", "NetworkPolicy with endport", "✅")
419+
table.Append([]string{"NetworkPolicy with endport", "✅", ""})
426420
} else {
427-
fmt.Printf("%-30s | %-30s \n", "NetworkPolicy with endport", "❌")
428-
fmt.Println("Policies affected:")
421+
table.Append([]string{"NetworkPolicy with endport", "❌", "Policies affected:"})
429422
for _, policy := range ingressEndportNetworkPolicy {
430423
policyNamespace := strings.Split(policy, "/")[0]
431424
policyName := strings.Split(policy, "/")[1]
432-
fmt.Printf("❌ Found NetworkPolicy: \033[31m%s\033[0m with ingress endPort field in namespace: \033[31m%s\033[0m\n", policyName, policyNamespace)
425+
table.Append([]string{"", "❌", fmt.Sprintf("Found NetworkPolicy: \033[31m%s\033[0m with ingress endPort field in namespace: \033[31m%s\033[0m\n", policyName, policyNamespace)})
433426
}
434427
for _, policy := range egressEndportNetworkPolicy {
435428
policyNamespace := strings.Split(policy, "/")[0]
436429
policyName := strings.Split(policy, "/")[1]
437-
fmt.Printf("❌ Found NetworkPolicy: \033[31m%s\033[0m with engress endPort field in namespace: \033[31m%s\033[0m\n", policyName, policyNamespace)
430+
table.Append([]string{"", "❌", fmt.Sprintf("Found NetworkPolicy: \033[31m%s\033[0m\n with egress endPort field in namespace: \033[31m%s\033[0m\n", policyName, policyNamespace)})
438431
}
439432
}
440433
}
441434

442-
func printPoliciesWithCIDR(ingressPoliciesWithCIDR, egressPoliciesWithCIDR []string) {
435+
func addPoliciesWithCIDRToTable(table *tablewriter.Table, ingressPoliciesWithCIDR, egressPoliciesWithCIDR []string) {
443436
if len(ingressPoliciesWithCIDR) == 0 && len(egressPoliciesWithCIDR) == 0 {
444-
fmt.Printf("%-30s | %-30s \n", "NetworkPolicy with CIDR", "✅")
437+
table.Append([]string{"NetworkPolicy with CIDR", "✅", ""})
445438
} else {
446-
fmt.Printf("%-30s | %-30s \n", "NetworkPolicy with CIDR", "❌")
447-
fmt.Println("Policies affected:")
439+
table.Append([]string{"NetworkPolicy with CIDR", "❌", "Policies affected:"})
448440
for _, policy := range ingressPoliciesWithCIDR {
449441
policyNamespace := strings.Split(policy, "/")[0]
450442
policyName := strings.Split(policy, "/")[1]
451-
fmt.Printf("❌ Found NetworkPolicy: \033[31m%s\033[0m with ingress CIDR field in namespace: \033[31m%s\033[0m\n", policyName, policyNamespace)
443+
table.Append([]string{"", "❌", fmt.Sprintf("Found NetworkPolicy: \033[31m%s\033[0m with ingress CIDR field in namespace: \033[31m%s\033[0m\n", policyName, policyNamespace)})
452444
}
453445
for _, policy := range egressPoliciesWithCIDR {
454446
policyNamespace := strings.Split(policy, "/")[0]
455447
policyName := strings.Split(policy, "/")[1]
456-
fmt.Printf("❌ Found NetworkPolicy: \033[31m%s\033[0m with egress CIDR field in namespace: \033[31m%s\033[0m\n", policyName, policyNamespace)
448+
table.Append([]string{"", "❌", fmt.Sprintf("Found NetworkPolicy: \033[31m%s\033[0m with egress CIDR field in namespace: \033[31m%s\033[0m\n", policyName, policyNamespace)})
457449
}
458450
}
459451
}
460452

461-
func printEgressPolicies(egressPolicies []string) {
453+
func addEgressPoliciesToTable(table *tablewriter.Table, egressPolicies []string) {
462454
if len(egressPolicies) == 0 {
463-
fmt.Printf("%-30s | %-30s \n", "NetworkPolicy with egress", "✅")
455+
table.Append([]string{"NetworkPolicy with egress (Not allow all egress)", "✅", ""})
464456
} else {
465-
fmt.Printf("%-30s | %-30s \n", "NetworkPolicy with egress", "❌")
466-
fmt.Printf("%-30s | %-30s \n", "(Not allow all egress)", "")
467-
fmt.Println("Policies affected:")
457+
table.Append([]string{"NetworkPolicy with egress (Not allow all egress)", "❌", "Policies affected:"})
468458
for _, policy := range egressPolicies {
469459
policyNamespace := strings.Split(policy, "/")[0]
470460
policyName := strings.Split(policy, "/")[1]
471-
fmt.Printf("❌ Found NetworkPolicy: \033[31m%s\033[0m with egress field (non-allow all) in namespace: \033[31m%s\033[0m\n", policyName, policyNamespace)
461+
table.Append([]string{"", "❌", fmt.Sprintf("Found NetworkPolicy: \033[31m%s\033[0m with egress field (non-allow all) in namespace: \033[31m%s\033[0m\n", policyName, policyNamespace)})
472462
}
473463
}
474464
}
475465

476-
func printUnsafeServices(unsafeRiskServices, unsafeNoSelectorServices []string) {
466+
func addUnsafeServicesToTable(table *tablewriter.Table, unsafeRiskServices, unsafeNoSelectorServices []string) {
477467
// If there is no unsafe services and services with no selectors then migration is safe for services with extranalTrafficPolicy=Cluster
478468
if len(unsafeRiskServices) == 0 {
479-
fmt.Printf("%-30s | %-30s \n", "Disruption for some", "✅")
480-
fmt.Printf("%-30s | %-30s \n", "Services with", "")
481-
fmt.Printf("%-30s | %-30s \n", "externalTrafficPolicy=Cluster", "")
469+
table.Append([]string{"Disruption for some Services with externalTrafficPolicy=Cluster", "✅", ""})
482470
} else {
483471
// Remove all no selector services from unsafe services to prevent repeating the same flagged service
484472
unsafeRiskServices = difference(unsafeRiskServices, unsafeNoSelectorServices)
485-
fmt.Printf("%-30s | %-30s \n", "Disruption for some", "❌")
486-
fmt.Printf("%-30s | %-30s \n", "Services with", "")
487-
fmt.Printf("%-30s | %-30s \n", "externalTrafficPolicy=Cluster", "")
473+
table.Append([]string{"Disruption for some Services with externalTrafficPolicy=Cluster", "❌", "Services affected:"})
488474
fmt.Println("Services affected:")
489475
// If there are any no selector services or unsafe services then print them as they could be impacted by migration
490476
if len(unsafeNoSelectorServices) > 0 {
491477
for _, service := range unsafeNoSelectorServices {
492478
serviceName := strings.Split(service, "/")[1]
493479
serviceNamespace := strings.Split(service, "/")[0]
494-
fmt.Printf("❌ Found Service: \033[31m%s\033[0m without selectors in namespace: \033[31m%s\033[0m\n", serviceName, serviceNamespace)
480+
table.Append([]string{"", "❌", fmt.Sprintf("Found Service: \033[31m%s\033[0m without selectors in namespace: \033[31m%s\033[0m\n", serviceName, serviceNamespace)})
495481
}
496482
}
497483
if len(unsafeRiskServices) > 0 {
498484
for _, service := range unsafeRiskServices {
499485
serviceName := strings.Split(service, "/")[1]
500486
serviceNamespace := strings.Split(service, "/")[0]
501-
fmt.Printf("❌ Found Service: \033[31m%s\033[0m with selectors in namespace: \033[31m%s\033[0m\n", serviceName, serviceNamespace)
487+
table.Append([]string{"", "❌", fmt.Sprintf("Found Service: \033[31m%s\033[0m with selectors in namespace: \033[31m%s\033[0m\n", serviceName, serviceNamespace)})
502488
}
503489
}
504-
fmt.Println("Manual investigation is required to evaluate if ingress is allowed to the service's backend Pods.")
505-
fmt.Println("Please evaluate if these services would be impacted by migration.")
490+
table.Append([]string{"", "", "Manual investigation is required to evaluate if ingress is allowed to the service's backend Pods."})
491+
table.Append([]string{"", "", "Please evaluate if these services would be impacted by migration."})
506492
}
507493
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module azure-npm-to-cilium-validator
33
go 1.16
44

55
require (
6+
github.com/olekukonko/tablewriter v0.0.5
67
k8s.io/api v0.23.0
78
k8s.io/apimachinery v0.23.0
89
k8s.io/client-go v0.23.0

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
186186
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
187187
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
188188
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
189+
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
190+
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
189191
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
190192
github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c=
191193
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -200,6 +202,8 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWb
200202
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
201203
github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78=
202204
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
205+
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
206+
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
203207
github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
204208
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
205209
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=

0 commit comments

Comments
 (0)