Skip to content

Commit

Permalink
Add cri command
Browse files Browse the repository at this point in the history
  • Loading branch information
MandssS authored and xcaspar committed Dec 1, 2021
1 parent 3d386bd commit 3573c34
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 21 deletions.
32 changes: 25 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ BLADE_EXEC_OS_BRANCH=v1.3.0
BLADE_EXEC_DOCKER_PROJECT=https://github.com/chaosblade-io/chaosblade-exec-docker.git
BLADE_EXEC_DOCKER_BRANCH=v1.3.0

# chaosblade-exec-cri
BLADE_EXEC_CRI_PROJECT=https://github.com/chaosblade-io/chaosblade-exec-cri.git
BLADE_EXEC_CRI_BRANCH=v1.3.0

# chaosblade-exec-kubernetes
BLADE_OPERATOR_PROJECT=https://github.com/chaosblade-io/chaosblade-operator.git
BLADE_OPERATOR_BRANCH=v1.3.0
Expand All @@ -65,6 +69,10 @@ BLADE_EXEC_CPLUS_BRANCH=master
DOCKER_YAML_FILE_NAME=chaosblade-docker-spec-$(BLADE_VERSION).yaml
DOCKER_YAML_FILE_PATH=$(BUILD_TARGET_BIN)/$(DOCKER_YAML_FILE_NAME)

# cri yaml
CRI_YAML_FILE_NAME=chaosblade-cri-spec-$(BLADE_VERSION).yaml
CRI_YAML_FILE_PATH=$(BUILD_TARGET_BIN)/$(CRI_YAML_FILE_NAME)

# check yaml
CHECK_YAML_FILE_NAME=chaosblade-check-spec-$(BLADE_VERSION).yaml
CHECK_YANL_FILE_OSS=https://chaosblade.oss-cn-hangzhou.aliyuncs.com/agent/github/$(BLADE_VERSION)/$(CHECK_YAML_FILE_NAME)
Expand All @@ -82,24 +90,24 @@ help:
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>...\033[0m\n"} /^[a-zA-Z0-9_-]+:.*?##/ { printf " \033[36m%-20s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

##@ Build
build: pre_build cli os docker cplus java kubernetes upx package check_yaml ## Build all scenarios
build: pre_build cli os docker cri cplus java kubernetes upx package check_yaml ## Build all scenarios

# for example: make build_with cli os_darwin
build_with: pre_build ## Select scenario build, for example `make build_with cli os docker kubernetes java cplus`
build_with: pre_build ## Select scenario build, for example `make build_with cli os docker cri kubernetes java cplus`

# for example: make build_with_linux cli os
build_with_linux: pre_build build_linux_with_arg ## Select scenario build linux version by docker image, for example `make build_with_linux ARGS="cli os"`
build_with_linux: pre_build build_linux_with_arg ## Select scenario build linux version by docker cri image, for example `make build_with_linux ARGS="cli os"`

build_with_linux_arm: pre_build build_linux_arm_with_arg ## Select scenario build linux version by docker image, for example `make build_with_linux_arm ARGS="cli os"`
build_with_linux_arm: pre_build build_linux_arm_with_arg ## Select scenario build linux version by docker cri image, for example `make build_with_linux_arm ARGS="cli os"`

# build chaosblade linux version by docker image
build_linux: ## Build linux version of all scenarios by docker image
make build_with_linux ARGS="cli os docker kubernetes java cplus check_yaml" upx package
make build_with_linux ARGS="cli os docker cri kubernetes java cplus check_yaml" upx package

build_linux_arm: ## Build linux arm version of all scenarios by docker image
make build_with_linux_arm ARGS="cli os docker kubernetes java cplus check_yaml" upx package
make build_with_linux_arm ARGS="cli os docker cri kubernetes java cplus check_yaml" upx package

build_darwin: pre_build cli os_darwin docker cplus java kubernetes upx package check_yaml ## Build all scenarios darwin version
build_darwin: pre_build cli os_darwin docker cri cplus java kubernetes upx package check_yaml ## Build all scenarios darwin version

##@ Build sub

Expand Down Expand Up @@ -158,6 +166,16 @@ endif
cp $(BUILD_TARGET_CACHE)/chaosblade-operator/$(BUILD_TARGET_BIN)/* $(BUILD_TARGET_BIN)
cp $(BUILD_TARGET_CACHE)/chaosblade-operator/$(BUILD_TARGET_YAML)/* $(BUILD_TARGET_YAML)

cri: ## Build cri experimental scenarios.
ifneq ($(BUILD_TARGET_CACHE)/chaosblade-exec-cri, $(wildcard $(BUILD_TARGET_CACHE)/chaosblade-exec-cri))
git clone -b $(BLADE_EXEC_CRI_BRANCH) $(BLADE_EXEC_CRI_PROJECT) $(BUILD_TARGET_CACHE)/chaosblade-exec-cri
else
git -C $(BUILD_TARGET_CACHE)/chaosblade-exec-cri pull origin $(BLADE_EXEC_CRI_BRANCH)
endif
make -C $(BUILD_TARGET_CACHE)/chaosblade-exec-cri
cp $(BUILD_TARGET_CACHE)/chaosblade-exec-cri/$(BUILD_TARGET_YAML)/* $(BUILD_TARGET_YAML)


java: ## Build java experimental scenarios.
ifneq ($(BUILD_TARGET_CACHE)/chaosblade-exec-jvm, $(wildcard $(BUILD_TARGET_CACHE)/chaosblade-exec-jvm))
git clone -b $(BLADE_EXEC_JVM_BRANCH) $(BLADE_EXEC_JVM_PROJECT) $(BUILD_TARGET_CACHE)/chaosblade-exec-jvm
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![logo](https://chaosblade.oss-cn-hangzhou.aliyuncs.com/doc/image/chaosblade-logo.png)
![logo](https://chaosblade.oss-cn-hangzhou.aliyuncs.com/doc/image/chaosblade-logo.png)

# Chaosblade: An Easy to Use and Powerful Chaos Engineering Toolkit
[![Build Status](https://travis-ci.org/chaosblade-io/chaosblade.svg?branch=master)](https://travis-ci.org/chaosblade-io/chaosblade)
Expand All @@ -17,14 +17,15 @@ ChaosBlade is not only easy to use, but also supports rich experimental scenario
* Basic resources: such as CPU, memory, network, disk, process and other experimental scenarios;
* Java applications: such as databases, caches, messages, JVM itself, microservices, etc. You can also specify any class method to inject various complex experimental scenarios;
* C ++ applications: such as specifying arbitrary methods or experimental lines of code injection delay, tampering with variables and return values;
* Docker container: such as killing the container, the CPU in the container, memory, network, disk, process and other experimental scenarios;
* container: such as killing the container, the CPU in the container, memory, network, disk, process and other experimental scenarios;
* Cloud-native platforms: For example, CPU, memory, network, disk, and process experimental scenarios on Kubernetes platform nodes, Pod network and Pod itself experimental scenarios such as killing Pods, and container experimental scenarios such as the aforementioned Docker container experimental scenario;

Encapsulating scenes by domain into individual projects can not only standardize the scenes in the domain, but also facilitate the horizontal and vertical expansion of the scenes. By following the chaos experimental model, the chaosblade cli can be called uniformly. The items currently included are:
* [chaosblade](https://github.com/chaosblade-io/chaosblade): Chaos experiment management tool, including commands for creating experiments, destroying experiments, querying experiments, preparing experimental environments, and canceling experimental environments. It is the execution of chaotic experiments. Tools, execution methods include CLI and HTTP. Provides complete commands, experimental scenarios, and scenario parameter descriptions, and the operation is simple and clear.
* [chaosblade-spec-go](https://github.com/chaosblade-io/chaosblade-spec-go): Chaos experimental model Golang language definition, scenes implemented using Golang language are easy to implement based on this specification.
* [chaosblade-exec-os](https://github.com/chaosblade-io/chaosblade-exec-os): Implementation of basic resource experimental scenarios.
* [chaosblade-exec-docker](https://github.com/chaosblade-io/chaosblade-exec-docker): Docker container experimental scenario implementation, standardized by calling the Docker API.
* [chaosblade-exec-cri](https://github.com/chaosblade-io/chaosblade-exec-cri): Container experimental scenario implementation, standardized by calling the CRI.
* [chaosblade-operator](https://github.com/chaosblade-io/chaosblade-operator): Kubernetes platform experimental scenario is implemented, chaos experiments are defined by Kubernetes standard CRD method, it is very convenient to use Kubernetes resource operation method To create, update, and delete experimental scenarios, including using kubectl, client-go, etc., and also using the chaosblade cli tool described above.
* [chaosblade-exec-jvm](https://github.com/chaosblade-io/chaosblade-exec-jvm): Java application experimental scenario implementation, using Java Agent technology to mount dynamically, without any access, zero-cost use It also supports uninstallation and completely recycles various resources created by the Agent.
* [chaosblade-exec-cplus](https://github.com/chaosblade-io/chaosblade-exec-cplus): C ++ application experimental scenario implementation, using GDB technology to implement method and code line level experimental scenario injection.
Expand All @@ -44,8 +45,8 @@ Use the `blade help [COMMAND]` or `blade [COMMAND] -h` command to view help

## Experience Demo
Download the chaosblade demo image and experience the use of the blade toolkit
![demo.gif](https://chaosblade.oss-cn-hangzhou.aliyuncs.com/agent/release/chaosblade-demo-0.0.1.gif)

![demo.gif](https://chaosblade.oss-cn-hangzhou.aliyuncs.com/agent/release/chaosblade-demo-0.0.1.gif)

Download image command:
```shell script
Expand Down Expand Up @@ -83,7 +84,7 @@ ARGS="cli os" make build_with_linux
```

## Bugs and Feedback
For bug report, questions and discussions please submit [GitHub Issues](https://github.com/chaosblade-io/chaosblade/issues).
For bug report, questions and discussions please submit [GitHub Issues](https://github.com/chaosblade-io/chaosblade/issues).

You can also contact us via:
* Dingding group (recommended for chinese): 23177705
Expand Down
11 changes: 6 additions & 5 deletions README_CN.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![logo](https://chaosblade.oss-cn-hangzhou.aliyuncs.com/doc/image/chaosblade-logo.png)
![logo](https://chaosblade.oss-cn-hangzhou.aliyuncs.com/doc/image/chaosblade-logo.png)

# ChaosBlade: 一个简单易用且功能强大的混沌实验实施工具
[![Build Status](https://travis-ci.org/chaosblade-io/chaosblade.svg?branch=master)](https://travis-ci.org/chaosblade-io/chaosblade)
Expand All @@ -23,6 +23,7 @@ ChaosBlade 不仅使用简单,而且支持丰富的实验场景,场景包括
* [chaosblade-spec-go](https://github.com/chaosblade-io/chaosblade-spec-go): 混沌实验模型 Golang 语言定义,便于使用 Golang 语言实现的场景都基于此规范便捷实现。
* [chaosblade-exec-os](https://github.com/chaosblade-io/chaosblade-exec-os): 基础资源实验场景实现。
* [chaosblade-exec-docker](https://github.com/chaosblade-io/chaosblade-exec-docker): Docker 容器实验场景实现,通过调用 Docker API 标准化实现。
* [chaosblade-exec-cri](https://github.com/chaosblade-io/chaosblade-exec-cri): 容器实验场景实现,通过调用 CRI 标准化实现。
* [chaosblade-operator](https://github.com/chaosblade-io/chaosblade-operator): Kubernetes 平台实验场景实现,将混沌实验通过 Kubernetes 标准的 CRD 方式定义,很方便的使用 Kubernetes 资源操作的方式来创建、更新、删除实验场景,包括使用 kubectl、client-go 等方式执行,而且还可以使用上述的 chaosblade cli 工具执行。
* [chaosblade-exec-jvm](https://github.com/chaosblade-io/chaosblade-exec-jvm): Java 应用实验场景实现,使用 Java Agent 技术动态挂载,无需任何接入,零成本使用,而且支持卸载,完全回收 Agent 创建的各种资源。
* [chaosblade-exec-cplus](https://github.com/chaosblade-io/chaosblade-exec-cplus): C++ 应用实验场景实现,使用 GDB 技术实现方法、代码行级别的实验场景注入。
Expand All @@ -37,12 +38,12 @@ chaosblade 支持 CLI 和 HTTP 两种调用方式,支持的命令如下:
* destroy:简写是 d,销毁之前的混沌实验,比如销毁上面提到的 Dubbo 延迟实验,命令是 `blade destroy UID`
* status:简写 s,查询准备阶段或者实验的状态,命令是 `blade status UID` 或者 `blade status --type create`
* server:启动 web server,暴露 HTTP 服务,可以通过 HTTP 请求来调用 chaosblade。例如在目标机器xxxx上执行:`blade server start -p 9526`,执行 CPU 满载实验:`curl "http:/xxxx:9526/chaosblade?cmd=create%20cpu%20fullload"`

以上命令帮助均可使用 `blade help [COMMAND]` 或者 `blade [COMMAND] -h` 查看,也可查看[新手指南](https://github.com/chaosblade-io/chaosblade/wiki/%E6%96%B0%E6%89%8B%E6%8C%87%E5%8D%97),或者上述中文使用文档,快速上手使用。

## 快速体验
如果想不下载 chaosblade 工具包,快速体验 chaosblade,可以拉取 docker 镜像并运行,在容器内体验。
![demo.gif](https://chaosblade.oss-cn-hangzhou.aliyuncs.com/agent/release/chaosblade-demo-0.0.1.gif)
如果想不下载 chaosblade 工具包,快速体验 chaosblade,可以拉取 docker 镜像并运行,在容器内体验。
![demo.gif](https://chaosblade.oss-cn-hangzhou.aliyuncs.com/agent/release/chaosblade-demo-0.0.1.gif)

操作步骤如下:
下载镜像:
Expand Down Expand Up @@ -83,7 +84,7 @@ ARGS="cli os" make build_with_linux
```

## 缺陷&建议
欢迎提交缺陷、问题、建议和新功能,所有项目(包含其他子项目)的问题都可以提交到[Github Issues](https://github.com/chaosblade-io/chaosblade/issues)
欢迎提交缺陷、问题、建议和新功能,所有项目(包含其他子项目)的问题都可以提交到[Github Issues](https://github.com/chaosblade-io/chaosblade/issues)

你也可以通过以下方式联系我们:
* 钉钉群(推荐):23177705
Expand Down
12 changes: 11 additions & 1 deletion build/spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,18 @@ func main() {
osSpecFile := path.Join(filePath, fmt.Sprintf("chaosblade-os-spec-%s.yaml", version))
k8sSpecFile := path.Join(filePath, fmt.Sprintf("chaosblade-k8s-spec-%s.yaml", version))
dockerSpecFile := path.Join(filePath, fmt.Sprintf("chaosblade-docker-spec-%s.yaml", version))
criSpecFile := path.Join(filePath, fmt.Sprintf("chaosblade-cri-spec-%s.yaml", version))
cplusSpecFile := path.Join(filePath, "chaosblade-cplus-spec.yaml")
chaosSpecFile := path.Join(targetPath, "chaosblade.spec.yaml")

osModels := getOsModels(osSpecFile)
jvmModels := getJvmModels(jvmSpecFile)
cplusModels := getCplusModels(cplusSpecFile)
dockerModels := getDockerModels(dockerSpecFile)
criModels := getCriModels(criSpecFile)
k8sModels := getKubernetesModels(k8sSpecFile)

models := mergeModels(osModels, jvmModels, dockerModels, k8sModels, cplusModels)
models := mergeModels(osModels, jvmModels, dockerModels, cplusModels, criModels, k8sModels)

file, err := os.OpenFile(chaosSpecFile, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0755)
if err != nil {
Expand Down Expand Up @@ -90,6 +92,14 @@ func getDockerModels(dockerSpecFile string) *spec.Models {
return models
}

func getCriModels(criSpecFile string) *spec.Models {
models, err := util.ParseSpecsToModel(criSpecFile, nil)
if err != nil {
logrus.Fatalf("parse cri spec failed, %s", err)
}
return models
}

func getKubernetesModels(k8sSpecFile string) *spec.Models {
models, err := util.ParseSpecsToModel(k8sSpecFile, nil)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cli/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (cc *CreateCommand) Init() {
Example: createExample(),
}
flags := cc.command.PersistentFlags()
flags.StringVar(&uid, UidFlag, "", "Set Uid for the experiment, adapt to docker")
flags.StringVar(&uid, UidFlag, "", "Set Uid for the experiment, adapt to docker and cri")
flags.BoolVarP(&cc.async, AsyncFlag, "a", false, "whether to create asynchronously, default is false")
flags.StringVarP(&cc.endpoint, EndpointFlag, "e", "", "the create result reporting address. It takes effect only when the async value is true and the value is not empty")
flags.BoolVarP(&cc.nohup, NohupFlag, "n", false, "used to internal async create, no need to config")
Expand Down Expand Up @@ -144,7 +144,7 @@ func (cc *CreateCommand) actionRunEFunc(target, scope string, actionCommand *act
var args string
if scope == "host" {
args = fmt.Sprintf("create %s %s --uid %s --nohup=true", target, actionCommand.Name(), model.Uid)
} else if scope == "docker" {
} else if scope == "docker" || scope == "cri" {
args = fmt.Sprintf("create %s %s %s --uid %s --nohup=true", scope, target, actionCommand.Name(), model.Uid)
} else {
args = fmt.Sprintf("create k8s %s-%s %s --uid %s --nohup=true", scope, target, actionCommand.Name(), model.Uid)
Expand Down
28 changes: 27 additions & 1 deletion cli/cmd/exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/spf13/pflag"

"github.com/chaosblade-io/chaosblade/exec/cplus"
"github.com/chaosblade-io/chaosblade/exec/cri"
"github.com/chaosblade-io/chaosblade/exec/docker"
"github.com/chaosblade-io/chaosblade/exec/jvm"
"github.com/chaosblade-io/chaosblade/exec/kubernetes"
Expand Down Expand Up @@ -119,6 +120,8 @@ func (ec *baseExpCommandService) registerSubCommands() {
ec.registerCplusExpCommands()
// register docker command
ec.registerDockerExpCommands()
// register cri command
ec.registerCriExpCommands()
// register k8s command
ec.registerK8sExpCommands()
}
Expand Down Expand Up @@ -216,10 +219,32 @@ func (ec *baseExpCommandService) registerK8sExpCommands() []*modelCommand {
return modelCommands
}

// registerCriExpCommands
func (ec *baseExpCommandService) registerCriExpCommands() []*modelCommand {
file := path.Join(util.GetYamlHome(), fmt.Sprintf("chaosblade-cri-spec-%s.yaml", version.Ver))
models, err := specutil.ParseSpecsToModel(file, cri.NewExecutor())
if err != nil {
return nil
}
criSpec := cri.NewCommandModelSpec()
modelCommands := make([]*modelCommand, 0)
for idx := range models.Models {
model := &models.Models[idx]
command := ec.registerExpCommand(model, criSpec.Name())
modelCommands = append(modelCommands, command)
}
criCmd := ec.registerExpCommand(criSpec, "")
cobraCmd := criCmd.CobraCmd()
for _, child := range modelCommands {
copyAndAddCommand(cobraCmd, child.command)
}
return modelCommands
}

// registerExpCommand
func (ec *baseExpCommandService) registerExpCommand(commandSpec spec.ExpModelCommandSpec, parentTargetCmd string) *modelCommand {
cmdName := commandSpec.Name()
if commandSpec.Scope() != "" && commandSpec.Scope() != "host" && commandSpec.Scope() != "docker" && commandSpec.Scope() != OperatorCommand {
if commandSpec.Scope() != "" && commandSpec.Scope() != "host" && commandSpec.Scope() != "docker" && commandSpec.Scope() != "cri" && commandSpec.Scope() != OperatorCommand {
cmdName = fmt.Sprintf("%s-%s", commandSpec.Scope(), commandSpec.Name())
}
cmd := &cobra.Command{
Expand Down Expand Up @@ -365,4 +390,5 @@ func copyAndAddCommand(parent, child *cobra.Command) {
for _, command := range commands {
copyAndAddCommand(newChild, command)
}

}
54 changes: 54 additions & 0 deletions exec/cri/executor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* 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,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cri

import (
"context"

"github.com/chaosblade-io/chaosblade-exec-cri/exec"
"github.com/chaosblade-io/chaosblade-spec-go/channel"
"github.com/chaosblade-io/chaosblade-spec-go/spec"
"github.com/chaosblade-io/chaosblade-spec-go/util"
)

type Executor struct {
executors map[string]spec.Executor
}

func NewExecutor() spec.Executor {
return &Executor{
executors: exec.GetAllExecutors(),
}
}

func (*Executor) Name() string {
return "cri"
}

func (e *Executor) Exec(uid string, ctx context.Context, model *spec.ExpModel) *spec.Response {
key := exec.GetExecutorKey(model.Target, model.ActionName)
executor := e.executors[key]
if executor == nil {
util.Errorf(uid, util.GetRunFuncName(), spec.CriExecNotFound.Sprintf(key))
return spec.ResponseFailWithFlags(spec.CriExecNotFound, key)
}
executor.SetChannel(channel.NewLocalChannel())
return executor.Exec(uid, ctx, model)
}

func (*Executor) SetChannel(channel spec.Channel) {
}
Loading

0 comments on commit 3573c34

Please sign in to comment.