Skip to content

Commit 1ed5a3f

Browse files
authored
Merge pull request #889 from YaoZengzeng/cd
implement `kmeshctl dump`
2 parents bc24a20 + 7d848e7 commit 1ed5a3f

File tree

6 files changed

+143
-93
lines changed

6 files changed

+143
-93
lines changed

ctl/dump/dump.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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 dump
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+
)
30+
31+
const (
32+
configDumpPrefix = "/debug/config_dump"
33+
)
34+
35+
var log = logger.NewLoggerScope("kmeshctl/dump")
36+
37+
func NewCmd() *cobra.Command {
38+
cmd := &cobra.Command{
39+
Use: "dump",
40+
Short: "Dump config of ads or workload mode",
41+
Example: `# Ads mode:
42+
kmeshctl dump <kmesh-daemon-pod> ads
43+
44+
# Workload mode:
45+
kmeshctl dump <kmesh-daemon-pod> workload`,
46+
Args: cobra.ExactArgs(2),
47+
Run: func(cmd *cobra.Command, args []string) {
48+
_ = RunDump(cmd, args)
49+
},
50+
}
51+
return cmd
52+
}
53+
54+
func RunDump(cmd *cobra.Command, args []string) error {
55+
podName := args[0]
56+
mode := args[1]
57+
if mode != "ads" && mode != "workload" {
58+
log.Errorf("Error: Argument must be 'ads' or 'workload'")
59+
os.Exit(1)
60+
}
61+
62+
fw, err := utils.CreateKmeshPortForwarder(podName)
63+
if err != nil {
64+
log.Errorf("failed to create port forwarder for Kmesh daemon pod %s: %v", podName, err)
65+
os.Exit(1)
66+
}
67+
if err := fw.Start(); err != nil {
68+
log.Errorf("failed to start port forwarder for Kmesh daemon pod %s: %v", podName, err)
69+
}
70+
71+
url := fmt.Sprintf("http://%s%s/%s", fw.Address(), configDumpPrefix, mode)
72+
resp, err := http.Get(url)
73+
if err != nil {
74+
log.Errorf("failed to make HTTP request: %v", err)
75+
os.Exit(1)
76+
}
77+
defer resp.Body.Close()
78+
79+
body, err := io.ReadAll(resp.Body)
80+
if err != nil {
81+
fmt.Printf("failed to read HTTP response body: %v\n", err)
82+
os.Exit(1)
83+
}
84+
85+
fmt.Println(string(body))
86+
return nil
87+
}

ctl/log/log.go

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,12 @@ import (
2626
"strings"
2727

2828
"github.com/spf13/cobra"
29-
"istio.io/istio/pkg/kube"
3029

30+
"kmesh.net/kmesh/ctl/utils"
3131
"kmesh.net/kmesh/pkg/logger"
3232
)
3333

3434
const (
35-
KmeshNamespace = "kmesh-system"
36-
KmeshAdminPort = 15200
37-
3835
patternLoggers = "/debug/loggers"
3936
)
4037

@@ -142,7 +139,7 @@ func SetLoggerLevel(url string, setFlag string) {
142139
client := &http.Client{}
143140
resp, err := client.Do(req)
144141
if err != nil {
145-
log.Errorf("Error making request: %v", err)
142+
log.Errorf("failed to make HTTP request: %v", err)
146143
return
147144
}
148145
defer resp.Body.Close()
@@ -154,34 +151,22 @@ func SetLoggerLevel(url string, setFlag string) {
154151

155152
body, err := io.ReadAll(resp.Body)
156153
if err != nil {
157-
log.Errorf("Error reading response body: %v", err)
154+
log.Errorf("failed to read HTTP response body: %v", err)
158155
return
159156
}
160157
fmt.Println(string(body))
161158
}
162159

163160
func RunGetOrSetLoggerLevel(cmd *cobra.Command, args []string) {
164-
rc, err := kube.DefaultRestConfig("", "")
165-
if err != nil {
166-
log.Errorf("failed to get rest.Config for given kube config file and context: %v", err)
167-
os.Exit(1)
168-
}
169-
170-
cli, err := kube.NewCLIClient(kube.NewClientConfigForRestConfig(rc))
171-
if err != nil {
172-
log.Errorf("failed to create kube client: %v", err)
173-
os.Exit(1)
174-
}
175-
176161
podName := args[0]
177162

178-
fw, err := cli.NewPortForwarder(podName, KmeshNamespace, "", 0, KmeshAdminPort)
163+
fw, err := utils.CreateKmeshPortForwarder(podName)
179164
if err != nil {
180-
log.Errorf("failed to create port forwarder: %v", err)
165+
log.Errorf("failed to create port forwarder for Kmesh daemon pod %s: %v", podName, err)
181166
os.Exit(1)
182167
}
183168
if err := fw.Start(); err != nil {
184-
log.Errorf("failed to start port forwarder: %v", err)
169+
log.Errorf("failed to start port forwarder for Kmesh daemon pod %s: %v", podName, err)
185170
os.Exit(1)
186171
}
187172
defer fw.Close()

ctl/main.go

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

2222
"github.com/spf13/cobra"
2323

24+
"kmesh.net/kmesh/ctl/dump"
2425
logcmd "kmesh.net/kmesh/ctl/log"
2526
)
2627

@@ -35,6 +36,7 @@ func main() {
3536
}
3637

3738
rootCmd.AddCommand(logcmd.NewCmd())
39+
rootCmd.AddCommand(dump.NewCmd())
3840

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

ctl/utils/portforwarder.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 utils
18+
19+
import (
20+
"fmt"
21+
22+
"istio.io/istio/pkg/kube"
23+
)
24+
25+
const (
26+
KmeshNamespace = "kmesh-system"
27+
KmeshAdminPort = 15200
28+
)
29+
30+
// Create a new PortForwarder configured for the given Kmesh daemon pod.
31+
func CreateKmeshPortForwarder(podName string) (kube.PortForwarder, error) {
32+
rc, err := kube.DefaultRestConfig("", "")
33+
if err != nil {
34+
return nil, fmt.Errorf("failed to get rest.Config for given kube config file and context: %v", err)
35+
}
36+
37+
cli, err := kube.NewCLIClient(kube.NewClientConfigForRestConfig(rc))
38+
if err != nil {
39+
return nil, fmt.Errorf("failed to create kube client: %v", err)
40+
}
41+
42+
fw, err := cli.NewPortForwarder(podName, KmeshNamespace, "", 0, KmeshAdminPort)
43+
if err != nil {
44+
return nil, fmt.Errorf("failed to create port forwarder: %v", err)
45+
}
46+
47+
return fw, nil
48+
}

daemon/manager/dump/dump.go

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

daemon/manager/manager.go

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

30-
"kmesh.net/kmesh/daemon/manager/dump"
3130
"kmesh.net/kmesh/daemon/manager/uninstall"
3231
"kmesh.net/kmesh/daemon/manager/version"
3332
"kmesh.net/kmesh/daemon/options"
@@ -69,7 +68,6 @@ func NewCommand() *cobra.Command {
6968

7069
// add sub commands
7170
cmd.AddCommand(version.NewCmd())
72-
cmd.AddCommand(dump.NewCmd())
7371
cmd.AddCommand(uninstall.NewCmd())
7472

7573
return cmd

0 commit comments

Comments
 (0)