天天看點

使用VS code 調試 Kubernetes 源代碼

請參照 我的另一篇文章 使用 VS Code 閱讀 Kubernetes 代碼 配置好環境

打開Shell 并且設定 GOPATH 和 GOROOT

export GOPATH=~/k8s
export GOROOT=/usr/local/go
           

安裝Delve和其他Go 工具

go get -u -v github.com/derekparker/delve/cmd/dlv
go get -u -v github.com/ramya-rao-a/go-outline  
go get -u -v github.com/acroca/go-symbols   
go get -u -v github.com/nsf/gocode  
go get -u -v github.com/rogpeppe/godef  
go get -u -v golang.org/x/tools/cmd/godoc   
go get -u -v github.com/zmb3/gogetdoc   
go get -u -v github.com/golang/lint/golint
go get -u -v github.com/fatih/gomodifytags
go get -u -v github.com/uudashr/gopkgs/cmd/gopkgs
go get -u -v golang.org/x/tools/cmd/gorename
go get -u -v sourcegraph.com/sqs/goreturns
go get -u -v github.com/cweill/gotests/...
go get -u -v golang.org/x/tools/cmd/guru
go get -u -v github.com/josharian/impl
go get -u -v github.com/haya14busa/goplay/cmd/goplay
           

使用 VS Code打開代碼

code $GOPATH
           

設定斷點

~/k8s/src/k8s.io/kubernetes/cmd/hyperkube/main.go 

114行

編輯 launch.json, 

下面的示例開始調試Kubelet, 請替換 ReplaceWithYourName 為系統目前的使用者名

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "exec",
            "remotePath": "",
            "port": 2345,
            "host": "127.0.0.1",
            "program": "/home/ReplaceWithYourName/k8s/src/k8s.io/kubernetes/cmd/hyperkube/hyperkube",
            "env": {},
            "args": ["kubelet",
            "--cluster-dns=10.96.0.10",
            "--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf",
            "--kubeconfig=/etc/kubernetes/kubelet.conf",
            "--v=2"],
            "showLog": true
        }
    ]
}
           

F5 開始調試

使用VS code 調試 Kubernetes 源代碼

繼續閱讀