当我转向使用 go mod 时,陷入了依赖性问题

    • いままではgo modは使わずにGOPATHを利用する普通のパッケージ管理方式でやっていた

 

    ビルドしようとしたときに最新のパッケージをダウンロードするからか、どこかのタイミングでビルドが失敗するようになった

要做的事情 zuò de

    go modへ移行して、ビルド成功していた時のバージョンを指定する

样本

package main

import (
    "context"
    "encoding/json"
    "fmt"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/tools/clientcmd"
    "log"
    "os"
    "path/filepath"
)

func main() {
    // Kubeconfig縺ョ繝輔ぃ繧、繝ォ繝代せ繧呈欠螳・    kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config")
    config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
    if err != nil {
        log.Fatal(err)
    }

    // Kubeconfig繧定ェュ縺ソ霎シ繧€
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        log.Fatal(err)
    }

    // Pod荳€隕ァ繧貞他縺ウ蜃コ縺・    namespace := "default"
    pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{
        LabelSelector: "app=vamdemic111aaa-app",
    })
    if err != nil {
        log.Fatalln("failed to get pods:", err)
    }

    m := map[string]string{}

    for _, pod := range pods.Items {
       m[pod.Name] = string(pod.Status.Phase)
    }

    data, _ := json.Marshal(m)
    fmt.Printf(string(data))
}

整理 is to organize or tidy up.

go mod tidy

错误内容 cuò

go: finding k8s.io/utils latest
go: finding golang.org/x/time latest
main imports
        k8s.io/client-go/kubernetes imports
        k8s.io/client-go/discovery imports
        github.com/googleapis/gnostic/OpenAPIv2: module github.com/googleapis/gnostic@latest found (v0.5.5), but does not contain package github.com/googleapis/gnostic/OpenAPIv2
main imports
        k8s.io/client-go/kubernetes imports
        k8s.io/client-go/kubernetes/typed/auditregistration/v1alpha1 imports
        k8s.io/api/auditregistration/v1alpha1: module k8s.io/api@latest found (v0.21.1), but does not contain package k8s.io/api/auditregistration/v1alpha1
main imports
        k8s.io/client-go/kubernetes imports
        k8s.io/client-go/kubernetes/typed/batch/v2alpha1 imports
        k8s.io/api/batch/v2alpha1: module k8s.io/api@latest found (v0.21.1), but does not contain package k8s.io/api/batch/v2alpha1
main imports
        k8s.io/client-go/kubernetes imports
        k8s.io/client-go/kubernetes/typed/settings/v1alpha1 imports
        k8s.io/api/settings/v1alpha1: module k8s.io/api@latest found (v0.21.1), but does not contain package k8s.io/api/settings/v1alpha1

逐个击碎

k8s.io/api 可以被改写为「Kubernetes 官方 API」。

main imports
        k8s.io/client-go/kubernetes imports
        k8s.io/client-go/kubernetes/typed/auditregistration/v1alpha1 imports
        k8s.io/api/auditregistration/v1alpha1: module k8s.io/api@latest found (v0.21.1), but does not contain package k8s.io/api/auditregistration/v1alpha1

版本列表

image.png

特定/指定的/确定的

go mod edit  -require  k8s.io/api/auditregistration@v0.20.2

尝试

已消失

yuta:/tmp/go $ go mod tidy
go: downloading k8s.io/api v0.20.2
go: extracting k8s.io/api v0.20.2
main imports
        k8s.io/client-go/kubernetes imports
        k8s.io/client-go/discovery imports
        github.com/googleapis/gnostic/OpenAPIv2: module github.com/googleapis/gnostic@latest found (v0.5.5), but does not contain package github.com/googleapis/gnostic/OpenAPIv2
main imports
        k8s.io/client-go/kubernetes imports
        k8s.io/client-go/kubernetes/typed/auditregistration/v1alpha1 imports
        k8s.io/api/auditregistration/v1alpha1: module k8s.io/api@latest found (v0.21.1), but does not contain package k8s.io/api/auditregistration/v1alpha1
main imports
        k8s.io/client-go/kubernetes imports
        k8s.io/client-go/kubernetes/typed/settings/v1alpha1 imports
        k8s.io/api/settings/v1alpha1: module k8s.io/api@latest found (v0.21.1), but does not contain package k8s.io/api/settings/v1alpha1
go mod edit  -require  k8s.io/apimachinery@v0.20.2

我一直在做各种事情,但发现有人正在做同样的事情!

建议使用k8s.io/client-go@v0.20.4进行安装。

bannerAds