diff --git a/ctl/dump/dump.go b/ctl/dump/dump.go index 34ccffd11..41330741b 100644 --- a/ctl/dump/dump.go +++ b/ctl/dump/dump.go @@ -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) } diff --git a/ctl/main.go b/ctl/main.go index 179423aae..db73c629a 100644 --- a/ctl/main.go +++ b/ctl/main.go @@ -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" ) @@ -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) diff --git a/ctl/version/version.go b/ctl/version/version.go new file mode 100644 index 000000000..9869b4929 --- /dev/null +++ b/ctl/version/version.go @@ -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 `, + 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 +} diff --git a/daemon/manager/manager.go b/daemon/manager/manager.go index 715ce0da9..a54de22ac 100644 --- a/daemon/manager/manager.go +++ b/daemon/manager/manager.go @@ -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" @@ -67,7 +66,6 @@ func NewCommand() *cobra.Command { addFlags(cmd, configs) // add sub commands - cmd.AddCommand(version.NewCmd()) cmd.AddCommand(uninstall.NewCmd()) return cmd diff --git a/daemon/manager/version/version.go b/daemon/manager/version/version.go deleted file mode 100644 index 7d0605bee..000000000 --- a/daemon/manager/version/version.go +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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 ( - "encoding/json" - "fmt" - - "github.com/spf13/cobra" - - "kmesh.net/kmesh/pkg/version" -) - -func NewCmd() *cobra.Command { - cmd := &cobra.Command{ - Use: "version", - Short: "Print the version of kmesh daemon", - Run: func(cmd *cobra.Command, args []string) { - _ = RunVersion(cmd) - }, - } - return cmd -} - -// RunVersion provides the version information of kmesh daemon in format depending on arguments -// specified in cobra.Command. -func RunVersion(cmd *cobra.Command) error { - v := version.Get() - - y, err := json.MarshalIndent(&v, "", " ") - if err != nil { - return err - } - fmt.Println(string(y)) - - return nil -} diff --git a/pkg/status/status_server.go b/pkg/status/status_server.go index 18afe368c..288d2b1be 100644 --- a/pkg/status/status_server.go +++ b/pkg/status/status_server.go @@ -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") @@ -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" @@ -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) @@ -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 {