Show fission deployment version with cli (#538)

Add version information to binary through go ldflags
This commit is contained in:
Ta-Ching Chen
2018-03-19 14:14:55 +08:00
committed by GitHub
parent 048ab6149a
commit 1447e6949d
12 changed files with 194 additions and 18 deletions
+7
View File
@@ -33,6 +33,8 @@ import (
"time" "time"
"github.com/dchest/uniuri" "github.com/dchest/uniuri"
"github.com/fission/fission"
) )
const ( const (
@@ -68,6 +70,11 @@ func MakeBuilder(sharedVolumePath string) *Builder {
} }
} }
func (builder *Builder) VersionHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintf(w, fission.VersionInfo().String())
}
func (builder *Builder) Handler(w http.ResponseWriter, r *http.Request) { func (builder *Builder) Handler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" { if r.Method != "POST" {
e := fmt.Sprintf("Method not allowed: %v", r.Method) e := fmt.Sprintf("Method not allowed: %v", r.Method)
+16 -1
View File
@@ -1,3 +1,18 @@
#!/bin/bash #!/bin/bash
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH -o builder . version=$1
if [ -z $version ]; then
version=$(git rev-parse HEAD)
fi
date=$2
if [ -z $date ]; then
date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
fi
gitcommit=$3
if [ -z $gitcommit ]; then
gitcommit=$(git rev-parse HEAD)
fi
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH -ldflags "-X github.com/fission/fission.GitCommit=$gitcommit -X github.com/fission/fission.BuildDate=$date -X github.com/fission/fission.Version=$version" -o builder .
+1
View File
@@ -38,6 +38,7 @@ func main() {
builder := builder.MakeBuilder(dir) builder := builder.MakeBuilder(dir)
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/", builder.Handler) mux.HandleFunc("/", builder.Handler)
mux.HandleFunc("/version", builder.VersionHandler)
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
}) })
+1 -1
View File
@@ -137,7 +137,7 @@ func (api *API) getLogDBConfig(dbType string) logDBConfig {
func (api *API) HomeHandler(w http.ResponseWriter, r *http.Request) { func (api *API) HomeHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintf(w, "{\"message\": \"Fission API\", \"version\": \"0.6.0\"}\n") fmt.Fprintf(w, fission.VersionInfo().String())
} }
func (api *API) ApiVersionMismatchHandler(w http.ResponseWriter, r *http.Request) { func (api *API) ApiVersionMismatchHandler(w http.ResponseWriter, r *http.Request) {
+16 -1
View File
@@ -1,2 +1,17 @@
#!/bin/sh #!/bin/sh
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH -o fetcher . version=$1
if [ -z $version ]; then
version=$(git rev-parse HEAD)
fi
date=$2
if [ -z $date ]; then
date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
fi
gitcommit=$3
if [ -z $gitcommit ]; then
gitcommit=$(git rev-parse HEAD)
fi
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH -ldflags "-X github.com/fission/fission.GitCommit=$gitcommit -X github.com/fission/fission.BuildDate=$date -X github.com/fission/fission.Version=$version" -o fetcher .
+1
View File
@@ -71,6 +71,7 @@ func main() {
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/", fetcher.FetchHandler) mux.HandleFunc("/", fetcher.FetchHandler)
mux.HandleFunc("/upload", fetcher.UploadHandler) mux.HandleFunc("/upload", fetcher.UploadHandler)
mux.HandleFunc("/version", fetcher.VersionHandler)
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
}) })
+5
View File
@@ -163,6 +163,11 @@ func writeSecretOrConfigMap(dataMap map[string][]byte, dirPath string) error {
return nil return nil
} }
func (fetcher *Fetcher) VersionHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintf(w, fission.VersionInfo().String())
}
func (fetcher *Fetcher) FetchHandler(w http.ResponseWriter, r *http.Request) { func (fetcher *Fetcher) FetchHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" { if r.Method != "POST" {
http.Error(w, "only POST is supported on this endpoint", http.StatusMethodNotAllowed) http.Error(w, "only POST is supported on this endpoint", http.StatusMethodNotAllowed)
+16 -1
View File
@@ -1,2 +1,17 @@
#!/bin/sh #!/bin/sh
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH version=$1
if [ -z $version ]; then
version=$(git rev-parse HEAD)
fi
date=$2
if [ -z $date ]; then
date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
fi
gitcommit=$3
if [ -z $gitcommit ]; then
gitcommit=$(git rev-parse HEAD)
fi
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH -ldflags "-X github.com/fission/fission.GitCommit=$gitcommit -X github.com/fission/fission.BuildDate=$date -X github.com/fission/fission.Version=$version"
+6 -1
View File
@@ -1,12 +1,14 @@
package main package main
import ( import (
"fmt"
"log" "log"
"os" "os"
"strconv" "strconv"
"github.com/docopt/docopt-go" "github.com/docopt/docopt-go"
"github.com/fission/fission"
"github.com/fission/fission/buildermgr" "github.com/fission/fission/buildermgr"
"github.com/fission/fission/controller" "github.com/fission/fission/controller"
"github.com/fission/fission/executor" "github.com/fission/fission/executor"
@@ -120,6 +122,7 @@ Usage:
fission-bundle --builderMgr [--storageSvcUrl=<url>] [--envbuilder-namespace=<namespace>] fission-bundle --builderMgr [--storageSvcUrl=<url>] [--envbuilder-namespace=<namespace>]
fission-bundle --timer [--routerUrl=<url>] fission-bundle --timer [--routerUrl=<url>]
fission-bundle --mqt [--routerUrl=<url>] fission-bundle --mqt [--routerUrl=<url>]
fission-bundle --version
Options: Options:
--controllerPort=<port> Port that the controller should listen on. --controllerPort=<port> Port that the controller should listen on.
--routerPort=<port> Port that the router should listen on. --routerPort=<port> Port that the router should listen on.
@@ -135,8 +138,10 @@ Options:
--timer Start Timer. --timer Start Timer.
--mqt Start message queue trigger. --mqt Start message queue trigger.
--builderMgr Start builder manager. --builderMgr Start builder manager.
--version Print version information
` `
arguments, err := docopt.Parse(usage, nil, true, "fission-bundle", false) version := fmt.Sprintf("Fission Bundle Version: %v", fission.VersionInfo().String())
arguments, err := docopt.Parse(usage, nil, true, version, false)
if err != nil { if err != nil {
log.Fatalf("Error: %v", err) log.Fatalf("Error: %v", err)
} }
+31 -1
View File
@@ -17,9 +17,14 @@ limitations under the License.
package main package main
import ( import (
"fmt"
"io/ioutil"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings"
version "github.com/fission/fission"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@@ -41,11 +46,25 @@ func getKubeConfigPath() string {
return kubeConfig return kubeConfig
} }
func getFissionAPIVersion(apiUrl string) (string, error) {
resp, err := http.Get(apiUrl)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return strings.TrimRight(string(body), "\n"), nil
}
func main() { func main() {
app := cli.NewApp() app := cli.NewApp()
app.Name = "fission" app.Name = "fission"
app.Usage = "Serverless functions for Kubernetes" app.Usage = "Serverless functions for Kubernetes"
app.Version = "0.6.0" app.Version = version.Version
// fetch the FISSION_URL env variable. If not set, port-forward to controller. // fetch the FISSION_URL env variable. If not set, port-forward to controller.
var value string var value string
@@ -60,6 +79,17 @@ func main() {
value = fissionUrl value = fissionUrl
} }
cli.VersionPrinter = func(c *cli.Context) {
clientVer := version.VersionInfo().String()
fmt.Printf("Client Version: %v\n", clientVer)
serverVer, err := getFissionAPIVersion(value)
if err != nil {
fmt.Printf("Error getting Fission API version: %v", err)
} else {
fmt.Printf("Server Version: %v", serverVer)
}
}
app.Flags = []cli.Flag{ app.Flags = []cli.Flag{
cli.StringFlag{Name: "server", Value: value, Usage: "Fission server URL"}, cli.StringFlag{Name: "server", Value: value, Usage: "Fission server URL"},
} }
+46 -12
View File
@@ -28,15 +28,22 @@ check_clean() {
# Build CLI binaries for mac/linux/windows # Build CLI binaries for mac/linux/windows
build_all_cli() { build_all_cli() {
build_cli "linux" "linux" local version=$1
build_cli "darwin" "osx" local date=$2
build_cli "windows" "windows" local gitcommit=$3
build_cli "linux" "linux" $version $date $gitcommit
build_cli "darwin" "osx" $version $date $gitcommit
build_cli "windows" "windows" $version $date $gitcommit
} }
# Build cli binary for one OS, and put it in $BUILDDIR/cli/<os>/ # Build cli binary for one OS, and put it in $BUILDDIR/cli/<os>/
build_cli() { build_cli() {
os=$1 os=$1
osName=$2 osName=$2
local version=$3
local date=$4
local gitcommit=$5
arch="amd64" # parameterize if/when we need to arch="amd64" # parameterize if/when we need to
pushd $DIR/fission pushd $DIR/fission
@@ -48,7 +55,7 @@ build_cli() {
binary=fission-cli-${osName} binary=fission-cli-${osName}
fi fi
GOOS=$os GOARCH=$arch go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH -o $binary . GOOS=$os GOARCH=$arch go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH -ldflags "-X github.com/fission/fission.GitCommit=$gitcommit -X github.com/fission/fission.BuildDate=$date -X github.com/fission/fission.Version=$version" -o $binary .
outdir=$BUILDDIR/cli/$osName/ outdir=$BUILDDIR/cli/$osName/
mkdir -p $outdir mkdir -p $outdir
@@ -60,11 +67,14 @@ build_cli() {
# Build fission-bundle image # Build fission-bundle image
build_fission_bundle_image() { build_fission_bundle_image() {
local version=$1 local version=$1
local date=$2
local gitcommit=$3
local tag=fission/fission-bundle:$version local tag=fission/fission-bundle:$version
pushd $DIR/fission-bundle pushd $DIR/fission-bundle
./build.sh ./build.sh $version $date $gitcommit
docker build -t $tag . docker build -t $tag .
docker tag $tag fission/fission-bundle:latest docker tag $tag fission/fission-bundle:latest
@@ -80,11 +90,13 @@ push_fission_bundle_image() {
build_fetcher_image() { build_fetcher_image() {
local version=$1 local version=$1
local date=$2
local gitcommit=$3
local tag=fission/fetcher:$version local tag=fission/fetcher:$version
pushd $DIR/environments/fetcher/cmd pushd $DIR/environments/fetcher/cmd
./build.sh ./build.sh $version $date $gitcommit
docker build -t $tag . docker build -t $tag .
docker tag $tag fission/fetcher:latest docker tag $tag fission/fetcher:latest
@@ -99,11 +111,13 @@ push_fetcher_image() {
build_builder_image() { build_builder_image() {
local version=$1 local version=$1
local date=$2
local gitcommit=$3
local tag=fission/builder:$version local tag=fission/builder:$version
pushd $DIR/builder/cmd pushd $DIR/builder/cmd
./build.sh ./build.sh $version $date $gitcommit
docker build -t $tag . docker build -t $tag .
docker tag $tag fission/builder:latest docker tag $tag fission/builder:latest
@@ -238,11 +252,28 @@ build_charts() {
build_all() { build_all() {
local version=$1 local version=$1
if [ -z "$version" ] if [ -z "$version" ]
then then
echo "Version unspecified" echo "Version unspecified"
exit 1 exit 1
fi fi
local date=$2
if [ -z "$date" ]
then
echo "Build date unspecified"
exit 1
fi
local gitcommit=$3
if [ -z "gitcommit" ]
then
echo "Git commit unspecified"
exit 1
fi
if [ -e $BUILDDIR ] if [ -e $BUILDDIR ]
then then
@@ -252,11 +283,11 @@ build_all() {
mkdir -p $BUILDDIR mkdir -p $BUILDDIR
build_fission_bundle_image $version build_fission_bundle_image $version $date $gitcommit
build_fetcher_image $version build_fetcher_image $version $date $gitcommit
build_builder_image $version build_builder_image $version $date $gitcommit
build_logger_image $version build_logger_image $version
build_all_cli build_all_cli $version $date $gitcommit
build_charts $version build_charts $version
} }
@@ -394,10 +425,13 @@ export GITHUB_TOKEN=$(cat ~/.gh-access-token)
version=$1 version=$1
date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
gitcommit=$(git rev-parse HEAD)
check_branch $version check_branch $version
check_clean check_clean
build_all $version build_all $version $date $gitcommit
push_all $version push_all $version
build_and_push_all_envs $version build_and_push_all_envs $version
build_and_push_all_env_builders $version build_and_push_all_env_builders $version
+48
View File
@@ -0,0 +1,48 @@
/*
Copyright 2017 The Fission Authors.
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 fission
import (
"encoding/json"
)
var (
GitCommit string // $(git rev-parse HEAD) (1b4716ab84903b2e477135a3dc5afdb07f685cb7)
BuildDate string // $(date -u +'%Y-%m-%dT%H:%M:%SZ') (2018-03-08T18:54:38Z)
Version string // fission release version (0.6.0)
)
type (
Info struct {
GitCommit string `json:"GitCommit,omitempty"`
BuildDate string `json:"BuildDate,omitempty"`
Version string `json:"Version,omitempty"`
}
)
func VersionInfo() Info {
return Info{
GitCommit: GitCommit,
BuildDate: BuildDate,
Version: Version,
}
}
func (info Info) String() string {
v, _ := json.Marshal(info)
return string(v)
}