Skip to content

Commit 82ef8f3

Browse files
authored
[Feature] Version Endpoint (#752)
1 parent aee63c6 commit 82ef8f3

File tree

10 files changed

+181
-10
lines changed

10 files changed

+181
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Change Log
22

33
## [master](https://github.com/arangodb/kube-arangodb/tree/master) (N/A)
4+
- Add Operator `/api/v1/version` endpoint
45

56
## [1.1.10](https://github.com/arangodb/kube-arangodb/tree/1.1.10) (2021-07-06)
67
- Switch K8S CRD API to V1

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ GOVERSION := 1.10.0-alpine
3030
PULSAR := $(GOBUILDDIR)/bin/pulsar$(shell go env GOEXE)
3131
GOASSETSBUILDER := $(GOBUILDDIR)/bin/go-assets-builder$(shell go env GOEXE)
3232

33+
BUILDTIME = $(shell go run "$(ROOT)/tools/dategen/")
34+
3335
DOCKERFILE := Dockerfile
3436

3537
HELM ?= $(shell which helm)
@@ -236,7 +238,7 @@ bin: $(BIN)
236238

237239
$(BIN): $(SOURCES) dashboard/assets.go VERSION
238240
@mkdir -p $(BINDIR)
239-
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -installsuffix netgo -ldflags "-X main.projectVersion=$(VERSION) -X main.projectBuild=$(COMMIT)" -o $(BIN) $(REPOPATH)
241+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -installsuffix netgo -ldflags "-X $(REPOPATH)/pkg/version.version=$(VERSION) -X $(REPOPATH)/pkg/version.buildDate=$(BUILDTIME) -X $(REPOPATH)/pkg/version.build=$(COMMIT)" -o $(BIN) $(REPOPATH)
240242

241243
.PHONY: docker
242244
docker: check-vars $(BIN)

lifecycle.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929
"path/filepath"
3030
"time"
3131

32+
"github.com/arangodb/kube-arangodb/pkg/version"
33+
3234
"github.com/spf13/cobra"
3335
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3436

@@ -70,7 +72,8 @@ func init() {
7072

7173
// Wait until all finalizers of the current pod have been removed.
7274
func cmdLifecyclePreStopRun(cmd *cobra.Command, args []string) {
73-
cliLog.Info().Msgf("Starting arangodb-operator, lifecycle preStop, version %s build %s", projectVersion, projectBuild)
75+
76+
cliLog.Info().Msgf("Starting arangodb-operator (%s), lifecycle preStop, version %s build %s", version.GetVersionV1().Edition.Title(), version.GetVersionV1().Version, version.GetVersionV1().Build)
7477

7578
// Get environment
7679
namespace := os.Getenv(constants.EnvOperatorPodNamespace)
@@ -119,7 +122,7 @@ func cmdLifecyclePreStopRun(cmd *cobra.Command, args []string) {
119122

120123
// Copy the executable to a given place.
121124
func cmdLifecycleCopyRun(cmd *cobra.Command, args []string) {
122-
cliLog.Info().Msgf("Starting arangodb-operator, lifecycle copy, version %s build %s", projectVersion, projectBuild)
125+
cliLog.Info().Msgf("Starting arangodb-operator (%s), lifecycle copy, version %s build %s", version.GetVersionV1().Edition.Title(), version.GetVersionV1().Version, version.GetVersionV1().Build)
123126

124127
exePath, err := os.Executable()
125128
if err != nil {

main.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import (
3333
"strings"
3434
"time"
3535

36+
"github.com/arangodb/kube-arangodb/pkg/version"
37+
3638
"github.com/arangodb/kube-arangodb/pkg/util/arangod"
3739

3840
"github.com/arangodb/kube-arangodb/pkg/operator/scope"
@@ -86,9 +88,6 @@ const (
8688
)
8789

8890
var (
89-
projectVersion = "dev"
90-
projectBuild = "dev"
91-
9291
maskAny = errors.WithStack
9392

9493
cmdMain = cobra.Command{
@@ -206,7 +205,7 @@ func cmdMainRun(cmd *cobra.Command, args []string) {
206205
cliLog.Info().
207206
Str("pod-name", name).
208207
Str("pod-namespace", namespace).
209-
Msgf("Starting arangodb-operator, version %s build %s", projectVersion, projectBuild)
208+
Msgf("Starting arangodb-operator (%s), version %s build %s", version.GetVersionV1().Edition.Title(), version.GetVersionV1().Version, version.GetVersionV1().Build)
210209

211210
// Check environment
212211
if len(namespace) == 0 {

pkg/server/server.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ import (
2929
"strings"
3030
"time"
3131

32+
operatorHTTP "github.com/arangodb/kube-arangodb/pkg/util/http"
33+
"github.com/arangodb/kube-arangodb/pkg/version"
34+
3235
"github.com/arangodb/kube-arangodb/pkg/util/errors"
3336

3437
"github.com/arangodb-helper/go-certificates"
@@ -157,6 +160,13 @@ func NewServer(cli corev1.CoreV1Interface, cfg Config, deps Dependencies) (*Serv
157160
r.Use(gin.Recovery())
158161
r.GET("/health", gin.WrapF(deps.LivenessProbe.LivenessHandler))
159162

163+
versionV1Responser, err := operatorHTTP.NewSimpleJSONResponse(version.GetVersionV1())
164+
if err != nil {
165+
return nil, errors.WithStack(err)
166+
}
167+
r.GET("/_api/version", gin.WrapF(versionV1Responser.ServeHTTP))
168+
r.GET("/api/v1/version", gin.WrapF(versionV1Responser.ServeHTTP))
169+
160170
var readyProbes []*probe.ReadyProbe
161171
if deps.Deployment.Enabled {
162172
r.GET("/ready/deployment", gin.WrapF(deps.Deployment.Probe.ReadyHandler))

pkg/util/http/response.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Adam Janikowski
21+
//
22+
23+
package http
24+
25+
import (
26+
"encoding/json"
27+
"net/http"
28+
"strings"
29+
)
30+
31+
// NewSimpleJSONResponse returns handler which server static json on GET request
32+
func NewSimpleJSONResponse(obj interface{}) (http.Handler, error) {
33+
data, err := json.Marshal(obj)
34+
if err != nil {
35+
return nil, err
36+
}
37+
38+
return simpleJSONResponse{data: data}, nil
39+
}
40+
41+
type simpleJSONResponse struct {
42+
data []byte
43+
}
44+
45+
func (s simpleJSONResponse) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
46+
if strings.ToUpper(request.Method) != http.MethodGet {
47+
writer.WriteHeader(http.StatusMethodNotAllowed)
48+
return
49+
}
50+
51+
writer.WriteHeader(http.StatusOK)
52+
writer.Write(s.data)
53+
}

pkg/version/version.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2021 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Adam Janikowski
21+
//
22+
23+
package version
24+
25+
import (
26+
"runtime"
27+
"strings"
28+
29+
"github.com/arangodb/go-driver"
30+
)
31+
32+
type License string
33+
34+
const (
35+
CommunityEdition License = "community"
36+
)
37+
38+
func (s License) Title() string {
39+
return strings.Title(string(s))
40+
}
41+
42+
var (
43+
version = "dev"
44+
build = "dev"
45+
buildDate = ""
46+
edition = CommunityEdition
47+
goVersion = runtime.Version()
48+
)
49+
50+
type InfoV1 struct {
51+
Version driver.Version `json:"version"`
52+
Build string `json:"build"`
53+
Edition License `json:"edition"`
54+
GoVersion string `json:"go_version"`
55+
BuildDate string `json:"build_date,omitempty"`
56+
}
57+
58+
func GetVersionV1() InfoV1 {
59+
return InfoV1{
60+
Version: driver.Version(version),
61+
Build: build,
62+
Edition: edition,
63+
GoVersion: goVersion,
64+
BuildDate: buildDate,
65+
}
66+
}

storage.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import (
2828
"os"
2929
"strconv"
3030

31+
"github.com/arangodb/kube-arangodb/pkg/version"
32+
3133
"github.com/spf13/cobra"
3234

3335
"github.com/arangodb/kube-arangodb/pkg/logging"
@@ -70,7 +72,8 @@ func cmdStorageProvisionerRun(cmd *cobra.Command, args []string) {
7072
}
7173

7274
// Log version
73-
cliLog.Info().Msgf("Starting arangodb local storage provisioner, version %s build %s", projectVersion, projectBuild)
75+
76+
cliLog.Info().Msgf("Starting arangodb local storage provisioner (%s), version %s build %s", version.GetVersionV1().Edition.Title(), version.GetVersionV1().Version, version.GetVersionV1().Build)
7477

7578
// Get environment
7679
nodeName := os.Getenv(constants.EnvOperatorNodeName)

tools/dategen/main.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2021 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Adam Janikowski
21+
//
22+
23+
package main
24+
25+
import (
26+
"io"
27+
"os"
28+
"time"
29+
)
30+
31+
func main() {
32+
io.WriteString(os.Stdout, time.Now().UTC().Format(time.RFC3339))
33+
}

version.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ package main
2424

2525
import (
2626
"fmt"
27-
"runtime"
2827

28+
"github.com/arangodb/kube-arangodb/pkg/version"
2929
"github.com/spf13/cobra"
3030
)
3131

@@ -39,5 +39,6 @@ var cmdVersion = &cobra.Command{
3939
}
4040

4141
func versionRun(cmd *cobra.Command, args []string) {
42-
println(fmt.Sprintf("Version: %s, Build: %s, Go: %s", projectVersion, projectBuild, runtime.Version()))
42+
v := version.GetVersionV1()
43+
println(fmt.Sprintf("Version: %s %s, Build: %s, Go: %s, Build Date: %s", v.Edition.Title(), v.Version, v.Build, v.GoVersion, v.BuildDate))
4344
}

0 commit comments

Comments
 (0)