Skip to content

Commit

Permalink
Merge pull request #7 from warm-metal/version
Browse files Browse the repository at this point in the history
version command and build chain
  • Loading branch information
kitt1987 authored Jan 26, 2021
2 parents 967612b + b13ab0e commit fb8e27e
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/_output
/.idea
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ The [csi-driver-image](https://github.com/warm-metal/csi-driver-image) is also n
You can install the predefined manifests via the `--also-apply-csi-driver` option while starting the debug command.
Or, you can also apply custom manifests manually.

If you have golang toolchains, run
```go
go install github.com/warm-metal/kubectl-dev
```
You can download the binary for either Linux or MacOS from the Release page. Then put it into your `PATH` directory.

## Usage

Expand All @@ -57,3 +54,12 @@ kubectl dev build install --minikube
# Build image in cluster using docker parameters and options.
kubectl dev build -t docker.io/warmmetal/image:tag -f test.dockerfile .
```

## Build
```shell script
# For MacOS
kubectl dev build -f hack/dev/Dockerfile --local _output/ --target mac-cli

# For Linux
kubectl dev build -f hack/dev/Dockerfile --local _output/ --target linux-cli
```
File renamed without changes.
34 changes: 34 additions & 0 deletions hack/dev/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
FROM golang:1-buster as builder

WORKDIR /go/src/kubectl-dev
COPY go.mod go.sum ./
RUN go mod download
COPY .git ./.git

FROM builder as linux
ENV GOOS=linux
COPY cmd ./cmd
COPY pkg ./pkg
RUN CGO_ENABLED=0 go build \
-o kubectl-dev \
-ldflags "-X github.com/warm-metal/kubectl-dev/pkg/release.Version=$(git for-each-ref --points-at=HEAD --format='%(refname:short)' refs/tags --sort=-version:refname --count=2)$(git symbolic-ref -q --short HEAD) -X github.com/warm-metal/kubectl-dev/pkg/release.Commit=$(git rev-parse HEAD)" \
./cmd/dev

FROM builder as macos
ENV GOOS=darwin
COPY cmd ./cmd
COPY pkg ./pkg
RUN CGO_ENABLED=0 go build \
-o kubectl-dev \
-ldflags "-X github.com/warm-metal/kubectl-dev/pkg/release.Version=$(git for-each-ref --points-at=HEAD --format='%(refname:short)' refs/tags --sort=-version:refname --count=2)$(git symbolic-ref -q --short HEAD) -X github.com/warm-metal/kubectl-dev/pkg/release.Commit=$(git rev-parse HEAD)" \
./cmd/dev

FROM bash:5 as app
COPY --from=linux /go/src/kubectl-dev/kubectl-dev /usr/local/bin/
ENTRYPOINT ["tail", "-f", "/dev/null"]

FROM scratch as linux-cli
COPY --from=linux /go/src/kubectl-dev/kubectl-dev .

FROM scratch as mac-cli
COPY --from=macos /go/src/kubectl-dev/kubectl-dev .
29 changes: 23 additions & 6 deletions pkg/cmd/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type BuildOptions struct {

dockerfile string
tag string
localDir string
targetStage string
noCache bool
buildArgs []string
Expand Down Expand Up @@ -105,17 +106,27 @@ func (o *BuildOptions) Complete(cmd *cobra.Command, args []string) error {
o.solveOpt.FrontendAttrs["build-arg:"+kv[0]] = kv[1]
}

if len(o.tag) == 0 {
return fmt.Errorf("-t or --tag is required")
if len(o.tag) == 0 && len(o.localDir) == 0 {
return fmt.Errorf("set either a tag or a local path")
}

o.solveOpt.Exports = []buildkit.ExportEntry{
{
if len(o.tag) > 0 {
o.solveOpt.Exports = append(o.solveOpt.Exports, buildkit.ExportEntry{
Type: "image",
Attrs: map[string]string{
"name": o.tag,
},
},
})
}

if len(o.localDir) > 0 {
o.solveOpt.Exports = append(o.solveOpt.Exports, buildkit.ExportEntry{
Type: "local",
Attrs: map[string]string{
"dest": o.localDir,
},
OutputDir: o.localDir,
})
}

return nil
Expand Down Expand Up @@ -177,7 +188,11 @@ func NewCmd(opts *opts.GlobalOptions, streams genericclioptions.IOStreams) *cobr
"kubectl-dev build" use buildkitd as its build engine. Since buildkitd only support containerd or oci
as its worker, the build command also only support containerd as the container runtime.`,
Example: `# Build image in the cluster using docker parameters and options.
kubectl dev build -t foo:latest -f Dockerfile .`,
kubectl dev build -t foo:latest -f Dockerfile .
# Build a binary and save to a local directory.
kubectl dev build -f Dockerfile --local foo/bar/ .
`,
SilenceErrors: false,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -199,6 +214,8 @@ kubectl dev build -t foo:latest -f Dockerfile .`,
"Name of the Dockerfile (Default is 'PATH/Dockerfile')")
cmd.Flags().StringVarP(&o.tag, "tag", "t", "",
"Name and optionally a tag in the 'name:tag' format")
cmd.Flags().StringVar(&o.localDir, "local", "",
"Build binaries instead an image and copy them to the specified path.")
cmd.Flags().StringVar(&o.targetStage, "target", "", "Set the target build stage to build.")
cmd.Flags().BoolVar(&o.noCache, "no-cache", false, "Do not use cache when building.")
cmd.Flags().StringSliceVar(&o.buildArgs, "build-arg", nil, "Set build-time variables.")
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ kubectl dev build -t foo:tag -f Dockerfile .`,
}

cmd.AddCommand(NewCmdDebug(o, streams), build.NewCmd(o, streams))
cmd.AddCommand(NewVersionCmd())

cmd.PersistentFlags().StringVar(&o.DevNamespace, "dev-namespace", "dev",
"Namespace in which kubectl-dev coordinators installed")
o.AddFlags(cmd.Flags())
Expand Down
17 changes: 17 additions & 0 deletions pkg/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"github.com/warm-metal/kubectl-dev/pkg/release"
)

func NewVersionCmd() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Print the version information.",
Run: func(_ *cobra.Command, _ []string) {
fmt.Println(release.Version, release.Commit)
},
}
}
4 changes: 4 additions & 0 deletions pkg/release/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package release

var Version = "v0.1.0"
var Commit = ""

0 comments on commit fb8e27e

Please sign in to comment.