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

WIP: Add option to list all profiles #4512

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
52 changes: 52 additions & 0 deletions cmd/minikube/cmd/config/profiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
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 config

import (
"os"
"github.com/spf13/cobra"
"k8s.io/minikube/pkg/minikube/console"
"k8s.io/minikube/pkg/minikube/exit"
pkgutil "k8s.io/minikube/pkg/util"
"k8s.io/minikube/pkg/minikube/constants"
)

// ProfileCmd represents the profile command
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change "ProfileCmd" to "ProfilesCmd"

var ProfilesCmd = &cobra.Command{
Use: "profiles",
Short: "profiles gets the list of all the present profiles",
Long: "profiles displays a list of all the profiles which have been created earlier.",
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 1 {
exit.Usage("usage: minikube profile [MINIKUBE_PROFILE_NAME]")
}
profiles, err := pkgutil.GetProfiles(constants.KubeconfigPath)
if err != nil {
exit.WithError("Failed to fetch profiles", err)
}

// check length og profiles, if zero then print suitable message
if len(profiles) == 0 {
console.OutLn("No profiles created yet")
os.Exit(0)
}

for _, profile := range profiles {
console.OutLn(profile)
}
},
}
1 change: 1 addition & 0 deletions cmd/minikube/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func init() {
RootCmd.AddCommand(configCmd.ConfigCmd)
RootCmd.AddCommand(configCmd.AddonsCmd)
RootCmd.AddCommand(configCmd.ProfileCmd)
RootCmd.AddCommand(configCmd.ProfilesCmd)
pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
if err := viper.BindPFlags(RootCmd.PersistentFlags()); err != nil {
exit.WithError("Unable to bind flags", err)
Expand Down
13 changes: 13 additions & 0 deletions pkg/util/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,16 @@ func UnsetCurrentContext(filename, machineName string) error {
}
return nil
}

//Fetch all the profiles and return
func GetProfiles(filename string) ([]string, error) {
config, err := ReadConfigOrNew(filename)
if err != nil {
return nil, errors.Wrap(err, "Error getting kubeconfig status")
}
profiles := make([]string, 0, 4)
for context, _ := range config.Contexts {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you are mean: "for _, context := ..." here, is it right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls run make test before committing the code

profiles = append(profiles, context)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is getting all kubecontexts... Inclduing the ones not created by minikube.

Copy link
Member

@medyagh medyagh Jun 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@medyagh the contexts which I get via the above way and the profiles present in ~/.minikube/profiles directory. Although it contains the profile created by kubernetes (minikube), shouldn't it be shown too? Because it is also there under the profiles directory.

that code will get all the kubectl contexts, so for example if I have a GKE cluster or another k8s cluster, they will be in the list of kubectl config get-contexts, and when you create minikube profile it will create another context.

if you get list of all contexts and show it as list of profiles, it will show my gke cluster or my other k8s cluster as a minikube profile , so it is wrong

}
return profiles, nil
}