go mod download 不会下载单个库;如何 mod 在不操作整个供应商缓存的情况下导入单个库
go mod download won't download a single library; how to mod import a single library without manipulating entire vendor cache
(免责声明:对于此描述中使用的错误词语,我深表歉意。我仍在熟悉 go)
我的理解是 go
模块非常挑剔,所以这个问题涵盖了一些与 go
模块和我正在尝试下载的 prometheus
运算符库相关的主题并导入。
一个巨大的要求:我不能做大量工作go mod
download/upgrade因为下游部门在我的供应商目录中升级并且各种破坏发生在我们的代码。我正在努力避免这种情况。我只想“安装”这个库(以及任何必要的辅助工具)并完成它。
综上所述,使用 go mod
(或其他合适的工具)获取不会更新我所有供应商库的 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
的正确方法是什么?我只需要在那里导入 v1
api 以编程方式在我的集群中设置和读取 servicemonitor
s。
$ go mod download -x github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1
module github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1: not a known dependency
或
$ go get github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1
go: found github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1 in github.com/coreos/prometheus-operator v0.41.1
我的进口
import (
"context"
"encoding/base64"
"fmt"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"strings"
"text/template"
"time"
"github.com/google/uuid"
coreosv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/go-kit/kit/log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
我使用这个库的实际代码如下所示:
// define service monitor object
serviceMonitorObject := &coreosv1.ServiceMonitor{
ObjectMeta: metav1.ObjectMeta{
Name: objectMetaDataName,
// TODO: fix namespace
Namespace: "monitoring",
Labels: labels,
},
Spec: coreosv1.ServiceMonitorSpec{
Selector: metav1.LabelSelector{
MatchLabels: labels,
},
NamespaceSelector: coreosv1.NamespaceSelector{
MatchNames: []string{
"monitoring",
},
},
Endpoints: []coreosv1.Endpoint{
{
Port: "metrics",
Interval: "10s",
HonorLabels: true,
},
},
},
}
但是,我似乎不能只导入一个库。如有任何建议或帮助,我们将不胜感激。
错误 github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1 in github.com/coreos/prometheus-operator v0.41.1
表明我可能只是导入 github.com/coreos/prometheus-operator
但是当我这样做时,我的 servicemonitor
定义失败了。
编辑
我 rm
编辑了我的 go.{mod,sum}
和 vendor
目录以及 git reset --hard
我的环境,这将供应商状态恢复到我工作之前的状态。我 运行 以下内容,但如您所见,get
似乎有效,但 /project/vendor/*/
.
下不存在 coreos
目录
$ go get -x github.com/coreos/prometheus-operator/
# get https://proxy.golang.org/github.com/coreos/@v/list
# get https://proxy.golang.org/github.com/@v/list
# get https://proxy.golang.org/github.com/coreos/prometheus-operator/@v/list
# get https://proxy.golang.org/github.com/@v/list: 410 Gone (0.794s)
# get https://proxy.golang.org/github.com/coreos/@v/list: 410 Gone (0.886s)
# get https://proxy.golang.org/github.com/coreos/prometheus-operator/@v/list: 200 OK (0.897s)
go: github.com/coreos/prometheus-operator upgrade => v0.41.1
$ find vendor -name '*prometheus*'
vendor/github.com/go-kit/kit/metrics/prometheus
vendor/github.com/go-kit/kit/metrics/prometheus/prometheus.go
vendor/github.com/prometheus
vendor/github.com/prometheus/client_golang/prometheus
再次重置我的环境。执行了以下操作:
$ go mod download -x github.com/coreos/prometheus-operator/...
warning: pattern "github.com/coreos/prometheus-operator/..." matched no module dependencies
$ go mod download -x github.com/coreos/prometheus-operator/
module github.com/coreos/prometheus-operator/: not a known dependency
根据文档,以下应显示所有使用的模组(添加grep core
以获得更简短的输出):
$ go list -m all | grep core
github.com/coreos/bbolt v1.3.2
github.com/coreos/etcd v3.3.10+incompatible
github.com/coreos/go-etcd v2.0.0+incompatible
github.com/coreos/go-oidc v2.2.1+incompatible
github.com/coreos/go-semver v0.3.0
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f
github.com/coreos/prometheus-operator v0.41.1
然而,我仍然无法编译:
$ make -f build/Makefile generators ims-bin-darwin && ./ims-api --config_file=/var/tmp/jim-ims-api.yaml --kubeconfig $HOME/.kube/config --populate_demo_db=true --populate_demo_file hack/demo1.yaml --debug_logging --db_debug_logging
./build/generators/api.sh
Ensuring Bin Directory (./build/generators/../../bin) Exists...
Ensuring Destination Directory ( pkg/generated/api ) Exists...
Ensuring Destination Directory ( pkg/generated/kaas-api ) Exists...
Ensuring Swagger Asset Directory ( assets/generated/swagger ) Exists...
go: inconsistent vendoring in /Users/jimconn/projects/src/ims-api:
github.com/go-kit/kit@v0.10.0: is explicitly required in go.mod, but vendor/modules.txt indicates github.com/go-kit/kit@v0.9.0
github.com/golang/protobuf@v1.4.1: is explicitly required in go.mod, but vendor/modules.txt indicates github.com/golang/protobuf@v1.3.2
github.com/grpc-ecosystem/grpc-gateway@v1.14.6: is explicitly required in go.mod, but vendor/modules.txt indicates github.com/grpc-ecosystem/grpc-gateway@v1.9.6
github.com/pkg/errors@v0.9.1: is explicitly required in go.mod, but vendor/modules.txt indicates github.com/pkg/errors@v0.8.1
github.com/prometheus/client_golang@v1.6.0: is explicitly required in go.mod, but vendor/modules.txt indicates github.com/prometheus/client_golang@v1.1.0
golang.org/x/time@v0.0.0-20200416051211-89c76fbcd5d1: is explicitly required in go.mod, but vendor/modules.txt indicates golang.org/x/time@v0.0.0-20190308202827-9d24e82272b4
google.golang.org/genproto@v0.0.0-20200603110839-e855014d5736: is explicitly required in go.mod, but vendor/modules.txt indicates google.golang.org/genproto@v0.0.0-20190817000702-55e96fffbd48
google.golang.org/grpc@v1.29.1: is explicitly required in go.mod, but vendor/modules.txt indicates google.golang.org/grpc@v1.23.1
gopkg.in/yaml.v2@v2.3.0: is explicitly required in go.mod, but vendor/modules.txt indicates gopkg.in/yaml.v2@v2.2.8
k8s.io/api@v0.18.3: is explicitly required in go.mod, but vendor/modules.txt indicates k8s.io/api@v0.17.2
k8s.io/apimachinery@v0.18.3: is explicitly required in go.mod, but vendor/modules.txt indicates k8s.io/apimachinery@v0.17.2
k8s.io/client-go@v0.18.3: is explicitly required in go.mod, but vendor/modules.txt indicates k8s.io/client-go@v0.17.2
run 'go mod vendor' to sync, or use -mod=mod or -mod=readonly to ignore the vendor directory
make: *** [generators] Error 1
我 运行 go mod vendor
并且我回到了我的整个供应商树都已更改的情况:
$ g st
On branch expose-bm-prom
Your branch is up to date with 'origin/expose-bm-prom'.
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: assets/generated/swagger/api.swagger.json
modified: go.mod
modified: go.sum
modified: pkg/generated/api/api.pb.go
modified: pkg/generated/api/api.pb.gw.go
...
modified: vendor/sigs.k8s.io/yaml/README.md
modified: vendor/sigs.k8s.io/yaml/yaml.go
Untracked files:
(use "git add <file>..." to include in what will be committed)
vendor/github.com/cespare/
vendor/github.com/coreos/
vendor/github.com/golang/protobuf/internal/
vendor/github.com/golang/protobuf/jsonpb/decode.go
vendor/github.com/golang/protobuf/jsonpb/encode.go
...
vendor/k8s.io/client-go/util/workqueue/
vendor/sigs.k8s.io/structured-merge-diff/
vendor/sigs.k8s.io/yaml/go.mod
vendor/sigs.k8s.io/yaml/go.sum
no changes added to commit (use "git add" and/or "git commit -a")
$ g st | grep -c vendor
942 # files changed
这只是世界的现状吗?如果不处理对整个供应商树的大量更改,我是否无法导入此库,这完全破坏了我的代码!这太荒谬了!
go get -d github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1
是添加对提供该包的模块的最新版本的依赖的正确方法。
当go get
添加模块依赖时,它会根据需要升级其他依赖以满足新模块的要求。这可能就是为什么您在 运行 go mod vendor
.
时会看到很多变化
如果您想尽量减少这些更改,您可能需要明确选择一个较旧的版本(这样它就会有较旧的要求):
go get -d github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1@$OLD_VERSION
(免责声明:对于此描述中使用的错误词语,我深表歉意。我仍在熟悉 go)
我的理解是 go
模块非常挑剔,所以这个问题涵盖了一些与 go
模块和我正在尝试下载的 prometheus
运算符库相关的主题并导入。
一个巨大的要求:我不能做大量工作go mod
download/upgrade因为下游部门在我的供应商目录中升级并且各种破坏发生在我们的代码。我正在努力避免这种情况。我只想“安装”这个库(以及任何必要的辅助工具)并完成它。
综上所述,使用 go mod
(或其他合适的工具)获取不会更新我所有供应商库的 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
的正确方法是什么?我只需要在那里导入 v1
api 以编程方式在我的集群中设置和读取 servicemonitor
s。
$ go mod download -x github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1
module github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1: not a known dependency
或
$ go get github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1
go: found github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1 in github.com/coreos/prometheus-operator v0.41.1
我的进口
import (
"context"
"encoding/base64"
"fmt"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"strings"
"text/template"
"time"
"github.com/google/uuid"
coreosv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/go-kit/kit/log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
我使用这个库的实际代码如下所示:
// define service monitor object
serviceMonitorObject := &coreosv1.ServiceMonitor{
ObjectMeta: metav1.ObjectMeta{
Name: objectMetaDataName,
// TODO: fix namespace
Namespace: "monitoring",
Labels: labels,
},
Spec: coreosv1.ServiceMonitorSpec{
Selector: metav1.LabelSelector{
MatchLabels: labels,
},
NamespaceSelector: coreosv1.NamespaceSelector{
MatchNames: []string{
"monitoring",
},
},
Endpoints: []coreosv1.Endpoint{
{
Port: "metrics",
Interval: "10s",
HonorLabels: true,
},
},
},
}
但是,我似乎不能只导入一个库。如有任何建议或帮助,我们将不胜感激。
错误 github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1 in github.com/coreos/prometheus-operator v0.41.1
表明我可能只是导入 github.com/coreos/prometheus-operator
但是当我这样做时,我的 servicemonitor
定义失败了。
编辑
我 rm
编辑了我的 go.{mod,sum}
和 vendor
目录以及 git reset --hard
我的环境,这将供应商状态恢复到我工作之前的状态。我 运行 以下内容,但如您所见,get
似乎有效,但 /project/vendor/*/
.
coreos
目录
$ go get -x github.com/coreos/prometheus-operator/
# get https://proxy.golang.org/github.com/coreos/@v/list
# get https://proxy.golang.org/github.com/@v/list
# get https://proxy.golang.org/github.com/coreos/prometheus-operator/@v/list
# get https://proxy.golang.org/github.com/@v/list: 410 Gone (0.794s)
# get https://proxy.golang.org/github.com/coreos/@v/list: 410 Gone (0.886s)
# get https://proxy.golang.org/github.com/coreos/prometheus-operator/@v/list: 200 OK (0.897s)
go: github.com/coreos/prometheus-operator upgrade => v0.41.1
$ find vendor -name '*prometheus*'
vendor/github.com/go-kit/kit/metrics/prometheus
vendor/github.com/go-kit/kit/metrics/prometheus/prometheus.go
vendor/github.com/prometheus
vendor/github.com/prometheus/client_golang/prometheus
再次重置我的环境。执行了以下操作:
$ go mod download -x github.com/coreos/prometheus-operator/...
warning: pattern "github.com/coreos/prometheus-operator/..." matched no module dependencies
$ go mod download -x github.com/coreos/prometheus-operator/
module github.com/coreos/prometheus-operator/: not a known dependency
根据文档,以下应显示所有使用的模组(添加grep core
以获得更简短的输出):
$ go list -m all | grep core
github.com/coreos/bbolt v1.3.2
github.com/coreos/etcd v3.3.10+incompatible
github.com/coreos/go-etcd v2.0.0+incompatible
github.com/coreos/go-oidc v2.2.1+incompatible
github.com/coreos/go-semver v0.3.0
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f
github.com/coreos/prometheus-operator v0.41.1
然而,我仍然无法编译:
$ make -f build/Makefile generators ims-bin-darwin && ./ims-api --config_file=/var/tmp/jim-ims-api.yaml --kubeconfig $HOME/.kube/config --populate_demo_db=true --populate_demo_file hack/demo1.yaml --debug_logging --db_debug_logging
./build/generators/api.sh
Ensuring Bin Directory (./build/generators/../../bin) Exists...
Ensuring Destination Directory ( pkg/generated/api ) Exists...
Ensuring Destination Directory ( pkg/generated/kaas-api ) Exists...
Ensuring Swagger Asset Directory ( assets/generated/swagger ) Exists...
go: inconsistent vendoring in /Users/jimconn/projects/src/ims-api:
github.com/go-kit/kit@v0.10.0: is explicitly required in go.mod, but vendor/modules.txt indicates github.com/go-kit/kit@v0.9.0
github.com/golang/protobuf@v1.4.1: is explicitly required in go.mod, but vendor/modules.txt indicates github.com/golang/protobuf@v1.3.2
github.com/grpc-ecosystem/grpc-gateway@v1.14.6: is explicitly required in go.mod, but vendor/modules.txt indicates github.com/grpc-ecosystem/grpc-gateway@v1.9.6
github.com/pkg/errors@v0.9.1: is explicitly required in go.mod, but vendor/modules.txt indicates github.com/pkg/errors@v0.8.1
github.com/prometheus/client_golang@v1.6.0: is explicitly required in go.mod, but vendor/modules.txt indicates github.com/prometheus/client_golang@v1.1.0
golang.org/x/time@v0.0.0-20200416051211-89c76fbcd5d1: is explicitly required in go.mod, but vendor/modules.txt indicates golang.org/x/time@v0.0.0-20190308202827-9d24e82272b4
google.golang.org/genproto@v0.0.0-20200603110839-e855014d5736: is explicitly required in go.mod, but vendor/modules.txt indicates google.golang.org/genproto@v0.0.0-20190817000702-55e96fffbd48
google.golang.org/grpc@v1.29.1: is explicitly required in go.mod, but vendor/modules.txt indicates google.golang.org/grpc@v1.23.1
gopkg.in/yaml.v2@v2.3.0: is explicitly required in go.mod, but vendor/modules.txt indicates gopkg.in/yaml.v2@v2.2.8
k8s.io/api@v0.18.3: is explicitly required in go.mod, but vendor/modules.txt indicates k8s.io/api@v0.17.2
k8s.io/apimachinery@v0.18.3: is explicitly required in go.mod, but vendor/modules.txt indicates k8s.io/apimachinery@v0.17.2
k8s.io/client-go@v0.18.3: is explicitly required in go.mod, but vendor/modules.txt indicates k8s.io/client-go@v0.17.2
run 'go mod vendor' to sync, or use -mod=mod or -mod=readonly to ignore the vendor directory
make: *** [generators] Error 1
我 运行 go mod vendor
并且我回到了我的整个供应商树都已更改的情况:
$ g st
On branch expose-bm-prom
Your branch is up to date with 'origin/expose-bm-prom'.
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: assets/generated/swagger/api.swagger.json
modified: go.mod
modified: go.sum
modified: pkg/generated/api/api.pb.go
modified: pkg/generated/api/api.pb.gw.go
...
modified: vendor/sigs.k8s.io/yaml/README.md
modified: vendor/sigs.k8s.io/yaml/yaml.go
Untracked files:
(use "git add <file>..." to include in what will be committed)
vendor/github.com/cespare/
vendor/github.com/coreos/
vendor/github.com/golang/protobuf/internal/
vendor/github.com/golang/protobuf/jsonpb/decode.go
vendor/github.com/golang/protobuf/jsonpb/encode.go
...
vendor/k8s.io/client-go/util/workqueue/
vendor/sigs.k8s.io/structured-merge-diff/
vendor/sigs.k8s.io/yaml/go.mod
vendor/sigs.k8s.io/yaml/go.sum
no changes added to commit (use "git add" and/or "git commit -a")
$ g st | grep -c vendor
942 # files changed
这只是世界的现状吗?如果不处理对整个供应商树的大量更改,我是否无法导入此库,这完全破坏了我的代码!这太荒谬了!
go get -d github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1
是添加对提供该包的模块的最新版本的依赖的正确方法。
当go get
添加模块依赖时,它会根据需要升级其他依赖以满足新模块的要求。这可能就是为什么您在 运行 go mod vendor
.
如果您想尽量减少这些更改,您可能需要明确选择一个较旧的版本(这样它就会有较旧的要求):
go get -d github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1@$OLD_VERSION