More cleanup in Host creation.

pull/19/head
dlorenc 2016-04-21 15:00:22 -07:00
parent 038a285073
commit 26af1ad0bd
4 changed files with 47 additions and 34 deletions

View File

@ -41,11 +41,18 @@ var Minipath = filepath.Join(os.Getenv("HOME"), "minikube")
func StartHost(api *libmachine.Client) (*host.Host, error) {
setupDirs()
h, err := getOrCreateHost(api)
if err != nil {
log.Panicf("Error getting host: ", err)
if exists, err := api.Exists(machineName); err != nil {
return nil, fmt.Errorf("Error checking if host exists: %s", err)
} else if exists {
log.Println("Machine exists!")
h, err := api.Load(machineName)
if err != nil {
return nil, fmt.Errorf("Error loading existing host.")
}
return h, nil
} else {
return createHost(api)
}
return h, nil
}
// StartCluster starts as k8s cluster on the specified Host.
@ -87,21 +94,6 @@ func StartCluster(h *host.Host) (string, error) {
return kubeHost, nil
}
func getOrCreateHost(api *libmachine.Client) (*host.Host, error) {
if exists, err := api.Exists(machineName); err != nil {
return nil, fmt.Errorf("Error checking if host exists: %s", err)
} else if exists {
log.Println("Machine exists!")
h, err := api.Load(machineName)
if err != nil {
return nil, fmt.Errorf("Error loading existing host.")
}
return h, nil
} else {
return createHost(api)
}
}
func createHost(api *libmachine.Client) (*host.Host, error) {
rawDriver, err := json.Marshal(&drivers.BaseDriver{
MachineName: machineName,

View File

@ -29,18 +29,8 @@ var RootCmd = &cobra.Command{
Long: `Minikube is a CLI tool that provisions and manages single-node Kubernetes
clusters optimized for development workflows.
`,
Run: func(cmd *cobra.Command, args []string) {
if os.Getenv(localbinary.PluginEnvKey) == localbinary.PluginEnvVal {
driverName := os.Getenv(localbinary.PluginEnvDriverName)
machine.StartDriver(driverName)
return
}
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if os.Getenv(localbinary.PluginEnvKey) != localbinary.PluginEnvVal {
localbinary.CurrentBinaryIsDockerMachine = true
}
},
Run: runRoot,
PersistentPreRun: preRunRoot,
}
// Execute adds all child commands to the root command sets flags appropriately.
@ -52,6 +42,24 @@ func Execute() {
}
}
func runRoot(cmd *cobra.Command, args []string) {
// The libmachine driver model attempts to start the same binary it's run from as a "driver" process.
// If this environment varialbe is set, we have to act as a "driver" isntead of a CLI.
if os.Getenv(localbinary.PluginEnvKey) == localbinary.PluginEnvVal {
driverName := os.Getenv(localbinary.PluginEnvDriverName)
machine.StartDriver(driverName)
return
}
}
func preRunRoot(cmd *cobra.Command, args []string) {
// Libmachine code uses this boolean to indicate that this process should run as the main CLI, not as
// a "driver".
if os.Getenv(localbinary.PluginEnvKey) != localbinary.PluginEnvVal {
localbinary.CurrentBinaryIsDockerMachine = true
}
}
func init() {
cobra.OnInitialize(initConfig)
}

View File

@ -28,10 +28,10 @@ var startCmd = &cobra.Command{
Short: "Starts a local kubernetes cluster.",
Long: `Starts a local kubernetes cluster using Virtualbox. This command
assumes you already have Virtualbox installed.`,
Run: run,
Run: runStart,
}
func run(cmd *cobra.Command, args []string) {
func runStart(cmd *cobra.Command, args []string) {
fmt.Println("Starting local Kubernetes cluster...")
api := libmachine.NewClient(cluster.Minipath, cluster.MakeMiniPath("certs"))
defer api.Close()
@ -45,7 +45,7 @@ func run(cmd *cobra.Command, args []string) {
}
log.Printf("Kubernetes is available at %s.\n", kubeHost)
log.Println("Run this command to use the cluster: ")
log.Printf("kubectl config set-cluster localkube --insecure-skip-tls-verify=true --server=%s\n", kubeHost)
log.Printf("kubectl config set-cluster minikube --insecure-skip-tls-verify=true --server=%s\n", kubeHost)
}

View File

@ -1,3 +1,16 @@
/*
Copyright 2015 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 machine
import (