Skip to content

Commit 561c9c0

Browse files
committed
code review fixes
1 parent 9af4ddf commit 561c9c0

File tree

2 files changed

+100
-69
lines changed

2 files changed

+100
-69
lines changed

internal/cmd/skupper/debug/nonkube/debug.go

Lines changed: 83 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ import (
88
"os"
99

1010
"path/filepath"
11-
"strings"
1211
"time"
1312

14-
"github.com/skupperproject/skupper/api/types"
1513
"github.com/skupperproject/skupper/internal/cmd/skupper/common"
1614
"github.com/skupperproject/skupper/internal/cmd/skupper/common/utils"
17-
"github.com/skupperproject/skupper/internal/config"
1815
internalclient "github.com/skupperproject/skupper/internal/nonkube/client/compat"
1916
"github.com/skupperproject/skupper/internal/nonkube/client/fs"
17+
"github.com/skupperproject/skupper/internal/nonkube/client/runtime"
18+
nk_common "github.com/skupperproject/skupper/internal/nonkube/common"
2019
"github.com/skupperproject/skupper/internal/utils/validator"
2120
"github.com/skupperproject/skupper/pkg/nonkube/api"
2221
"github.com/spf13/cobra"
@@ -83,12 +82,26 @@ func (cmd *CmdDebug) ValidateInput(args []string) error {
8382
}
8483

8584
func (cmd *CmdDebug) InputToOptions() {
85+
if cmd.namespace == "" {
86+
cmd.namespace = "default"
87+
}
8688
datetime := time.Now().Format("20060102150405")
8789
cmd.fileName = fmt.Sprintf("%s-%s-%s", cmd.fileName, cmd.namespace, datetime)
8890
}
8991

9092
func (cmd *CmdDebug) Run() error {
9193
dumpFile := cmd.fileName
94+
rpath := "/runtime/"
95+
inpath := "/input/"
96+
intpath := "/internal/"
97+
certFiles := []string{"ca.crt", "tls.crt"}
98+
flags := []string{"-g", "-c", "-l", "-n", "-e", "-a", "-m", "-p"}
99+
100+
//check if namespace exists
101+
path := api.GetInternalOutputPath(cmd.namespace, api.InputSiteStatePath)
102+
if _, err := os.ReadDir(path); err != nil {
103+
return fmt.Errorf("Namespace %s has not been configured, cannot run debug dump command", cmd.namespace)
104+
}
92105

93106
// Add extension if not present
94107
if filepath.Ext(dumpFile) == "" {
@@ -106,23 +119,20 @@ func (cmd *CmdDebug) Run() error {
106119
tw := tar.NewWriter(gz)
107120
defer tw.Close()
108121

109-
platform := config.GetPlatform()
122+
nsl := &nk_common.NamespacePlatformLoader{}
123+
platform, err := nsl.Load(cmd.namespace)
110124

111-
if platform == types.PlatformPodman {
112-
pv, err := utils.RunCommand("podman", "version")
113-
if err == nil {
114-
utils.WriteTar("/versions/podman.txt", pv, time.Now(), tw)
115-
}
116-
} else if platform == types.PlatformDocker {
117-
pv, err := utils.RunCommand("docker", "version")
118-
if err == nil {
119-
utils.WriteTar("/versions/docker.txt", pv, time.Now(), tw)
120-
}
121-
} else if platform == types.PlatformLinux {
122-
pv, err := utils.RunCommand("skrouterd", "--version")
123-
if err == nil {
124-
utils.WriteTar("/versions/skrouterd.txt", pv, time.Now(), tw)
125-
}
125+
pv, err := utils.RunCommand("podman", "version")
126+
if err == nil {
127+
utils.WriteTar("/versions/podman.txt", pv, time.Now(), tw)
128+
}
129+
pv, err = utils.RunCommand("docker", "version")
130+
if err == nil {
131+
utils.WriteTar("/versions/docker.txt", pv, time.Now(), tw)
132+
}
133+
pv, err = utils.RunCommand("skrouterd", "--version")
134+
if err == nil {
135+
utils.WriteTar("/versions/skrouterd.txt", pv, time.Now(), tw)
126136
}
127137

128138
manifest, err := utils.RunCommand("skupper", "version", "-o", "yaml", "-n", cmd.namespace)
@@ -131,18 +141,19 @@ func (cmd *CmdDebug) Run() error {
131141
utils.WriteTar("/versions/skupper.yaml.txt", manifest, time.Now(), tw)
132142
}
133143

134-
rpath := "/runtime/"
135-
inpath := "/input/"
136-
intpath := "/internal/"
137-
certFiles := []string{"ca.crt", "tls.crt"}
138-
139144
//input/certs
140-
path := inpath + "certs/"
141-
certPath := api.GetDefaultOutputPath(cmd.namespace) + "/" + string(api.InputCertificatesPath)
142-
for x := range certFiles {
143-
file, err := os.ReadFile(certPath + "/" + certFiles[x])
144-
if file != nil && err == nil {
145-
utils.WriteTar(path+certFiles[x], file, time.Now(), tw)
145+
path = inpath + "certs/"
146+
certPath := api.GetInternalOutputPath(cmd.namespace, api.InputCertificatesPath)
147+
certDirs, err := os.ReadDir(certPath)
148+
if err == nil && certDirs != nil && len(certDirs) != 0 {
149+
for _, certDir := range certDirs {
150+
for x := range certFiles {
151+
fileName := certDir.Name() + "/" + certFiles[x]
152+
file, err := os.ReadFile(certPath + "/" + fileName)
153+
if file != nil && err == nil {
154+
utils.WriteTar(path+fileName, file, time.Now(), tw)
155+
}
156+
}
146157
}
147158
}
148159

@@ -211,7 +222,7 @@ func (cmd *CmdDebug) Run() error {
211222

212223
//internal/scripts
213224
path = intpath + "scripts/"
214-
scriptsPath := api.GetDefaultOutputPath(cmd.namespace) + "/" + string(api.ScriptsPath)
225+
scriptsPath := api.GetInternalOutputPath(cmd.namespace, api.ScriptsPath)
215226
scripts, err := os.ReadDir(scriptsPath)
216227
if err == nil && scripts != nil && len(scripts) != 0 {
217228
for _, script := range scripts {
@@ -225,8 +236,8 @@ func (cmd *CmdDebug) Run() error {
225236

226237
// runtime/certs
227238
path = rpath + "certs/"
228-
certPath = api.GetDefaultOutputPath(cmd.namespace) + "/" + string(api.CertificatesPath)
229-
certDirs, err := os.ReadDir(certPath)
239+
certPath = api.GetInternalOutputPath(cmd.namespace, api.CertificatesPath)
240+
certDirs, err = os.ReadDir(certPath)
230241
if err == nil && certDirs != nil && len(certDirs) != 0 {
231242
for _, certDir := range certDirs {
232243
for x := range certFiles {
@@ -241,7 +252,7 @@ func (cmd *CmdDebug) Run() error {
241252

242253
// runtime/issuers
243254
path = rpath + "issuers/"
244-
iPath := api.GetDefaultOutputPath(cmd.namespace) + "/" + string(api.IssuersPath)
255+
iPath := api.GetInternalOutputPath(cmd.namespace, api.IssuersPath)
245256
iDirs, err := os.ReadDir(iPath)
246257
if err == nil && iDirs != nil && len(iDirs) != 0 {
247258
for _, iDir := range iDirs {
@@ -257,7 +268,7 @@ func (cmd *CmdDebug) Run() error {
257268

258269
// runtime/links
259270
path = rpath + "links/"
260-
lpath := api.GetDefaultOutputPath(cmd.namespace) + "/" + string(api.RuntimeTokenPath)
271+
lpath := api.GetInternalOutputPath(cmd.namespace, api.RuntimeTokenPath)
261272
lopts := fs.GetOptions{ResourcesPath: lpath, RuntimeFirst: true, LogWarning: false}
262273
links, err = cmd.linkHandler.List(lopts)
263274
if err == nil && links != nil && len(links) != 0 {
@@ -344,51 +355,56 @@ func (cmd *CmdDebug) Run() error {
344355

345356
//runtime/router
346357
path = rpath + "router/"
347-
skrPath := api.GetDefaultOutputPath(cmd.namespace) + "/" + string(api.RouterConfigPath+"/skrouterd.json")
358+
skrPath := api.GetInternalOutputPath(cmd.namespace, api.RouterConfigPath+"/skrouterd.json")
348359
//skrPath := pathProvider.GetRuntimeNamespace()+ api.
349360
skrouterd, err := os.ReadFile(skrPath)
350361
if err == nil && skrouterd != nil {
351362
utils.WriteTar(path+"skrouterd.json", skrouterd, time.Now(), tw)
352363
}
353364

354365
//logs and skupper-router statistics
355-
cli, err := internalclient.NewCompatClient(os.Getenv("CONTAINER_ENDPOINT"), "")
356-
if err == nil {
357-
rtrContainerName := cmd.namespace + "-skupper-router"
358-
if container, err := cli.ContainerInspect(rtrContainerName); err == nil {
359-
encodedOutput, _ := utils.Encode("yaml", container)
360-
utils.WriteTar(rpath+"Container-"+container.Name+".yaml", []byte(encodedOutput), time.Now(), tw)
361-
}
366+
if err := os.Setenv("SKUPPER_PLATFORM", platform); err == nil {
367+
cli, err := internalclient.NewCompatClient(os.Getenv("CONTAINER_ENDPOINT"), "")
368+
if err == nil {
369+
rtrContainerName := cmd.namespace + "-skupper-router"
370+
if container, err := cli.ContainerInspect(rtrContainerName); err == nil {
371+
encodedOutput, _ := utils.Encode("yaml", container)
372+
utils.WriteTar(rpath+"Container-"+container.Name+".yaml", []byte(encodedOutput), time.Now(), tw)
373+
}
362374

363-
/*
364-
for x := range flags {
365-
command := "skstat "+ flags[x]
366-
out, err := cli.ContainerExec(rtrContainerName, strings.Split(command, " "))
367-
if err == nil {
368-
fmt.Println("containerexec: ", err, out)
375+
localRouterAddress, err := runtime.GetLocalRouterAddress(cmd.namespace)
376+
if err == nil {
377+
for x := range flags {
378+
skStatCommand := []string{
379+
"/bin/skstat", flags[x],
380+
"-b", localRouterAddress,
381+
"--ssl-certificate", "/etc/skupper-router/runtime/certs/skupper-local-client/tls.crt",
382+
"--ssl-key", "/etc/skupper-router/runtime/certs/skupper-local-client/tls.key",
383+
"--ssl-trustfile", "/etc/skupper-router/runtime/certs/skupper-local-client/ca.crt",
384+
}
385+
386+
out, err := cli.ContainerExec(rtrContainerName, skStatCommand) //strings.Split(skStatCommand, " "))
387+
if err == nil {
388+
utils.WriteTar(path+"skstat/"+rtrContainerName+"-skstat"+flags[x]+".txt", []byte(out), time.Now(), tw)
389+
}
369390
}
370391
}
371-
*/
372-
373-
out, err := cli.ContainerExec(rtrContainerName, strings.Split("skstat -c", " "))
374-
if err == nil {
375-
fmt.Println("containerexec: ", err, out)
376-
}
377392

378-
logs, err := cli.ContainerLogs(rtrContainerName)
379-
if err == nil {
380-
utils.WriteTar(rpath+"logs/"+rtrContainerName+".txt", []byte(logs), time.Now(), tw)
381-
}
393+
logs, err := cli.ContainerLogs(rtrContainerName)
394+
if err == nil {
395+
utils.WriteTar(rpath+"logs/"+rtrContainerName+".txt", []byte(logs), time.Now(), tw)
396+
}
382397

383-
ctlContainerName := "system-controller"
384-
if container, err := cli.ContainerInspect(ctlContainerName); err == nil {
385-
encodedOutput, _ := utils.Encode("yaml", container)
386-
utils.WriteTar(rpath+"Container-"+container.Name+".yaml", []byte(encodedOutput), time.Now(), tw)
387-
}
398+
ctlContainerName := "system-controller"
399+
if container, err := cli.ContainerInspect(ctlContainerName); err == nil {
400+
encodedOutput, _ := utils.Encode("yaml", container)
401+
utils.WriteTar(rpath+"Container-"+container.Name+".yaml", []byte(encodedOutput), time.Now(), tw)
402+
}
388403

389-
logs, err = cli.ContainerLogs(ctlContainerName)
390-
if err == nil {
391-
utils.WriteTar(rpath+"logs/"+ctlContainerName+".txt", []byte(logs), time.Now(), tw)
404+
logs, err = cli.ContainerLogs(ctlContainerName)
405+
if err == nil {
406+
utils.WriteTar(rpath+"logs/"+ctlContainerName+".txt", []byte(logs), time.Now(), tw)
407+
}
392408
}
393409
}
394410

internal/cmd/skupper/debug/nonkube/debug_test.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,25 @@ func TestCmdDebug_InputToOptions(t *testing.T) {
109109
args: []string{},
110110
flags: common.CommandDebugFlags{},
111111
},
112+
{
113+
name: "name",
114+
filename: "skupper-dump",
115+
args: []string{},
116+
flags: common.CommandDebugFlags{},
117+
},
112118
}
113119

114120
for _, test := range testTable {
115121
t.Run(test.name, func(t *testing.T) {
116122
command := &CmdDebug{Flags: &common.CommandDebugFlags{}}
117123
command.CobraCmd = &cobra.Command{Use: "test"}
124+
namespace := test.namespace
125+
if namespace == "" {
126+
namespace = "default"
127+
}
118128
command.namespace = test.namespace
119129
command.fileName = test.filename
120-
name := fmt.Sprintf("%s-%s-%s", test.filename, command.namespace, time.Now().Format("20060102150405"))
130+
name := fmt.Sprintf("%s-%s-%s", test.filename, namespace, time.Now().Format("20060102150405"))
121131
command.InputToOptions()
122132

123133
assert.Check(t, command.fileName == name)
@@ -135,9 +145,14 @@ func TestCmdDebug_Run(t *testing.T) {
135145

136146
testTable := []test{
137147
{
138-
name: "run default",
148+
name: "run namespace exists",
139149
namespace: "test2",
140150
},
151+
{
152+
name: "no namespace",
153+
namespace: "default",
154+
errorMessage: "Namespace default has not been configured, cannot run debug dump command",
155+
},
141156
}
142157
// Add a temp file so listener/connector/site exists for delete tests
143158
listenerResource := v2alpha1.Listener{

0 commit comments

Comments
 (0)