使用 VS Code 阅读 Kubernetes 代码

时间:2024-03-26 18:05:41

问题:无法跳转到定义在其他文件的方法 (如下图所示)

使用 VS Code 阅读 Kubernetes 代码

使用 VS Code 阅读 Kubernetes 代码

解决方法:

  1. 打开 shell
  2. 安装 GO
    sudo apt-get update
    sudo apt install -y wget
    sudo wget https://storage.googleapis.com/golang/go1.9.2.linux-amd64.tar.gz
    sudo tar -xvf go1.9.2.linux-amd64.tar.gz
    sudo mv go /usr/local
  3. GOPATH 和 GOROOT
    export GOPATH=~/k8s
    export GOROOT=/usr/local/go
  4. 下载代码: sudo git clone https://github.com/kubernetes/kubernetes $GOPATH
  5. 安装 VS Code
  6. curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
    sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
    sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main" > /etc/apt/sources.list.d/vscode.list'
    sudo apt-get update
    sudo apt-get install code
    sudo -H -u $LOCALUSER bash -c "code --install-extension lukehoban.Go"
    安装 Docker
    sudo apt-get update
    sudo apt-get install -y \
        apt-transport-https \
        ca-certificates \
        curl \
        software-properties-common
    sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    sudo add-apt-repository \
       "deb https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \
       $(lsb_release -cs) \
       stable"
    sudo apt-get update && sudo apt-get install -y docker-ce=$(apt-cache madison docker-ce | grep 17.12 | head -1 | awk '{print $3}')

  7. 设置Kubernetes路径
     cd $GOPATH
     ln -sf  staging/src src
     cd $GOPATH/staging/src/k8s.io
     ln -sf $GOPATH kubernetes
  8. 编译
    cd $GOPATH
    build/run.sh make what=cmd/hyperkube
  9. 直接编译Hyperkube
    cd $GOPATH/src/k8s.io/kubernetes/cmd/hyperkube
    go build -gcflags "-l -N"
  10. 打开Kubernetes 代码
    code $GOPATH
  11. 找到文件 /home/honcao/k8s/src/k8s.io/kubernetes/cmd/hyperkube/main.go

    使用 VS Code 阅读 Kubernetes 代码