@@ -85,7 +85,59 @@ Kubebuilder 内置插件通过模板生成代码文件。例如 `go/v4` 在初
8585示例:` go/v4 ` 通过实现 machinery 接口对象来脚手架生成 ` go.mod ` ,其原始模板在 ` Template.SetTemplateDefaults ` 的 ` TemplateBody ` 字段中定义:
8686
8787``` go
88- {{#include ./../../../../../pkg/plugins/golang/v4/scaffolds/internal/templates/gomod.go }}
88+ /*
89+ Copyright 2022 The Kubernetes Authors.
90+
91+ Licensed under the Apache License, Version 2.0 (the "License");
92+ you may not use this file except in compliance with the License.
93+ You may obtain a copy of the License at
94+
95+ http://www.apache.org/licenses/LICENSE-2.0
96+
97+ Unless required by applicable law or agreed to in writing, software
98+ distributed under the License is distributed on an "AS IS" BASIS,
99+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
100+ See the License for the specific language governing permissions and
101+ limitations under the License.
102+ */
103+
104+ package templates
105+
106+ import (
107+ " sigs.k8s.io/kubebuilder/v4/pkg/machinery"
108+ )
109+
110+ var _ machinery.Template = &GoMod{}
111+
112+ // GoMod scaffolds a file that defines the project dependencies
113+ type GoMod struct {
114+ machinery.TemplateMixin
115+ machinery.RepositoryMixin
116+
117+ ControllerRuntimeVersion string
118+ }
119+
120+ // SetTemplateDefaults implements machinery.Template
121+ func (f *GoMod ) SetTemplateDefaults () error {
122+ if f.Path == " " {
123+ f.Path = " go.mod"
124+ }
125+
126+ f.TemplateBody = goModTemplate
127+
128+ f.IfExistsAction = machinery.OverwriteFile
129+
130+ return nil
131+ }
132+
133+ const goModTemplate = ` module {{ .Repo }}
134+
135+ go 1.24.5
136+
137+ require (
138+ sigs.k8s.io/controller-runtime {{ .ControllerRuntimeVersion }}
139+ )
140+ `
89141```
90142
91143随后,该对象会被传入脚手架执行:
0 commit comments