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"
"github.com/dchest/uniuri"
"github.com/fission/fission"
)
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) {
if r.Method != "POST" {
e := fmt.Sprintf("Method not allowed: %v", r.Method)
+16 -1
View File
@@ -1,3 +1,18 @@
#!/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)
mux := http.NewServeMux()
mux.HandleFunc("/", builder.Handler)
mux.HandleFunc("/version", builder.VersionHandler)
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
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) {
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) {
+16 -1
View File
@@ -1,2 +1,17 @@
#!/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.HandleFunc("/", fetcher.FetchHandler)
mux.HandleFunc("/upload", fetcher.UploadHandler)
mux.HandleFunc("/version", fetcher.VersionHandler)
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
+5
View File
@@ -163,6 +163,11 @@ func writeSecretOrConfigMap(dataMap map[string][]byte, dirPath string) error {
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) {
if r.Method != "POST" {
http.Error(w, "only POST is supported on this endpoint", http.StatusMethodNotAllowed)
+16 -1
View File
@@ -1,2 +1,17 @@
#!/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
import (
"fmt"
"log"
"os"
"strconv"
"github.com/docopt/docopt-go"
"github.com/fission/fission"
"github.com/fission/fission/buildermgr"
"github.com/fission/fission/controller"
"github.com/fission/fission/executor"
@@ -120,6 +122,7 @@ Usage:
fission-bundle --builderMgr [--storageSvcUrl=<url>] [--envbuilder-namespace=<namespace>]
fission-bundle --timer [--routerUrl=<url>]
fission-bundle --mqt [--routerUrl=<url>]
fission-bundle --version
Options:
--controllerPort=<port> Port that the controller should listen on.
--routerPort=<port> Port that the router should listen on.
@@ -135,8 +138,10 @@ Options:
--timer Start Timer.
--mqt Start message queue trigger.
--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 {
log.Fatalf("Error: %v", err)
}
+31 -1
View File
@@ -17,9 +17,14 @@ limitations under the License.
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
version "github.com/fission/fission"
"github.com/urfave/cli"
)
@@ -41,11 +46,25 @@ func getKubeConfigPath() string {
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() {
app := cli.NewApp()
app.Name = "fission"
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.
var value string
@@ -60,6 +79,17 @@ func main() {
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{
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_all_cli() {
build_cli "linux" "linux"
build_cli "darwin" "osx"
build_cli "windows" "windows"
local version=$1
local date=$2
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() {
os=$1
osName=$2
local version=$3
local date=$4
local gitcommit=$5
arch="amd64" # parameterize if/when we need to
pushd $DIR/fission
@@ -48,7 +55,7 @@ build_cli() {
binary=fission-cli-${osName}
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/
mkdir -p $outdir
@@ -60,11 +67,14 @@ build_cli() {
# Build fission-bundle image
build_fission_bundle_image() {
local version=$1
local date=$2
local gitcommit=$3
local tag=fission/fission-bundle:$version
pushd $DIR/fission-bundle
./build.sh
./build.sh $version $date $gitcommit
docker build -t $tag .
docker tag $tag fission/fission-bundle:latest
@@ -80,11 +90,13 @@ push_fission_bundle_image() {
build_fetcher_image() {
local version=$1
local date=$2
local gitcommit=$3
local tag=fission/fetcher:$version
pushd $DIR/environments/fetcher/cmd
./build.sh
./build.sh $version $date $gitcommit
docker build -t $tag .
docker tag $tag fission/fetcher:latest
@@ -99,11 +111,13 @@ push_fetcher_image() {
build_builder_image() {
local version=$1
local date=$2
local gitcommit=$3
local tag=fission/builder:$version
pushd $DIR/builder/cmd
./build.sh
./build.sh $version $date $gitcommit
docker build -t $tag .
docker tag $tag fission/builder:latest
@@ -238,11 +252,28 @@ build_charts() {
build_all() {
local version=$1
if [ -z "$version" ]
then
echo "Version unspecified"
exit 1
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 ]
then
@@ -252,11 +283,11 @@ build_all() {
mkdir -p $BUILDDIR
build_fission_bundle_image $version
build_fetcher_image $version
build_builder_image $version
build_fission_bundle_image $version $date $gitcommit
build_fetcher_image $version $date $gitcommit
build_builder_image $version $date $gitcommit
build_logger_image $version
build_all_cli
build_all_cli $version $date $gitcommit
build_charts $version
}
@@ -394,10 +425,13 @@ export GITHUB_TOKEN=$(cat ~/.gh-access-token)
version=$1
date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
gitcommit=$(git rev-parse HEAD)
check_branch $version
check_clean
build_all $version
build_all $version $date $gitcommit
push_all $version
build_and_push_all_envs $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)
}