55 "fmt"
66 "os"
77 "os/exec"
8+ "path/filepath"
9+ "time"
810
911 "github.com/spf13/cobra"
1012 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -139,7 +141,8 @@ func (o *createOptions) create() error {
139141 }
140142
141143 // Apply using kubectl/oc
142- if err := o .applyYAML (yamlBytes ); err != nil {
144+ artifactName := fmt .Sprintf ("testextensionadmission-%s.yaml" , o .name )
145+ if err := o .applyYAML (yamlBytes , artifactName ); err != nil {
143146 return fmt .Errorf ("failed to apply TestExtensionAdmission: %w" , err )
144147 }
145148
@@ -148,15 +151,37 @@ func (o *createOptions) create() error {
148151}
149152
150153func (o * extensionAdmissionOptions ) installCRD () error {
151- if err := o .applyYAML (crdYAML ); err != nil {
154+ if err := o .applyYAML (crdYAML , "testextensionadmission-crd.yaml" ); err != nil {
152155 return fmt .Errorf ("failed to install CRD: %w" , err )
153156 }
154157
155158 fmt .Fprintln (o .ioStreams .Out , "TestExtensionAdmission CRD installed successfully" )
156159 return nil
157160}
158161
159- func (o * extensionAdmissionOptions ) applyYAML (yamlBytes []byte ) error {
162+ // saveToArtifactDir saves the YAML content to ARTIFACT_DIR if the environment variable is set.
163+ // This helps with debugging by preserving the applied manifests.
164+ func saveToArtifactDir (yamlBytes []byte , basename string ) {
165+ artifactDir := os .Getenv ("ARTIFACT_DIR" )
166+ if artifactDir == "" {
167+ return
168+ }
169+
170+ // Create a timestamped filename to avoid collisions
171+ timestamp := time .Now ().UTC ().Format ("20060102-150405" )
172+ filename := fmt .Sprintf ("%s-%s" , timestamp , basename )
173+ artifactPath := filepath .Join (artifactDir , filename )
174+
175+ if err := os .WriteFile (artifactPath , yamlBytes , 0644 ); err != nil {
176+ // Don't fail the operation, just log the error
177+ fmt .Fprintf (os .Stderr , "Warning: Failed to save artifact to %s: %v\n " , artifactPath , err )
178+ return
179+ }
180+
181+ fmt .Fprintf (os .Stderr , "Saved artifact to %s\n " , artifactPath )
182+ }
183+
184+ func (o * extensionAdmissionOptions ) applyYAML (yamlBytes []byte , artifactName string ) error {
160185 // Write YAML to a temporary file
161186 tmpFile , err := os .CreateTemp ("" , "testextensionadmission-*.yaml" )
162187 if err != nil {
@@ -187,5 +212,6 @@ func (o *extensionAdmissionOptions) applyYAML(yamlBytes []byte) error {
187212 }
188213
189214 fmt .Fprintf (o .ioStreams .Out , "%s" , string (output ))
215+ saveToArtifactDir (yamlBytes , artifactName )
190216 return nil
191217}
0 commit comments