Skip to content

Commit

Permalink
Add a build command, for building container images
Browse files Browse the repository at this point in the history
  • Loading branch information
afbjorklund committed Jul 25, 2019
1 parent d07dd39 commit 529feba
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 16 deletions.
114 changes: 114 additions & 0 deletions cmd/minikube/cmd/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
Copyright 2019 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"
"os"
"os/exec"
"path/filepath"
"runtime"
"syscall"

"github.com/golang/glog"
"github.com/spf13/cobra"
"k8s.io/minikube/pkg/minikube/cluster"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/machine"
)

// buildCmd represents the build command
var buildCmd = &cobra.Command{
Use: "build",
Short: "Build a container image",
Long: `Run the docker client, download it if necessary.
Examples:
minikube build .`,
Run: func(cmd *cobra.Command, args []string) {
api, err := machine.NewAPIClient()
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting client: %v\n", err)
os.Exit(1)
}
defer api.Close()

version := constants.DefaultBuildDockerVersion
if runtime.GOOS == "windows" {
version = constants.FallbackBuildDockerVersion
}
archive, err := machine.CacheDockerArchive("docker", version, runtime.GOOS, runtime.GOARCH)
if err != nil {
exit.WithError("Failed to download docker", err)
}

binary := "docker"
if runtime.GOOS == "windows" {
binary = "docker.exe"
}
path := filepath.Join(filepath.Dir(archive), binary)

err = machine.ExtractBinary(archive, path, fmt.Sprintf("docker/%s", binary))
if err != nil {
exit.WithError("Failed to extract docker", err)
}

envMap, err := cluster.GetHostDockerEnv(api)
if err != nil {
exit.WithError("Failed to get docker env", err)
}

tlsVerify := envMap["DOCKER_TLS_VERIFY"]
certPath := envMap["DOCKER_CERT_PATH"]
dockerHost := envMap["DOCKER_HOST"]

options := []string{}
if tlsVerify != "" {
options = append(options, "--tlsverify")
}
if certPath != "" {
options = append(options, "--tlscacert", filepath.Join(certPath, "ca.pem"))
options = append(options, "--tlscert", filepath.Join(certPath, "cert.pem"))
options = append(options, "--tlskey", filepath.Join(certPath, "key.pem"))
}
options = append(options, "-H", dockerHost)

options = append(options, "build")
args = append(options, args...)

glog.Infof("Running %s %v", path, args)
c := exec.Command(path, args...)
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if err := c.Run(); err != nil {
var rc int
if exitError, ok := err.(*exec.ExitError); ok {
waitStatus := exitError.Sys().(syscall.WaitStatus)
rc = waitStatus.ExitStatus()
} else {
fmt.Fprintf(os.Stderr, "Error running %s: %v\n", path, err)
rc = 1
}
os.Exit(rc)
}
},
}

func init() {
RootCmd.AddCommand(buildCmd)
}
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ kubectl get pods --namespace kube-system`,
version = cc.KubernetesConfig.KubernetesVersion
}

path, err := machine.CacheBinary(binary, version, runtime.GOOS, runtime.GOARCH)
path, err := machine.CacheKubernetesBinary(binary, version, runtime.GOOS, runtime.GOARCH)
if err != nil {
exit.WithError("Failed to download kubectl", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/minikube/bootstrapper/kubeadm/kubeadm.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ func downloadBinaries(cfg config.KubernetesConfig, c command.Runner) error {
for _, bin := range constants.GetKubeadmCachedBinaries() {
bin := bin
g.Go(func() error {
path, err := machine.CacheBinary(bin, cfg.KubernetesVersion, "linux", runtime.GOARCH)
path, err := machine.CacheKubernetesBinary(bin, cfg.KubernetesVersion, "linux", runtime.GOARCH)
if err != nil {
return errors.Wrapf(err, "downloading %s", bin)
}
Expand Down
41 changes: 41 additions & 0 deletions pkg/minikube/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ var NewestKubernetesVersion = "v1.15.0"
// OldestKubernetesVersion is the oldest Kubernetes version to test against
var OldestKubernetesVersion = "v1.10.13"

// DefaultBuildDockerVersion is the Docker version to use for building images
var DefaultBuildDockerVersion = "18.09.8"

// FallbackBuildDockerVersion is old Docker version to use for building images
var FallbackBuildDockerVersion = "17.09.0-ce"

// ConfigFilePath is the path of the config directory
var ConfigFilePath = MakeMiniPath("config")

Expand Down Expand Up @@ -244,6 +250,41 @@ var ImageRepositories = map[string][]string{
"cn": {"registry.cn-hangzhou.aliyuncs.com/google_containers"},
}

// GetDockerReleaseURL gets the location of a docker client archive
func GetDockerReleaseURL(binaryName, version, osName, archName string) string {

var osDir string
switch osName {
case "linux":
osDir = "linux"
case "darwin":
osDir = "mac"
case "windows":
osDir = "win"
default:
osDir = osName
}

var archDir string
switch archName {
case "amd64":
archDir = "x86_64"
default:
archDir = archName
}

var binaryType string
switch osName {
case "windows":
binaryType = "zip"
default:
binaryType = "tgz"
}

binaryArchive := fmt.Sprintf("%s-%s.%s", binaryName, version, binaryType)
return fmt.Sprintf("https://download.docker.com/%s/static/stable/%s/%s", osDir, archDir, binaryArchive)
}

// GetKubernetesReleaseURL gets the location of a kubernetes client
func GetKubernetesReleaseURL(binaryName, version, osName, archName string) string {
return fmt.Sprintf("https://storage.googleapis.com/kubernetes-release/release/%s/bin/%s/%s/%s", version, osName, archName, binaryName)
Expand Down
Loading

0 comments on commit 529feba

Please sign in to comment.