forked from istio/istio
/
version.go
137 lines (124 loc) · 3.48 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2018 Istio 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 version provides build version information.
package version
import (
"fmt"
"runtime"
"strings"
)
// The following fields are populated at build time using -ldflags -X.
// Note that DATE is omitted for reproducible builds
var (
buildVersion = "unknown"
buildGitRevision = "unknown"
buildUser = "unknown"
buildHost = "unknown"
buildDockerHub = "unknown"
buildStatus = "unknown"
buildTag = "unknown"
)
// BuildInfo describes version information about the binary build.
type BuildInfo struct {
Version string `json:"version"`
GitRevision string `json:"revision"`
User string `json:"user"`
Host string `json:"host"`
GolangVersion string `json:"golang_version"`
DockerHub string `json:"hub"`
BuildStatus string `json:"status"`
GitTag string `json:"tag"`
}
// ServerInfo contains the version for a single control plane component
type ServerInfo struct {
Component string
Info BuildInfo
}
// MeshInfo contains the versions for all Istio control plane components
type MeshInfo []ServerInfo
// NewBuildInfoFromOldString creates a BuildInfo struct based on the output
// of previous Istio components '-- version' output
func NewBuildInfoFromOldString(oldOutput string) (BuildInfo, error) {
res := BuildInfo{}
lines := strings.Split(oldOutput, "\n")
for _, line := range lines {
if strings.TrimSpace(line) == "" {
continue
}
fields := strings.SplitN(line, ":", 2)
if fields != nil {
if len(fields) != 2 {
return BuildInfo{}, fmt.Errorf("invalid BuildInfo input, field '%s' is not valid", fields[0])
}
value := strings.TrimSpace(fields[1])
switch fields[0] {
case "Version":
res.Version = value
case "GitRevision":
res.GitRevision = value
case "User":
res.User = value
case "Hub":
res.DockerHub = value
case "GolangVersion":
res.GolangVersion = value
case "BuildStatus":
res.BuildStatus = value
case "GitTag":
res.GitTag = value
default:
return BuildInfo{}, fmt.Errorf("invalid BuildInfo input, field '%s' is not valid", fields[0])
}
}
}
return res, nil
}
var (
// Info exports the build version information.
Info BuildInfo
)
// String produces a single-line version info
//
// This looks like:
//
// ```
// user@host-<docker hub>-<version>-<git revision>-<build status>
// ```
func (b BuildInfo) String() string {
return fmt.Sprintf("%v@%v-%v-%v-%v-%v",
b.User,
b.Host,
b.DockerHub,
b.Version,
b.GitRevision,
b.BuildStatus)
}
// LongForm returns a dump of the Info struct
// This looks like:
//
func (b BuildInfo) LongForm() string {
return fmt.Sprintf("%#v", b)
}
func init() {
Info = BuildInfo{
Version: buildVersion,
GitRevision: buildGitRevision,
User: buildUser,
Host: buildHost,
GolangVersion: runtime.Version(),
DockerHub: buildDockerHub,
BuildStatus: buildStatus,
GitTag: buildTag,
}
}