K8s API 依赖导入链的版本问题

建议点击 查看原文 查看最新内容。

原文链接: https://typonotes.com/posts/2024/03/25/k8s-api-import-chain-problem/

1
2
3
4
5
6
7
8
// go.mod

// 异常的版本依赖指定和引用
require (
	k8s.io/api v0.28.8
	k8s.io/client-go v0.28.8
	k8s.io/apimachinery v0.28.8
)

由于依赖链的中的版本问题, 导致 k8s.io 被默认指向了当前最新版本 v0.29.3。 但是出现了一点点小问题, 导致 go mod tidy 失败。

go: github.com/example/kubez/pkg/healthy 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.29.3), but does not contain package k8s.io/api/auditregistration/v1alpha1
go: github.com/example/kubez/pkg/healthy 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.29.3), but does not contain package k8s.io/api/batch/v2alpha1
go: github.com/example/kubez/pkg/healthy 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.29.3), but does not contain package k8s.io/api/settings/v1alpha1
go: github.com/example/kubez/pkg/healthy imports
        k8s.io/client-go/kubernetes imports
        k8s.io/client-go/util/flowcontrol imports
        k8s.io/apimachinery/pkg/util/clock: module k8s.io/apimachinery@latest found (v0.29.3), but does not contain package k8s.io/apimachinery/pkg/util/clock

解决方法使用 replace 强制替换所有版本

1
2
3
4
5
6
7
8
9
// go.mod

// 正常的强制版本依赖指定和引用
// https://github.com/kubernetes/client-go/issues/874
replace k8s.io/client-go => k8s.io/client-go v0.28.8

replace k8s.io/apimachinery => k8s.io/apimachinery v0.28.8

replace k8s.io/api => k8s.io/api v0.28.8