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

feat: chaosctl for print debug info #1074

Merged
merged 51 commits into from Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
1baed24
networkchaos debug output
Yiyiyimu Oct 17, 2020
be9ddaf
Merge branch 'master' into network-debug
Yiyiyimu Oct 20, 2020
c7e0ba2
add stresschaos & refactor
Yiyiyimu Oct 22, 2020
99cfdeb
add completion for namespace
Yiyiyimu Oct 24, 2020
02cd30d
Merge branch 'master' of https://github.com/chaos-mesh/chaos-mesh int…
Yiyiyimu Oct 24, 2020
a68f734
implement iochaos debug
Yiyiyimu Oct 25, 2020
0df456b
1. add compare-with-expect demo 2. wait till chaos start 3. format ou…
Yiyiyimu Oct 26, 2020
e08803d
move from shell-wise to client-go
Yiyiyimu Oct 28, 2020
26eaa3a
init client in one place & refactor
Yiyiyimu Oct 29, 2020
e20ae0f
1. Support larger radius 2. use type assertion to replace ExtractFrom…
Yiyiyimu Nov 2, 2020
3303cc4
1. kubectl like ctl format; 2.chaos completion
Yiyiyimu Nov 4, 2020
cc2943c
Merge branch 'master' into network-debug
Yiyiyimu Nov 4, 2020
f4bb471
create local daemon client, port forward and get pid, to replace Log()
Yiyiyimu Nov 5, 2020
4ace7bd
Merge branch 'network-debug' of https://github.com/yiyiyimu/chaos-mes…
Yiyiyimu Nov 5, 2020
3a373dd
1. add test case; 2.make GetChaos private
Yiyiyimu Nov 9, 2020
b7fc6cb
fix error
Yiyiyimu Nov 10, 2020
3ced32a
Merge branch 'master' into network-debug
Yiyiyimu Nov 10, 2020
52362d9
1. add color lib for better color control 2. add container for exec 3…
Yiyiyimu Nov 10, 2020
7ec5e19
Merge branch 'network-debug' of https://github.com/yiyiyimu/chaos-mes…
Yiyiyimu Nov 10, 2020
f90ff88
Merge branch 'master' into network-debug
Yiyiyimu Nov 22, 2020
79ad029
fix: ps/cat not exist in pod
Yiyiyimu Nov 22, 2020
5f29208
Merge branch 'network-debug' of https://github.com/yiyiyimu/chaos-mes…
Yiyiyimu Nov 22, 2020
7ebc3a4
fix nscat location
Yiyiyimu Nov 23, 2020
d9214b7
Debug returns a struct to process and print together, instead of prin…
Yiyiyimu Nov 23, 2020
413a4f3
fix nsenter ps error
Yiyiyimu Nov 23, 2020
8f38dcb
fix: ls not in pod
Yiyiyimu Nov 24, 2020
ed27c08
fix exec error
Yiyiyimu Nov 24, 2020
b9fb378
Merge branch 'master' of https://github.com/chaos-mesh/chaos-mesh int…
Yiyiyimu Nov 24, 2020
1bb7223
fix CI and rm control coupling
Yiyiyimu Nov 24, 2020
09c1f9e
fix: should print fail message and keep printing debug info, while no…
Yiyiyimu Nov 25, 2020
46fb709
check controller-daemon connection when rpc error happens
Yiyiyimu Nov 25, 2020
6c12c09
fix: situation when mount is not in pod, use 'cat /proc/mount' instead
Yiyiyimu Nov 25, 2020
ccc1fd7
fix typo
Yiyiyimu Nov 25, 2020
a623238
fix test
Yiyiyimu Nov 25, 2020
2088210
add guidance in ISSUE_TEMPLATE
Yiyiyimu Nov 26, 2020
e0c49aa
refactor
Yiyiyimu Nov 26, 2020
27ebebc
feat: logs & refactor
Yiyiyimu Nov 26, 2020
df1210b
add how to use logs in README
Yiyiyimu Nov 27, 2020
ed13f3a
merge master
Yiyiyimu Nov 30, 2020
efef2ba
Merge branch 'master' of https://github.com/chaos-mesh/chaos-mesh int…
Yiyiyimu Dec 4, 2020
f3df88a
Use ExecBypass to exec in target pod & fix as review suggest
Yiyiyimu Dec 5, 2020
e65b84b
merge master
Yiyiyimu Dec 5, 2020
5993a7d
print info of all PID when needed
Yiyiyimu Dec 7, 2020
5dc5c4e
return not-support error when using stress-ng stressors
Yiyiyimu Dec 8, 2020
44ac0b7
Merge branch 'master' of https://github.com/chaos-mesh/chaos-mesh int…
Yiyiyimu Dec 8, 2020
9e84532
show cgroups info for stressng stressors
Yiyiyimu Dec 8, 2020
7f0e334
use chaos-mesh/nsexec
Yiyiyimu Dec 13, 2020
bd6baa1
merge master
Yiyiyimu Dec 14, 2020
43909e5
disable port forward log for ctl
Yiyiyimu Dec 14, 2020
befa280
move portforward from e2e to pkg
Yiyiyimu Dec 14, 2020
2128638
Merge branch 'master' into network-debug
ti-srebot Dec 17, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Expand Up @@ -134,6 +134,10 @@ binary: chaosdaemon manager chaosfs chaos-dashboard bin/pause bin/suicide
watchmaker:
$(CGOENV) go build -ldflags '$(LDFLAGS)' -o bin/watchmaker ./cmd/watchmaker/...

# Build chaosctl
chaosctl:
$(GO) build -ldflags '$(LDFLAGS)' -o bin/chaosctl ./cmd/chaosctl/*.go

# Run against the configured Kubernetes cluster in ~/.kube/config
run: generate fmt vet manifests
$(GO) run ./cmd/controller-manager/main.go
Expand Down
20 changes: 20 additions & 0 deletions cmd/chaosctl/main.go
@@ -0,0 +1,20 @@
// Copyright 2020 Chaos Mesh 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import "github.com/chaos-mesh/chaos-mesh/pkg/chaosctl/cmd"

func main() {
cmd.Execute()
}
5 changes: 3 additions & 2 deletions go.mod
@@ -1,6 +1,7 @@
module github.com/chaos-mesh/chaos-mesh

require (
code.cloudfoundry.org/bytefmt v0.0.0-20200131002437-cf55d5288a48
github.com/chaos-mesh/k8s_dns_chaos v0.0.0-20200922120555-7ced93637075
github.com/containerd/cgroups v0.0.0-20200404012852-53ba5634dc0f
github.com/containerd/containerd v1.2.3
Expand All @@ -13,6 +14,7 @@ require (
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c // indirect
github.com/ethercflow/hookfs v0.3.0
github.com/fatih/color v1.9.0
github.com/ghodss/yaml v1.0.0
github.com/gin-contrib/pprof v1.3.0
github.com/gin-gonic/gin v1.6.3
Expand All @@ -22,7 +24,6 @@ require (
github.com/gogo/googleapis v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.4.2
github.com/gorilla/websocket v1.4.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.2.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/grpc-ecosystem/grpc-gateway v1.14.1 // indirect
Expand All @@ -49,7 +50,7 @@ require (
github.com/shirou/gopsutil v0.0.0-20180427012116-c95755e4bcd7
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 // indirect
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd
github.com/spf13/cobra v0.0.6 // indirect
github.com/spf13/cobra v1.1.1
github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14
github.com/swaggo/gin-swagger v1.2.0
github.com/swaggo/swag v1.6.7
Expand Down
113 changes: 106 additions & 7 deletions go.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pkg/chaosctl/README.md
@@ -0,0 +1 @@
use `make chaosctl` to build chaosctl
78 changes: 78 additions & 0 deletions pkg/chaosctl/cmd/completion.go
@@ -0,0 +1,78 @@
// Copyright 2020 Chaos Mesh 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"os"

"github.com/spf13/cobra"
)

// completionCmd represents the completion command
var completionCmd = &cobra.Command{
Use: "completion [bash|zsh|fish|powershell]",
Short: "Generate completion script",
Long: `To load completions:

Bash:

$ source <(chaosctl completion bash)

# To load completions for each session, execute once:
Linux:
$ chaosctl completion bash > /etc/bash_completion.d/chaosctl
MacOS:
$ chaosctl completion bash > /usr/local/etc/bash_completion.d/chaosctl

Zsh:

$ compdef _chaosctl chaosctl

# If shell completion is not already enabled in your environment you will need
# to enable it. You can execute the following once:

$ echo "autoload -U compinit; compinit" >> ~/.zshrc

# To load completions for each session, execute once:
$ chaosctl completion zsh > "${fpath[1]}/_chaosctl"

# You will need to start a new shell for this setup to take effect.

Fish:

$ chaosctl completion fish | source

# To load completions for each session, execute once:
$ chaosctl completion fish > ~/.config/fish/completions/chaosctl.fish
`,
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.ExactValidArgs(1),
Run: func(cmd *cobra.Command, args []string) {
switch args[0] {
case "bash":
cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
cmd.Root().GenZshCompletion(os.Stdout)
yeya24 marked this conversation as resolved.
Show resolved Hide resolved
case "fish":
cmd.Root().GenFishCompletion(os.Stdout, true)
case "powershell":
cmd.Root().GenPowerShellCompletion(os.Stdout)
}
Yiyiyimu marked this conversation as resolved.
Show resolved Hide resolved
},
}

func init() {
rootCmd.AddCommand(completionCmd)
}
196 changes: 196 additions & 0 deletions pkg/chaosctl/cmd/debug.go
@@ -0,0 +1,196 @@
// Copyright 2020 Chaos Mesh 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"context"
"fmt"
"log"
"strings"

"github.com/spf13/cobra"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client"

cm "github.com/chaos-mesh/chaos-mesh/pkg/debug/common"
"github.com/chaos-mesh/chaos-mesh/pkg/debug/iochaos"
"github.com/chaos-mesh/chaos-mesh/pkg/debug/networkchaos"
"github.com/chaos-mesh/chaos-mesh/pkg/debug/stresschaos"
)

type DebugOptions struct {
ChaosName string
Namespace string
}
Yiyiyimu marked this conversation as resolved.
Show resolved Hide resolved

func init() {
o := &DebugOptions{}

c, err := cm.InitClientSet()
if err != nil {
log.Fatal(err)
}

// debugCmd represents the debug command
debugCmd := &cobra.Command{
Use: `debug (CHAOSTYPE) [-c CHAOSNAME] [-n NAMESPACE]`,
Short: `Print the debug information for certain chaos`,
Long: `Print the debug information for certain chaos.
Currently only support networkchaos and stresschaos.

Examples:
# Return debug information from all networkchaos in default namespace
chaosctl debug networkchaos

# Return debug information from certain networkchaos
chaosctl debug networkchaos -c web-show-network-delay -n chaos-testing`,
ValidArgsFunction: noCompletions,
}

networkCmd := &cobra.Command{
Use: `networkchaos (CHAOSNAME) [-n NAMESPACE]`,
Short: `Print the debug information for certain network chaos`,
Long: `Print the debug information for certain network chaos`,
Run: func(cmd *cobra.Command, args []string) {
if err := o.Run("networkchaos", args, c); err != nil {
log.Fatal(err)
}
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return listChaos("networkchaos", o.Namespace, toComplete, c.CtrlCli)
},
}

stressCmd := &cobra.Command{
Use: `stresschaos (CHAOSNAME) [-n NAMESPACE]`,
Short: `Print the debug information for certain stress chaos`,
Long: `Print the debug information for certain stress chaos`,
Run: func(cmd *cobra.Command, args []string) {
if err := o.Run("stresschaos", args, c); err != nil {
log.Fatal(err)
}
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return listChaos("stresschaos", o.Namespace, toComplete, c.CtrlCli)
},
}

ioCmd := &cobra.Command{
Use: `iochaos (CHAOSNAME) [-n NAMESPACE]`,
Short: `Print the debug information for certain io chaos`,
Long: `Print the debug information for certain io chaos`,
Run: func(cmd *cobra.Command, args []string) {
if err := o.Run("iochaos", args, c); err != nil {
log.Fatal(err)
}
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return listChaos("iochaos", o.Namespace, toComplete, c.CtrlCli)
},
}

debugCmd.AddCommand(networkCmd)
debugCmd.AddCommand(stressCmd)
debugCmd.AddCommand(ioCmd)

debugCmd.PersistentFlags().StringVarP(&o.Namespace, "namespace", "n", "default", "namespace to find chaos")
err = debugCmd.RegisterFlagCompletionFunc("namespace", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return listNamespace(toComplete, c.KubeCli)
})
if err != nil {
log.Fatal(err)
}

rootCmd.AddCommand(debugCmd)
}

func (o *DebugOptions) Run(chaosType string, args []string, c *cm.ClientSet) error {
if len(args) > 1 {
return fmt.Errorf("Only one chaos could be specified")
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

chaosNameArg := ""
if len(args) == 1 {
chaosNameArg = args[0]
}

chaosList, nameList, err := cm.GetChaosList(ctx, chaosType, chaosNameArg, o.Namespace, c.CtrlCli)
if err != nil {
return fmt.Errorf(err.Error())
}

for i, chaos := range chaosList {
cm.Print("[CHAOSNAME]: "+nameList[i], 0, "Blue")
switch chaosType {
case "networkchaos":
if err := networkchaos.Debug(ctx, chaos, c); err != nil {
return err
}
case "stresschaos":
if err := stresschaos.Debug(ctx, chaos, c); err != nil {
return err
}
case "iochaos":
if err := iochaos.Debug(ctx, chaos, c); err != nil {
return err
}
default:
return fmt.Errorf("chaos not supported")
}
}

return nil
}

func listNamespace(toComplete string, c *kubernetes.Clientset) ([]string, cobra.ShellCompDirective) {
namespaces, err := c.CoreV1().Namespaces().List(v1.ListOptions{})
if err != nil {
return nil, cobra.ShellCompDirectiveDefault
}
var ret []string
for _, ns := range namespaces.Items {
if strings.HasPrefix(ns.Name, toComplete) {
ret = append(ret, ns.Name)
}
}
return ret, cobra.ShellCompDirectiveNoFileComp
}

func listChaos(chaosType string, namespace string, toComplete string, c client.Client) ([]string, cobra.ShellCompDirective) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, chaosList, err := cm.GetChaosList(ctx, chaosType, "", namespace, c)
if err != nil {
return nil, cobra.ShellCompDirectiveDefault
}
var ret []string
for _, chaos := range chaosList {
if strings.HasPrefix(chaos, toComplete) {
ret = append(ret, chaos)
}
}
return ret, cobra.ShellCompDirectiveNoFileComp
}
46 changes: 46 additions & 0 deletions pkg/chaosctl/cmd/root.go
@@ -0,0 +1,46 @@
// Copyright 2020 Chaos Mesh 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "chaosctl [command] [options]",
Short: "Interacting with chaos mesh",
Long: `
Interacting with chaos mesh

Currently only support printing debug information. For example:
$ chaosctl debug networkchaos
will show infomation about networkchaos`,
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func noCompletions(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveNoFileComp
}