Skip to content

Commit 29c526d

Browse files
gracefully skip webhook test injection if file or markers are missing
1 parent f74fc2e commit 29c526d

File tree

2 files changed

+59
-29
lines changed

2 files changed

+59
-29
lines changed

pkg/machinery/scaffold.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"strings"
2626
"text/template"
2727

28+
log "github.com/sirupsen/logrus"
2829
"github.com/spf13/afero"
2930
"golang.org/x/tools/imports"
3031

@@ -236,7 +237,15 @@ func doTemplate(t Template) ([]byte, error) {
236237
func (s Scaffold) updateFileModel(i Inserter, models map[string]*File) error {
237238
m, err := s.loadPreviousModel(i, models)
238239
if err != nil {
239-
return fmt.Errorf("failed to load previous model: %w", err)
240+
// TODO(kubebuilder/issues/4960): Create Machinery implementation to allow defining IfNotExistsAction
241+
// If the file path starts with test/, warn and skip
242+
// Workaround to allow projects be backwards compatible with previous versions
243+
if strings.HasPrefix(i.GetPath(), "test/") {
244+
log.Warnf("Skipping missing test file: %s", i.GetPath())
245+
log.Warn("The code fragments will not be inserted.")
246+
return nil
247+
}
248+
return fmt.Errorf("failed to load previous model for %s: %w", i.GetPath(), err)
240249
}
241250

242251
// Get valid code fragments

pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e/test.go

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ limitations under the License.
1717
package e2e
1818

1919
import (
20+
"bytes"
2021
"fmt"
22+
"os"
2123
"path/filepath"
2224

25+
log "github.com/sirupsen/logrus"
26+
2327
"sigs.k8s.io/kubebuilder/v4/pkg/machinery"
2428
)
2529

@@ -77,41 +81,58 @@ func (f *WebhookTestUpdater) GetMarkers() []machinery.Marker {
7781

7882
// GetCodeFragments implements file.Inserter
7983
func (f *WebhookTestUpdater) GetCodeFragments() machinery.CodeFragmentsMap {
80-
codeFragments := machinery.CodeFragmentsMap{}
8184
if !f.WireWebhook {
8285
return nil
8386
}
84-
codeFragments[machinery.NewMarkerFor(f.GetPath(), webhookChecksMarker)] = append(
85-
codeFragments[machinery.NewMarkerFor(f.GetPath(), webhookChecksMarker)],
86-
webhookChecksFragment,
87-
)
88-
89-
if f.Resource != nil && f.Resource.HasDefaultingWebhook() {
90-
mutatingWebhookCode := fmt.Sprintf(mutatingWebhookChecksFragment, f.ProjectName)
91-
codeFragments[machinery.NewMarkerFor(f.GetPath(), webhookChecksMarker)] = append(
92-
codeFragments[machinery.NewMarkerFor(f.GetPath(), webhookChecksMarker)],
93-
mutatingWebhookCode,
94-
)
87+
88+
filePath := f.GetPath()
89+
90+
content, err := os.ReadFile(filePath)
91+
if err != nil {
92+
log.Warnf("Unable to read %q: %v", filePath, err)
93+
log.Warnf("Webhook test code injection will be skipped for this file.")
94+
log.Warnf("This typically occurs when the file was removed and is missing.")
95+
log.Warnf("If you intend to scaffold webhook tests, ensure the file and its markers exist.")
96+
return nil
9597
}
9698

97-
if f.Resource.HasValidationWebhook() {
98-
validatingWebhookCode := fmt.Sprintf(validatingWebhookChecksFragment, f.ProjectName)
99-
codeFragments[machinery.NewMarkerFor(f.GetPath(), webhookChecksMarker)] = append(
100-
codeFragments[machinery.NewMarkerFor(f.GetPath(), webhookChecksMarker)],
101-
validatingWebhookCode,
102-
)
99+
codeFragments := machinery.CodeFragmentsMap{}
100+
markers := f.GetMarkers()
101+
102+
for _, marker := range markers {
103+
if !bytes.Contains(content, []byte(marker.String())) {
104+
log.Warnf("Marker %q not found in %s; skipping injection of webhook test code",
105+
marker.String(), filePath)
106+
continue // skip this marker
107+
}
108+
109+
var fragments []string
110+
fragments = append(fragments, webhookChecksFragment)
111+
112+
if f.Resource != nil && f.Resource.HasDefaultingWebhook() {
113+
mutatingWebhookCode := fmt.Sprintf(mutatingWebhookChecksFragment, f.ProjectName)
114+
fragments = append(fragments, mutatingWebhookCode)
115+
}
116+
117+
if f.Resource != nil && f.Resource.HasValidationWebhook() {
118+
validatingWebhookCode := fmt.Sprintf(validatingWebhookChecksFragment, f.ProjectName)
119+
fragments = append(fragments, validatingWebhookCode)
120+
}
121+
122+
if f.Resource != nil && f.Resource.HasConversionWebhook() {
123+
conversionWebhookCode := fmt.Sprintf(
124+
conversionWebhookChecksFragment,
125+
f.Resource.Kind,
126+
f.Resource.Plural+"."+f.Resource.Group+"."+f.Resource.Domain,
127+
)
128+
fragments = append(fragments, conversionWebhookCode)
129+
}
130+
131+
codeFragments[marker] = fragments
103132
}
104133

105-
if f.Resource.HasConversionWebhook() {
106-
conversionWebhookCode := fmt.Sprintf(
107-
conversionWebhookChecksFragment,
108-
f.Resource.Kind,
109-
f.Resource.Plural+"."+f.Resource.Group+"."+f.Resource.Domain,
110-
)
111-
codeFragments[machinery.NewMarkerFor(f.GetPath(), webhookChecksMarker)] = append(
112-
codeFragments[machinery.NewMarkerFor(f.GetPath(), webhookChecksMarker)],
113-
conversionWebhookCode,
114-
)
134+
if len(codeFragments) == 0 {
135+
return nil
115136
}
116137

117138
return codeFragments

0 commit comments

Comments
 (0)