我是 Go 和 Kubernetes 的新手。在制作自定义 K8 schduler 插件时遇到此错误。我使用此链接完成了所有操作Kube-Scheduler-Plugin,一切都已完成,但是当我运行时
./hack/update-codegen.sh
此文件未创建 DeepCopyObject,因为当我运行
make
命令时,它给出了此错误
$ make
fatal: No names found, cannot describe anything.
fatal: No names found, cannot describe anything.
fatal: No names found, cannot describe anything.
GOOS=linux CGO_ENABLED=0 go build -ldflags '-w' -o bin/controller cmd/controller/controller.go
GOOS=linux CGO_ENABLED=0 go build -ldflags '-X k8s.io/component-base/version.gitVersion=v0.0.20240430 -w' -o bin/kube-scheduler cmd/scheduler/main.go
# sigs.k8s.io/scheduler-plugins/apis/config
apis/config/register.go:47:3: cannot use &PIDControllerArgs{} (value of type *PIDControllerArgs) as "k8s.io/apimachinery/pkg/runtime".Object value in argument to scheme.AddKnownTypes: *PIDControllerArgs does not implement "k8s.io/apimachinery/pkg/runtime".Object (missing method DeepCopyObject)
make: *** [Makefile:62: build-scheduler] Error 1
我进行了故障排除,但无法找到问题并按应有的方式使用标签。任何人都可以指导进一步排除故障的正确方向。谢谢你
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type PIDControllerArgs struct {
metav1.TypeMeta
// EndpointURL is the URL to which score requests will be sent.
EndpointURL *string
// MaxIdleConnections defines the maximum number of idle connections to the server.
MaxIdleConnections *int
// IdleConnectionTimeoutSec defines the timeout for idle connections in seconds.
IdleConnectionTimeoutSec *int
// RequestTimeoutSec defines the timeout for requests in seconds.
RequestTimeoutSec *int
}
/config/v1/type.go
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +k8s:defaulter-gen=true
type PIDControllerArgs struct {
metav1.TypeMeta `json:",inline"`
// EndpointURL is the URL to which score requests will be sent.
EndpointURL *string `json:"endpointURL,omitempty"`
// MaxIdleConnections defines the maximum number of idle connections to the server.
MaxIdleConnections *int `json:"maxIdleConnections,omitempty"`
// IdleConnectionTimeoutSec defines the timeout for idle connections in seconds.
IdleConnectionTimeoutSec *int `json:"idleConnectionTimeoutSec,omitempty"`
// RequestTimeoutSec defines the timeout for requests in seconds.
RequestTimeoutSec *int `json:"requestTimeoutSec,omitempty"`
}
您想要生成代码的每个包中应该有
doc.go
文件:
.
└── pkg
└── apis
└── config
├── doc.go
├── types.go
└── v1
├── doc.go
└── types.go
doc.go
文件应包含:
// +k8s:deepcopy-gen=package,register
package {name}
其中
{name}
替换为包名称:config
和 v1
2 个
types.go
文件中的结构应包含您的注释:
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
然后
make
应该可以工作,或者,如果你已经安装了 deepcopy-gen
:
go install k8s.io/code-generator/cmd/deepcopy-gen@latest
您可以:
deepcopy-gen \
--v=9 \
./pkg/apis/config/...
这应该会在两个包中产生
generated.deepcopy.go
文件。
出现错误消息(如下)是因为结构体上没有 DeepCopyObject
方法,无法满足
runtime#Object接口。
generated.deepcopy.go
文件满足此要求。
cannot use PIDControllerArgs as "k8s.io/apimachinery/pkg/runtime".Object
deepcopy-gen
文档。