@@ -35,10 +35,9 @@ import (
3535 "github.com/kubefleet-dev/kubefleet/pkg/utils/overrider"
3636)
3737
38- // fetchClusterResourceOverrideSnapshots returns the ClusterResourceOverrideSnapshots referenced
39- // by the given binding, keyed by the ResourceIdentifier each snapshot's selectors target. The
40- // returned map preserves the order in which the binding lists the snapshots, so the caller can
41- // apply them in a deterministic sequence.
38+ // fetchClusterResourceOverrideSnapshots returns the binding's CRO snapshots keyed by the
39+ // ResourceIdentifier their selectors target. Order matches the binding's snapshot list so
40+ // callers can apply deterministically.
4241//
4342// TODO: combine the following two functions into one, as they are very similar.
4443func (r * Reconciler ) fetchClusterResourceOverrideSnapshots (ctx context.Context , resourceBinding placementv1beta1.BindingObj ) (map [placementv1beta1.ResourceIdentifier ][]* placementv1beta1.ClusterResourceOverrideSnapshot , error ) {
@@ -75,10 +74,9 @@ func (r *Reconciler) fetchClusterResourceOverrideSnapshots(ctx context.Context,
7574 return croMap , nil
7675}
7776
78- // fetchResourceOverrideSnapshots returns the ResourceOverrideSnapshots referenced by the given
79- // binding, keyed by the ResourceIdentifier each snapshot's selectors target. The returned map
80- // preserves the order in which the binding lists the snapshots, so the caller can apply them in
81- // a deterministic sequence.
77+ // fetchResourceOverrideSnapshots returns the binding's RO snapshots keyed by the
78+ // ResourceIdentifier their selectors target. Order matches the binding's snapshot list so
79+ // callers can apply deterministically.
8280func (r * Reconciler ) fetchResourceOverrideSnapshots (ctx context.Context , resourceBinding placementv1beta1.BindingObj ) (map [placementv1beta1.ResourceIdentifier ][]* placementv1beta1.ResourceOverrideSnapshot , error ) {
8381 roMap := make (map [placementv1beta1.ResourceIdentifier ][]* placementv1beta1.ResourceOverrideSnapshot )
8482
@@ -158,8 +156,8 @@ func (r *Reconciler) applyOverrides(resource *placementv1beta1.ResourceContent,
158156 }
159157 if err := applyOverrideRules (resource , cluster , snapshot .Spec .OverrideSpec .Policy .OverrideRules ); err != nil {
160158 klog .ErrorS (err , "Failed to apply the override rules" , "clusterResourceOverrideSnapshot" , klog .KObj (snapshot ))
161- return false , fmt .Errorf ("%w: ClusterResourceOverrideSnapshot %q failed to apply on %s: %s" ,
162- controller . ErrUserError , snapshot .Name , formatOverrideTarget (& uResource ), err .Error ())
159+ return false , controller . NewUserError ( fmt .Errorf ("ClusterResourceOverrideSnapshot %q failed to apply on %s: %s" ,
160+ snapshot .Name , formatOverrideTarget (& uResource ), err .Error () ))
163161 }
164162 }
165163 klog .V (2 ).InfoS ("Applied clusterResourceOverrideSnapshots" , "resource" , klog .KObj (& uResource ), "numberOfOverrides" , len (croMap [key ]))
@@ -182,36 +180,38 @@ func (r *Reconciler) applyOverrides(resource *placementv1beta1.ResourceContent,
182180 }
183181 if err := applyOverrideRules (resource , cluster , snapshot .Spec .OverrideSpec .Policy .OverrideRules ); err != nil {
184182 klog .ErrorS (err , "Failed to apply the override rules" , "resourceOverrideSnapshot" , klog .KObj (snapshot ))
185- return false , fmt .Errorf ("%w: ResourceOverrideSnapshot %q failed to apply on %s: %s" ,
186- controller . ErrUserError , snapshot .Name , formatOverrideTarget (& uResource ), err .Error ())
183+ return false , controller . NewUserError ( fmt .Errorf ("ResourceOverrideSnapshot %q failed to apply on %s: %s" ,
184+ snapshot .Name , formatOverrideTarget (& uResource ), err .Error () ))
187185 }
188186 }
189187 klog .V (2 ).InfoS ("Applied resourceOverrideSnapshots" , "resource" , klog .KObj (& uResource ), "numberOfOverrides" , len (roMap [key ]))
190188 }
191189 return resource .Raw == nil , nil
192190}
193191
194- // formatOverrideTarget returns a human-readable identifier for the resource being overridden,
195- // suitable for inclusion in a user-facing error message (e.g. `Deployment "my-app" in namespace "default"`) .
192+ // formatOverrideTarget renders the target as e.g. `Deployment "my-app" in namespace "default"`
193+ // for inclusion in a user-facing error message.
196194func formatOverrideTarget (target * unstructured.Unstructured ) string {
195+ // Fall back to "Unknown" so a snapshot missing `kind` doesn't render as `"" "name"`.
197196 kind := target .GetObjectKind ().GroupVersionKind ().Kind
197+ if kind == "" {
198+ kind = "Unknown"
199+ }
198200 if ns := target .GetNamespace (); ns != "" {
199201 return fmt .Sprintf ("%s %q in namespace %q" , kind , target .GetName (), ns )
200202 }
201203 return fmt .Sprintf ("%s %q" , kind , target .GetName ())
202204}
203205
204- // applyOverrideRules applies the given override rules to the resource for the cluster. Rules
205- // whose cluster selector does not match the cluster are skipped. A DeleteOverrideType rule
206- // clears the resource and ends rule application; otherwise the rule's JSON patches are applied
207- // in order. The first rule that fails returns its raw error to the caller, which is responsible
208- // for tagging the failure as a user error.
206+ // applyOverrideRules applies matching rules to the resource. A DeleteOverrideType rule clears
207+ // the resource and stops; otherwise JSON patches apply in order. Errors are returned raw — the
208+ // caller (applyOverrides) tags them as user errors so we don't double-wrap the sentinel.
209209func applyOverrideRules (resource * placementv1beta1.ResourceContent , cluster * clusterv1beta1.MemberCluster , rules []placementv1beta1.OverrideRule ) error {
210210 for _ , rule := range rules {
211211 matched , err := overrider .IsClusterMatched (cluster , rule )
212212 if err != nil {
213- klog .ErrorS (controller . NewUnexpectedBehaviorError ( err ) , "Found an invalid override rule" )
214- return err // should not happen though and should be rejected by the webhook
213+ klog .ErrorS (err , "Found an invalid override rule" )
214+ return err
215215 }
216216 if ! matched {
217217 continue
0 commit comments