Skip to content

Commit 18c383a

Browse files
Merge pull request #919 from YaoZengzeng/version
implement `kmeshctl version`
2 parents 88dceea + ef71fe9 commit 18c383a

File tree

6 files changed

+109
-54
lines changed

6 files changed

+109
-54
lines changed

ctl/dump/dump.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func RunDump(cmd *cobra.Command, args []string) error {
7878

7979
body, err := io.ReadAll(resp.Body)
8080
if err != nil {
81-
fmt.Printf("failed to read HTTP response body: %v\n", err)
81+
log.Errorf("failed to read HTTP response body: %v", err)
8282
os.Exit(1)
8383
}
8484

ctl/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"kmesh.net/kmesh/ctl/dump"
2525
logcmd "kmesh.net/kmesh/ctl/log"
26+
"kmesh.net/kmesh/ctl/version"
2627
"kmesh.net/kmesh/ctl/waypoint"
2728
)
2829

@@ -39,6 +40,7 @@ func main() {
3940
rootCmd.AddCommand(logcmd.NewCmd())
4041
rootCmd.AddCommand(dump.NewCmd())
4142
rootCmd.AddCommand(waypoint.NewCmd())
43+
rootCmd.AddCommand(version.NewCmd())
4244

4345
if err := rootCmd.Execute(); err != nil {
4446
os.Exit(1)

ctl/version/version.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright The Kmesh Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package version
18+
19+
import (
20+
"fmt"
21+
"io"
22+
"net/http"
23+
"os"
24+
25+
"github.com/spf13/cobra"
26+
27+
"kmesh.net/kmesh/ctl/utils"
28+
"kmesh.net/kmesh/pkg/logger"
29+
"kmesh.net/kmesh/pkg/version"
30+
)
31+
32+
var log = logger.NewLoggerScope("kmeshctl/version")
33+
34+
func NewCmd() *cobra.Command {
35+
cmd := &cobra.Command{
36+
Use: "version",
37+
Short: "Prints out build version info",
38+
Example: `# Show version of kmeshctl
39+
kmeshctl version
40+
41+
# Show version info of a specific Kmesh daemon
42+
kmeshctl version <kmesh-daemon-pod>`,
43+
Run: func(cmd *cobra.Command, args []string) {
44+
_ = RunVersion(cmd, args)
45+
},
46+
}
47+
return cmd
48+
}
49+
50+
// RunVersion provides the version info of kmeshctl or specific Kmesh daemon.
51+
func RunVersion(cmd *cobra.Command, args []string) error {
52+
if len(args) == 0 {
53+
v := version.Get()
54+
cmd.Printf("%s\n", v.GitVersion)
55+
56+
return nil
57+
}
58+
59+
podName := args[0]
60+
61+
fw, err := utils.CreateKmeshPortForwarder(podName)
62+
if err != nil {
63+
log.Errorf("failed to create port forwarder for Kmesh daemon pod %s: %v", podName, err)
64+
os.Exit(1)
65+
}
66+
if err := fw.Start(); err != nil {
67+
log.Errorf("failed to start port forwarder for Kmesh daemon pod %s: %v", podName, err)
68+
os.Exit(1)
69+
}
70+
defer fw.Close()
71+
72+
url := fmt.Sprintf("http://%s/version", fw.Address())
73+
resp, err := http.Get(url)
74+
if err != nil {
75+
log.Errorf("failed to make HTTP request: %v", err)
76+
os.Exit(1)
77+
}
78+
defer resp.Body.Close()
79+
80+
body, err := io.ReadAll(resp.Body)
81+
if err != nil {
82+
log.Errorf("failed to read HTTP response body: %v", err)
83+
os.Exit(1)
84+
}
85+
86+
cmd.Println(string(body))
87+
88+
return nil
89+
}

daemon/manager/manager.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/spf13/pflag"
2929

3030
"kmesh.net/kmesh/daemon/manager/uninstall"
31-
"kmesh.net/kmesh/daemon/manager/version"
3231
"kmesh.net/kmesh/daemon/options"
3332
"kmesh.net/kmesh/pkg/bpf"
3433
"kmesh.net/kmesh/pkg/cni"
@@ -67,7 +66,6 @@ func NewCommand() *cobra.Command {
6766
addFlags(cmd, configs)
6867

6968
// add sub commands
70-
cmd.AddCommand(version.NewCmd())
7169
cmd.AddCommand(uninstall.NewCmd())
7270

7371
return cmd

daemon/manager/version/version.go

Lines changed: 0 additions & 51 deletions
This file was deleted.

pkg/status/status_server.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"kmesh.net/kmesh/pkg/controller/ads"
4040
"kmesh.net/kmesh/pkg/controller/workload/bpfcache"
4141
"kmesh.net/kmesh/pkg/logger"
42+
"kmesh.net/kmesh/pkg/version"
4243
)
4344

4445
var log = logger.NewLoggerScope("status")
@@ -48,6 +49,7 @@ const (
4849

4950
patternHelp = "/help"
5051
patternOptions = "/options"
52+
patternVersion = "/version"
5153
patternBpfAdsMaps = "/debug/config_dump/bpf/ads"
5254
patternBpfWorkloadMaps = "/debug/config_dump/bpf/workload"
5355
configDumpPrefix = "/debug/config_dump"
@@ -95,6 +97,7 @@ func NewServer(c *controller.XdsClient, configs *options.BootstrapConfigs, bpfLo
9597

9698
s.mux.HandleFunc(patternHelp, s.httpHelp)
9799
s.mux.HandleFunc(patternOptions, s.httpOptions)
100+
s.mux.HandleFunc(patternVersion, s.version)
98101
s.mux.HandleFunc(patternBpfAdsMaps, s.bpfAdsMaps)
99102
s.mux.HandleFunc(patternBpfWorkloadMaps, s.bpfWorkloadMaps)
100103
s.mux.HandleFunc(patternConfigDumpAds, s.configDumpAds)
@@ -137,6 +140,20 @@ func (s *Server) httpOptions(w http.ResponseWriter, r *http.Request) {
137140
fmt.Fprintln(w, s.config.String())
138141
}
139142

143+
func (s *Server) version(w http.ResponseWriter, r *http.Request) {
144+
v := version.Get()
145+
146+
data, err := json.MarshalIndent(&v, "", " ")
147+
if err != nil {
148+
log.Errorf("Failed to marshal version info: %v", err)
149+
w.WriteHeader(http.StatusInternalServerError)
150+
return
151+
}
152+
153+
w.WriteHeader(http.StatusOK)
154+
_, _ = w.Write(data)
155+
}
156+
140157
func (s *Server) checkWorkloadMode(w http.ResponseWriter) bool {
141158
client := s.xdsClient
142159
if client == nil || client.WorkloadController == nil {

0 commit comments

Comments
 (0)