相关推荐recommended
go mod出现zip: not a valid zip file的解决办法
作者:mmseoamin日期:2024-04-27

最近在准备学习下kubernetes operator的开发,在通过kubebuilder 脚手架创建operator项目时,出现报错:

Error: failed to initialize project: unable to scaffold with "base.go.kubebuilder.io/v4": exit status 1

详情如下:

% kubebuilder init --domain demo.io --plugins=go/v4
INFO Writing kustomize manifests for you to edit...
INFO Writing scaffold for you to edit...
INFO Get controller runtime:
$ go get sigs.k8s.io/controller-runtime@v0.17.0
go: downloading github.com/golang/protobuf v1.5.3
go: downloading github.com/prometheus/procfs v0.12.0
go: sigs.k8s.io/controller-runtime imports
	sigs.k8s.io/controller-runtime/pkg/builder imports
	sigs.k8s.io/controller-runtime/pkg/client/apiutil imports
	k8s.io/client-go/discovery imports
	github.com/golang/protobuf/proto: zip: not a valid zip file
go: sigs.k8s.io/controller-runtime imports
	sigs.k8s.io/controller-runtime/pkg/builder imports
	sigs.k8s.io/controller-runtime/pkg/webhook/admission imports
	sigs.k8s.io/controller-runtime/pkg/webhook/internal/metrics imports
	github.com/prometheus/client_golang/prometheus imports
	github.com/prometheus/procfs: zip: not a valid zip file
go: sigs.k8s.io/controller-runtime imports
	sigs.k8s.io/controller-runtime/pkg/builder imports
	sigs.k8s.io/controller-runtime/pkg/client/apiutil imports
	k8s.io/client-go/discovery imports
	github.com/google/gnostic-models/openapiv2 imports
	github.com/google/gnostic-models/compiler imports
	github.com/golang/protobuf/ptypes/any: zip: not a valid zip file
go: sigs.k8s.io/controller-runtime imports
	sigs.k8s.io/controller-runtime/pkg/builder imports
	sigs.k8s.io/controller-runtime/pkg/client/apiutil imports
	k8s.io/client-go/discovery imports
	github.com/google/gnostic-models/openapiv2 imports
	github.com/google/gnostic-models/compiler imports
	github.com/google/gnostic-models/extensions imports
	github.com/golang/protobuf/ptypes: zip: not a valid zip file
Error: failed to initialize project: unable to scaffold with "base.go.kubebuilder.io/v4": exit status 1
Usage:
  kubebuilder init [flags]
Examples:
  # Initialize a new project with your domain and name in copyright
  kubebuilder init --plugins go/v4 --domain example.org --owner "Your name"
  # Initialize a new project defining a specific project version
  kubebuilder init --plugins go/v4 --project-version 3
Flags:
      --domain string            domain for groups (default "my.domain")
      --fetch-deps               ensure dependencies are downloaded (default true)
  -h, --help                     help for init
      --license string           license to use to boilerplate, may be one of 'apache2', 'none' (default "apache2")
      --owner string             owner to add to the copyright
      --project-name string      name of this project
      --project-version string   project version (default "3")
      --repo string              name to use for go module (e.g., github.com/user/repo), defaults to the go package of the current working directory.
      --skip-go-version-check    if specified, skip checking the Go version
Global Flags:
      --plugins strings   plugin keys to be used for this subcommand execution
FATA failed to initialize project: unable to scaffold with "base.go.kubebuilder.io/v4": exit status 1

原因和解决方法

问题原因:go mod代理服务器下载出错;导致在下载时出现": zip: not a valid zip file"

通过go env GOPROXY命令查看代理服务器:

% go env GOPROXY
https://mirrors.aliyun.com/goproxy/,https://goproxy.cn

优先使用aliyun的代理服务,调整直接使用https://goproxy.cn

% go env -w GOPROXY=https://goproxy.cn,direct

重新执行kubebuilder init 正常完成

% kubebuilder init --domain demo.io --plugins=go/v4
INFO Writing kustomize manifests for you to edit...
INFO Writing scaffold for you to edit...
INFO Get controller runtime:
$ go get sigs.k8s.io/controller-runtime@v0.17.0
INFO Update dependencies:
$ go mod tidy
go: downloading github.com/onsi/ginkgo/v2 v2.14.0
go: downloading github.com/onsi/gomega v1.30.0
go: downloading github.com/stretchr/testify v1.8.4
go: downloading github.com/go-logr/zapr v1.3.0
go: downloading go.uber.org/zap v1.26.0
go: downloading go.uber.org/goleak v1.3.0
go: downloading github.com/rogpeppe/go-internal v1.10.0
go: downloading go.uber.org/multierr v1.11.0
go: downloading github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572
go: downloading golang.org/x/tools v0.16.1
Next: define a resource with:
$ kubebuilder create api