Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement kmeshctl version #919

Merged
merged 4 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
"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 @@

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 @@

s.mux.HandleFunc(patternHelp, s.httpHelp)
s.mux.HandleFunc(patternOptions, s.httpOptions)
s.mux.HandleFunc(patternVersion, s.version)

Check warning on line 100 in pkg/status/status_server.go

View check run for this annotation

Codecov / codecov/patch

pkg/status/status_server.go#L100

Added line #L100 was not covered by tests
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 @@
fmt.Fprintln(w, s.config.String())
}

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

Check warning on line 144 in pkg/status/status_server.go

View check run for this annotation

Codecov / codecov/patch

pkg/status/status_server.go#L143-L144

Added lines #L143 - L144 were not covered by tests

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

Check warning on line 150 in pkg/status/status_server.go

View check run for this annotation

Codecov / codecov/patch

pkg/status/status_server.go#L146-L150

Added lines #L146 - L150 were not covered by tests
}

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

Check warning on line 154 in pkg/status/status_server.go

View check run for this annotation

Codecov / codecov/patch

pkg/status/status_server.go#L153-L154

Added lines #L153 - L154 were not covered by tests
}

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