Skip to content

Commit

Permalink
Merge pull request #919 from YaoZengzeng/version
Browse files Browse the repository at this point in the history
implement `kmeshctl version`
  • Loading branch information
LiZhenCheng9527 authored Oct 10, 2024
2 parents 88dceea + ef71fe9 commit 18c383a
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 54 deletions.
2 changes: 1 addition & 1 deletion ctl/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func RunDump(cmd *cobra.Command, args []string) error {

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

Expand Down
2 changes: 2 additions & 0 deletions ctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"kmesh.net/kmesh/ctl/dump"
logcmd "kmesh.net/kmesh/ctl/log"
"kmesh.net/kmesh/ctl/version"
"kmesh.net/kmesh/ctl/waypoint"
)

Expand All @@ -39,6 +40,7 @@ func main() {
rootCmd.AddCommand(logcmd.NewCmd())
rootCmd.AddCommand(dump.NewCmd())
rootCmd.AddCommand(waypoint.NewCmd())
rootCmd.AddCommand(version.NewCmd())

if err := rootCmd.Execute(); err != nil {
os.Exit(1)
Expand Down
89 changes: 89 additions & 0 deletions ctl/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright The Kmesh 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

import (
"fmt"
"io"
"net/http"
"os"

"github.com/spf13/cobra"

"kmesh.net/kmesh/ctl/utils"
"kmesh.net/kmesh/pkg/logger"
"kmesh.net/kmesh/pkg/version"
)

var log = logger.NewLoggerScope("kmeshctl/version")

func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Prints out build version info",
Example: `# Show version of kmeshctl
kmeshctl version
# Show version info of a specific Kmesh daemon
kmeshctl version <kmesh-daemon-pod>`,
Run: func(cmd *cobra.Command, args []string) {
_ = RunVersion(cmd, args)
},
}
return cmd
}

// RunVersion provides the version info of kmeshctl or specific Kmesh daemon.
func RunVersion(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
v := version.Get()
cmd.Printf("%s\n", v.GitVersion)

return nil
}

podName := args[0]

fw, err := utils.CreateKmeshPortForwarder(podName)
if err != nil {
log.Errorf("failed to create port forwarder for Kmesh daemon pod %s: %v", podName, err)
os.Exit(1)
}
if err := fw.Start(); err != nil {
log.Errorf("failed to start port forwarder for Kmesh daemon pod %s: %v", podName, err)
os.Exit(1)
}
defer fw.Close()

url := fmt.Sprintf("http://%s/version", fw.Address())
resp, err := http.Get(url)
if err != nil {
log.Errorf("failed to make HTTP request: %v", err)
os.Exit(1)
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
log.Errorf("failed to read HTTP response body: %v", err)
os.Exit(1)
}

cmd.Println(string(body))

return nil
}
2 changes: 0 additions & 2 deletions daemon/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/spf13/pflag"

"kmesh.net/kmesh/daemon/manager/uninstall"
"kmesh.net/kmesh/daemon/manager/version"
"kmesh.net/kmesh/daemon/options"
"kmesh.net/kmesh/pkg/bpf"
"kmesh.net/kmesh/pkg/cni"
Expand Down Expand Up @@ -67,7 +66,6 @@ func NewCommand() *cobra.Command {
addFlags(cmd, configs)

// add sub commands
cmd.AddCommand(version.NewCmd())
cmd.AddCommand(uninstall.NewCmd())

return cmd
Expand Down
51 changes: 0 additions & 51 deletions daemon/manager/version/version.go

This file was deleted.

17 changes: 17 additions & 0 deletions pkg/status/status_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"kmesh.net/kmesh/pkg/controller/ads"
"kmesh.net/kmesh/pkg/controller/workload/bpfcache"
"kmesh.net/kmesh/pkg/logger"
"kmesh.net/kmesh/pkg/version"
)

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

patternHelp = "/help"
patternOptions = "/options"
patternVersion = "/version"
patternBpfAdsMaps = "/debug/config_dump/bpf/ads"
patternBpfWorkloadMaps = "/debug/config_dump/bpf/workload"
configDumpPrefix = "/debug/config_dump"
Expand Down Expand Up @@ -95,6 +97,7 @@ func NewServer(c *controller.XdsClient, configs *options.BootstrapConfigs, bpfLo

s.mux.HandleFunc(patternHelp, s.httpHelp)
s.mux.HandleFunc(patternOptions, s.httpOptions)
s.mux.HandleFunc(patternVersion, s.version)
s.mux.HandleFunc(patternBpfAdsMaps, s.bpfAdsMaps)
s.mux.HandleFunc(patternBpfWorkloadMaps, s.bpfWorkloadMaps)
s.mux.HandleFunc(patternConfigDumpAds, s.configDumpAds)
Expand Down Expand Up @@ -137,6 +140,20 @@ func (s *Server) httpOptions(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, s.config.String())
}

func (s *Server) version(w http.ResponseWriter, r *http.Request) {
v := version.Get()

data, err := json.MarshalIndent(&v, "", " ")
if err != nil {
log.Errorf("Failed to marshal version info: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
_, _ = w.Write(data)
}

func (s *Server) checkWorkloadMode(w http.ResponseWriter) bool {
client := s.xdsClient
if client == nil || client.WorkloadController == nil {
Expand Down

0 comments on commit 18c383a

Please sign in to comment.