Skip to content

Commit 3f4d5f4

Browse files
authored
Merge pull request #4572 from sarthaksarthak9/alpha
🐛 Allow 'kubebuilder alpha generate' rescaffolds work with no longer supported/available plugins ( go/v3 and go/v2 )
2 parents 4559dd5 + 258246d commit 3f4d5f4

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Test Alpha Generate
2+
3+
on:
4+
push:
5+
paths:
6+
- 'pkg/cli/alpha/**'
7+
- '.github/workflows/test-alpha-generate.yml'
8+
pull_request:
9+
paths:
10+
- 'pkg/cli/alpha/**'
11+
- '.github/workflows/test-alpha-generate.yml'
12+
13+
jobs:
14+
unsupported:
15+
runs-on: ubuntu-latest
16+
strategy:
17+
fail-fast: true
18+
if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository
19+
steps:
20+
- name: Checkout repository
21+
uses: actions/checkout@v4
22+
23+
- name: Setup Go
24+
uses: actions/setup-go@v5
25+
with:
26+
go-version-file: go.mod
27+
28+
- name: Install dependencies and generate binary
29+
run: make install
30+
31+
- name: Navigate to testdata/project-v4
32+
run: cd testdata/project-v4
33+
34+
- name: Update PROJECT file
35+
run: |
36+
sed -i 's#go.kubebuilder.io/v4#go.kubebuilder.io/v3#g' testdata/project-v4/PROJECT
37+
38+
- name: Run kubebuilder alpha generate
39+
run: |
40+
cd testdata/project-v4 && kubebuilder alpha generate

pkg/cli/alpha/internal/generate.go

+18
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,24 @@ func getInputPath(inputPath string) (string, error) {
256256
func getInitArgs(store store.Store) []string {
257257
var args []string
258258
plugins := store.Config().GetPluginChain()
259+
260+
// Define outdated plugin versions that need replacement
261+
outdatedPlugins := map[string]string{
262+
"go.kubebuilder.io/v3": "go.kubebuilder.io/v4",
263+
"go.kubebuilder.io/v3-alpha": "go.kubebuilder.io/v4",
264+
"go.kubebuilder.io/v2": "go.kubebuilder.io/v4",
265+
}
266+
267+
// Replace outdated plugins and exit after the first replacement
268+
for i, plugin := range plugins {
269+
if newPlugin, exists := outdatedPlugins[plugin]; exists {
270+
log.Warnf("We checked that your PROJECT file is configured with the layout '%s', which is no longer supported.\n"+
271+
"However, we will try our best to re-generate the project using '%s'.", plugin, newPlugin)
272+
plugins[i] = newPlugin
273+
break
274+
}
275+
}
276+
259277
if len(plugins) > 0 {
260278
args = append(args, "--plugins", strings.Join(plugins, ","))
261279
}

pkg/cli/cli.go

+8
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ func (c *CLI) buildCmd() error {
147147

148148
var uve config.UnsupportedVersionError
149149

150+
// Workaround for kubebuilder alpha generate
151+
if len(os.Args) > 2 && os.Args[1] == "alpha" && os.Args[2] == "generate" {
152+
err := updateProjectFileForAlphaGenerate()
153+
if err != nil {
154+
return fmt.Errorf("failed to update PROJECT file: %w", err)
155+
}
156+
}
157+
150158
// Get project version and plugin keys.
151159
switch err := c.getInfo(); {
152160
case err == nil:

pkg/cli/cmd_helpers.go

+39
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import (
2020
"errors"
2121
"fmt"
2222
"os"
23+
"strings"
2324

25+
log "github.com/sirupsen/logrus"
2426
"github.com/spf13/cobra"
2527

2628
"sigs.k8s.io/kubebuilder/v4/pkg/config"
@@ -336,3 +338,40 @@ func (factory *executionHooksFactory) postRunEFunc() func(*cobra.Command, []stri
336338
return nil
337339
}
338340
}
341+
342+
func updateProjectFileForAlphaGenerate() error {
343+
projectFilePath := "PROJECT"
344+
345+
content, err := os.ReadFile(projectFilePath)
346+
if err != nil {
347+
return fmt.Errorf("failed to read PROJECT file: %w", err)
348+
}
349+
350+
projectStr := string(content)
351+
352+
// Define outdated plugin versions that need replacement
353+
outdatedPlugins := []string{"go.kubebuilder.io/v3", "go.kubebuilder.io/v3-alpha", "go.kubebuilder.io/v2"}
354+
updated := false
355+
356+
for _, oldPlugin := range outdatedPlugins {
357+
if strings.Contains(projectStr, oldPlugin) {
358+
log.Warnf("Detected '%s' in PROJECT file.", oldPlugin)
359+
log.Warnf("Kubebuilder v4 no longer supports this. It will be replaced with 'go.kubebuilder.io/v4'.")
360+
361+
projectStr = strings.ReplaceAll(projectStr, oldPlugin, "go.kubebuilder.io/v4")
362+
updated = true
363+
break
364+
}
365+
}
366+
367+
// Only update the file if changes were made
368+
if updated {
369+
err = os.WriteFile(projectFilePath, []byte(projectStr), 0644)
370+
if err != nil {
371+
return fmt.Errorf("failed to update PROJECT file: %w", err)
372+
}
373+
log.Infof("PROJECT file updated successfully.")
374+
}
375+
376+
return nil
377+
}

0 commit comments

Comments
 (0)