Skip to content

Commit

Permalink
feat(kubectl-trace): support version for image version
Browse files Browse the repository at this point in the history
Signed-off-by: Lorenzo Fontana <lo@linux.com>
  • Loading branch information
fntlnz committed Dec 24, 2018
1 parent b4a9c9c commit 38922d3
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 3 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@ IMAGE_BPFTRACE_COMMIT := quay.io/fntlnz/kubectl-trace-bpftrace:$(GIT_COMMIT)

IMAGE_BUILD_FLAGS ?= "--no-cache"

LDFLAGS := -ldflags '-X github.com/fntlnz/kubectl-trace/pkg/version.buildTime=$(shell date +%s) -X github.com/fntlnz/kubectl-trace/pkg/version.gitCommit=${GIT_COMMIT}'

kubectl_trace ?= _output/bin/kubectl-trace
trace_runner ?= _output/bin/trace-runner

.PHONY: build
build: clean ${kubectl_trace} ${trace_runner}

${kubectl_trace}:
CGO_ENABLED=0 $(GO) build -o $@ ./cmd/kubectl-trace
CGO_ENABLED=1 $(GO) build ${LDFLAGS} -o $@ ./cmd/kubectl-trace

${trace_runner}:
CGO_ENABLED=1 $(GO) build -o $@ ./cmd/trace-runner
CGO_ENABLED=1 $(GO) build ${LDFLAGS} -o $@ ./cmd/trace-runner

.PHONY: clean
clean:
Expand Down
1 change: 1 addition & 0 deletions cmd/kubectl-trace/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func main() {
Out: os.Stdout,
ErrOut: os.Stderr,
}

root := cmd.NewTraceCommand(streams)
if err := root.Execute(); err != nil {
os.Exit(1)
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func NewTraceCommand(streams genericclioptions.IOStreams) *cobra.Command {
cmd.AddCommand(NewGetCommand(f, streams))
cmd.AddCommand(NewAttachCommand(f, streams))
cmd.AddCommand(NewDeleteCommand(f, streams))
cmd.AddCommand(NewVersionCommand(streams))

return cmd
}
21 changes: 21 additions & 0 deletions pkg/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

import (
"fmt"

"github.com/fntlnz/kubectl-trace/pkg/version"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

func NewVersionCommand(streams genericclioptions.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Print the version information for kubectl trace",
RunE: func(c *cobra.Command, args []string) error {
fmt.Fprintln(streams.Out, version.String())
return nil
},
}
return cmd
}
4 changes: 3 additions & 1 deletion pkg/tracejob/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package tracejob
import (
"fmt"

"github.com/fntlnz/kubectl-trace/pkg/version"

"io"
"io/ioutil"

Expand Down Expand Up @@ -248,7 +250,7 @@ func (t *TraceJobClient) CreateJob(nj TraceJob) (*batchv1.Job, error) {
Containers: []apiv1.Container{
apiv1.Container{
Name: nj.Name,
Image: "quay.io/fntlnz/kubectl-trace-bpftrace:feature-pod-support-tracerunner", //TODO(fntlnz): yes this should be configurable!
Image: version.ImageNameTag(),
Command: bpfTraceCmd,
TTY: true,
Stdin: true,
Expand Down
41 changes: 41 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package version

import (
"fmt"
"strconv"
"time"
)

// Populated by makefile
var gitCommit string
var buildTime string
var versionFormat = "git commit: %s\nbuild date: %s"
var imageNameTagFormat = "quay.io/fntlnz/kubectl-trace-bpftrace:%s"

func GitCommit() string {
return gitCommit
}

func ImageNameTag() string {
return fmt.Sprintf(imageNameTagFormat, GitCommit())
}

func Time() *time.Time {
if len(buildTime) == 0 {
return nil
}
i, err := strconv.ParseInt(buildTime, 10, 64)
if err != nil {
return nil
}
t := time.Unix(i, 0)
return &t
}

func String() string {
ts := Time()
if ts == nil {
return fmt.Sprintf(versionFormat, GitCommit(), "undefined")
}
return fmt.Sprintf(versionFormat, GitCommit(), ts.String())
}

0 comments on commit 38922d3

Please sign in to comment.