Skip to content

Commit

Permalink
added display functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hccheng72 committed Apr 8, 2022
1 parent 7e9e368 commit 45a433e
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 65 deletions.
61 changes: 43 additions & 18 deletions pkg/tool/az-analyze/cmd/azv.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ package cmd

import (
"fmt"
"os"
//"reflect"

v1beta1 "sigs.k8s.io/azuredisk-csi-driver/pkg/apis/azuredisk/v1beta1"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
v1beta1 "sigs.k8s.io/azuredisk-csi-driver/pkg/apis/azuredisk/v1beta1"
)

// azvCmd represents the azv command
Expand All @@ -33,15 +36,19 @@ This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
pod, _ := cmd.Flags().GetString("pod")
var podNames []string
var result []AzvResource

var azv AzvResource
if pod == "" {
azv = GetAzVolumesByPod(pod)
// display
fmt.Println(azv)
if pod != "" {
podNames = append(podNames, pod)
result = GetAzVolumesByPod(podNames)
} else {
// list all pods
fmt.Println("all pods")
}

// display
displayAzv(result)
},
}

Expand All @@ -61,20 +68,38 @@ func init() {
}

type AzvResource struct {
resourceType string
namespace string
name string
state v1beta1.AzVolumeState
phase v1beta1.AzVolumePhase
ResourceType string
Namespace string
Name string
State v1beta1.AzVolumeState
Phase v1beta1.AzVolumePhase
}

func GetAzVolumesByPod(podName string) AzvResource {
func GetAzVolumesByPod(podNames []string) []AzvResource {
// implemetation
var result []AzvResource
result = append(result, AzvResource {
ResourceType: "example-pod-123",
Namespace: "azure-disk-csi",
Name: "pvc-b2578f0d-e99b-49d9-b1da-66ad771e073b",
State: v1beta1.VolumeCreated,
Phase: v1beta1.VolumeBound })

return result
}

func displayAzv(result []AzvResource) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"PODNAME", "NAMESPACE", "NAME", "STATE", "PHASE"})
// var row []string
// v := reflect.ValueOf(azv)

return AzvResource {
resourceType: "example-pod-123",
namespace: "azure-disk-csi",
name: "pvc-b2578f0d-e99b-49d9-b1da-66ad771e073b",
state: v1beta1.VolumeCreated,
phase: v1beta1.VolumeBound }
// for i := 0; i < v.NumField(); i++ {
// row = append(row, v.Field(i).Interface())
// }
for _, azv := range result {
table.Append([]string{azv.ResourceType, azv.Namespace, azv.Name,string(azv.State), string(azv.Phase)})
}

table.Render()
}
103 changes: 56 additions & 47 deletions pkg/tool/az-analyze/cmd/azva.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ package cmd

import (
"fmt"
"os"
"strings"
"time"

v1beta1 "sigs.k8s.io/azuredisk-csi-driver/pkg/apis/azuredisk/v1beta1"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
v1beta1 "sigs.k8s.io/azuredisk-csi-driver/pkg/apis/azuredisk/v1beta1"
)

// azvaCmd represents the azva command
Expand All @@ -34,37 +37,39 @@ Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
numFlag := cmd.Flags().NFlag()

// typesFlag := []string{"pod", "node", "zone"}
// var valuesFlag []string

// for _, t := range typesFlag {
// value, _ := cmd.Flags().GetString(t)
// valuesFlag = append(valuesFlag, value)
// }
pod, _ := cmd.Flags().GetString("pod")
node, _ := cmd.Flags().GetString("node")
zone, _ := cmd.Flags().GetString("zone")

flags := []bool{pod != "", node != "", zone != ""}

sum := 0
for _, f := range flags {
sum += btoi(f)
}

var azva AzvaResource

if sum > 1 {
if numFlag > 1 {
fmt.Printf("only one of the flags is allowed.\n" + "Run 'az-analyze --help' for usage.\n")
} else {
if sum == 0 {
// display all?
if numFlag == 0 {
fmt.Println("no flags")
// the same as kubectl get AzVolumeAttachment
} else if pod != "" {
azva = GetAzVolumeAttachementsByPod(pod)
// display
fmt.Println(azva)
displayAzva(azva, pod)
} else if node != "" {
azva = GetAzVolumeAttachementsByNode(node)
// display
fmt.Println(azva)
displayAzva(azva, node)
} else if zone != "" {
azva = GetAzVolumeAttachementsByZone(zone)
// display
fmt.Println(azva)
displayAzva(azva,zone)
} else {
fmt.Printf("invalid flag name\n" + "Run 'az-analyze --help' for usage.\n")
}
Expand All @@ -90,57 +95,61 @@ func init() {
}

type AzvaResource struct {
resourceType string
namespace string
name string
age time.Duration
requestRole v1beta1.Role
role v1beta1.Role
state v1beta1.AzVolumeAttachmentAttachmentState
ResourceType string
Namespace string
Name string
Age time.Duration
RequestRole v1beta1.Role
Role v1beta1.Role
State v1beta1.AzVolumeAttachmentAttachmentState
}

func GetAzVolumeAttachementsByPod(podName string) AzvaResource {
// implemetation

return AzvaResource {
resourceType: "k8s-agentpool1-47843266-vmss000004",
namespace: "azure-disk-csi",
name: "pvc-b2578f0d-e99b-49d9-b1da-66ad771e073b-k8s-agentpool1-47843266-vmss000004-attachment",
age: time.Hour,
requestRole: v1beta1.PrimaryRole,
role: v1beta1.PrimaryRole,
state: v1beta1.Attached }
ResourceType: "k8s-agentpool1-47843266-vmss000004",
Namespace: "azure-disk-csi",
Name: "pvc-b2578f0d-e99b-49d9-b1da-66ad771e073b-k8s-agentpool1-47843266-vmss000004-attachment",
Age: time.Hour,
RequestRole: v1beta1.PrimaryRole,
Role: v1beta1.PrimaryRole,
State: v1beta1.Attached }
}

func GetAzVolumeAttachementsByNode(nodeName string) AzvaResource {
// implemetation

return AzvaResource {
resourceType: "k8s-agentpool1-47843266-vmss000004",
namespace: "azure-disk-csi",
name: "pvc-b2578f0d-e99b-49d9-b1da-66ad771e073b-k8s-agentpool1-47843266-vmss000004-attachment",
age: time.Hour,
requestRole: v1beta1.PrimaryRole,
role: v1beta1.PrimaryRole,
state: v1beta1.Attached }
ResourceType: "k8s-agentpool1-47843266-vmss000004",
Namespace: "azure-disk-csi",
Name: "pvc-b2578f0d-e99b-49d9-b1da-66ad771e073b-k8s-agentpool1-47843266-vmss000004-attachment",
Age: time.Hour,
RequestRole: v1beta1.PrimaryRole,
Role: v1beta1.PrimaryRole,
State: v1beta1.Attached }
}

func GetAzVolumeAttachementsByZone(nodeName string) AzvaResource {
// implemetation

return AzvaResource {
resourceType: "k8s-agentpool1-47843266-vmss000004",
namespace: "azure-disk-csi",
name: "pvc-b2578f0d-e99b-49d9-b1da-66ad771e073b-k8s-agentpool1-47843266-vmss000004-attachment",
age: time.Hour,
requestRole: v1beta1.PrimaryRole,
role: v1beta1.PrimaryRole,
state: v1beta1.Attached }
ResourceType: "k8s-agentpool1-47843266-vmss000004",
Namespace: "azure-disk-csi",
Name: "pvc-b2578f0d-e99b-49d9-b1da-66ad771e073b-k8s-agentpool1-47843266-vmss000004-attachment",
Age: time.Hour,
RequestRole: v1beta1.PrimaryRole,
Role: v1beta1.PrimaryRole,
State: v1beta1.Attached }
}

func btoi(b bool) int {
if b {
return 1
}
return 0
func displayAzva(azva AzvaResource, typeName string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{strings.ToUpper(typeName) + "NAME", "NAMESPACE", "NAME", "AGE", "REQUESTEDROLE", "ROLE", "STATE"})
table.Append([]string{azva.ResourceType, azva.Namespace, azva.Name, azva.Age.String()[:2], string(azva.RequestRole),string(azva.Role), string(azva.State)})
// for _, azva := range result {
// table.Append([]string{azva.ResourceType, azva.Namespace, azva.Name, string(azva.Age), string(azva.RequestRole),string(azva.Role), string(azva.State)})
// }

table.Render()
}
2 changes: 2 additions & 0 deletions pkg/tool/az-analyze/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.18

require (
github.com/mitchellh/go-homedir v1.1.0
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.4.0
github.com/spf13/viper v1.10.1
sigs.k8s.io/azuredisk-csi-driver v1.14.1-0.20220406220237-37935413efe1
Expand All @@ -19,6 +20,7 @@ require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand Down
4 changes: 4 additions & 0 deletions pkg/tool/az-analyze/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaW
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
Expand All @@ -101,6 +103,8 @@ github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
Expand Down

0 comments on commit 45a433e

Please sign in to comment.