From f3d60c313cde2a61c3e4cdce0bc0861d2ffc989c Mon Sep 17 00:00:00 2001 From: KingsonKai <562419972@qq.com> Date: Fri, 15 Sep 2023 16:57:24 +0800 Subject: [PATCH] feature: chaosmetad support containerd --- chaosmeta-deploy/deploy.sh | 9 +- .../templates/chaosmeta-daemon-template.yaml | 2 +- .../templates/chaosmeta-inject-template.yaml | 2 +- .../config/chaosmeta-inject.json | 2 +- chaosmeta-platform/go.sum | 1 + .../build/chaosmeta-daemonset.Dockerfile | 4 +- ...{Dockerfile => chaosmetad-demo.Dockerfile} | 4 +- chaosmetad/build/ci/build.sh | 2 +- chaosmetad/go.mod | 21 +- chaosmetad/go.sum | 30 ++ chaosmetad/pkg/crclient/client.go | 6 +- chaosmetad/pkg/crclient/containerd/client.go | 311 ++++++++++++++++++ chaosmetad/pkg/crclient/docker/client.go | 2 +- chaosmetad/pkg/crclient/pouch/client.go | 2 +- chaosmetad/pkg/utils/filesys/filesystem.go | 28 +- 15 files changed, 378 insertions(+), 48 deletions(-) rename {chaosmeta-inject-operator => chaosmetad}/build/chaosmeta-daemonset.Dockerfile (78%) rename chaosmetad/build/{Dockerfile => chaosmetad-demo.Dockerfile} (84%) create mode 100644 chaosmetad/pkg/crclient/containerd/client.go diff --git a/chaosmeta-deploy/deploy.sh b/chaosmeta-deploy/deploy.sh index b17e03d..41ce5b1 100644 --- a/chaosmeta-deploy/deploy.sh +++ b/chaosmeta-deploy/deploy.sh @@ -18,6 +18,7 @@ BASE_DIR=$( pwd ) +mkdir -p ${BASE_DIR}/yamls # param check OP=$1 NAMESPACE=$2 @@ -27,10 +28,10 @@ for ((i = 3; i <= $#; i++)); do done if [ "$OP" == "-h" ] || [ "$OP" == "--help" ] || [ -z "$OP" ] || [ ${#COMPONENT_LIST[@]} -eq 0 ]; then - echo "format: sh deploy.sh [operation] [namespace] [component list]" - echo "operation support: ${OP_INSTALL},${OP_UNINSTALL}" - echo "component support: ${COMPONENT_ALL} ${COMPONENT_PLATFORM} ${COMPONENT_WORKFLOW} ${COMPONENT_INJECT} ${COMPONENT_MEASURE} ${COMPONENT_FLOW} ${COMPONENT_DAEMON}" - echo "example: sh deploy.sh install default ${COMPONENT_PLATFORM} ${COMPONENT_WORKFLOW} ${COMPONENT_INJECT} ${COMPONENT_DAEMON}" + echo -e "\nformat: sh deploy.sh [operation] [namespace] [component list]" + echo -e "example: sh deploy.sh install default ${COMPONENT_PLATFORM} ${COMPONENT_WORKFLOW} ${COMPONENT_INJECT} ${COMPONENT_DAEMON}\n" + echo "operation support: ${OP_INSTALL} ${OP_UNINSTALL}" + echo -e "component support: ${COMPONENT_ALL} ${COMPONENT_PLATFORM} ${COMPONENT_WORKFLOW} ${COMPONENT_INJECT} ${COMPONENT_MEASURE} ${COMPONENT_FLOW} ${COMPONENT_DAEMON}\n" exit 1 fi diff --git a/chaosmeta-deploy/templates/chaosmeta-daemon-template.yaml b/chaosmeta-deploy/templates/chaosmeta-daemon-template.yaml index 58f829c..3b7fea3 100644 --- a/chaosmeta-deploy/templates/chaosmeta-daemon-template.yaml +++ b/chaosmeta-deploy/templates/chaosmeta-daemon-template.yaml @@ -19,7 +19,7 @@ spec: # chaos-role.chaosmeta.io: chaosmeta-daemon containers: - name: chaosmeta-daemon - image: registry.cn-hangzhou.aliyuncs.com/chaosmeta/chaosmeta-daemon:v0.1.1 + image: registry.cn-hangzhou.aliyuncs.com/chaosmeta/chaosmeta-daemon:v0.2.0 securityContext: privileged: true volumeMounts: diff --git a/chaosmeta-deploy/templates/chaosmeta-inject-template.yaml b/chaosmeta-deploy/templates/chaosmeta-inject-template.yaml index 4156023..2d1119e 100644 --- a/chaosmeta-deploy/templates/chaosmeta-inject-template.yaml +++ b/chaosmeta-deploy/templates/chaosmeta-inject-template.yaml @@ -504,7 +504,7 @@ data: "executor": { "mode": "daemonset", "executor": "chaosmetad", - "version": "0.1.1", + "version": "0.2.0", "agentConfig": { "agentPort": 29595 }, diff --git a/chaosmeta-inject-operator/config/chaosmeta-inject.json b/chaosmeta-inject-operator/config/chaosmeta-inject.json index 1dc1b08..d9dd720 100644 --- a/chaosmeta-inject-operator/config/chaosmeta-inject.json +++ b/chaosmeta-inject-operator/config/chaosmeta-inject.json @@ -8,7 +8,7 @@ "executor": { "mode": "daemonset", "executor": "chaosmetad", - "version": "0.1.1", + "version": "0.2.0", "agentConfig": { "agentPort": 29595 }, diff --git a/chaosmeta-platform/go.sum b/chaosmeta-platform/go.sum index fcdce15..5917a4a 100644 --- a/chaosmeta-platform/go.sum +++ b/chaosmeta-platform/go.sum @@ -260,6 +260,7 @@ github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00/go.mod github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= +github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= github.com/onsi/ginkgo/v2 v2.9.4 h1:xR7vG4IXt5RWx6FfIjyAtsoMAtnc3C/rFXBBd2AjZwE= github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE= github.com/panjf2000/ants v1.3.0 h1:8pQ+8leaLc9lys2viEEr8md0U4RN6uOSUCE9bOYjQ9M= diff --git a/chaosmeta-inject-operator/build/chaosmeta-daemonset.Dockerfile b/chaosmetad/build/chaosmeta-daemonset.Dockerfile similarity index 78% rename from chaosmeta-inject-operator/build/chaosmeta-daemonset.Dockerfile rename to chaosmetad/build/chaosmeta-daemonset.Dockerfile index 19b3821..7740eb1 100644 --- a/chaosmeta-inject-operator/build/chaosmeta-daemonset.Dockerfile +++ b/chaosmetad/build/chaosmeta-daemonset.Dockerfile @@ -1,5 +1,5 @@ -# docker build -t registry.cn-hangzhou.aliyuncs.com/chaosmeta/chaosmeta-daemon:v0.1.1 -f chaosmeta-daemonset.Dockerfile . +# docker build -t registry.cn-hangzhou.aliyuncs.com/chaosmeta/chaosmeta-daemon:v0.2.0 -f chaosmeta-daemonset.Dockerfile . From centos:centos7 -ENV CHAOSMETAD_VERSION=0.1.1 +ENV CHAOSMETAD_VERSION=0.2.0 ADD ./chaosmetad-$CHAOSMETAD_VERSION.tar.gz /opt/chaosmeta CMD while true; do if [ ! -d "/tmp/chaosmetad-$CHAOSMETAD_VERSION" ]; then cp -r /opt/chaosmeta/chaosmetad-$CHAOSMETAD_VERSION /tmp/chaosmetad-$CHAOSMETAD_VERSION; fi; sleep 600; done diff --git a/chaosmetad/build/Dockerfile b/chaosmetad/build/chaosmetad-demo.Dockerfile similarity index 84% rename from chaosmetad/build/Dockerfile rename to chaosmetad/build/chaosmetad-demo.Dockerfile index beae203..97818b0 100644 --- a/chaosmetad/build/Dockerfile +++ b/chaosmetad/build/chaosmetad-demo.Dockerfile @@ -1,7 +1,7 @@ -# docker build -t registry.cn-hangzhou.aliyuncs.com/chaosmeta/chaosmetad-demo:v0.1.1 . +# docker build -t registry.cn-hangzhou.aliyuncs.com/chaosmeta/chaosmetad-demo:v0.2.0 -f chaosmetad-demo.Dockerfile . From centos:centos7 ADD ./jdk-8u361-linux-x64.tar.gz /usr/local RUN yum install -y iproute && yum clean all -ENV CHAOSMETAD_VERSION=0.1.1 +ENV CHAOSMETAD_VERSION=0.2.0 ADD ./chaosmetad-$CHAOSMETAD_VERSION.tar.gz /opt/chaosmeta RUN echo 'export JAVA_HOME=/usr/local/jdk1.8.0_361' >> /etc/profile && echo 'export PATH=$PATH:$JAVA_HOME/bin' >> /etc/profile && echo 'export PATH=$PATH:/opt/chaosmeta/chaosmetad-'${CHAOSMETAD_VERSION} >> /etc/profile diff --git a/chaosmetad/build/ci/build.sh b/chaosmetad/build/ci/build.sh index 0332c76..074868a 100644 --- a/chaosmetad/build/ci/build.sh +++ b/chaosmetad/build/ci/build.sh @@ -27,7 +27,7 @@ fi # base info BUILD_NAME="chaosmetad" -VERSION="0.1.1" +VERSION="0.2.0" BUILD_DATE=$(date "+%Y-%m-%d %H:%M:%S") # env var diff --git a/chaosmetad/go.mod b/chaosmetad/go.mod index 676b73b..16aa58d 100644 --- a/chaosmetad/go.mod +++ b/chaosmetad/go.mod @@ -6,8 +6,11 @@ require ( github.com/alibaba/pouch v0.0.0-20221109061116-9caf9bae0b23 github.com/bndr/gotabulate v1.1.2 github.com/containerd/cgroups v1.0.4 + github.com/containerd/containerd v1.6.12 github.com/docker/docker v20.10.21+incompatible + github.com/google/uuid v1.3.0 github.com/gorilla/mux v1.8.0 + github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 github.com/shirou/gopsutil v3.21.11+incompatible github.com/sirupsen/logrus v1.9.0 github.com/spf13/cobra v1.5.0 @@ -21,11 +24,15 @@ require ( github.com/PuerkitoBio/purell v1.1.1 // indirect github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef // indirect - github.com/containerd/containerd v1.6.12 // indirect + github.com/containerd/continuity v0.3.0 // indirect + github.com/containerd/fifo v1.0.0 // indirect + github.com/containerd/ttrpc v1.1.0 // indirect + github.com/containerd/typeurl v1.0.2 // indirect github.com/contiv/executor v0.0.0-20180626233236-d263f4daa3ad // indirect github.com/coreos/go-systemd/v22 v22.3.2 // indirect github.com/docker/distribution v2.8.1+incompatible // indirect github.com/docker/go-connections v0.4.0 // indirect + github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c // indirect github.com/docker/go-units v0.5.0 // indirect github.com/go-ole/go-ole v1.2.6 // indirect github.com/go-openapi/analysis v0.21.2 // indirect @@ -38,24 +45,28 @@ require ( github.com/go-openapi/swag v0.21.1 // indirect github.com/go-openapi/validate v0.22.1 // indirect github.com/godbus/dbus/v5 v5.0.6 // indirect + github.com/gogo/googleapis v1.4.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect - github.com/google/uuid v1.3.0 // indirect + github.com/golang/protobuf v1.5.2 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/josharian/intern v1.0.0 // indirect + github.com/klauspost/compress v1.13.6 // indirect github.com/mailru/easyjson v0.7.6 // indirect github.com/mattn/go-sqlite3 v1.14.15 // indirect github.com/mitchellh/mapstructure v1.4.1 // indirect + github.com/moby/locker v1.0.1 // indirect github.com/moby/sys/mount v0.3.3 // indirect github.com/moby/sys/mountinfo v0.6.2 // indirect + github.com/moby/sys/signal v0.6.0 // indirect github.com/moby/term v0.0.0-20221128092401-c43b287e0e0f // indirect github.com/oklog/ulid v1.3.1 // indirect github.com/opencontainers/go-digest v1.0.0 // indirect github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799 // indirect github.com/opencontainers/runc v1.1.4 // indirect - github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 // indirect + github.com/opencontainers/selinux v1.10.1 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/tklauser/go-sysconf v0.3.10 // indirect @@ -65,10 +76,14 @@ require ( go.opencensus.io v0.23.0 // indirect golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect + golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect golang.org/x/text v0.3.7 // indirect golang.org/x/time v0.2.0 // indirect golang.org/x/tools v0.1.12 // indirect + google.golang.org/genproto v0.0.0-20220502173005-c8bf987b8c21 // indirect + google.golang.org/grpc v1.47.0 // indirect + google.golang.org/protobuf v1.28.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gotest.tools/v3 v3.4.0 // indirect ) diff --git a/chaosmetad/go.sum b/chaosmetad/go.sum index e09364f..e4b930d 100644 --- a/chaosmetad/go.sum +++ b/chaosmetad/go.sum @@ -118,7 +118,11 @@ github.com/cilium/ebpf v0.7.0/go.mod h1:/oI2+1shJiTGAMgl6/RgJr36Eo1jzrRcAWbcXO2u github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= +github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4/go.mod h1:6pvJx4me5XPnfI9Z40ddWsdw2W/uZgQLFXToKeRcDiI= github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= +github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= +github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= +github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8= github.com/containerd/aufs v0.0.0-20200908144142-dab0cbea06f4/go.mod h1:nukgQABAEopAHvB6j7cnP5zJ+/3aVcE7hCYqvIwAHyE= github.com/containerd/aufs v0.0.0-20201003224125-76a6863f2989/go.mod h1:AkGGQs9NM2vtYHaUen+NljV0/baGCAPELGm2q9ZXpWU= @@ -166,11 +170,13 @@ github.com/containerd/continuity v0.0.0-20201208142359-180525291bb7/go.mod h1:kR github.com/containerd/continuity v0.0.0-20210208174643-50096c924a4e/go.mod h1:EXlVlkqNba9rJe3j7w3Xa924itAMLgZH4UD/Q4PExuQ= github.com/containerd/continuity v0.1.0/go.mod h1:ICJu0PwR54nI0yPEnJ6jcS+J7CZAUXrLh8lPo2knzsM= github.com/containerd/continuity v0.3.0 h1:nisirsYROK15TAMVukJOUyGJjz4BNQJBVsNvAXZJ/eg= +github.com/containerd/continuity v0.3.0/go.mod h1:wJEAIwKOm/pBZuBd0JmeTvnLquTB1Ag8espWhkykbPM= github.com/containerd/fifo v0.0.0-20180307165137-3d5202aec260/go.mod h1:ODA38xgv3Kuk8dQz2ZQXpnv/UZZUHUCL7pnLehbXgQI= github.com/containerd/fifo v0.0.0-20190226154929-a9fb20d87448/go.mod h1:ODA38xgv3Kuk8dQz2ZQXpnv/UZZUHUCL7pnLehbXgQI= github.com/containerd/fifo v0.0.0-20200410184934-f15a3290365b/go.mod h1:jPQ2IAeZRCYxpS/Cm1495vGFww6ecHmMk1YJH2Q5ln0= github.com/containerd/fifo v0.0.0-20201026212402-0724c46b320c/go.mod h1:jPQ2IAeZRCYxpS/Cm1495vGFww6ecHmMk1YJH2Q5ln0= github.com/containerd/fifo v0.0.0-20210316144830-115abcc95a1d/go.mod h1:ocF/ME1SX5b1AOlWi9r677YJmCPSwwWnQ9O123vzpE4= +github.com/containerd/fifo v1.0.0 h1:6PirWBr9/L7GDamKr+XM0IeUFXu5mf3M/BPpH9gaLBU= github.com/containerd/fifo v1.0.0/go.mod h1:ocF/ME1SX5b1AOlWi9r677YJmCPSwwWnQ9O123vzpE4= github.com/containerd/go-cni v1.0.1/go.mod h1:+vUpYxKvAF72G9i1WoDOiPGRtQpqsNW/ZHtSlv++smU= github.com/containerd/go-cni v1.0.2/go.mod h1:nrNABBHzu0ZwCug9Ije8hL2xBCYh/pjfMb1aZGrrohk= @@ -192,10 +198,12 @@ github.com/containerd/ttrpc v0.0.0-20190828172938-92c8520ef9f8/go.mod h1:PvCDdDG github.com/containerd/ttrpc v0.0.0-20191028202541-4f1b8fe65a5c/go.mod h1:LPm1u0xBw8r8NOKoOdNMeVHSawSsltak+Ihv+etqsE8= github.com/containerd/ttrpc v1.0.1/go.mod h1:UAxOpgT9ziI0gJrmKvgcZivgxOp8iFPSk8httJEt98Y= github.com/containerd/ttrpc v1.0.2/go.mod h1:UAxOpgT9ziI0gJrmKvgcZivgxOp8iFPSk8httJEt98Y= +github.com/containerd/ttrpc v1.1.0 h1:GbtyLRxb0gOLR0TYQWt3O6B0NvT8tMdorEHqIQo/lWI= github.com/containerd/ttrpc v1.1.0/go.mod h1:XX4ZTnoOId4HklF4edwc4DcqskFZuvXB1Evzy5KFQpQ= github.com/containerd/typeurl v0.0.0-20180627222232-a93fcdb778cd/go.mod h1:Cm3kwCdlkCfMSHURc+r6fwoGH6/F1hH3S4sg0rLFWPc= github.com/containerd/typeurl v0.0.0-20190911142611-5eb25027c9fd/go.mod h1:GeKYzf2pQcqv7tJ0AoCuuhtnqhva5LNU3U+OyKxxJpk= github.com/containerd/typeurl v1.0.1/go.mod h1:TB1hUtrpaiO88KEK56ijojHS1+NeF0izUACaJW2mdXg= +github.com/containerd/typeurl v1.0.2 h1:Chlt8zIieDbzQFzXzAeBEF92KhExuE4p9p92/QmY7aY= github.com/containerd/typeurl v1.0.2/go.mod h1:9trJWW2sRlGub4wZJRTW83VtbOLS6hwcDZXTn6oPz9s= github.com/containerd/zfs v0.0.0-20200918131355-0a33824f23a2/go.mod h1:8IgZOBdv8fAgXddBT4dBXJPtxyRsejFIpXoklgxgEjw= github.com/containerd/zfs v0.0.0-20210301145711-11e8f1707f62/go.mod h1:A9zfAbMlQwE+/is6hi0Xw8ktpL+6glmqZYtevJgaB8Y= @@ -260,6 +268,7 @@ github.com/docker/docker-credential-helpers v0.6.3/go.mod h1:WRaJzqw3CTB9bk10avu github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= github.com/docker/go-events v0.0.0-20170721190031-9461782956ad/go.mod h1:Uw6UezgYA44ePAFQYUehOuCzmy5zmg/+nl2ZfMWGkpA= +github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c h1:+pKlWGMw7gf6bQ+oDZB4KHQFypsfjYlq/C4rfL7D3g8= github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c/go.mod h1:Uw6UezgYA44ePAFQYUehOuCzmy5zmg/+nl2ZfMWGkpA= github.com/docker/go-metrics v0.0.0-20180209012529-399ea8c73916/go.mod h1:/u0gXw0Gay3ceNrsHubL3BtdOL2fHf93USgMTe0W5dI= github.com/docker/go-metrics v0.0.1/go.mod h1:cG1hvH2utMXtqgqqYE9plW6lDxS3/5ayHzueweSI3Vw= @@ -279,6 +288,7 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.mod h1:hliV/p42l8fGbc6Y9bQ70uLwIvmJyVE5k4iMKlh8wCQ= +github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.mod h1:KJwIaB5Mv44NWtYuAOFCVOjcI94vtpEz2JU/D2v6IjE= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= @@ -370,6 +380,7 @@ github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5x github.com/godbus/dbus/v5 v5.0.6 h1:mkgN1ofwASrYnJ5W6U/BxG15eXXXjirgZc7CLqkcaro= github.com/godbus/dbus/v5 v5.0.6/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gogo/googleapis v1.2.0/go.mod h1:Njal3psf3qN6dwBtQfUmBZh2ybovJ0tlu3o/AC7HYjU= +github.com/gogo/googleapis v1.4.0 h1:zgVt4UpGxcqVOw97aRGxT4svlcmdK35fynLNctY32zI= github.com/gogo/googleapis v1.4.0/go.mod h1:5YRNX2z1oM5gXdAkurHa942MDgEJyk02w4OecKY87+c= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= @@ -408,6 +419,8 @@ github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QD github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= @@ -503,6 +516,7 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/compress v1.11.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= github.com/klauspost/compress v1.11.13/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= +github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc= github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= @@ -545,6 +559,7 @@ github.com/mitchellh/mapstructure v1.3.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag= github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/mitchellh/osext v0.0.0-20151018003038-5e2d6d41470f/go.mod h1:OkQIRizQZAeMln+1tSwduZz7+Af5oFlKirV/MSYes2A= +github.com/moby/locker v1.0.1 h1:fOXqR41zeveg4fFODix+1Ch4mj/gT0NE1XJbp/epuBg= github.com/moby/locker v1.0.1/go.mod h1:S7SDdo5zpBK84bzzVlKr2V0hz+7x9hWbYC/kq7oQppc= github.com/moby/sys/mount v0.3.3 h1:fX1SVkXFJ47XWDoeFW4Sq7PdQJnV2QIDZAqjNqgEjUs= github.com/moby/sys/mount v0.3.3/go.mod h1:PBaEorSNTLG5t/+4EgukEQVlAvVEc6ZjTySwKdqp5K0= @@ -553,6 +568,8 @@ github.com/moby/sys/mountinfo v0.4.1/go.mod h1:rEr8tzG/lsIZHBtN/JjGG+LMYx9eXgW2J github.com/moby/sys/mountinfo v0.5.0/go.mod h1:3bMD3Rg+zkqx8MRYPi7Pyb0Ie97QEBmdxbhnCLlSvSU= github.com/moby/sys/mountinfo v0.6.2 h1:BzJjoreD5BMFNmD9Rus6gdd1pLuecOFPt8wC+Vygl78= github.com/moby/sys/mountinfo v0.6.2/go.mod h1:IJb6JQeOklcdMU9F5xQ8ZALD+CUr5VlGpwtX+VE0rpI= +github.com/moby/sys/signal v0.6.0 h1:aDpY94H8VlhTGa9sNYUFCFsMZIUh5wm0B6XkIoJj/iY= +github.com/moby/sys/signal v0.6.0/go.mod h1:GQ6ObYZfqacOwTtlXvcmh9A26dVRul/hbOZn88Kg8Tg= github.com/moby/sys/symlink v0.1.0/go.mod h1:GGDODQmbFOjFsXvfLVn3+ZRxkch54RkSiGqsZeMYowQ= github.com/moby/term v0.0.0-20200312100748-672ec06f55cd/go.mod h1:DdlQx2hp0Ss5/fLikoLlEeIYiATotOjgB//nb973jeo= github.com/moby/term v0.0.0-20221128092401-c43b287e0e0f h1:J/7hjLaHLD7epG0m6TBMGmp4NQ+ibBYLfeyJWdAIFLA= @@ -622,6 +639,8 @@ github.com/opencontainers/selinux v1.6.0/go.mod h1:VVGKuOLlE7v4PJyT6h7mNWvq1rzqi github.com/opencontainers/selinux v1.8.0/go.mod h1:RScLhm78qiWa2gbVCcGkC7tCGdgk3ogry1nUQF8Evvo= github.com/opencontainers/selinux v1.8.2/go.mod h1:MUIHuUEvKB1wtJjQdOyYRgOnLD2xAPP8dBsCoU0KuF8= github.com/opencontainers/selinux v1.10.0/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI= +github.com/opencontainers/selinux v1.10.1 h1:09LIPVRP3uuZGQvgR+SgMSNBd1Eb3vlRbGqQpoHsF8w= +github.com/opencontainers/selinux v1.10.1/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE= github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc= @@ -662,6 +681,7 @@ github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+Gx github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= github.com/prometheus/procfs v0.2.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= +github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0VU= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= @@ -899,6 +919,8 @@ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -1053,6 +1075,7 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.0.0-20160322025152-9bf6e6e569ff/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0= google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= @@ -1098,6 +1121,8 @@ google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfG google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= google.golang.org/genproto v0.0.0-20200527145253-8367513e4ece/go.mod h1:jDfRM7FcilCzHH/e9qn6dsT145K34l5v+OpcnNgKAAA= google.golang.org/genproto v0.0.0-20201110150050-8816d57aaa9a/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= +google.golang.org/genproto v0.0.0-20220502173005-c8bf987b8c21 h1:hrbNEivu7Zn1pxvHk6MBrq9iE22woVILTHqexqBxe6I= +google.golang.org/genproto v0.0.0-20220502173005-c8bf987b8c21/go.mod h1:RAyBrSAP7Fh3Nc84ghnVLDPuV51xc9agzmm4Ph6i0Q4= google.golang.org/grpc v0.0.0-20160317175043-d3ddb4469d5a/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= @@ -1116,6 +1141,9 @@ google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTp google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= google.golang.org/grpc v1.40.0/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34= +google.golang.org/grpc v1.46.0/go.mod h1:vN9eftEi1UMyUsIF80+uQXhHjbXYbm0uXoFCACuMGWk= +google.golang.org/grpc v1.47.0 h1:9n77onPX5F3qfFCqjy9dhn8PbNQsIKeVU04J9G7umt8= +google.golang.org/grpc v1.47.0/go.mod h1:vN9eftEi1UMyUsIF80+uQXhHjbXYbm0uXoFCACuMGWk= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= @@ -1129,6 +1157,8 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw= +google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/chaosmetad/pkg/crclient/client.go b/chaosmetad/pkg/crclient/client.go index 5d31b9f..35eb11d 100644 --- a/chaosmetad/pkg/crclient/client.go +++ b/chaosmetad/pkg/crclient/client.go @@ -20,6 +20,7 @@ import ( "context" "fmt" "github.com/traas-stack/chaosmeta/chaosmetad/pkg/crclient/base" + "github.com/traas-stack/chaosmeta/chaosmetad/pkg/crclient/containerd" "github.com/traas-stack/chaosmeta/chaosmetad/pkg/crclient/docker" "github.com/traas-stack/chaosmeta/chaosmetad/pkg/crclient/pouch" "github.com/traas-stack/chaosmeta/chaosmetad/pkg/log" @@ -29,9 +30,6 @@ const ( CrDocker = "docker" CrContainerd = "containerd" CrPouch = "pouch" - - // /var/run/pouchd.sock - // /var/run/containerd.sock ) type Client interface { @@ -54,7 +52,7 @@ func GetClient(ctx context.Context, cr string) (Client, error) { case CrDocker: return docker.GetClient(ctx) case CrContainerd: - return nil, fmt.Errorf("to be supported") + return containerd.GetClient(ctx) case CrPouch: return pouch.GetClient(ctx) default: diff --git a/chaosmetad/pkg/crclient/containerd/client.go b/chaosmetad/pkg/crclient/containerd/client.go new file mode 100644 index 0000000..321db55 --- /dev/null +++ b/chaosmetad/pkg/crclient/containerd/client.go @@ -0,0 +1,311 @@ +/* + * Copyright 2022-2023 Chaos Meta 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, + * 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 containerd + +import ( + "bytes" + "context" + "fmt" + "github.com/containerd/containerd" + "github.com/containerd/containerd/cio" + "github.com/google/uuid" + "github.com/opencontainers/runtime-spec/specs-go" + "github.com/shirou/gopsutil/process" + "github.com/traas-stack/chaosmeta/chaosmetad/pkg/crclient/base" + "github.com/traas-stack/chaosmeta/chaosmetad/pkg/log" + "os" + "sync" + "syscall" + "time" +) + +const ( + defaultSocket = "/run/containerd/containerd.sock" + + termExitCode = 143 + // DefaultNS TODO: need to be a command line args + defaultNS = "k8s.io" + //defaultNS = "moby" +) + +type Client struct { + client *containerd.Client +} + +var ( + clientInstance *Client + mutex sync.Mutex +) + +func GetClient(ctx context.Context) (d *Client, err error) { + defer func() { + if e := recover(); e != any(nil) { + // catch exception from create client + mutex.Unlock() + err = fmt.Errorf("catch exception: %v", e) + } + }() + + if clientInstance == nil { + mutex.Lock() + if clientInstance == nil { + log.GetLogger(ctx).Debugf("new containerd client, ns: %s, socket: %s", defaultNS, defaultSocket) + cli, err := containerd.New(defaultSocket, containerd.WithDefaultNamespace(defaultNS)) + if err != nil { + return nil, fmt.Errorf("new containerd client error: %s", err.Error()) + } + + clientInstance = &Client{ + client: cli, + } + } + mutex.Unlock() + } + + return clientInstance, nil +} + +func (d *Client) getContainerTask(ctx context.Context, containerID string) (containerd.Task, error) { + container, err := d.client.LoadContainer(ctx, containerID) + if err != nil { + return nil, fmt.Errorf("load container error: %s", err.Error()) + } + + return container.Task(ctx, nil) +} + +func (d *Client) GetAllPidList(ctx context.Context, containerID string) ([]base.SimpleProcess, error) { + task, err := d.getContainerTask(ctx, containerID) + if err != nil { + return nil, fmt.Errorf("get task of container error: %s", err.Error()) + } + + procsList, err := task.Pids(ctx) + if err != nil { + return nil, fmt.Errorf("get container's process error: %s", err.Error()) + } + + var reProList = make([]base.SimpleProcess, len(procsList)) + for i, proc := range procsList { + reProList[i].Pid = int(proc.Pid) + p, err := process.NewProcess(int32(reProList[i].Pid)) + if err != nil { + return nil, fmt.Errorf("process[%d] is not exist, error: %s", reProList[i].Pid, err.Error()) + } + + reProList[i].Cmd, err = p.Cmdline() + if err != nil { + return nil, fmt.Errorf("get cmd of process[%d] error: %s", reProList[i].Pid, err.Error()) + } + } + + return reProList, nil +} + +func (d *Client) GetPidById(ctx context.Context, containerID string) (int, error) { + task, err := d.getContainerTask(ctx, containerID) + if err != nil { + return 0, fmt.Errorf("get task of container error: %s", err.Error()) + } + + re := int(task.Pid()) + + if re <= 0 { + return -1, fmt.Errorf("no such container[%s]", containerID) + } + + return re, nil +} + +func (d *Client) Exec(ctx context.Context, containerID, cmd string) (string, error) { + task, err := d.getContainerTask(ctx, containerID) + if err != nil { + return "", fmt.Errorf("get task of container error: %s", err.Error()) + } + + pId := uuid.New().String() + var stdout, stderr bytes.Buffer + pro, err := task.Exec(ctx, pId, &specs.Process{ + Args: []string{"/bin/bash", "-c", cmd}, + Cwd: "/", + }, cio.NewCreator(append([]cio.Opt{cio.WithStreams(nil, &stdout, &stderr)})...)) + + if err != nil { + return "", fmt.Errorf("container exec error: %s", err.Error()) + } + + defer pro.Delete(ctx) + + eStatusC, err := pro.Wait(ctx) + if err != nil { + return "", fmt.Errorf("wait exec error: %s", err.Error()) + } + + if err := pro.Start(ctx); err != nil { + return "", fmt.Errorf("task start error: %s", err.Error()) + } + + eStatus := <-eStatusC + output := stdout.String() + stderr.String() + if eStatus.ExitCode() != 0 { + return output, fmt.Errorf("exit code: %d, exit error: %v, msg: %s", eStatus.ExitCode(), eStatus.Error(), output) + } + + return output, nil +} + +// KillContainerById convert to static container +func (d *Client) KillContainerById(ctx context.Context, containerID string) error { + task, err := d.getContainerTask(ctx, containerID) + if err != nil { + return fmt.Errorf("get task of container error: %s", err.Error()) + } + + return task.Kill(ctx, syscall.SIGKILL) +} + +// RmFContainerById remove container +func (d *Client) RmFContainerById(ctx context.Context, containerID string) error { + task, err := d.getContainerTask(ctx, containerID) + if err != nil { + return fmt.Errorf("get task of container error: %s", err.Error()) + } + + if err := task.Kill(ctx, syscall.SIGKILL); err != nil { + return fmt.Errorf("kill container error: %s", err.Error()) + } + + _, err = task.Delete(ctx) + return err +} + +func (d *Client) PauseContainerById(ctx context.Context, containerID string) error { + task, err := d.getContainerTask(ctx, containerID) + if err != nil { + return fmt.Errorf("get task of container error: %s", err.Error()) + } + + return task.Pause(ctx) +} + +func (d *Client) UnPauseContainerById(ctx context.Context, containerID string) error { + task, err := d.getContainerTask(ctx, containerID) + if err != nil { + return fmt.Errorf("get task of container error: %s", err.Error()) + } + + return task.Resume(ctx) +} + +func (d *Client) RestartContainerById(ctx context.Context, containerID string, timeout int64) error { + container, err := d.client.LoadContainer(ctx, containerID) + if err != nil { + return fmt.Errorf("load container error: %s", err.Error()) + } + + task, err := container.Task(ctx, nil) + if err != nil { + return fmt.Errorf("get task of container error: %s", err.Error()) + } + + if err := task.Kill(ctx, syscall.SIGKILL); err != nil { + return fmt.Errorf("signal container error: %s", err.Error()) + } + + if _, err := task.Wait(ctx); err != nil { + return fmt.Errorf("wait task error: %s", err.Error()) + } + + newTask, err := container.NewTask(ctx, cio.NullIO) + if err != nil { + return fmt.Errorf("create new task of container error: %s", err.Error()) + } + + if err := newTask.Start(ctx); err != nil { + return fmt.Errorf("start task error: %s", err.Error()) + } + + return nil +} + +func (d *Client) ListId(ctx context.Context) ([]string, error) { + containerList, err := d.client.Containers(ctx) + if err != nil { + return nil, fmt.Errorf("get container list error: %s", err.Error()) + } + + var idList = make([]string, len(containerList)) + for i, c := range containerList { + idList[i] = c.ID() + } + + return idList, nil +} + +func (d *Client) CpFile(ctx context.Context, containerID, src, dst string) error { + task, err := d.getContainerTask(ctx, containerID) + if err != nil { + return fmt.Errorf("get task of container error: %s", err.Error()) + } + + srcFile, err := os.Open(src) + if err != nil { + return fmt.Errorf("open file error: %s", err.Error()) + } + defer srcFile.Close() + + fileInfo, err := os.Stat(src) + if err != nil { + return fmt.Errorf("stat file error: %s", err.Error()) + } + + perm := fmt.Sprintf("%o", fileInfo.Mode().Perm()) + pId := uuid.New().String() + var stdout, stderr bytes.Buffer + + pro, err := task.Exec(ctx, pId, &specs.Process{ + Args: []string{"/bin/bash", "-c", fmt.Sprintf("touch %s && chmod %s %s && cat > %s", dst, perm, dst, dst)}, + Cwd: "/", + }, cio.NewCreator(append([]cio.Opt{cio.WithStreams(srcFile, &stdout, &stderr)})...)) + + if err != nil { + return fmt.Errorf("container exec error: %s", err.Error()) + } + + defer pro.Delete(ctx) + eStatusC, err := pro.Wait(ctx) + if err != nil { + return fmt.Errorf("wait exec error: %s", err.Error()) + } + + if err := pro.Start(ctx); err != nil { + return fmt.Errorf("task start error: %s", err.Error()) + } + + time.Sleep(500 * time.Millisecond) + if err := pro.Kill(ctx, syscall.SIGTERM); err != nil { + return fmt.Errorf("terminal process error: %s", err.Error()) + } + + eStatus := <-eStatusC + output := stdout.String() + stderr.String() + if eStatus.ExitCode() != termExitCode && eStatus.ExitCode() != 0 { + return fmt.Errorf("exit code: %d, exit error: %v, msg: %s", eStatus.ExitCode(), eStatus.Error(), output) + } + + return nil +} diff --git a/chaosmetad/pkg/crclient/docker/client.go b/chaosmetad/pkg/crclient/docker/client.go index 502a138..51c60ec 100644 --- a/chaosmetad/pkg/crclient/docker/client.go +++ b/chaosmetad/pkg/crclient/docker/client.go @@ -148,7 +148,7 @@ func (d *Client) Exec(ctx context.Context, containerID, cmd string) (string, err defer attach.Close() data, err := ioutil.ReadAll(attach.Reader) if err != nil { - return "", fmt.Errorf("read container exec date error: %s", err.Error()) + return "", fmt.Errorf("read container exec data error: %s", err.Error()) } logger.Debugf("container exec output: %s", string(data)) diff --git a/chaosmetad/pkg/crclient/pouch/client.go b/chaosmetad/pkg/crclient/pouch/client.go index 7242aca..3821c59 100644 --- a/chaosmetad/pkg/crclient/pouch/client.go +++ b/chaosmetad/pkg/crclient/pouch/client.go @@ -139,7 +139,7 @@ func (d *Client) Exec(ctx context.Context, containerID, cmd string) (string, err defer conn.Close() data, err := ioutil.ReadAll(r) if err != nil { - return "", fmt.Errorf("read container exec date error: %s", err.Error()) + return "", fmt.Errorf("read container exec data error: %s", err.Error()) } logger.Debugf("container exec output: %s", string(data)) diff --git a/chaosmetad/pkg/utils/filesys/filesystem.go b/chaosmetad/pkg/utils/filesys/filesystem.go index 50e804c..111ed34 100644 --- a/chaosmetad/pkg/utils/filesys/filesystem.go +++ b/chaosmetad/pkg/utils/filesys/filesystem.go @@ -35,10 +35,6 @@ func Chmod(ctx context.Context, path, perm string) error { return cmdexec.RunBashCmdWithoutOutput(ctx, fmt.Sprintf("chmod %s %s", perm, path)) } -//func GetPermCmd(path string) string { -// return "stat -c '%a' " + path -//} - func GetPerm(ctx context.Context, cr, cId string, path string) (perm string, err error) { cmd := "stat -c '%a' " + path if cr == "" { @@ -55,29 +51,7 @@ func GetPermission(path string) (string, error) { return "", fmt.Errorf("get stat of path[%s] error: %s", path, err.Error()) } - p := info.Mode().Perm().String() - if len(p) != 10 { - return "", fmt.Errorf("perm[%s] is too short", p) - } - - return fmt.Sprintf("%s%s%s", getPermNum(p[1:4]), getPermNum(p[4:7]), getPermNum(p[7:10])), nil -} - -func getPermNum(unit string) string { - var v int - for i, c := range unit { - if c != '-' { - if i == 0 { - v += 4 - } else if i == 1 { - v += 2 - } else if i == 2 { - v += 1 - } - } - } - - return strconv.Itoa(v) + return fmt.Sprintf("%o", info.Mode().Perm()), nil } func CheckDir(dir string) error {