@@ -74,44 +74,9 @@ func (d *DryRunner) dryRun(cmd *cobra.Command, args []string) error {
7474 return fmt .Errorf ("unable to setup the dryrun reconciler: %w" , err )
7575 }
7676
77- // Apply the user's resources to the fake cluster
78- for _ , obj := range inputObjects {
79- gvk := obj .GroupVersionKind ()
80-
81- scopedGVR , err := rec .DynamicWatcher .GVKToGVR (gvk )
82- if err != nil {
83- if errors .Is (err , depclient .ErrNoVersionedResource ) {
84- return fmt .Errorf ("%w for kind %v: if this is a custom resource, it may need an " +
85- "entry in the mappings file" , err , gvk .Kind )
86- }
87-
88- return fmt .Errorf ("unable to apply an input resource: %w" , err )
89- }
90-
91- var resInt dynamic.ResourceInterface
92-
93- if scopedGVR .Namespaced {
94- if obj .GetNamespace () == "" {
95- obj .SetNamespace ("default" )
96- }
97-
98- resInt = rec .TargetK8sDynamicClient .Resource (scopedGVR .GroupVersionResource ).Namespace (obj .GetNamespace ())
99- } else {
100- resInt = rec .TargetK8sDynamicClient .Resource (scopedGVR .GroupVersionResource )
101- }
102-
103- sanitizeForCreation (obj )
104-
105- if _ , err := resInt .Create (ctx , obj , metav1.CreateOptions {}); err != nil &&
106- ! k8serrors .IsAlreadyExists (err ) {
107- return fmt .Errorf ("unable to apply an input resource: %w" , err )
108- }
109-
110- // Manually convert resources from the dynamic client to the runtime client
111- err = rec .Client .Create (ctx , obj )
112- if err != nil && ! k8serrors .IsAlreadyExists (err ) {
113- return err
114- }
77+ err = d .applyInputResources (ctx , rec , inputObjects )
78+ if err != nil {
79+ return fmt .Errorf ("unable to apply input resources: %w" , err )
11580 }
11681
11782 cfgPolicyNN := types.NamespacedName {
@@ -367,6 +332,52 @@ func (d *DryRunner) readInputResources(cmd *cobra.Command, args []string) (
367332 return rawInputs , nil
368333}
369334
335+ func (d * DryRunner ) applyInputResources (
336+ ctx context.Context , rec * ctrl.ConfigurationPolicyReconciler , inputObjects []* unstructured.Unstructured ,
337+ ) error {
338+ // Apply the user's resources to the fake cluster
339+ for _ , obj := range inputObjects {
340+ gvk := obj .GroupVersionKind ()
341+
342+ scopedGVR , err := rec .DynamicWatcher .GVKToGVR (gvk )
343+ if err != nil {
344+ if errors .Is (err , depclient .ErrNoVersionedResource ) {
345+ return fmt .Errorf ("%w for kind %v: if this is a custom resource, it may need an " +
346+ "entry in the mappings file" , err , gvk .Kind )
347+ }
348+
349+ return fmt .Errorf ("unable to apply an input resource: %w" , err )
350+ }
351+
352+ var resInt dynamic.ResourceInterface
353+
354+ if scopedGVR .Namespaced {
355+ if obj .GetNamespace () == "" {
356+ obj .SetNamespace ("default" )
357+ }
358+
359+ resInt = rec .TargetK8sDynamicClient .Resource (scopedGVR .GroupVersionResource ).Namespace (obj .GetNamespace ())
360+ } else {
361+ resInt = rec .TargetK8sDynamicClient .Resource (scopedGVR .GroupVersionResource )
362+ }
363+
364+ sanitizeForCreation (obj )
365+
366+ if _ , err := resInt .Create (ctx , obj , metav1.CreateOptions {}); err != nil &&
367+ ! k8serrors .IsAlreadyExists (err ) {
368+ return fmt .Errorf ("unable to apply an input resource: %w" , err )
369+ }
370+
371+ // Manually convert resources from the dynamic client to the runtime client
372+ err = rec .Client .Create (ctx , obj )
373+ if err != nil && ! k8serrors .IsAlreadyExists (err ) {
374+ return err
375+ }
376+ }
377+
378+ return nil
379+ }
380+
370381// setupLogs configures klog and the controller-runtime logger to send logs to the
371382// path defined in the configuration. If that option is empty, logs will be discarded.
372383func (d * DryRunner ) setupLogs () error {
0 commit comments