Move minikube build cmd to minikube image build

Use the long name also for examples and usage
pull/11164/head
Anders F Björklund 2021-03-10 08:27:27 +01:00
parent 6f26e4b8ec
commit 6cfbf2cc3d
7 changed files with 113 additions and 148 deletions

View File

@ -1,97 +0,0 @@
/*
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 (
"io"
"io/ioutil"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/machine"
"k8s.io/minikube/pkg/minikube/reason"
docker "k8s.io/minikube/third_party/go-dockerclient"
)
var (
tag string
dockerFile string
)
func createTar(dir string) (string, error) {
tmp, err := ioutil.TempFile("", "build.*.tar")
if err != nil {
return "", err
}
tar, err := docker.CreateTarStream(dir, dockerFile)
if err != nil {
return "", err
}
_, err = io.Copy(tmp, tar)
if err != nil {
return "", err
}
err = tmp.Close()
if err != nil {
return "", err
}
return tmp.Name(), nil
}
// buildCmd represents the build command
var buildCmd = &cobra.Command{
Use: "build",
Short: "Build a container image",
Long: `Build a container image, using the container runtime.
Examples:
minikube build .`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
exit.Message(reason.Usage, "minikube build -- [OPTIONS] PATH | URL | -")
}
// Cache and load images into docker daemon
profile, err := config.LoadProfile(viper.GetString(config.ProfileName))
if err != nil {
exit.Error(reason.Usage, "loading profile", err)
}
img := args[0]
var tmp string
info, err := os.Stat(img)
if err == nil && info.IsDir() {
tmp, err := createTar(img)
if err != nil {
exit.Error(reason.GuestImageBuild, "Failed to build image", err)
}
img = tmp
}
if err := machine.BuildImage(img, tag, []*config.Profile{profile}); err != nil {
exit.Error(reason.GuestImageBuild, "Failed to build image", err)
}
if tmp != "" {
os.Remove(tmp)
}
},
}
func init() {
buildCmd.Flags().StringVarP(&tag, "tag", "t", "", "Tag to apply to the new image (optional)")
buildCmd.Flags().StringVarP(&dockerFile, "file", "f", "Dockerfile", "Path to the Dockerfile to use")
}

View File

@ -29,6 +29,7 @@ import (
"k8s.io/minikube/pkg/minikube/image"
"k8s.io/minikube/pkg/minikube/machine"
"k8s.io/minikube/pkg/minikube/reason"
docker "k8s.io/minikube/third_party/go-dockerclient"
)
// imageCmd represents the image command
@ -59,6 +60,11 @@ func saveFile(r io.Reader) (string, error) {
return tmp.Name(), nil
}
var (
tag string
dockerFile string
)
// loadImageCmd represents the image load command
var loadImageCmd = &cobra.Command{
Use: "load IMAGE | ARCHIVE | -",
@ -173,6 +179,61 @@ $ minikube image list
},
}
func createTar(dir string) (string, error) {
tmp, err := ioutil.TempFile("", "build.*.tar")
if err != nil {
return "", err
}
tar, err := docker.CreateTarStream(dir, dockerFile)
if err != nil {
return "", err
}
_, err = io.Copy(tmp, tar)
if err != nil {
return "", err
}
err = tmp.Close()
if err != nil {
return "", err
}
return tmp.Name(), nil
}
// buildImageCmd represents the image build command
var buildImageCmd = &cobra.Command{
Use: "build",
Short: "Build a container image in minikube",
Long: "Build a container image, using the container runtime.",
Example: `minikube image build .`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
exit.Message(reason.Usage, "Please provide a path to build")
}
// Cache and load images into docker daemon
profile, err := config.LoadProfile(viper.GetString(config.ProfileName))
if err != nil {
exit.Error(reason.Usage, "loading profile", err)
}
img := args[0]
var tmp string
info, err := os.Stat(img)
if err == nil && info.IsDir() {
tmp, err := createTar(img)
if err != nil {
exit.Error(reason.GuestImageBuild, "Failed to build image", err)
}
img = tmp
}
if err := machine.BuildImage(img, tag, []*config.Profile{profile}); err != nil {
exit.Error(reason.GuestImageBuild, "Failed to build image", err)
}
if tmp != "" {
os.Remove(tmp)
}
},
}
func init() {
imageCmd.AddCommand(loadImageCmd)
imageCmd.AddCommand(removeImageCmd)
@ -180,4 +241,7 @@ func init() {
loadImageCmd.Flags().BoolVar(&imgDaemon, "daemon", false, "Cache image from docker daemon")
loadImageCmd.Flags().BoolVar(&imgRemote, "remote", false, "Cache image from remote registry")
imageCmd.AddCommand(listImageCmd)
buildImageCmd.Flags().StringVarP(&tag, "tag", "t", "", "Tag to apply to the new image (optional)")
buildImageCmd.Flags().StringVarP(&dockerFile, "file", "f", "Dockerfile", "Path to the Dockerfile to use")
imageCmd.AddCommand(buildImageCmd)
}

View File

@ -250,7 +250,6 @@ func init() {
mountCmd,
sshCmd,
kubectlCmd,
buildCmd,
nodeCmd,
cpCmd,
},

View File

@ -204,6 +204,7 @@ func (r *CRIO) BuildImage(path string, tag string) error {
if tag != "" {
args = append(args, "-t", tag)
}
args = append(args, path)
c := exec.Command("sudo", args...)
if _, err := r.Runner.RunCmd(c); err != nil {
return errors.Wrap(err, "crio build image")

View File

@ -224,6 +224,7 @@ func (r *Docker) BuildImage(path string, tag string) error {
if tag != "" {
args = append(args, "-t", tag)
}
args = append(args, path)
c := exec.Command("docker", args...)
if _, err := r.Runner.RunCmd(c); err != nil {
return errors.Wrap(err, "buildimage docker.")

View File

@ -1,50 +0,0 @@
---
title: "build"
description: >
Build a container image
---
## minikube build
Build a container image
### Synopsis
Build a container image, using the container runtime.
Examples:
minikube build .
```shell
minikube build [flags]
```
### Options
```
-f, --file string Path to the Dockerfile to use (default "Dockerfile")
-t, --tag string Tag to apply to the new image (optional)
```
### Options inherited from parent commands
```
--add_dir_header If true, adds the file directory to the header of the log messages
--alsologtostderr log to standard error as well as files
-b, --bootstrapper string The name of the cluster bootstrapper that will set up the Kubernetes cluster. (default "kubeadm")
-h, --help
--log_backtrace_at traceLocation when logging hits line file:N, emit a stack trace (default :0)
--log_dir string If non-empty, write log files in this directory
--log_file string If non-empty, use this log file
--log_file_max_size uint Defines the maximum size a log file can grow to. Unit is megabytes. If the value is 0, the maximum file size is unlimited. (default 1800)
--logtostderr log to standard error instead of files
--one_output If true, only write logs to their native severity level (vs also writing to each lower severity level
-p, --profile string The name of the minikube VM being used. This can be set to allow having multiple instances of minikube independently. (default "minikube")
--skip_headers If true, avoid header prefixes in the log messages
--skip_log_headers If true, avoid headers when opening log files
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
--user string Specifies the user executing the operation. Useful for auditing operations executed by 3rd party tools. Defaults to the operating system username.
-v, --v Level number for the log level verbosity
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
```

View File

@ -35,6 +35,53 @@ Manage images
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
```
## minikube image build
Build a container image in minikube
### Synopsis
Build a container image, using the container runtime.
```shell
minikube image build [flags]
```
### Examples
```
minikube image build .
```
### Options
```
-f, --file string Path to the Dockerfile to use (default "Dockerfile")
-t, --tag string Tag to apply to the new image (optional)
```
### Options inherited from parent commands
```
--add_dir_header If true, adds the file directory to the header of the log messages
--alsologtostderr log to standard error as well as files
-b, --bootstrapper string The name of the cluster bootstrapper that will set up the Kubernetes cluster. (default "kubeadm")
-h, --help
--log_backtrace_at traceLocation when logging hits line file:N, emit a stack trace (default :0)
--log_dir string If non-empty, write log files in this directory
--log_file string If non-empty, use this log file
--log_file_max_size uint Defines the maximum size a log file can grow to. Unit is megabytes. If the value is 0, the maximum file size is unlimited. (default 1800)
--logtostderr log to standard error instead of files
--one_output If true, only write logs to their native severity level (vs also writing to each lower severity level)
-p, --profile string The name of the minikube VM being used. This can be set to allow having multiple instances of minikube independently. (default "minikube")
--skip_headers If true, avoid header prefixes in the log messages
--skip_log_headers If true, avoid headers when opening log files
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
--user string Specifies the user executing the operation. Useful for auditing operations executed by 3rd party tools. Defaults to the operating system username.
-v, --v Level number for the log level verbosity
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
```
## minikube image help
Help about any command