Skip to content

Commit

Permalink
Update the Jaeger Operator version at build time
Browse files Browse the repository at this point in the history
Signed-off-by: Juraci Paixão Kröhling <[email protected]>
  • Loading branch information
jpkrohling committed Sep 3, 2018
1 parent 15645fc commit 764e981
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
tmp/_output
tmp/_test
deploy/test

_output

# Created by https://www.gitignore.io/api/go,vim,emacs,visualstudiocode

Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,11 @@ required = [
[[constraint]]
name = "github.com/stretchr/testify"
version = "1.2.2"

[[constraint]]
name = "github.com/spf13/cobra"
version = "0.0.3"

[[constraint]]
name = "github.com/spf13/viper"
version = "1.1.0"
31 changes: 22 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
VERSION_DATE ?= $(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
CI_COMMIT_SHA ?= $(shell git rev-parse HEAD)
GO_FLAGS ?= GOOS=linux GOARCH=amd64 CGO_ENABLED=0
OPERATOR_NAME=jaeger-operator
NAMESPACE="$(USER)"
BUILD_IMAGE="$(NAMESPACE)/$(OPERATOR_NAME):latest"
KUBERNETES_CONFIG ?= "$(HOME)/.kube/config"
WATCH_NAMESPACE ?= default
BIN_DIR ?= "_output/bin"

OPERATOR_NAME ?= jaeger-operator
NAMESPACE ?= "$(USER)"
BUILD_IMAGE ?= "$(NAMESPACE)/$(OPERATOR_NAME):latest"
OUTPUT_BINARY ?= "$(BIN_DIR)/$(OPERATOR_NAME)"
VERSION_PKG ?= "github.com/jaegertracing/jaeger-operator/pkg/cmd/version"

LD_FLAGS ?= "-X $(VERSION_PKG).gitCommit=$(CI_COMMIT_SHA) -X $(VERSION_PKG).buildDate=$(VERSION_DATE)"
PACKAGES := $(shell go list ./cmd/... ./pkg/...)

.DEFAULT_GOAL := build
Expand All @@ -24,23 +31,29 @@ lint:

build: format
@echo Building...
@operator-sdk build $(BUILD_IMAGE) > /dev/null
@${GO_FLAGS} go build -o $(OUTPUT_BINARY) -ldflags $(LD_FLAGS)

docker:
@docker build -t "$(BUILD_IMAGE)" .

push:
@echo Pushing image...
@echo Pushing image $(BUILD_IMAGE)...
@docker push $(BUILD_IMAGE) > /dev/null

unit-tests:
@echo Running unit tests...
@go test $(PACKAGES) -cover -coverprofile=cover.out

e2e-tests: build push
e2e-tests: build docker push
@echo Running end-to-end tests...
@operator-sdk test --test-location ./test/e2e
@cp deploy/rbac.yaml deploy/test/namespace-manifests.yaml
@echo "---" >> deploy/test/namespace-manifests.yaml
@cat deploy/operator.yaml | sed "s~image: jaegertracing\/jaeger-operator\:.*~image: $(BUILD_IMAGE)~gi" >> deploy/test/namespace-manifests.yaml
@go test ./test/e2e/... -kubeconfig $(KUBERNETES_CONFIG) -namespacedMan ../../deploy/test/namespace-manifests.yaml -globalMan ../../deploy/crd.yaml -root .

run:
@kubectl create -f deploy/crd.yaml > /dev/null 2>&1 || true
@OPERATOR_NAME=$(OPERATOR_NAME) operator-sdk up local
@OPERATOR_NAME=$(OPERATOR_NAME) KUBERNETES_CONFIG=$(KUBERNETES_CONFIG) WATCH_NAMESPACE=$(WATCH_NAMESPACE) ./_output/bin/jaeger-operator start

es:
@kubectl create -f ./test/elasticsearch.yml
Expand All @@ -54,5 +67,5 @@ generate:
@operator-sdk generate k8s

test: unit-tests e2e-tests
all: check format lint unit-tests
all: check format lint unit-tests docker
ci: all
38 changes: 0 additions & 38 deletions cmd/jaeger-operator/main.go

This file was deleted.

66 changes: 66 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cmd

import (
"fmt"
"os"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/jaegertracing/jaeger-operator/pkg/cmd/start"
"github.com/jaegertracing/jaeger-operator/pkg/cmd/version"
)

var cfgFile string

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "jaeger-operator",
Short: "The Kubernetes operator for Jaeger",
Long: `The Kubernetes operator for Jaeger`,
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func init() {
cobra.OnInitialize(initConfig)

RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.jaeger-operator.yaml)")

RootCmd.AddCommand(start.NewStartCommand())
RootCmd.AddCommand(version.NewVersionCommand())
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Search config in home directory with name ".jaeger-operator" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".jaeger-operator")
}

viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
3 changes: 2 additions & 1 deletion deploy/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ spec:
spec:
containers:
- name: jaeger-operator
image: jpkroehling/jaeger-operator:latest
image: jaegertracing/jaeger-operator:1.6
ports:
- containerPort: 60000
name: metrics
command:
- jaeger-operator
args: ["start"]
imagePullPolicy: Always
env:
- name: WATCH_NAMESPACE
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/jaegertracing/jaeger-operator/cmd"

func main() {
cmd.Execute()
}
57 changes: 57 additions & 0 deletions pkg/cmd/start/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package start

import (
"context"
"os"
"os/signal"
"syscall"

sdk "github.com/operator-framework/operator-sdk/pkg/sdk"
k8sutil "github.com/operator-framework/operator-sdk/pkg/util/k8sutil"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // this

stub "github.com/jaegertracing/jaeger-operator/pkg/stub"
)

// NewStartCommand starts the Jaeger Operator
func NewStartCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "start",
Short: "Starts a new Jaeger Operator",
Long: "Starts a new Jaeger Operator",
Run: func(cmd *cobra.Command, args []string) {
start(cmd, args)
},
}

return cmd
}

func start(cmd *cobra.Command, args []string) {
var ch = make(chan os.Signal, 0)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)

ctx := context.Background()

sdk.ExposeMetricsPort()

resource := "io.jaegertracing/v1alpha1"
kind := "Jaeger"
namespace, err := k8sutil.GetWatchNamespace()
if err != nil {
logrus.Fatalf("failed to get watch namespace: %v", err)
}
resyncPeriod := 5
logrus.Infof("Watching %s, %s, %s, %d", resource, kind, namespace, resyncPeriod)
sdk.Watch(resource, kind, namespace, resyncPeriod)
sdk.Handle(stub.NewHandler())
go sdk.Run(ctx)

select {
case <-ch:
ctx.Done()
logrus.Info("Jaeger Operator finished")
}
}
52 changes: 52 additions & 0 deletions pkg/cmd/version/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package version

import (
"encoding/json"
"fmt"
"runtime"

sdkVersion "github.com/operator-framework/operator-sdk/version"
"github.com/spf13/cobra"
)

var (
gitCommit string
buildDate string
)

// Info holds build information
type Info struct {
GoVersion string `json:"go-version"`
OperatorSdkVersion string `json:"operator-sdk-version"`
GitCommit string `json:"commit"`
BuildDate string `json:"date"`
}

// Get creates and initialized Info object
func Get() Info {
return Info{
GitCommit: gitCommit,
BuildDate: buildDate,
GoVersion: runtime.Version(),
OperatorSdkVersion: sdkVersion.Version,
}
}

// NewVersionCommand creates the command that exposes the version
func NewVersionCommand() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Print the version",
Long: `Print the version and build information`,
RunE: func(cmd *cobra.Command, args []string) error {
info := Get()
json, err := json.Marshal(info)
if err != nil {
return err
}
fmt.Println(string(json))

return nil
},
}
}
18 changes: 0 additions & 18 deletions tmp/build/build.sh

This file was deleted.

5 changes: 0 additions & 5 deletions version/version.go

This file was deleted.

0 comments on commit 764e981

Please sign in to comment.