From 764e981334120c402b2c2e9d61c425ed62c0432a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraci=20Paix=C3=A3o=20Kr=C3=B6hling?= Date: Mon, 3 Sep 2018 15:15:34 +0200 Subject: [PATCH] Update the Jaeger Operator version at build time MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Juraci Paixão Kröhling --- .gitignore | 2 +- tmp/build/Dockerfile => Dockerfile | 0 Gopkg.toml | 8 ++++ Makefile | 31 ++++++++++---- cmd/jaeger-operator/main.go | 38 ----------------- cmd/root.go | 66 ++++++++++++++++++++++++++++++ deploy/operator.yaml | 3 +- main.go | 7 ++++ pkg/cmd/start/main.go | 57 ++++++++++++++++++++++++++ pkg/cmd/version/main.go | 52 +++++++++++++++++++++++ tmp/build/build.sh | 18 -------- version/version.go | 5 --- 12 files changed, 215 insertions(+), 72 deletions(-) rename tmp/build/Dockerfile => Dockerfile (100%) delete mode 100644 cmd/jaeger-operator/main.go create mode 100644 cmd/root.go create mode 100644 main.go create mode 100644 pkg/cmd/start/main.go create mode 100644 pkg/cmd/version/main.go delete mode 100755 tmp/build/build.sh delete mode 100644 version/version.go diff --git a/.gitignore b/.gitignore index 31da42dab..e360e80fb 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,7 @@ tmp/_output tmp/_test deploy/test - +_output # Created by https://www.gitignore.io/api/go,vim,emacs,visualstudiocode diff --git a/tmp/build/Dockerfile b/Dockerfile similarity index 100% rename from tmp/build/Dockerfile rename to Dockerfile diff --git a/Gopkg.toml b/Gopkg.toml index 5017ed34b..6162127dc 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -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" diff --git a/Makefile b/Makefile index f172877a6..483024b04 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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 @@ -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 diff --git a/cmd/jaeger-operator/main.go b/cmd/jaeger-operator/main.go deleted file mode 100644 index 41372e5c0..000000000 --- a/cmd/jaeger-operator/main.go +++ /dev/null @@ -1,38 +0,0 @@ -package main - -import ( - "context" - "runtime" - - sdk "github.com/operator-framework/operator-sdk/pkg/sdk" - k8sutil "github.com/operator-framework/operator-sdk/pkg/util/k8sutil" - sdkVersion "github.com/operator-framework/operator-sdk/version" - "github.com/sirupsen/logrus" - _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" - - stub "github.com/jaegertracing/jaeger-operator/pkg/stub" -) - -func printVersion() { - logrus.Infof("Go Version: %s", runtime.Version()) - logrus.Infof("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH) - logrus.Infof("operator-sdk Version: %v", sdkVersion.Version) -} - -func main() { - printVersion() - - 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()) - sdk.Run(context.TODO()) -} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 000000000..6a1180972 --- /dev/null +++ b/cmd/root.go @@ -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()) + } +} diff --git a/deploy/operator.yaml b/deploy/operator.yaml index 4e2664656..e6e80df37 100644 --- a/deploy/operator.yaml +++ b/deploy/operator.yaml @@ -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 diff --git a/main.go b/main.go new file mode 100644 index 000000000..30c39ee25 --- /dev/null +++ b/main.go @@ -0,0 +1,7 @@ +package main + +import "github.com/jaegertracing/jaeger-operator/cmd" + +func main() { + cmd.Execute() +} diff --git a/pkg/cmd/start/main.go b/pkg/cmd/start/main.go new file mode 100644 index 000000000..143a5bcae --- /dev/null +++ b/pkg/cmd/start/main.go @@ -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") + } +} diff --git a/pkg/cmd/version/main.go b/pkg/cmd/version/main.go new file mode 100644 index 000000000..3b45dbd78 --- /dev/null +++ b/pkg/cmd/version/main.go @@ -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 + }, + } +} diff --git a/tmp/build/build.sh b/tmp/build/build.sh deleted file mode 100755 index b06ecd0d2..000000000 --- a/tmp/build/build.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env bash - -set -o errexit -set -o nounset -set -o pipefail - -if ! which go > /dev/null; then - echo "golang needs to be installed" - exit 1 -fi - -BIN_DIR="$(pwd)/tmp/_output/bin" -mkdir -p ${BIN_DIR} -PROJECT_NAME="jaeger-operator" -REPO_PATH="github.com/jaegertracing/jaeger-operator" -BUILD_PATH="${REPO_PATH}/cmd/${PROJECT_NAME}" -echo "building "${PROJECT_NAME}"..." -GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o ${BIN_DIR}/${PROJECT_NAME} $BUILD_PATH diff --git a/version/version.go b/version/version.go deleted file mode 100644 index e3e130bf9..000000000 --- a/version/version.go +++ /dev/null @@ -1,5 +0,0 @@ -package version - -var ( - Version = "0.0.1" -)