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

Added a new command: profile list #4811

Merged
merged 12 commits into from
Jul 19, 2019
55 changes: 55 additions & 0 deletions cmd/minikube/cmd/config/profile_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright 2019 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 config

import (
"os"
"strconv"

"k8s.io/minikube/pkg/minikube/config"

"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)

var profileListCmd = &cobra.Command{
medyagh marked this conversation as resolved.
Show resolved Hide resolved
Use: "list",
Short: "Lists all minikube profiles",
Long: "Lists all valid minikube profiles",
medyagh marked this conversation as resolved.
Show resolved Hide resolved
Run: func(cmd *cobra.Command, args []string) {

var tData [][]string

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Profile", "VM Driver", "NodeIP", "Node Port", "Kubernetes Version"})
medyagh marked this conversation as resolved.
Show resolved Hide resolved
table.SetAutoFormatHeaders(false)
table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})
table.SetCenterSeparator("|")

for _, p := range config.AllProfiles() {
tData = append(tData, []string{p.Name, p.Config.MachineConfig.VMDriver, p.Config.KubernetesConfig.NodeIP, strconv.Itoa(p.Config.KubernetesConfig.NodePort), p.Config.KubernetesConfig.KubernetesVersion})
}

table.AppendBulk(tData) // Add Bulk Data
medyagh marked this conversation as resolved.
Show resolved Hide resolved
table.Render()

},
}

func init() {
ProfileCmd.AddCommand(profileListCmd)
}
70 changes: 70 additions & 0 deletions pkg/minikube/config/profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Copyright 2019 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 config

import (
"os"
"path/filepath"

"github.com/golang/glog"
"k8s.io/minikube/pkg/minikube/constants"
)

// AllProfiles returns all minikube profiles
medyagh marked this conversation as resolved.
Show resolved Hide resolved
func AllProfiles() (profiles []*Profile) {
medyagh marked this conversation as resolved.
Show resolved Hide resolved

pDirs, err := allProfileDirs()
if err != nil {
glog.Infof("Error getting profile directories %v", err)
medyagh marked this conversation as resolved.
Show resolved Hide resolved
}
for _, n := range pDirs {
p, err := loadProfile(n)
if err != nil {
// TODO warn user to delete this folder or maybe do it for them
medyagh marked this conversation as resolved.
Show resolved Hide resolved
glog.Errorf("Invalid profile config in profiles folder (%s):\n error: %v", n, err)
continue
}
profiles = append(profiles, p)
}
return profiles
}

// loadProfile loads type Profile based on its name
func loadProfile(n string) (*Profile, error) {

cfg, err := DefaultLoader.LoadConfigFromFile(n)
profile := &Profile{
Name: n,
Config: cfg,
}
return profile, err
}

// allProfileDirs gets all the folders in the user's profiles folder
medyagh marked this conversation as resolved.
Show resolved Hide resolved
func allProfileDirs() (dirs []string, err error) {
root := filepath.Join(constants.GetMinipath(), "profiles")
err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
medyagh marked this conversation as resolved.
Show resolved Hide resolved
if info.IsDir() {
if path != root {
dirs = append(dirs, filepath.Base(path))
}
}
return nil
})
return dirs, err

}
6 changes: 6 additions & 0 deletions pkg/minikube/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ import (
"k8s.io/minikube/pkg/util"
)

// Profile represents a minikube profile
type Profile struct {
Name string
Config *Config
}

// Config contains machine and k8s config
type Config struct {
MachineConfig MachineConfig
Expand Down