From a7cb57d44de0f9ecf8028401dc4646db42f5e6ea Mon Sep 17 00:00:00 2001 From: alei Date: Fri, 21 Dec 2018 23:17:20 +0800 Subject: [PATCH] setup circle ci --- .circleci/config.yml | 70 ++++++++++++++++++++++++++++++++++++++++++++ .dockerignore | 1 + .gitignore | 4 ++- Dockerfile | 7 +++++ cmd/agent/main.go | 2 +- pkg/agent/config.go | 3 ++ pkg/agent/runtime.go | 16 +++++----- pkg/plugin/cmd.go | 2 +- 8 files changed, 95 insertions(+), 10 deletions(-) create mode 100644 .circleci/config.yml create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..51c3fb2 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,70 @@ +# Golang CircleCI 2.0 configuration file +# +# Check https://circleci.com/docs/2.0/language-go/ for more details +version: 2 +jobs: + build: + docker: + # specify the version + - image: circleci/golang:1.10 + + # Specify service dependencies here if necessary + # CircleCI maintains a library of pre-built images + # documented at https://circleci.com/docs/2.0/circleci-images/ + # - image: circleci/postgres:9.4 + + #### TEMPLATE_NOTE: go expects specific checkout path representing url + #### expecting it in the form of + #### /go/src/github.com/circleci/go-tool + #### /go/src/bitbucket.org/circleci/go-tool + working_directory: /go/src/github.com/aylei/kubectl-debug + environment: + - GOCACHE: "/tmp/go/cache" + - DEP_VERSION: 0.4.1 + steps: + - checkout + + - setup_remote_docker: + docker_layer_caching: true + + # try to restore cache + - restore_cache: + key: gopkg-{{ .Branch }}-{{ checksum "Gopkg.lock" }} + paths: + - /go/src/github.com/aylei/kubectl-debug/vendor + + # install dep and do dep ensure + - run: + name: dep-ensure + command: | + if [ ! -d /go/src/github.com/aylei/kubectl-debug/vendor ]; then + curl -L -s https://github.com/golang/dep/releases/download/v${DEP_VERSION}/dep-linux-amd64 -o /go/bin/dep + chmod +x /go/bin/dep + /go/bin/dep ensure + fi + + - save_cache: + key: gopkg-{{ .Branch }}-{{ checksum "Gopkg.lock" }} + paths: + - /go/src/github.com/aylei/kubectl-debug/vendor + + # cache for build + - restore_cache: + keys: + - build-cache-{{ .Branch }}-{{ .Environment.CIRCLE_PREVIOUS_BUILD_NUM }} + paths: + - /tmp/go/cache + + - run: GOOS=linux GARCH=amd64 go build -o debug-agent ./cmd/agent + + - deploy: + command: | + if [ "${CIRCLE_BRANCH}" == "master" ]; then + docker build . -t aylei/debug-agent + docker login -u ${DOCKER_USER} -p ${DOCKER_PASS} https://index.docker.io/v1/ + docker push aylei/debug-agent + + - save_cache: + key: build-cache-{{ .Branch }}-{{ .Environment.CIRCLE_BUILD_NUM }} + paths: + - /tmp/go/cache diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..8eaec52 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +!debug-agent \ No newline at end of file diff --git a/.gitignore b/.gitignore index 43a2f47..2c2f84c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ .cache *.output .swp -vendor/ \ No newline at end of file +vendor/ +debug-agent +kubectl-debug diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ab36ff3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM alpine:3.4 +RUN apk add --update --no-cache ca-certificates && rm /var/cache/apk/* + +COPY ./debug-agent /bin/debug-agent +EXPOSE 10027 + +ENTRYPOINT ["/bin/debug-agent"] diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 00c366f..8c9bf57 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -10,7 +10,7 @@ import ( func main() { var configFile string - flag.StringVar(&configFile, "config.file", "debug-agent.yaml", "Config file location.") + flag.StringVar(&configFile, "config.file", "", "Config file location.") flag.Parse() config, err := agent.LoadFile(configFile) diff --git a/pkg/agent/config.go b/pkg/agent/config.go index 35acc28..15353b4 100644 --- a/pkg/agent/config.go +++ b/pkg/agent/config.go @@ -41,6 +41,9 @@ func Load(s string) (*Config, error) { } func LoadFile(filename string) (*Config, error) { + if len(filename) < 1 { + return &DefaultConfig, nil + } c, err := ioutil.ReadFile(filename) if err != nil { return nil, err diff --git a/pkg/agent/runtime.go b/pkg/agent/runtime.go index 24675c1..8c46200 100644 --- a/pkg/agent/runtime.go +++ b/pkg/agent/runtime.go @@ -55,7 +55,7 @@ func (m *RuntimeManager) GetAttacher(image string, command []string) kubeletremo // DebugContainer executes the main debug flow func (m *RuntimeManager) DebugContainer(container, image string, command []string, stdin io.Reader, stdout, stderr io.WriteCloser, tty bool, resize <-chan remotecommand.TerminalSize) error { - log.Printf("Accept new debug reqeust:\n\t target container: %s \n\t image: %s \n\t command: %s \n", container, image, command) + log.Printf("Accept new debug reqeust:\n\t target container: %s \n\t image: %s \n\t command: %v \n", container, image, command) // step 1: pull image stdout.Write([]byte(fmt.Sprintf("pulling image %s ...\n\r", image))) @@ -145,19 +145,21 @@ func (m *RuntimeManager) CleanContainer(id string) { defer cancel() // wait the container gracefully exit statusCh, errCh := m.client.ContainerWait(ctx, id, container.WaitConditionNotRunning) + var rmErr error select { case err := <-errCh: if err != nil { log.Println("error waiting container exit, kill with --force") // timeout or error occurs, try force remove anywawy - if err := m.RmContainer(id, true); err != nil { - log.Printf("error remove container: %s\n", id) - } + rmErr = m.RmContainer(id, true) } case <-statusCh: - if err := m.RmContainer(id, false); err != nil { - log.Printf("error remove container: %s\n", id) - } + rmErr = m.RmContainer(id, false) + } + if rmErr != nil { + log.Printf("error remove container: %s \n", id) + } else { + log.Printf("Debug session end, debug container %s removed") } } diff --git a/pkg/plugin/cmd.go b/pkg/plugin/cmd.go index 9718ace..82b4f81 100644 --- a/pkg/plugin/cmd.go +++ b/pkg/plugin/cmd.go @@ -203,7 +203,7 @@ func (o *DebugOptions) Run() error { } if err := t.Safe(fn); err != nil { - fmt.Printf("error execute remote", err) + fmt.Printf("error execute remote, %v\n", err) return err }