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

Feature/add disk-usage command #13112

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
128 changes: 128 additions & 0 deletions cmd/minikube/cmd/disk-usage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.

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 (
"fmt"
"os"
"strconv"
"strings"

"github.com/spf13/cobra"

"k8s.io/klog/v2"
"k8s.io/minikube/pkg/minikube/out"
)

var (
longOutFlag bool
rootOnlyFlag bool
)

var decimalUnitPrefixMap = map[int]string{
0: "",
1: "K",
2: "M",
3: "G",
4: "T",
5: "P",
6: "E",
7: "Z",
8: "Y",
}

// runDiskUsage represents the disk-usage command
var diskUsageCmd = &cobra.Command{
Use: "disk-usage",
Short: "Shows current disk usage of minikube",
Long: "Shows current disk usage of minikube",
Run: runDiskUsage,
}

func runDiskUsage(cmd *cobra.Command, args []string) {
minikubeDirs := []string{dirs[0]}
if !rootOnlyFlag {
minikubeSubDirs, err := os.ReadDir(dirs[0])
if err != nil {
klog.Errorf("Error reading minikube directories:\n%s", err)
}
for _, file := range minikubeSubDirs {
if file.IsDir() {
minikubeDirs = append(minikubeDirs, fmt.Sprintf("%s/%s", dirs[0], file.Name()))
}
}
}
for _, dir := range minikubeDirs {
dirNameList := strings.Split(dir, "/")
dirName := dirNameList[len(dirNameList)-1]
info, err := os.Lstat(dir)
if err != nil {
klog.Errorf("Error reading info about configured minikube path:\n%s", err)
os.Exit(1)
}
totalFileSize := diskUsage(dir, info)
totalFileSizeOut := strconv.FormatInt(diskUsage(dir, info), 10)

var decimalUnitPrefixMapKey int
if !longOutFlag {
totalFileSizeOut, decimalUnitPrefixMapKey = int64ToFloat32FormattedString(totalFileSize)
}

out.Infof("{{.directory}} - {{.diskSize}}{{.decimalUnitPrefix}}B", out.V{"directory": dirName, "diskSize": totalFileSizeOut, "decimalUnitPrefix": decimalUnitPrefixMap[decimalUnitPrefixMapKey]})
}
}

func int64ToFloat32FormattedString(in_int int64) (string, int) {
mpuckett159 marked this conversation as resolved.
Show resolved Hide resolved
decimalUnitPrefixMapKey := 0
totalFileSizeFloat := float32(in_int)
for totalFileSizeFloat > float32(1000) {
totalFileSizeFloat = totalFileSizeFloat / 1024
decimalUnitPrefixMapKey++
}
return fmt.Sprintf("%.2f", totalFileSizeFloat), decimalUnitPrefixMapKey
}

func diskUsage(currPath string, info os.FileInfo) int64 {
var size int64

dir, err := os.Open(currPath)
if err != nil {
klog.Errorf("Error opening configured minikube path:\n%s", err)
}
defer dir.Close()

files, err := dir.Readdir(-1)
if err != nil {
klog.Errorf("Error reading files minikube path:\n%s", err)
os.Exit(1)
}

for _, file := range files {
if file.IsDir() {
size += diskUsage(fmt.Sprintf("%s/%s", currPath, file.Name()), file)
} else {
size += file.Size()
}
}

return size
}

func init() {
diskUsageCmd.Flags().BoolVarP(&longOutFlag, "long", "l", false, "Whether to return a human readable formatted number, e.g. 1GB")
diskUsageCmd.Flags().BoolVarP(&rootOnlyFlag, "root-only", "r", false, "Only show total of root minikube file instead of breaking out all sub directories")
}
66 changes: 66 additions & 0 deletions cmd/minikube/cmd/disk-usage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright 2020 The Kubernetes Authors All rights reserved.

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 (
"testing"
)

const (
test1 int64 = 1234567890
test2 int64 = 12
test3 int64 = 0
)

func TestInt64ToFloat32FormattedString(t *testing.T) {
var tests = []struct {
name string
in int64
expectedNumberOutput string
expectedMapKeyOutput int
}{
{
"long number",
test1,
"1.15",
3,
},
{
"short number",
test2,
"12.00",
0,
},
{
"zero",
test3,
"0.00",
0,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
outNumber, outMapKey := int64ToFloat32FormattedString(tc.in)
if outNumber != tc.expectedNumberOutput {
t.Errorf("Error parsing float to string rounded to 2 digits\nExpected: %s\nGot: %s", tc.expectedNumberOutput, outNumber)
}
if outMapKey != tc.expectedMapKeyOutput {
t.Errorf("Error getting decimal prefix map key\nExpected: %d\nGot: %d", tc.expectedMapKeyOutput, outMapKey)
}
})
}
}
1 change: 1 addition & 0 deletions cmd/minikube/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ func init() {
configCmd.ConfigCmd,
configCmd.ProfileCmd,
updateContextCmd,
diskUsageCmd,
},
},
{
Expand Down