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 option to delete all profiles #4780

Merged
merged 80 commits into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
db6b0a5
Added --delete-all flag
mschwrz Jul 12, 2019
08b6edc
Added function to get all profiles
mschwrz Jul 13, 2019
6887128
Merge remote-tracking branch 'main/master' into DELETE_ALL_PROFILES
mschwrz Jul 13, 2019
6f2390b
Added function to check if profile-config is valid
mschwrz Jul 13, 2019
6c4749b
Bugfix: Imported wrong configname
mschwrz Jul 13, 2019
f9e8490
Removed debug code
mschwrz Jul 13, 2019
13f40f4
Check if machineConfig and kubernetesConfig is not empty
mschwrz Jul 14, 2019
dd9afd1
Ensure usage of --delete-all without -p flag
mschwrz Jul 14, 2019
1d2ead5
Merge branch 'master' into DELETE_ALL_PROFILES
mschwrz Jul 14, 2019
72a9493
Renamed delte-all flag to all
mschwrz Jul 15, 2019
925e803
Added deleteProfile function
mschwrz Jul 16, 2019
3c4b1b1
Renamed minikubeConfig to mkConfig
mschwrz Jul 16, 2019
f0e707c
Merge remote-tracking branch 'marekschwarz/DELETE_ALL_PROFILES' into …
mschwrz Jul 16, 2019
d0e749b
Implemented deleteAllProfiles
mschwrz Jul 16, 2019
019ee1f
Merge branch 'master' into DELETE_ALL_PROFILES
mschwrz Jul 17, 2019
9e73f6e
Refactor review findings
mschwrz Jul 18, 2019
09d8143
Merge branch 'master' into DELETE_ALL_PROFILES
mschwrz Jul 18, 2019
42a5850
Fixed lint errors
mschwrz Jul 18, 2019
af57143
Fixed translation errors
mschwrz Jul 18, 2019
4c6af02
Fixed goimports lint error
mschwrz Jul 18, 2019
70f4cb9
Merge branch 'master' into DELETE_ALL_PROFILES
mschwrz Jul 20, 2019
48bd69c
Integrated ListProfiles()
mschwrz Jul 20, 2019
e237a13
Fixed merge error
mschwrz Jul 20, 2019
45161fa
Keep deleting when profiles when the deletion of one profile fails
mschwrz Jul 22, 2019
cb58974
Added testdata for unit tests
mschwrz Jul 23, 2019
6d9ae13
Fixed Test
mschwrz Jul 23, 2019
6dcb45a
Refactor
mschwrz Jul 23, 2019
1ef11bf
Fix test
mschwrz Jul 23, 2019
96ef740
Improved logging
mschwrz Jul 24, 2019
d4d8cc4
Refactor error init
mschwrz Jul 24, 2019
6f81089
Fixed review findings
mschwrz Jul 24, 2019
a4fd885
Added valid configs for delete_test
mschwrz Jul 25, 2019
d7d05b7
Added more testdata
mschwrz Jul 25, 2019
1d9a61f
Changed username
mschwrz Jul 25, 2019
59eb974
Fixed testdata
mschwrz Jul 25, 2019
fa0cdbf
Fixed testdata
mschwrz Jul 25, 2019
342584a
Fixed testdata
mschwrz Jul 25, 2019
a1d73a7
Added method to delete invalid profiles
mschwrz Jul 29, 2019
50329ed
Added machine.go to Load and Validate machines
mschwrz Aug 7, 2019
6ed4262
Resolved import cycle
mschwrz Aug 8, 2019
f0dd5a7
Added unit test for deletion of all profiles
mschwrz Aug 8, 2019
229c9b7
Fixed lint errors
mschwrz Aug 8, 2019
4835945
Bugfix
mschwrz Aug 18, 2019
d5bfa87
Added delete --all to proxy_test
mschwrz Aug 18, 2019
ba67c85
Merge branch 'master' into DELETE_ALL_PROFILES
mschwrz Aug 19, 2019
da1f935
Readded Shutdown and Teardown
mschwrz Aug 19, 2019
5289e1f
Fixed lint errors
mschwrz Aug 19, 2019
b3741bd
Readded init
mschwrz Aug 20, 2019
2c6f659
Merge branch 'master' into DELETE_ALL_PROFILES
mschwrz Aug 21, 2019
808477a
Moved GetMachinePath to machine.go and renamed it to MachinePath
mschwrz Aug 23, 2019
ea20ba3
Merge branch 'master' into DELETE_ALL_PROFILES
mschwrz Aug 23, 2019
0dc7f8d
Merge branch 'master' into DELETE_ALL_PROFILES
mschwrz Aug 26, 2019
24d4eda
Merge branch 'master' into DELETE_ALL_PROFILES
mschwrz Aug 27, 2019
5b1528b
Merge branch 'master' into DELETE_ALL_PROFILES
mschwrz Sep 4, 2019
71a6cb5
Merge branch 'master' into DELETE_ALL_PROFILES
mschwrz Sep 19, 2019
01dd044
Merge branch 'master' into DELETE_ALL_PROFILES
mschwrz Sep 21, 2019
79e8250
Use localpath
mschwrz Sep 21, 2019
02bea60
Readded testdata
mschwrz Sep 21, 2019
42c017f
Readded testdata
mschwrz Sep 21, 2019
5cabb01
Removed certs
mschwrz Sep 21, 2019
d52730a
Fixed test errors
mschwrz Sep 21, 2019
3ec82dd
Merge branch 'master' into DELETE_ALL_PROFILES
mschwrz Sep 25, 2019
47ec8d3
Merge branch 'master' into DELETE_ALL_PROFILES
mschwrz Sep 25, 2019
c8c4491
Merge branch 'master' into DELETE_ALL_PROFILES
mschwrz Sep 26, 2019
e8c997d
Merge branch 'master' into DELETE_ALL_PROFILES
mschwrz Sep 26, 2019
4551a17
Merge branch 'master' into DELETE_ALL_PROFILES
mschwrz Sep 27, 2019
3396f5b
Merge branch 'master' into DELETE_ALL_PROFILES
mschwrz Sep 28, 2019
1efdf11
Merge branch 'master' into DELETE_ALL_PROFILES
mschwrz Oct 3, 2019
280e118
Merge branch 'master' into DELETE_ALL_PROFILES
mschwrz Oct 3, 2019
8a4b3b9
Added more debug code
mschwrz Oct 3, 2019
ae6f47d
Appended "/.minikube" to testMinikubeDir for machine_test
mschwrz Oct 3, 2019
ee97b87
Added debug code
mschwrz Oct 3, 2019
b8abe56
Added more debug code
mschwrz Oct 3, 2019
0c9cd49
Replaced driver "hyperkit" by "virtualbox" in testfiles
mschwrz Oct 3, 2019
5610849
Merge branch 'master' into DELETE_ALL_PROFILES
mschwrz Oct 4, 2019
c4107ae
Merge branch 'master' into DELETE_ALL_PROFILES
mschwrz Oct 6, 2019
a0c844e
Refactor
mschwrz Oct 10, 2019
ae731d4
Merge branch 'master' into DELETE_ALL_PROFILES
mschwrz Oct 10, 2019
09ce93f
Added delete --all to common.sh
mschwrz Oct 10, 2019
d13f58f
Merge branch 'master' into DELETE_ALL_PROFILES
mschwrz Oct 11, 2019
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
73 changes: 71 additions & 2 deletions cmd/minikube/cmd/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ limitations under the License.
package config

import (
"os"

"encoding/json"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io/ioutil"
mkConfig "k8s.io/minikube/pkg/minikube/config"
pkgConfig "k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/console"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/exit"
pkgutil "k8s.io/minikube/pkg/util"
"os"
"path/filepath"
"reflect"
)

// ProfileCmd represents the profile command
Expand Down Expand Up @@ -71,3 +75,68 @@ var ProfileCmd = &cobra.Command{
console.Success("minikube profile was successfully set to %s", profile)
},
}

func GetAllProfiles() []string {
mschwrz marked this conversation as resolved.
Show resolved Hide resolved
miniPath := constants.GetMinipath()
profilesPath := filepath.Join(miniPath, "profiles")
fileInfos, err := ioutil.ReadDir(profilesPath)
if err != nil {
console.ErrLn("Unable to list in dir: %s \n Error: %v", profilesPath, err)
}

var profiles []string
for _, fileInfo := range fileInfos {
if fileInfo.IsDir() {
profilePath := filepath.Join(profilesPath, fileInfo.Name())
if isValidProfile(profilePath) {
profiles = append(profiles, fileInfo.Name())
}
}
}
return profiles
}

func isValidProfile(profilePath string) bool {
mschwrz marked this conversation as resolved.
Show resolved Hide resolved
fileInfos, err := ioutil.ReadDir(profilePath)
if err != nil {
console.ErrLn("Unable to list in dir: %s \n Error: %v", profilePath, err)
}

hasConfigJSON := false
for _, fileInfo := range fileInfos {
if fileInfo.Name() == "config.json" {
hasConfigJSON = true
}
}

if !hasConfigJSON {
return false
}

// TODO: Use constants?
profileConfigPath := filepath.Join(profilePath, "config.json")
bytes, err := ioutil.ReadFile(profileConfigPath)
if err != nil {
console.ErrLn("Unable to read file: %s \n Error: %v", profileConfigPath, err)
}

var configObject mkConfig.Config
errUnmarshal := json.Unmarshal(bytes, &configObject)

if errUnmarshal != nil {
console.ErrLn("Could not unmarshal config json to config object: %s \n Error: %v", profileConfigPath, err)
}
return IsProfileConfigValid(configObject)
}

func IsProfileConfigValid(configObject mkConfig.Config) bool {
mschwrz marked this conversation as resolved.
Show resolved Hide resolved
machineConfig := configObject.MachineConfig
kubernetesConfig := configObject.KubernetesConfig
if reflect.DeepEqual(machineConfig, mkConfig.MachineConfig{}) || reflect.DeepEqual(kubernetesConfig, mkConfig.KubernetesConfig{}) {
return false
}

//TODO: Validate MachineConfig and KubernetesConfig?

return true
}
41 changes: 35 additions & 6 deletions cmd/minikube/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (
pkgutil "k8s.io/minikube/pkg/util"
)

var deleteAll bool

// deleteCmd represents the delete command
var deleteCmd = &cobra.Command{
Use: "delete",
Expand All @@ -46,10 +48,27 @@ associated files.`,

// runDelete handles the executes the flow of "minikube delete"
func runDelete(cmd *cobra.Command, args []string) {
if len(args) > 0 {
exit.Usage("usage: minikube delete")
profileFlag, _ := cmd.Flags().GetString("profile")
mschwrz marked this conversation as resolved.
Show resolved Hide resolved
deleteAllFlag, _ := cmd.Flags().GetBool("all")
mschwrz marked this conversation as resolved.
Show resolved Hide resolved

if profileFlag != constants.DefaultMachineName && deleteAllFlag {
exit.Usage("usage: minikube delete --all")
}

if deleteAllFlag {
profiles := cmdcfg.GetAllProfiles()
deleteAllProfiles(profiles)
} else {
if len(args) > 0 {
exit.Usage("usage: minikube delete")
}

profileName := viper.GetString(pkg_config.MachineProfile)
deleteProfile(profileName)
}
profile := viper.GetString(pkg_config.MachineProfile)
}

func deleteProfile(profileName string) {
api, err := machine.NewAPIClient()
if err != nil {
exit.WithError("Error getting client", err)
Expand All @@ -69,7 +88,7 @@ func runDelete(cmd *cobra.Command, args []string) {
if err = cluster.DeleteHost(api); err != nil {
switch err := errors.Cause(err).(type) {
case mcnerror.ErrHostDoesNotExist:
console.OutStyle(console.Meh, "%q cluster does not exist", profile)
console.OutStyle(console.Meh, "%q cluster does not exist", profileName)
default:
exit.WithError("Failed to delete cluster", err)
}
Expand All @@ -81,19 +100,28 @@ func runDelete(cmd *cobra.Command, args []string) {

if err := os.RemoveAll(constants.GetProfilePath(viper.GetString(pkg_config.MachineProfile))); err != nil {
if os.IsNotExist(err) {
console.OutStyle(console.Meh, "%q profile does not exist", profile)
console.OutStyle(console.Meh, "%q profile does not exist", profileName)
os.Exit(0)
}
exit.WithError("Failed to remove profile", err)
}
console.OutStyle(console.Crushed, "The %q cluster has been deleted.", profile)
console.OutStyle(console.Crushed, "The %q cluster has been deleted.", profileName)

machineName := pkg_config.GetMachineName()
if err := pkgutil.DeleteKubeConfigContext(constants.KubeconfigPath, machineName); err != nil {
exit.WithError("update config", err)
}
}

func deleteAllProfiles(profiles []string) {
for _, profile := range profiles {
// TODO: Refactor: viper.Set seems to be in the wrong place
viper.Set(pkg_config.MachineProfile, profile)
deleteProfile(profile)
}
os.Exit(0)
}

func uninstallKubernetes(api libmachine.API, kc pkg_config.KubernetesConfig, bsName string) {
console.OutStyle(console.Resetting, "Uninstalling Kubernetes %s using %s ...", kc.KubernetesVersion, bsName)
clusterBootstrapper, err := GetClusterBootstrapper(api, bsName)
Expand All @@ -105,5 +133,6 @@ func uninstallKubernetes(api libmachine.API, kc pkg_config.KubernetesConfig, bsN
}

func init() {
deleteCmd.Flags().BoolVar(&deleteAll, "all", false, "Set flag to delete all profiles")
RootCmd.AddCommand(deleteCmd)
}