Merge pull request #66 from dlorenc/buildlocalkube

Bundle localkube in the minikube binary as a blob, send that to the VM.
pull/71/merge
Lucas Käldström 2016-05-13 20:51:09 +03:00
commit 68654463fe
11 changed files with 411 additions and 33 deletions

2
.gitignore vendored
View File

@ -25,3 +25,5 @@ _testmain.go
/out
/.gopath
pkg/minikube/cluster/localkubecontents.go

View File

@ -15,7 +15,7 @@
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
BUILD_DIR ?= ./out
GOPATH := $(shell pwd)/.gopath
GOPATH ?= $(shell pwd)/.gopath
REPOPATH ?= k8s.io/minikube
export GO15VENDOREXPERIMENT=1
@ -23,34 +23,33 @@ clean:
rm -rf $(GOPATH)
rm -rf $(BUILD_DIR)
gopath:
.gopath:
mkdir -p $(shell dirname $(GOPATH)/src/$(REPOPATH))
ln -s -f $(shell pwd) $(GOPATH)/src/$(REPOPATH)
.PHONY: minikube
minikube: minikube-$(GOOS)-$(GOARCH)
out/minikube: out/minikube-$(GOOS)-$(GOARCH)
cp $(BUILD_DIR)/minikube-$(GOOS)-$(GOARCH) $(BUILD_DIR)/minikube
.PHONY: localkube
localkube: localkube-$(GOOS)-$(GOARCH)
cp $(BUILD_DIR)/localkube-$(GOOS)-$(GOARCH) $(BUILD_DIR)/localkube
out/localkube: .gopath
ifeq ($(GOOS),linux)
CGO_ENABLED=1 go build -ldflags="-s" -o $(BUILD_DIR)/localkube ./cmd/localkube
else
docker run -w /go/src/k8s.io/minikube -e GOPATH=/go -v $(shell pwd):/go/src/k8s.io/minikube golang:1.6 make out/localkube
endif
minikube-$(GOOS)-$(GOARCH): gopath
out/minikube-$(GOOS)-$(GOARCH): .gopath pkg/minikube/cluster/localkubecontents.go
CGO_ENABLED=0 GOARCH=$(GOARCH) GOOS=$(GOOS) go build --installsuffix cgo -a -o $(BUILD_DIR)/minikube-$(GOOS)-$(GOARCH) ./cmd/minikube
localkube-$(GOOS)-$(GOARCH): gopath
GOARCH=$(GOARCH) GOOS=$(GOOS) go build -o $(BUILD_DIR)/localkube-$(GOOS)-$(GOARCH) ./cmd/localkube
.PHONY: integration
integration: minikube
integration: out/minikube
go test -v ./test/integration --tags=integration
localkube-incremental:
GOPATH=/go CGO_ENABLED=1 GOBIN=$(shell pwd)/$(BUILD_DIR) go install ./cmd/localkube
docker/localkube:
docker run -w /go/src/k8s.io/minikube -v $(shell pwd):/go/src/k8s.io/minikube golang:1.6 make localkube-incremental
.PHONY: test
test: gopath
test: .gopath pkg/minikube/cluster/localkubecontents.go
./test.sh
pkg/minikube/cluster/localkubecontents.go: out/localkube $(GOPATH)/bin/go-bindata
$(GOPATH)/bin/go-bindata -nomemcopy -o pkg/minikube/cluster/localkubecontents.go -pkg cluster ./out/localkube
$(GOPATH)/bin/go-bindata: .gopath
go get github.com/jteeuwen/go-bindata/...

View File

@ -31,11 +31,15 @@ For more information about minikube, see the [proposal](https://github.com/kuber
* Simplifying kubernetes production deployment experience. Kube-deploy is attempting to tackle this problem.
* Supporting all possible deployment configurations of Kubernetes like various types of storage, networking, etc.
## Build Requirements
* A recent Go distribution (>1.6)
* If you're not on Linux, you'll need a Docker installation
## Build Instructions
```shell
make minikube
make out/minikube
```
## Run Instructions

View File

@ -56,6 +56,11 @@ func runStart(cmd *cobra.Command, args []string) {
LocalkubeURL: localkubeURL,
}
if err := cluster.UpdateCluster(host.Driver); err != nil {
log.Println("Error updating cluster: ", err)
os.Exit(1)
}
if err := cluster.StartCluster(host, config); err != nil {
log.Println("Error starting cluster: ", err)
os.Exit(1)

View File

@ -27,9 +27,11 @@ import (
"github.com/docker/machine/drivers/virtualbox"
"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/state"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/sshutil"
)
const (
@ -145,7 +147,7 @@ type KubernetesConfig struct {
// StartCluster starts a k8s cluster on the specified Host.
func StartCluster(h sshAble, config KubernetesConfig) error {
output, err := h.RunSSHCommand(getStartCommand(config.LocalkubeURL))
output, err := h.RunSSHCommand(getStartCommand())
log.Println(output)
if err != nil {
return err
@ -154,6 +156,24 @@ func StartCluster(h sshAble, config KubernetesConfig) error {
return nil
}
func UpdateCluster(d drivers.Driver) error {
localkube, err := Asset("out/localkube")
if err != nil {
log.Println("Error loading localkube: ", err)
return err
}
client, err := sshutil.NewSSHClient(d)
if err != nil {
return err
}
if err := sshutil.Transfer(localkube, "/usr/local/bin/", "localkube", "0777", client); err != nil {
return err
}
return nil
}
// GetCreds gets the generated credentials required to talk to the APIServer.
func GetCreds(h sshAble) error {
localPath := constants.Minipath

View File

@ -1,17 +1,10 @@
package cluster
import "fmt"
var startCommand = `sudo killall localkube || true
# Download and install localkube, if it doesn't exist yet.
if [ ! -e /usr/local/bin/localkube ]; then
sudo curl --compressed -L %s -o /usr/local/bin/localkube
sudo chmod a+x /usr/local/bin/localkube
fi
var startCommand = `
# Run with nohup so it stays up. Redirect logs to useful places.
PATH=/usr/local/sbin:$PATH nohup sudo /usr/local/bin/localkube start > /var/log/localkube.out 2> /var/log/localkube.err < /dev/null &
`
func getStartCommand(localkubeURL string) string {
return fmt.Sprintf(startCommand, localkubeURL)
func getStartCommand() string {
return startCommand
}

View File

@ -0,0 +1,128 @@
/*
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 sshutil
import (
"bytes"
"fmt"
"io"
"path/filepath"
"github.com/docker/machine/libmachine/drivers"
machinessh "github.com/docker/machine/libmachine/ssh"
"golang.org/x/crypto/ssh"
)
// SSHSession provides methods for running commands on a host.
type SSHSession interface {
Close() error
StdinPipe() (io.WriteCloser, error)
Run(cmd string) error
Wait() error
}
// NewSSHClient returns an SSH client object for running commands.
func NewSSHClient(d drivers.Driver) (*ssh.Client, error) {
h, err := newSSHHost(d)
if err != nil {
return nil, err
}
auth := &machinessh.Auth{}
if h.SSHKeyPath != "" {
auth.Keys = []string{h.SSHKeyPath}
}
config, err := machinessh.NewNativeConfig(h.Username, auth)
if err != nil {
return nil, err
}
client, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", h.IP, h.Port), &config)
if err != nil {
return nil, err
}
return client, nil
}
// Transfer uses an SSH session to copy a file to the remote machine.
func Transfer(data []byte, remotedir, filename string, perm string, c *ssh.Client) error {
// Delete the old file first. This makes sure permissions get reset.
deleteCmd := fmt.Sprintf("sudo rm -f %s", filepath.Join(remotedir, filename))
if err := RunCommand(c, deleteCmd); err != nil {
return err
}
s, err := c.NewSession()
if err != nil {
return err
}
go func() {
w, err := s.StdinPipe()
defer w.Close()
if err != nil {
return
}
header := fmt.Sprintf("C%s %d %s\n", perm, len(data), filename)
fmt.Fprint(w, header)
reader := bytes.NewReader(data)
io.Copy(w, reader)
fmt.Fprint(w, "\x00")
}()
scpcmd := fmt.Sprintf("sudo /usr/local/bin/scp -t %s", remotedir)
if err := s.Run(scpcmd); err != nil {
return err
}
return nil
}
func RunCommand(c *ssh.Client, cmd string) error {
s, err := c.NewSession()
defer s.Close()
if err != nil {
return err
}
return s.Run(cmd)
}
type sshHost struct {
IP string
Port int
SSHKeyPath string
Username string
}
func newSSHHost(d drivers.Driver) (*sshHost, error) {
ip, err := d.GetSSHHostname()
if err != nil {
return nil, err
}
port, err := d.GetSSHPort()
if err != nil {
return nil, err
}
return &sshHost{
IP: ip,
Port: port,
SSHKeyPath: d.GetSSHKeyPath(),
Username: d.GetSSHUsername(),
}, nil
}

View File

@ -0,0 +1,116 @@
/*
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 sshutil
import (
"strings"
"testing"
"github.com/docker/machine/libmachine/drivers"
"k8s.io/minikube/pkg/minikube/tests"
)
func TestNewSSHClient(t *testing.T) {
s, _ := tests.NewSSHServer()
port, err := s.Start()
if err != nil {
t.Fatalf("Error starting ssh server: %s", err)
}
d := &tests.MockDriver{
Port: port,
BaseDriver: drivers.BaseDriver{
IPAddress: "127.0.0.1",
SSHKeyPath: "",
},
}
c, err := NewSSHClient(d)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
cmd := "foo"
RunCommand(c, cmd)
if !s.Connected {
t.Fatalf("Error!")
}
if !strings.Contains(s.Commands[0], cmd) {
t.Fatalf("Expected command: %s, got %s", cmd, s.Commands[0])
}
}
func TestNewSSHHost(t *testing.T) {
sshKeyPath := "mypath"
ip := "localhost"
user := "myuser"
d := tests.MockDriver{
BaseDriver: drivers.BaseDriver{
IPAddress: ip,
SSHUser: user,
SSHKeyPath: sshKeyPath,
},
}
h, err := newSSHHost(&d)
if err != nil {
t.Fatalf("Unexpected error creating host: %s", err)
}
if h.SSHKeyPath != sshKeyPath {
t.Fatalf("%s != %s", h.SSHKeyPath, sshKeyPath)
}
if h.Username != user {
t.Fatalf("%s != %s", h.Username, user)
}
if h.IP != ip {
t.Fatalf("%s != %s", h.IP, ip)
}
}
func TestNewSSHHostError(t *testing.T) {
d := tests.MockDriver{HostError: true}
_, err := newSSHHost(&d)
if err == nil {
t.Fatal("Expected error creating host, got nil")
}
}
func TestTransfer(t *testing.T) {
s, _ := tests.NewSSHServer()
port, err := s.Start()
if err != nil {
t.Fatalf("Error starting ssh server: %s", err)
}
d := &tests.MockDriver{
Port: port,
BaseDriver: drivers.BaseDriver{
IPAddress: "127.0.0.1",
SSHKeyPath: "",
},
}
c, err := NewSSHClient(d)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
dest := "bar"
if err := Transfer([]byte("testcontents"), "/tmp", dest, "0777", c); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
}

View File

@ -29,6 +29,8 @@ type MockDriver struct {
drivers.BaseDriver
CurrentState state.State
RemoveError bool
HostError bool
Port int
}
// Create creates a MockDriver instance
@ -37,14 +39,30 @@ func (driver *MockDriver) Create() error {
return nil
}
func (driver *MockDriver) GetIP() (string, error) {
return driver.BaseDriver.GetIP()
}
// GetCreateFlags returns the flags used to create a MockDriver
func (driver *MockDriver) GetCreateFlags() []mcnflag.Flag {
return []mcnflag.Flag{}
}
func (driver *MockDriver) GetSSHPort() (int, error) {
return driver.Port, nil
}
// GetSSHHostname returns the hostname for SSH
func (driver *MockDriver) GetSSHHostname() (string, error) {
return "", nil
if driver.HostError {
return "", fmt.Errorf("Error getting host!")
}
return "localhost", nil
}
// GetSSHHostname returns the hostname for SSH
func (driver *MockDriver) GetSSHKeyPath() string {
return driver.BaseDriver.SSHKeyPath
}
// GetState returns the state of the driver

View File

@ -0,0 +1,93 @@
package tests
import (
"crypto/rand"
"crypto/rsa"
"io"
"io/ioutil"
"net"
"strconv"
"golang.org/x/crypto/ssh"
)
// SSHServer provides a mock SSH Server for testing. Commands are stored, not executed.
type SSHServer struct {
Config *ssh.ServerConfig
// Commands stores the raw commands executed against the server.
Commands []string
Connected bool
}
// NewSSHServer returns a NewSSHServer instance, ready for use.
func NewSSHServer() (*SSHServer, error) {
s := &SSHServer{}
s.Config = &ssh.ServerConfig{
NoClientAuth: true,
}
private, err := rsa.GenerateKey(rand.Reader, 2014)
if err != nil {
return nil, err
}
signer, err := ssh.NewSignerFromKey(private)
if err != nil {
return nil, err
}
s.Config.AddHostKey(signer)
return s, nil
}
// Start starts the mock SSH Server, and returns the port it's listening on.
func (s *SSHServer) Start() (int, error) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return 0, err
}
// Main loop, listen for connections and store the commands.
go func() {
for {
nConn, err := listener.Accept()
if err != nil {
return
}
_, chans, reqs, err := ssh.NewServerConn(nConn, s.Config)
if err != nil {
return
}
// The incoming Request channel must be serviced.
go ssh.DiscardRequests(reqs)
// Service the incoming Channel channel.
for newChannel := range chans {
channel, requests, err := newChannel.Accept()
s.Connected = true
if err != nil {
return
}
req := <-requests
req.Reply(true, nil)
s.Commands = append(s.Commands, string(req.Payload))
channel.SendRequest("exit-status", false, []byte{0, 0, 0, 0})
// Discard anything that comes in over stdin.
io.Copy(ioutil.Discard, channel)
channel.Close()
}
}
}()
// Parse and return the port.
_, p, err := net.SplitHostPort(listener.Addr().String())
if err != nil {
return 0, err
}
port, err := strconv.Atoi(p)
if err != nil {
return 0, err
}
return port, nil
}

View File

@ -22,4 +22,4 @@ for TEST in $(find -name "*.go" | grep -v vendor | grep -v integration | grep _t
done
# Check gofmt
diff -u <(echo -n) <(gofmt -l -s . | grep -v vendor)
diff -u <(echo -n) <(gofmt -l -s . | grep -v "vendor\|\.gopath\|localkubecontents.go")