Skip to content

Commit

Permalink
Adding version.Version, GitCommit, BuildDate; Put Version in App Gwy …
Browse files Browse the repository at this point in the history
…tag (#202)
  • Loading branch information
draychev authored and akshaysngupta committed May 23, 2019
1 parent 75335bc commit 64723c3
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 20 deletions.
7 changes: 7 additions & 0 deletions cmd/appgw-ingress/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/appgw"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/controller"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/k8scontext"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/version"
)

var (
Expand All @@ -40,6 +41,8 @@ var (

resyncPeriod = flags.Duration("sync-period", 30*time.Second,
"Interval at which to re-list and confirm cloud resources.")

versionInfo = flags.Bool("version", false, "Print version")
)

func main() {
Expand All @@ -49,6 +52,10 @@ func main() {
glog.Fatal("Error parsing command line arguments:", err)
}

if *versionInfo {
version.PrintVersionAndExit()
}

// Workaround for "ERROR: logging before flag.Parse"
// See: https://github.com/kubernetes/kubernetes/issues/17162#issuecomment-225596212
_ = flag.CommandLine.Parse([]string{})
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/consts.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller

// An App Gateway tag: Resources tagged with this are exclusively managed by a Kubernetes Ingress.
const isManagedByK8sIngress = "managed-by-k8s-ingress"
const managedByK8sIngress = "managed-by-k8s-ingress"
4 changes: 3 additions & 1 deletion pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ package controller
import (
"context"
"errors"
"fmt"
"time"

"github.com/Azure/application-gateway-kubernetes-ingress/pkg/appgw"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/k8scontext"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/version"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-12-01/network"
"github.com/Azure/go-autorest/autorest/to"
"github.com/eapache/channels"
Expand Down Expand Up @@ -135,7 +137,7 @@ func addTags(appGw *network.ApplicationGateway) {
appGw.Tags = make(map[string]*string)
}
// Identify the App Gateway as being exclusively managed by a Kubernetes Ingress.
appGw.Tags[isManagedByK8sIngress] = to.StringPtr("true")
appGw.Tags[managedByK8sIngress] = to.StringPtr(fmt.Sprintf("%s/%s/%s", version.Version, version.GitCommit, version.BuildDate))
}

// Start function runs the k8scontext and continues to listen to the
Expand Down
12 changes: 9 additions & 3 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
package controller

import (
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-12-01/network"
"github.com/Azure/go-autorest/autorest/to"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/Azure/application-gateway-kubernetes-ingress/pkg/version"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-12-01/network"
"github.com/Azure/go-autorest/autorest/to"
)

func TestController(t *testing.T) {
Expand All @@ -22,10 +24,14 @@ func TestController(t *testing.T) {
var _ = Describe("configure App Gateway", func() {
Context("ensure app gwy is tagged", func() {
agw := &network.ApplicationGateway{}
version.Version = "a"
version.GitCommit = "b"
version.BuildDate = "c"

addTags(agw)
It("should have 1 tag", func() {
expected := map[string]*string{
isManagedByK8sIngress: to.StringPtr("true"),
managedByK8sIngress: to.StringPtr("a/b/c"),
}
Expect(agw.Tags).To(Equal(expected))
Expect(len(agw.Tags)).To(Equal(1))
Expand Down
21 changes: 21 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package version

import (
"fmt"
"os"
)

// BuildDate is the date when the binary was built
var BuildDate string

// GitCommit is the commit hash when the binary was built
var GitCommit string

// Version is the version of the compiled software
var Version string

// PrintVersionAndExit prints the version and exits
func PrintVersionAndExit() {
fmt.Printf("Version: %s; Commit: %s; Date: %s\n", Version, GitCommit, BuildDate)
os.Exit(0)
}
52 changes: 37 additions & 15 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -1,44 +1,66 @@
#!/bin/bash

# colors
COLOR_RESET='\e[0m'
COLOR_BLUE='\e[44;97m'
COLOR_RED='\e[101;97m'
COLOR_GREEN='\e[42;97m'


export GOOS=linux
export GOBIN=`pwd`/bin

GO_PROJ="github.com/Azure/application-gateway-kubernetes-ingress"
GO_PKGS=`go list ./... | grep -v vendor/`
GO_FILES=`find . -type f -name '*.go' -not -path "./vendor/*"`

echo -e "\e[44;97m Running go lint.. \e[0m"
ORG_PATH="github.com/Azure"
PROJECT_NAME="application-gateway-kubernetes-ingress"
REPO_PATH="${ORG_PATH}/${PROJECT_NAME}"

VERSION_VAR="${REPO_PATH}/pkg/version.Version"
VERSION=$(git describe --abbrev=0 --tags)

DATE_VAR="${REPO_PATH}/pkg/version.BuildDate"
BUILD_DATE=$(date +%Y-%m-%d-%H:%MT%z)

COMMIT_VAR="${REPO_PATH}/pkg/version.GitCommit"
GIT_HASH=$(git rev-parse --short HEAD)


echo -e "$COLOR_BLUE Running go lint.. $COLOR_RESET"
golint $GO_PKGS > /tmp/lint.out
cat /tmp/lint.out
cat /tmp/lint.out
if [ -s /tmp/lint.out ]; then
echo -e "\e[101;97m golint FAILED \e[0m"``
echo -e "$COLOR_RED golint FAILED $COLOR_RESET"
exit 1
else
echo -e "\e[42;97m golint SUCCEEDED \e[0m"
echo -e "$COLOR_GREEN golint SUCCEEDED $COLOR_RESET"
fi

echo -e "\e[44;97m Running govet ... \e[0m"
echo -e "$COLOR_BLUE Running govet ... $COLOR_RESET"
if go vet -v $GO_PKGS; then
echo -e "\e[42;97m govet SUCCEEDED \e[0m"
echo -e "$COLOR_GREEN govet SUCCEEDED $COLOR_RESET"
else
echo -e "\e[101;97m govet FAILED \e[0m"``
echo -e "$COLOR_RED govet FAILED $COLOR_RESET"
exit 1
fi

echo -e "\e[44;97m Running goimports ... \e[0m"
echo -e "$COLOR_BLUE Running goimports ... $COLOR_RESET"
goimports -local $GO_PROJ -w $GO_FILES > /tmp/goimports.out
cat /tmp/goimports.out
if [ -s /tmp/goimports.out ]; then
echo -e "\e[101;97m goimports FAILED \e[0m"``
echo -e "$COLOR_RED goimports FAILED $COLOR_RESET"
exit 1
else
echo -e "\e[42;97m goimports SUCCEEDED \e[0m"
echo -e "$COLOR_GREEN goimports SUCCEEDED $COLOR_RESET"
fi

echo -e "\e[44;97m Compiling ... \e[0m"
if go install -v ./cmd/appgw-ingress; then
echo -e "$COLOR_BLUE Compiling ... $COLOR_RESET"
if go install -ldflags "-s -X ${VERSION_VAR}=${VERSION} -X ${DATE_VAR}=${BUILD_DATE} -X ${COMMIT_VAR}=${GIT_HASH}" -v ./cmd/appgw-ingress; then
chmod -R 777 bin
echo -e "\e[42;97m Build SUCCEEDED \e[0m"
echo -e "$COLOR_GREEN Build SUCCEEDED $COLOR_RESET"
else
echo -e "\e[101;97m Build FAILED \e[0m"
echo -e "$COLOR_RED Build FAILED $COLOR_RESET"
exit 1
fi
fi

0 comments on commit 64723c3

Please sign in to comment.