Skip to content

Commit

Permalink
feat: chaosctl for print debug info (#1074) (#1302)
Browse files Browse the repository at this point in the history
* feat: chaosctl for print debug info (#1074)

Signed-off-by: yiyiyimu <wosoyoung@gmail.com>

* move genrateNNodes out of test file

Signed-off-by: yiyiyimu <wosoyoung@gmail.com>
  • Loading branch information
Yiyiyimu committed Dec 18, 2020
1 parent 28de66e commit fec5346
Show file tree
Hide file tree
Showing 25 changed files with 1,887 additions and 113 deletions.
4 changes: 4 additions & 0 deletions .github/ISSUE_TEMPLATE/bug-report.md
Expand Up @@ -15,3 +15,7 @@ about: Something isn't working as expected
**What did you expect to see?**

**What did you see instead?**

**Output of chaosctl**
<!-- If it is related to networkchaos, stresschaos or iochaos, chaosctl would help maintainers better find the bug. -->
<!-- See pkg/chaosctl/README.md for detail -->
6 changes: 5 additions & 1 deletion .github/ISSUE_TEMPLATE/question.md
Expand Up @@ -12,4 +12,8 @@ Before asking a question, make sure you have:
- Searched open and closed [GitHub issues](https://github.com/chaos-mesh/chaos-mesh/issues?utf8=%E2%9C%93&q=is%3Aissue)
- Read the documentation:
- [Chaos Mesh Readme](https://github.com/chaos-mesh/chaos-mesh)
- [Chaos Mesh Doc](https://github.com/chaos-mesh/chaos-mesh/tree/master/doc)
- [Chaos Mesh Doc](https://chaos-mesh.org/docs)

**Output of chaosctl**
<!-- If it is related to networkchaos, stresschaos or iochaos, chaosctl would help maintainers better answer your questions. -->
<!-- See pkg/chaosctl/README.md for detail -->
4 changes: 4 additions & 0 deletions Makefile
Expand Up @@ -130,6 +130,10 @@ binary: chaosdaemon manager chaosfs chaos-dashboard bin/pause
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/containerd/cgroups v0.0.0-20200404012852-53ba5634dc0f
github.com/containerd/containerd v1.2.3
github.com/containerd/continuity v0.0.0-20200107194136-26c1120b8d41 // indirect
Expand All @@ -12,6 +13,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-gonic/gin v1.6.3
github.com/go-logr/logr v0.1.0
Expand All @@ -20,7 +22,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 @@ -46,7 +47,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.

4 changes: 2 additions & 2 deletions images/chaos-daemon/Dockerfile
Expand Up @@ -6,7 +6,7 @@ ARG HTTP_PROXY
ENV http_proxy $HTTP_PROXY
ENV https_proxy $HTTPS_PROXY

RUN apt-get update && apt-get install -y tzdata iptables ipset stress-ng iproute2 fuse util-linux && rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y tzdata iptables ipset stress-ng iproute2 fuse util-linux procps && rm -rf /var/lib/apt/lists/*

RUN update-alternatives --set iptables /usr/sbin/iptables-legacy

Expand All @@ -16,4 +16,4 @@ COPY --from=pingcap/chaos-binary /bin/chaos-daemon /usr/local/bin/chaos-daemon
COPY --from=pingcap/chaos-binary /bin/toda /usr/local/bin/toda
COPY --from=pingcap/chaos-binary /bin/nsexec /usr/local/bin/nsexec
COPY --from=pingcap/chaos-binary /bin/libnsenter.so /usr/local/lib/libnsenter.so
COPY --from=pingcap/chaos-binary /bin/pause /usr/local/bin/pause
COPY --from=pingcap/chaos-binary /bin/pause /usr/local/bin/pause
33 changes: 33 additions & 0 deletions pkg/chaosctl/README.md
@@ -0,0 +1,33 @@
# chaosctl

Chaostl is a tool (currently) used to print debug info. Maintainers would make use of it to better provide their suggestions for issues.

## How to build
```shell
cd $CHAOSMESH_DIR
make chaosctl
./bin/chaosctl --help #to see if build succeed.
```
Chaoctl support shell autocompletion, which could save you some typing. Do `./bin/chaosctl completion -h` for detail.

## How to use
**Debug**
`chaoctl debug` is used to print debug info of certain chaos. Currently, chaosctl support networkchaos, stresschaos and iochaos.
```shell
#to print info of each networkchaos
./bin/chaosctl debug networkchaos
#to print info of certain chaos in default namespace
./bin/chaosctl debug networkchaos CHAOSNAME
#to print info of each networkchaos in certain namespace
./bin/chaosctl debug networkchaos -n NAMESPACE
```

**Logs**
`chaoctl log` is used to easily print log from all chaos-mesh components, including controller-manager, chaos-daemon and chaos-dashboard.
```shell
# Default print all log of all chaosmesh components
chaosctl logs

# to print 100 log lines for chaosmesh components in node NODENAME
chaosctl logs -t 100 -n NODENAME
```
88 changes: 88 additions & 0 deletions pkg/chaosctl/cmd/completion.go
@@ -0,0 +1,88 @@
// 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:
$ ./bin/chaosctl completion bash > /etc/bash_completion.d/chaosctl
MacOS:
$ ./bin/chaosctl completion bash > /usr/local/etc/bash_completion.d/chaosctl
Zsh:
$ compdef _chaosctl ./bin/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:
$ ./bin/chaosctl completion zsh > "${fpath[1]}/_chaosctl"
# You will need to start a new shell for this setup to take effect.
Fish:
$ ./bin/chaosctl completion fish | source
# To load completions for each session, execute once:
$ ./bin/chaosctl completion fish > ~/.config/fish/completions/chaosctl.fish
Powershell:
PS> ./bin/chaosctl completion powershell | Out-String | Invoke-Expression
# To load completions for every new session, run:
PS> ./bin/chaosctl completion powershell > chaosctl.ps1
# and source this file from your powershell profile.
`,
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)
case "fish":
cmd.Root().GenFishCompletion(os.Stdout, true)
// TODO: powershell completion is still not fully supported, see https://github.com/spf13/cobra/pull/1208
// Need to update cobra version when this PR is merged
case "powershell":
cmd.Root().GenPowerShellCompletion(os.Stdout)
}
},
}

func init() {
rootCmd.AddCommand(completionCmd)
}

0 comments on commit fec5346

Please sign in to comment.