Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[V2] Storage Analysis Tool for Azure Disk CSI Driver V2 #1277

Merged
merged 28 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
7e9e368
initial commit
hccheng72 Apr 7, 2022
45a433e
added display functions
hccheng72 Apr 8, 2022
39de770
tested access to cluster
hccheng72 Apr 8, 2022
540b4d5
updated go.mod
hccheng72 Apr 8, 2022
ef8b17e
deleted extra go.mod and go.sum
hccheng72 Apr 11, 2022
1713f7d
added func GetAzVolumesByPod
hccheng72 Apr 12, 2022
70e6bd4
Merge branch 'main_v2' into storageCli
hccheng72 Apr 12, 2022
52471b7
moved directory, deleted LICENSE, edited description of commands
hccheng72 Apr 12, 2022
71a97a5
added packages to vendor
hccheng72 Apr 12, 2022
67e1fe0
fixed func GetAzVolumesByPod and removed testing lines
hccheng72 Apr 12, 2022
9f82cd6
moved common functions for commands to common.go
hccheng72 Apr 13, 2022
69c8afa
feat: Add new functions to get AzVolumeAttachments by pod/node/zone
hccheng72 Apr 14, 2022
ae380a9
Merge branch 'storageCli' of https://github.com/hccheng72/azuredisk-c…
hccheng72 Apr 14, 2022
730ee3a
feat: Update go.mod, go.sum, and verdor folder
hccheng72 Apr 14, 2022
4c8398d
fix: Fix formatting
hccheng72 Apr 15, 2022
8ad2ab5
fix: fix boilerplate format
hccheng72 Apr 15, 2022
261f49c
fix: Fix boilerplate in main.go
hccheng72 Apr 15, 2022
8fe1c50
perf: Get only one pod when a podName is provided
hccheng72 Apr 18, 2022
fa03d70
perf: use constants for string literals
hccheng72 Apr 18, 2022
5d32e79
feat: if no flag values, list all of the pods/nodes/zone
hccheng72 Apr 18, 2022
fb664dc
perf: change the way to access config and age format
hccheng72 Apr 20, 2022
b46a1a3
fix: fix format
hccheng72 Apr 20, 2022
3058ae7
perf: change .az-analyz extention to yaml, access driverNamaspace by …
hccheng72 Apr 20, 2022
2efaf78
fix: using default path if kubeconfig isn't provided
hccheng72 Apr 20, 2022
4ff15a2
perf: change format of get azva and add default value to driverNamespace
hccheng72 Apr 21, 2022
f5bbd06
fix: fix format
hccheng72 Apr 21, 2022
ea746d7
Merge branch 'main_v2' into storageCli
hccheng72 Apr 21, 2022
f5c75f7
fix: update go.sum
hccheng72 Apr 21, 2022
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
132 changes: 132 additions & 0 deletions cmd/az-analyze/cmd/azv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
Copyright 2022 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"context"
"fmt"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"os"
v1beta1 "sigs.k8s.io/azuredisk-csi-driver/pkg/apis/azuredisk/v1beta1"
consts "sigs.k8s.io/azuredisk-csi-driver/pkg/azureconstants"
)

// azvCmd represents the azv command
var azvCmd = &cobra.Command{
Use: "azv",
Short: "Azure Volume",
Long: `Azure Volume is a Kubernetes Custom Resource.`,
Run: func(cmd *cobra.Command, args []string) {
pod, _ := cmd.Flags().GetString("pod")
namespace, _ := cmd.Flags().GetString("namespace")

var result []AzvResource
result = GetAzVolumesByPod(pod, namespace)

// display
if len(result) != 0 {
displayAzv(result)
} else {
// not found, display an error
fmt.Println("No azVolume was found")
}
},
}

func init() {
getCmd.AddCommand(azvCmd)
azvCmd.PersistentFlags().StringP("pod", "p", "", "insert-pod-name")
azvCmd.PersistentFlags().StringP("namespace", "n", "default", "insert-namespace (optional).")
}

type AzvResource struct {
ResourceType string
Namespace string
Name string
State v1beta1.AzVolumeState
}

// return azVolumes by pod. If pod name isn't provided, return by all pods
func GetAzVolumesByPod(podName string, namespace string) []AzvResource {
// access to Config and Clientsets
config := getConfig()
clientsetK8s := getKubernetesClientset(config)
clientsetAzDisk := getAzDiskClientset(config)

result := make([]AzvResource, 0)

// get pvc claim name set of pod
pvcClaimNameSet := make(map[string]string)
if podName != "" {
singlePod, err := clientsetK8s.CoreV1().Pods(namespace).Get(context.Background(), podName, metav1.GetOptions{})
if err != nil {
panic(err.Error())
}

for _, v := range singlePod.Spec.Volumes {
if v.PersistentVolumeClaim != nil {
pvcClaimNameSet[v.PersistentVolumeClaim.ClaimName] = singlePod.Name
}
}
} else { // if pod name isn't provided, print all pods
pods, err := clientsetK8s.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
panic(err.Error())
}

for _, pod := range pods.Items {
for _, v := range pod.Spec.Volumes {
if v.PersistentVolumeClaim != nil {
pvcClaimNameSet[v.PersistentVolumeClaim.ClaimName] = pod.Name
}
}
}
}

// get azVolumes with the same claim name in pvcSet
azVolumes, err := clientsetAzDisk.DiskV1beta1().AzVolumes(getDriverNamesapce()).List(context.Background(), metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
for _, azVolume := range azVolumes.Items {
pvcClaimName := azVolume.Spec.Parameters[consts.PvcNameKey]

// if pvcClaimName is contained in pvcClaimNameSet, add the azVolume to result
if pName, ok := pvcClaimNameSet[pvcClaimName]; ok {
result = append(result, AzvResource{
ResourceType: pName,
Namespace: azVolume.Namespace,
Name: azVolume.Spec.VolumeName,
State: azVolume.Status.State})
}
}

return result
}

func displayAzv(result []AzvResource) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"PODNAME", "NAMESPACE", "NAME", "STATE"})

for _, azv := range result {
table.Append([]string{azv.ResourceType, azv.Namespace, azv.Name, string(azv.State)})
}

table.Render()
}
Loading