Skip to content

Commit 9172504

Browse files
Improve dryrun docs and errors
The command is now correctly marked as a dev preview feature. Errors are now generally wrapped to give some indication of where they came from, and if a resource failed to be applied because of a (likely) missing mapping, the error message now suggests how to fix it. Signed-off-by: Justin Kulikauskas <[email protected]>
1 parent 14b1eff commit 9172504

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

cmd/dryrun/cmd.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ var ErrNonCompliant = errors.New("policy is NonCompliant")
2525
func (d *DryRunner) GetCmd() *cobra.Command {
2626
cmd := &cobra.Command{
2727
Use: "dryrun",
28-
Short: "Locally execute a ConfigurationPolicy",
29-
Long: "Locally execute a ConfigurationPolicy",
30-
RunE: d.dryRun,
31-
Args: cobra.ArbitraryArgs,
28+
Short: "(Dev Preview feature) Locally execute a ConfigurationPolicy",
29+
Long: "(Dev Preview feature) Locally execute a ConfigurationPolicy against input files " +
30+
"representing the cluster state, and view the diffs and any compliance events that " +
31+
"would be generated.",
32+
RunE: d.dryRun,
33+
Args: cobra.ArbitraryArgs,
3234
}
3335

3436
cmd.Flags().StringVarP(
@@ -87,7 +89,8 @@ func (d *DryRunner) GetCmd() *cobra.Command {
8789
RunE: mappings.GenerateMappings,
8890
})
8991

90-
cmd.SetOut(os.Stdout) // sets default output to stdout, otherwise it is stderr
92+
cmd.SetOut(os.Stdout) // sets default output to stdout, otherwise it is stderr
93+
cmd.SilenceUsage = true // otherwise all errors will be followed by the usage doc
9194

9295
return cmd
9396
}

cmd/dryrun/dryrun.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,24 @@ import (
4646
func (d *DryRunner) dryRun(cmd *cobra.Command, args []string) error {
4747
cfgPolicy, err := d.readPolicy(cmd)
4848
if err != nil {
49-
return err
49+
return fmt.Errorf("unable to read input policy: %w", err)
5050
}
5151

5252
inputObjects, err := d.readInputResources(cmd, args)
5353
if err != nil {
54-
return err
54+
return fmt.Errorf("unable to read input resources: %w", err)
5555
}
5656

5757
if err := d.setupLogs(); err != nil {
58-
return err
58+
return fmt.Errorf("unable to setup the logging configuration: %w", err)
5959
}
6060

6161
ctx, cancel := context.WithCancel(cmd.Context())
6262
defer cancel()
6363

6464
rec, err := d.setupReconciler(ctx, cfgPolicy)
6565
if err != nil {
66-
return err
66+
return fmt.Errorf("unable to setup the dryrun reconciler: %w", err)
6767
}
6868

6969
// Apply the user's resources to the fake cluster
@@ -72,7 +72,12 @@ func (d *DryRunner) dryRun(cmd *cobra.Command, args []string) error {
7272

7373
scopedGVR, err := rec.DynamicWatcher.GVKToGVR(gvk)
7474
if err != nil {
75-
return err
75+
if errors.Is(depclient.ErrNoVersionedResource, err) {
76+
return fmt.Errorf("%w for kind %v: if this is a custom resource, it may need an "+
77+
"entry in the mappings file", err, gvk.Kind)
78+
}
79+
80+
return fmt.Errorf("unable to apply an input resource: %w", err)
7681
}
7782

7883
var resInt dynamic.ResourceInterface
@@ -84,7 +89,7 @@ func (d *DryRunner) dryRun(cmd *cobra.Command, args []string) error {
8489
}
8590

8691
if _, err := resInt.Create(ctx, obj, metav1.CreateOptions{}); err != nil {
87-
return err
92+
return fmt.Errorf("unable to apply an input resource: %w", err)
8893
}
8994
}
9095

@@ -94,16 +99,16 @@ func (d *DryRunner) dryRun(cmd *cobra.Command, args []string) error {
9499
}
95100

96101
if _, err := rec.Reconcile(ctx, runtime.Request{NamespacedName: cfgPolicyNN}); err != nil {
97-
return err
102+
return fmt.Errorf("unable to complete the dryrun reconcile: %w", err)
98103
}
99104

100105
if err := rec.Get(ctx, cfgPolicyNN, cfgPolicy); err != nil {
101-
return err
106+
return fmt.Errorf("unable to get the resulting policy state: %w", err)
102107
}
103108

104109
if d.statusPath != "" {
105110
if err := d.saveStatus(cfgPolicy.Status); err != nil {
106-
return err
111+
return fmt.Errorf("unable to save the resulting policy state: %w", err)
107112
}
108113
}
109114

@@ -112,12 +117,10 @@ func (d *DryRunner) dryRun(cmd *cobra.Command, args []string) error {
112117
}
113118

114119
if err := d.saveOrPrintComplianceMessages(ctx, cmd, rec.Client, cfgPolicy.Namespace); err != nil {
115-
return err
120+
return fmt.Errorf("unable to save or print the compliance messages: %w", err)
116121
}
117122

118123
if cfgPolicy.Status.ComplianceState != policyv1.Compliant {
119-
cmd.SilenceUsage = true
120-
121124
return ErrNonCompliant
122125
}
123126

0 commit comments

Comments
 (0)