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

Added 'get map' support to dump command #1216

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
170 changes: 93 additions & 77 deletions ctl/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,81 +14,97 @@
* limitations under the License.
*/

package dump
package dump

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

"github.com/spf13/cobra"

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

const (
configDumpPrefix = "/debug/config_dump"
)

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

func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "dump",
Short: "Dump config of kernel-native or dual-engine mode",
Example: `# Kernel Native mode:
kmeshctl dump <kmesh-daemon-pod> kernel-native

# Dual Engine mode:
kmeshctl dump <kmesh-daemon-pod> dual-engine`,
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
_ = RunDump(cmd, args)
},
}
return cmd
}

func RunDump(cmd *cobra.Command, args []string) error {
podName := args[0]
mode := args[1]
if mode != constants.KernelNativeMode && mode != constants.DualEngineMode {
log.Errorf("Error: Argument must be 'kernel-native' or 'dual-engine'")
os.Exit(1)
}

cli, err := utils.CreateKubeClient()
if err != nil {
log.Errorf("failed to create cli client: %v", err)
os.Exit(1)
}

fw, err := utils.CreateKmeshPortForwarder(cli, 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)
}

url := fmt.Sprintf("http://%s%s/%s", fw.Address(), configDumpPrefix, mode)
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)
}

fmt.Println(string(body))
return nil
}
import (
"fmt"
"io"
"net/http"
"os"

"github.com/spf13/cobra"

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

const (
configDumpPrefix = "/debug/config_dump"
)

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

func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "dump",
Short: "Dump config of kernel-native or dual-engine mode",
Example: `# Kernel Native mode:
kmeshctl dump <kmesh-daemon-pod> kernel-native

# Dual Engine mode:
kmeshctl dump <kmesh-daemon-pod> dual-engine
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about kmeshctl dump map?

I am thinking we should not require users to specify dual-engine or kernel-native, we can get it automatically


# Get map:
kmeshctl dump <kmesh-daemon-pod> kernel-native get map`,
Args: cobra.MinimumNArgs(2), // Allow optional third argument for 'get map'
Run: func(cmd *cobra.Command, args []string) {
_ = RunDump(cmd, args)
},
}
return cmd
}

func RunDump(cmd *cobra.Command, args []string) error {
podName := args[0]
mode := args[1]

if mode != constants.KernelNativeMode && mode != constants.DualEngineMode {
log.Errorf("Error: Argument must be 'kernel-native' or 'dual-engine'")
os.Exit(1)
}

cli, err := utils.CreateKubeClient()
if err != nil {
log.Errorf("failed to create cli client: %v", err)
os.Exit(1)
}

fw, err := utils.CreateKmeshPortForwarder(cli, 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)
}

// Default URL for config dump
url := fmt.Sprintf("http://%s%s/%s", fw.Address(), configDumpPrefix, mode)

// Check if 'get map' is provided as an extra argument
if len(args) > 2 && args[2] == "get map" {
url = fmt.Sprintf("http://%s%s/%s/get_map", fw.Address(), configDumpPrefix, mode)
}

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)
}

if len(args) > 2 && args[2] == "get map" {
fmt.Println("Fetching map details...")
}
fmt.Println(string(body))

return nil
}

Loading