forked from chaosblade-io/chaosblade-exec-cri
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
2,139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
.PHONY: build clean | ||
|
||
BLADE_SRC_ROOT=`pwd` | ||
|
||
GO_ENV=CGO_ENABLED=1 | ||
GO_MODULE=GO111MODULE=on | ||
GO=env $(GO_ENV) $(GO_MODULE) go | ||
|
||
UNAME := $(shell uname) | ||
|
||
ifeq ($(BLADE_VERSION), ) | ||
BLADE_VERSION=1.4.0 | ||
endif | ||
|
||
BUILD_TARGET=target | ||
BUILD_TARGET_DIR_NAME=chaosblade-$(BLADE_VERSION) | ||
BUILD_TARGET_PKG_DIR=$(BUILD_TARGET)/chaosblade-$(BLADE_VERSION) | ||
BUILD_TARGET_YAML=$(BUILD_TARGET_PKG_DIR)/yaml | ||
BUILD_IMAGE_PATH=build/image/blade | ||
|
||
OS_YAML_FILE_NAME=chaosblade-cri-spec-$(BLADE_VERSION).yaml | ||
OS_YAML_FILE_PATH=$(BUILD_TARGET_YAML)/$(OS_YAML_FILE_NAME) | ||
|
||
ifeq ($(GOOS), linux) | ||
GO_FLAGS=-ldflags="-linkmode external -extldflags -static" | ||
endif | ||
|
||
build: pre_build build_yaml | ||
|
||
build_linux: build | ||
|
||
pre_build: | ||
rm -rf $(BUILD_TARGET_PKG_DIR) | ||
mkdir -p $(BUILD_TARGET_YAML) | ||
|
||
build_yaml: build/spec.go | ||
$(GO) run $< $(OS_YAML_FILE_PATH) | ||
|
||
# test | ||
test: | ||
go test -race -coverprofile=coverage.txt -covermode=atomic ./... | ||
# clean all build result | ||
clean: | ||
go clean ./... | ||
rm -rf $(BUILD_TARGET) | ||
rm -rf $(BUILD_IMAGE_PATH)/$(BUILD_TARGET_DIR_NAME) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 1999-2019 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 main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
"github.com/chaosblade-io/chaosblade-spec-go/spec" | ||
"github.com/chaosblade-io/chaosblade-spec-go/util" | ||
|
||
"github.com/chaosblade-io/chaosblade-exec-cri/exec" | ||
) | ||
|
||
// main creates a yaml file of the experiments in the project | ||
func main() { | ||
if len(os.Args) != 2 { | ||
log.Panicln("less yaml file path") | ||
} | ||
err := util.CreateYamlFile(getModels(), os.Args[1]) | ||
if err != nil { | ||
log.Panicf("create yaml file error, %v", err) | ||
} | ||
} | ||
|
||
// getModels returns the supported experiment specs | ||
func getModels() *spec.Models { | ||
models := make([]*spec.Models, 0) | ||
dockerModelSpec := exec.NewCriExpModelSpec() | ||
for _, modelSpec := range dockerModelSpec.ExpModels() { | ||
model := util.ConvertSpecToModels(modelSpec, spec.ExpPrepareModel{}, dockerModelSpec.Scope()) | ||
models = append(models, model) | ||
} | ||
return util.MergeModels(models...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright 1999-2019 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 exec | ||
|
||
const CategorySystemContainer = "system_container" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 1999-2019 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 exec | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
"time" | ||
|
||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/client" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var cli *Client | ||
|
||
type Client struct { | ||
client *client.Client | ||
} | ||
|
||
// waitAndGetOutput returns the result | ||
func (c *Client) waitAndGetOutput(containerId string) (string, error) { | ||
containerWait() | ||
resp, err := c.client.ContainerLogs(context.Background(), containerId, types.ContainerLogsOptions{ | ||
ShowStderr: true, | ||
ShowStdout: true, | ||
}) | ||
if err != nil { | ||
logrus.Warningf("Get container: %s log err: %s", containerId, err) | ||
return "", err | ||
} | ||
defer resp.Close() | ||
bytes, err := ioutil.ReadAll(resp) | ||
return string(bytes), err | ||
} | ||
|
||
func containerWait() error { | ||
timer := time.NewTimer(500 * time.Millisecond) | ||
select { | ||
case <-timer.C: | ||
} | ||
return nil | ||
} | ||
|
||
//GetImageInspectById | ||
func (c *Client) getImageInspectById(imageId string) (types.ImageInspect, error) { | ||
inspect, _, err := c.client.ImageInspectWithRaw(context.Background(), imageId) | ||
return inspect, err | ||
} | ||
|
||
//DeleteImageByImageId | ||
func (c *Client) deleteImageByImageId(imageId string) error { | ||
_, err := c.client.ImageRemove(context.Background(), imageId, types.ImageRemoveOptions{ | ||
Force: false, | ||
PruneChildren: true, | ||
}) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/* | ||
* Copyright 1999-2019 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 exec | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/chaosblade-io/chaosblade-spec-go/spec" | ||
"github.com/chaosblade-io/chaosblade-spec-go/util" | ||
) | ||
|
||
const ( | ||
ForceFlag = "force" | ||
) | ||
|
||
type ContainerCommandModelSpec struct { | ||
spec.BaseExpModelCommandSpec | ||
} | ||
|
||
func NewContainerCommandSpec() spec.ExpModelCommandSpec { | ||
return &ContainerCommandModelSpec{ | ||
spec.BaseExpModelCommandSpec{ | ||
ExpActions: []spec.ExpActionCommandSpec{ | ||
NewRemoveActionCommand(), | ||
}, | ||
ExpFlags: []spec.ExpFlagSpec{}, | ||
}, | ||
} | ||
} | ||
|
||
func (cms *ContainerCommandModelSpec) Name() string { | ||
return "container" | ||
} | ||
|
||
func (cms *ContainerCommandModelSpec) ShortDesc() string { | ||
return `Execute a container experiment` | ||
} | ||
|
||
func (cms *ContainerCommandModelSpec) LongDesc() string { | ||
return `Execute a container experiment.` | ||
} | ||
|
||
type removeActionCommand struct { | ||
spec.BaseExpActionCommandSpec | ||
} | ||
|
||
func NewRemoveActionCommand() spec.ExpActionCommandSpec { | ||
return &removeActionCommand{ | ||
spec.BaseExpActionCommandSpec{ | ||
ActionMatchers: []spec.ExpFlagSpec{}, | ||
ActionFlags: []spec.ExpFlagSpec{ | ||
&spec.ExpFlag{ | ||
Name: ForceFlag, | ||
Desc: "force remove", | ||
NoArgs: true, | ||
}, | ||
}, | ||
ActionExecutor: &removeActionExecutor{}, | ||
ActionExample: `# Delete the container id that is a76d53933d3f", | ||
blade create cri container remove --container-id a76d53933d3f. If container-runtime is contained, the container-id shoud be full id`, | ||
ActionCategories: []string{CategorySystemContainer}, | ||
}, | ||
} | ||
} | ||
|
||
func (*removeActionCommand) Name() string { | ||
return "remove" | ||
} | ||
|
||
func (*removeActionCommand) Aliases() []string { | ||
return []string{"rm"} | ||
} | ||
|
||
func (*removeActionCommand) ShortDesc() string { | ||
return "remove a container" | ||
} | ||
|
||
func (r *removeActionCommand) LongDesc() string { | ||
if r.ActionLongDesc != "" { | ||
return r.ActionLongDesc | ||
} | ||
return "remove a container" | ||
} | ||
|
||
type removeActionExecutor struct { | ||
} | ||
|
||
func (*removeActionExecutor) Name() string { | ||
return "remove" | ||
} | ||
|
||
func (e *removeActionExecutor) SetChannel(channel spec.Channel) { | ||
} | ||
|
||
func (e *removeActionExecutor) Exec(uid string, ctx context.Context, model *spec.ExpModel) *spec.Response { | ||
if _, ok := spec.IsDestroy(ctx); ok { | ||
return spec.ReturnSuccess(uid) | ||
} | ||
flags := model.ActionFlags | ||
client, err := GetClientByRuntime(model) | ||
if err != nil { | ||
util.Errorf(uid, util.GetRunFuncName(), spec.ContainerExecFailed.Sprintf("GetClient", err)) | ||
return spec.ResponseFailWithFlags(spec.ContainerExecFailed, "GetClient", err) | ||
} | ||
containerId := flags[ContainerIdFlag.Name] | ||
containerName := flags[ContainerNameFlag.Name] | ||
container, response := GetContainer(client, uid, containerId, containerName) | ||
if !response.Success { | ||
return response | ||
} | ||
forceFlag := flags[ForceFlag] | ||
|
||
err = client.RemoveContainer(container.ContainerId, judgeForce(forceFlag)) | ||
if err != nil { | ||
util.Errorf(uid, util.GetRunFuncName(), spec.ContainerExecFailed.Sprintf("ContainerRemove", err)) | ||
return spec.ResponseFailWithFlags(spec.ContainerExecFailed, "ContainerRemove", err) | ||
} | ||
return spec.ReturnSuccess(uid) | ||
} | ||
|
||
func judgeForce(forceflag string) bool { | ||
if forceflag != "" { | ||
return true | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* 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 container | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
containertype "github.com/docker/docker/api/types/container" | ||
"github.com/docker/docker/api/types/network" | ||
"github.com/gogo/protobuf/types" | ||
) | ||
|
||
const ( | ||
ContainerdRuntime = "containerd" | ||
DockerRuntime = "docker" | ||
) | ||
|
||
const ( | ||
ChaosBladeImageVersion = "latest" | ||
DefaultImageRepo = "registry.cn-hangzhou.aliyuncs.com/chaosblade/chaosblade-tool" | ||
) | ||
|
||
type Container interface { | ||
GetContainerById(containerId string) (ContainerInfo, error, int32) | ||
GetContainerByName(containerName string) (ContainerInfo, error, int32) | ||
RemoveContainer(containerId string, force bool) error | ||
CopyToContainer(containerId, srcFile, dstPath, extractDirName string, override bool) error | ||
|
||
ExecContainer(containerId, command string) (output string, err error) | ||
ExecContainerPrivileged(containerId, command string) (output string, err error) | ||
ExecuteAndRemove(config *containertype.Config, hostConfig *containertype.HostConfig, | ||
networkConfig *network.NetworkingConfig, containerName string, removed bool, timeout time.Duration, | ||
command string, containerInfo ContainerInfo) (containerId string, output string, err error, code int32) | ||
} | ||
|
||
//ContainerInfo for server | ||
type ContainerInfo struct { | ||
ContainerId string | ||
ContainerName string | ||
Labels map[string]string | ||
Spec *types.Any | ||
} | ||
|
||
func GetChaosBladeImageRef(repo, version string) string { | ||
if repo == "" { | ||
repo = DefaultImageRepo | ||
} | ||
if version == "" { | ||
version = ChaosBladeImageVersion | ||
} | ||
return fmt.Sprintf("%s:%s", repo, version) | ||
} |
Oops, something went wrong.