From a62e9e5ca8f59241ab2ddf1e138a293beb678ef9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20K=C3=A4ldstr=C3=B6m?= Date: Sat, 28 May 2016 17:50:36 +0300 Subject: [PATCH 1/2] Removed localkube start and version commands in favor for starting as default and using version as a flag. Also the version is now set my the Makefile --- Makefile | 11 +++++---- cmd/localkube/cmd/options.go | 2 ++ cmd/localkube/cmd/root.go | 9 ++++---- cmd/localkube/cmd/start.go | 35 ++++++++++++---------------- cmd/localkube/cmd/version.go | 39 -------------------------------- cmd/localkube/main.go | 2 ++ cmd/minikube/cmd/version.go | 2 +- pkg/localkube/localkube.go | 1 + pkg/minikube/cluster/commands.go | 2 +- pkg/version/version.go | 7 +++++- 10 files changed, 39 insertions(+), 71 deletions(-) delete mode 100644 cmd/localkube/cmd/version.go diff --git a/Makefile b/Makefile index 91d97033d3..06498f2025 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,8 @@ # Use the native vendor/ dependency system export GO15VENDOREXPERIMENT=1 +# Bump this on release +VERSION ?= v0.0.1 GOOS ?= $(shell go env GOOS) GOARCH ?= $(shell go env GOARCH) BUILD_DIR ?= ./out @@ -31,8 +33,9 @@ endif # Use system python if it exists, otherwise use Docker. PYTHON := $(shell command -v python || docker run --rm -it -v $(shell pwd):/minikube -w /minikube python python) # Set the version information for the Kubernetes servers, and build localkube statically -VERSION_LDFLAGS := $(shell $(PYTHON) hack/get_k8s_version.py) -LDFLAGS := "$(VERSION_LDFLAGS) -s -w -extldflags '-static'" +K8S_VERSION_LDFLAGS := $(shell $(PYTHON) hack/get_k8s_version.py 2>&1) +MINIKUBE_LDFLAGS := -X k8s.io/minikube/pkg/version.version=$(VERSION) +LOCALKUBE_LDFLAGS := "$(K8S_VERSION_LDFLAGS) $(MINIKUBE_LDFLAGS) -s -w -extldflags '-static'" clean: rm -rf $(GOPATH) @@ -49,14 +52,14 @@ out/minikube: out/minikube-$(GOOS)-$(GOARCH) out/localkube: $(LOCALKUBEFILES) $(MKGOPATH) ifeq ($(GOOS),linux) - CGO_ENABLED=1 go build -ldflags=$(LDFLAGS) -o $(BUILD_DIR)/localkube ./cmd/localkube + CGO_ENABLED=1 go build -ldflags=$(LOCALKUBE_LDFLAGS) -o $(BUILD_DIR)/localkube ./cmd/localkube else docker run -w /go/src/$(REPOPATH) -e IN_DOCKER=1 -v $(shell pwd):/go/src/$(REPOPATH) $(BUILD_IMAGE) make out/localkube endif out/minikube-$(GOOS)-$(GOARCH): $(MINIKUBEFILES) pkg/minikube/cluster/localkubecontents.go $(MKGOPATH) - CGO_ENABLED=0 GOARCH=$(GOARCH) GOOS=$(GOOS) go build --installsuffix cgo -a -o $(BUILD_DIR)/minikube-$(GOOS)-$(GOARCH) ./cmd/minikube + CGO_ENABLED=0 GOARCH=$(GOARCH) GOOS=$(GOOS) go build --installsuffix cgo -ldflags="$(MINIKUBE_LDFLAGS)" -a -o $(BUILD_DIR)/minikube-$(GOOS)-$(GOARCH) ./cmd/minikube .PHONY: integration integration: out/minikube diff --git a/cmd/localkube/cmd/options.go b/cmd/localkube/cmd/options.go index da89007986..56153a2c74 100644 --- a/cmd/localkube/cmd/options.go +++ b/cmd/localkube/cmd/options.go @@ -41,6 +41,7 @@ func NewLocalkubeServer() *localkube.LocalkubeServer { APIServerInsecureAddress: net.ParseIP("127.0.0.1"), APIServerInsecurePort: 8080, ShouldGenerateCerts: true, + ShowVersion: false, } } @@ -57,6 +58,7 @@ func AddFlags(s *localkube.LocalkubeServer) { flag.IPVar(&s.APIServerInsecureAddress, "apiserver-insecure-address", s.APIServerInsecureAddress, "The address the apiserver will listen insecurely on") flag.IntVar(&s.APIServerInsecurePort, "apiserver-insecure-port", s.APIServerInsecurePort, "The port the apiserver will listen insecurely on") flag.BoolVar(&s.ShouldGenerateCerts, "generate-certs", s.ShouldGenerateCerts, "If localkube should generate it's own certificates") + flag.BoolVar(&s.ShowVersion, "version", s.ShowVersion, "If localkube should just print the version and exit.") // These two come from vendor/ packages that use flags. We should hide them flag.CommandLine.MarkHidden("google-json-key") diff --git a/cmd/localkube/cmd/root.go b/cmd/localkube/cmd/root.go index 5e38ee05c7..6617214d34 100644 --- a/cmd/localkube/cmd/root.go +++ b/cmd/localkube/cmd/root.go @@ -26,13 +26,12 @@ import ( var RootCmd = &cobra.Command{ Use: "localkube", Short: "localkube is a all-in-one kubernetes binary.", - Long: `localkube is a all-in-one kubernetes binary that runs all servers at the same time.`, + Long: `localkube is a all-in-one kubernetes binary that runs all Kubernetes server binaries.`, + Run: func(command *cobra.Command, args []string) { + StartLocalkube() + }, } func init() { pflag.CommandLine.AddGoFlagSet(goflag.CommandLine) - cobra.OnInitialize(initConfig) -} - -func initConfig() { } diff --git a/cmd/localkube/cmd/start.go b/cmd/localkube/cmd/start.go index 16dc99822d..be0e8b8283 100644 --- a/cmd/localkube/cmd/start.go +++ b/cmd/localkube/cmd/start.go @@ -21,39 +21,34 @@ import ( "os" "os/signal" - "github.com/spf13/cobra" - "k8s.io/kubernetes/pkg/capabilities" "k8s.io/kubernetes/pkg/kubelet/types" "k8s.io/minikube/pkg/localkube" + "k8s.io/minikube/pkg/version" ) +// The main instance of the current localkube server that is started var Server *localkube.LocalkubeServer -var StartCmd = &cobra.Command{ - Use: "start", - Short: "Start the localkube server.", - Long: `Start the localkube server.`, - Run: func(command *cobra.Command, args []string) { +func StartLocalkube(){ - // TODO: Require root + if Server.ShowVersion { + fmt.Println("localkube version:", version.GetVersion()) + os.Exit(0) + } - SetupServer(Server) - Server.StartAll() + // TODO: Require root - defer Server.StopAll() + SetupServer(Server) + Server.StartAll() - interruptChan := make(chan os.Signal, 1) - signal.Notify(interruptChan, os.Interrupt) + defer Server.StopAll() - <-interruptChan - fmt.Println("Shutting down...") - }, -} + interruptChan := make(chan os.Signal, 1) + signal.Notify(interruptChan, os.Interrupt) -func init() { - Server = NewLocalkubeServer() - RootCmd.AddCommand(StartCmd) + <-interruptChan + fmt.Println("Shutting down...") } func SetupServer(s *localkube.LocalkubeServer) { diff --git a/cmd/localkube/cmd/version.go b/cmd/localkube/cmd/version.go deleted file mode 100644 index 9940d92e09..0000000000 --- a/cmd/localkube/cmd/version.go +++ /dev/null @@ -1,39 +0,0 @@ -/* -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" - - "github.com/spf13/cobra" - - "k8s.io/minikube/pkg/version" -) - -var versionCmd = &cobra.Command{ - Use: "version", - Short: "Print the version of localkube.", - Long: `Print the version of localkube.`, - Run: func(command *cobra.Command, args []string) { - - fmt.Println("Version: ", version.Version) - }, -} - -func init() { - RootCmd.AddCommand(versionCmd) -} diff --git a/cmd/localkube/main.go b/cmd/localkube/main.go index ef786a20b2..e009549cf0 100644 --- a/cmd/localkube/main.go +++ b/cmd/localkube/main.go @@ -25,6 +25,8 @@ import ( func main() { + // Create the localkube server and parse the flags + cmd.Server = cmd.NewLocalkubeServer() cmd.AddFlags(cmd.Server) if err := cmd.RootCmd.Execute(); err != nil { diff --git a/cmd/minikube/cmd/version.go b/cmd/minikube/cmd/version.go index c34f8123ef..a05acedd99 100644 --- a/cmd/minikube/cmd/version.go +++ b/cmd/minikube/cmd/version.go @@ -30,7 +30,7 @@ var versionCmd = &cobra.Command{ Long: `Print the version of minikube.`, Run: func(command *cobra.Command, args []string) { - fmt.Println("Version: ", version.Version) + fmt.Println("minikube version:", version.GetVersion()) }, } diff --git a/pkg/localkube/localkube.go b/pkg/localkube/localkube.go index 1d1c64c6d2..3aacc156d5 100644 --- a/pkg/localkube/localkube.go +++ b/pkg/localkube/localkube.go @@ -48,6 +48,7 @@ type LocalkubeServer struct { APIServerInsecureAddress net.IP APIServerInsecurePort int ShouldGenerateCerts bool + ShowVersion bool } func (lk *LocalkubeServer) AddServer(server Server) { diff --git a/pkg/minikube/cluster/commands.go b/pkg/minikube/cluster/commands.go index 730ef5d44f..993cebeed1 100644 --- a/pkg/minikube/cluster/commands.go +++ b/pkg/minikube/cluster/commands.go @@ -34,7 +34,7 @@ var stopCommand = "sudo killall localkube | true" var startCommandFmtStr = ` # Run with nohup so it stays up. Redirect logs to useful places. -PATH=/usr/local/sbin:$PATH nohup sudo /usr/local/bin/localkube start %s --generate-certs=false > %s 2> %s < /dev/null & +PATH=/usr/local/sbin:$PATH nohup sudo /usr/local/bin/localkube %s --generate-certs=false > %s 2> %s < /dev/null & ` var logsCommand = fmt.Sprintf("tail -n +1 %s %s", remoteLocalKubeErrPath, remoteLocalKubeOutPath) diff --git a/pkg/version/version.go b/pkg/version/version.go index 0043f7dc27..3a683d54ec 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -17,4 +17,9 @@ limitations under the License. package version // The current version of the minikube and localkube -const Version = "0.0.1-developing" +// This is a private field and should be set when compiling with --ldflags="-X k8s.io/minikube/pkg/version.version=vX.Y.Z" +var version = "v0.0.0-unset" + +func GetVersion() string { + return version +} From 84004ad6c36a46f2e00676afe0c5be03ada49280 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20K=C3=A4ldstr=C3=B6m?= Date: Sat, 28 May 2016 18:10:56 +0300 Subject: [PATCH 2/2] Run gofmt --- cmd/localkube/cmd/start.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/localkube/cmd/start.go b/cmd/localkube/cmd/start.go index be0e8b8283..0ed73a38a4 100644 --- a/cmd/localkube/cmd/start.go +++ b/cmd/localkube/cmd/start.go @@ -30,7 +30,7 @@ import ( // The main instance of the current localkube server that is started var Server *localkube.LocalkubeServer -func StartLocalkube(){ +func StartLocalkube() { if Server.ShowVersion { fmt.Println("localkube version:", version.GetVersion())