Skip to content

Commit a1ae5ed

Browse files
committed
handle feature gates in docs scaffolds
1 parent 1e2aca0 commit a1ae5ed

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

hack/docs/internal/cronjob-tutorial/generate_cronjob.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,28 @@ type CronJob struct {`+`
226226
*/`)
227227
hackutils.CheckError("fixing schema for cronjob_types.go", err)
228228

229-
// fix lint
229+
// fix lint - handle the pattern with or without feature-gated fields
230230
err = pluginutil.ReplaceInFile(
231231
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_types.go"),
232232
`/
233233
234+
235+
// Example of a feature-gated field:
236+
// Bar is an experimental field that requires the "experimental-bar" feature gate to be enabled
237+
// TODO: When controller-tools supports feature gates (issue #1238), use:
238+
// +kubebuilder:feature-gate=experimental-bar
239+
// +feature-gate experimental-bar
240+
// +optional
241+
Bar *string `+"`"+`json:"bar,omitempty"`+"`"+`
242+
}`, "/")
243+
if err != nil {
244+
// Fallback to the original pattern if the feature-gated field is not present
245+
err = pluginutil.ReplaceInFile(
246+
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_types.go"),
247+
`/
248+
234249
}`, "/")
250+
}
235251
hackutils.CheckError("fixing cronjob_types.go end of status", err)
236252
}
237253

hack/docs/internal/multiversion-tutorial/generate_multiversion.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,11 +735,25 @@ We'll leave our spec largely unchanged, except to change the schedule field to a
735735

736736
err = pluginutil.ReplaceInFile(
737737
filepath.Join(sp.ctx.Dir, path),
738-
`// foo is an example field of CronJob. Edit cronjob_types.go to remove/update
738+
`// Example of a feature-gated field:
739+
// Bar is an experimental field that requires the "experimental-bar" feature gate to be enabled
740+
// TODO: When controller-tools supports feature gates (issue #1238), use:
741+
// +kubebuilder:feature-gate=experimental-bar
742+
// +feature-gate experimental-bar
739743
// +optional
740-
Foo *string `+"`json:\"foo,omitempty\"`",
744+
Bar *string `+"`"+`json:"bar,omitempty"`+"`",
741745
cronjobSpecMore,
742746
)
747+
if err != nil {
748+
// Fallback to the original pattern if the feature-gated field is not present
749+
err = pluginutil.ReplaceInFile(
750+
filepath.Join(sp.ctx.Dir, path),
751+
`// foo is an example field of CronJob. Edit cronjob_types.go to remove/update
752+
// +optional
753+
Foo *string `+"`json:\"foo,omitempty\"`",
754+
cronjobSpecMore,
755+
)
756+
}
743757
hackutils.CheckError("replace Foo with cronjob spec fields", err)
744758

745759
err = pluginutil.ReplaceInFile(

test/e2e/v4/featuregates_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ var _ = Describe("Feature Gates", func() {
8585
Expect(string(mainContent)).To(ContainSubstring("featuregates"))
8686

8787
// Verify API types have feature gate example
88-
typesFile := filepath.Join(ctx.Dir, "api", ctx.Version, ctx.Kind+"_types.go")
88+
typesFile := filepath.Join(ctx.Dir, "api", ctx.Version, strings.ToLower(ctx.Kind)+"_types.go")
8989
Expect(typesFile).To(BeAnExistingFile())
9090
typesContent, err := os.ReadFile(typesFile)
9191
Expect(err).NotTo(HaveOccurred())
@@ -109,7 +109,7 @@ var _ = Describe("Feature Gates", func() {
109109
Expect(err).NotTo(HaveOccurred())
110110

111111
// Add custom feature gates to the API types
112-
typesFile := filepath.Join(ctx.Dir, "api", ctx.Version, ctx.Kind+"_types.go")
112+
typesFile := filepath.Join(ctx.Dir, "api", ctx.Version, strings.ToLower(ctx.Kind)+"_types.go")
113113
typesContent, err := os.ReadFile(typesFile)
114114
Expect(err).NotTo(HaveOccurred())
115115

0 commit comments

Comments
 (0)