Repository navigation
Improve nested-XR output in simulate #541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -173,6 +173,14 @@ func (m *DefaultResourceManager) checkCompositeOwnership(ctx context.Context, cu | |
| return | ||
| } | ||
|
|
||
| // A new owner that is itself composed by the resource's current owner is | ||
| // not taking ownership away from it: the resource is moving within one | ||
| // composition tree, from a parent XR to a nested XR it composes. That is | ||
| // the normal shape of a nested composition, not something to warn about. | ||
| if composite.GetLabels()[LabelComposite] == owner { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not treat an empty label as a parent match. If the current resource has
|
||
| return | ||
| } | ||
|
|
||
| m.advisories.Raise(dt.OutputWarning{ | ||
| // TODO: should we fail by default here? maybe require a --force flag to proceed? | ||
| Message: "Resource already belongs to another composite. Applying this diff will assume ownership!", | ||
|
|
@@ -182,6 +190,8 @@ func (m *DefaultResourceManager) checkCompositeOwnership(ctx context.Context, cu | |
| "currentOwner": owner, | ||
| "newOwner": newOwner, | ||
| }, | ||
| // One line per owner pair is enough for a human; structured output keeps every resource. | ||
| GroupBy: []string{"currentOwner", "newOwner"}, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Include the namespace in the stderr advisory family. The ownership advisory groups stderr output by Add Suggested fix
|
||
| }) | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,12 +67,23 @@ type AdvisoryCollector struct { | |
| // seen holds the identity of every advisory already recorded, so an identical repeat is dropped | ||
| // from both channels rather than re-emitted. Keyed by warningKey. | ||
| seen map[string]struct{} | ||
| // groups tracks advisory families (see dt.OutputWarning.GroupBy): the first member of a family | ||
| // is streamed to stderr as usual, later members are only collected, and Summarize reports how | ||
| // many were withheld per family. Keyed by message plus the GroupBy context pairs. | ||
| groups map[string]*advisoryGroup | ||
| } | ||
|
|
||
| // advisoryGroup is the stderr-side state of one advisory family. | ||
| type advisoryGroup struct { | ||
| message string | ||
| context map[string]string | ||
| suppressed int | ||
| } | ||
|
|
||
| // NewAdvisoryCollector returns a collector that writes each advisory to stderr and collects it for | ||
| // structured output. A nil stderr only collects. | ||
| func NewAdvisoryCollector(stderr io.Writer) *AdvisoryCollector { | ||
| return &AdvisoryCollector{stderr: stderr, seen: map[string]struct{}{}} | ||
| return &AdvisoryCollector{stderr: stderr, seen: map[string]struct{}{}, groups: map[string]*advisoryGroup{}} | ||
| } | ||
|
|
||
| // Raise records w: written to stderr now, and collected for structured output. An advisory identical | ||
|
|
@@ -90,11 +101,65 @@ func (c *AdvisoryCollector) Raise(w dt.OutputWarning) { | |
| c.seen[key] = struct{}{} | ||
| c.advisories = append(c.advisories, w) | ||
|
|
||
| if c.stderr != nil { | ||
| // A failed write to stderr is not worth failing a diff over, and there is nowhere useful to | ||
| // report it: the reporting channel is the thing that just failed. | ||
| _, _ = fmt.Fprintln(c.stderr, w.FormatWarning()) | ||
| if c.stderr == nil { | ||
| return | ||
| } | ||
|
|
||
| // A grouped advisory streams only its family's first member; Summarize reports the rest. | ||
| if len(w.GroupBy) > 0 { | ||
| gk := groupKeyOf(w) | ||
| if g, ok := c.groups[gk]; ok { | ||
| g.suppressed++ | ||
| return | ||
| } | ||
| gctx := make(map[string]string, len(w.GroupBy)) | ||
| for _, k := range w.GroupBy { | ||
| gctx[k] = w.Context[k] | ||
| } | ||
| c.groups[gk] = &advisoryGroup{message: w.Message, context: gctx} | ||
| } | ||
|
|
||
| // A failed write to stderr is not worth failing a diff over, and there is nowhere useful to | ||
| // report it: the reporting channel is the thing that just failed. | ||
| _, _ = fmt.Fprintln(c.stderr, w.FormatWarning()) | ||
| } | ||
|
|
||
| // Summarize writes one stderr line per advisory family with withheld members, reporting how many | ||
| // similar advisories were not streamed. Call it once the run is done raising advisories; structured | ||
| // output needs no equivalent, since it kept every occurrence. | ||
| func (c *AdvisoryCollector) Summarize() { | ||
| c.mu.Lock() | ||
| defer c.mu.Unlock() | ||
|
|
||
| if c.stderr == nil { | ||
| return | ||
| } | ||
|
|
||
| keys := slices.Sorted(maps.Keys(c.groups)) | ||
| for _, gk := range keys { | ||
| g := c.groups[gk] | ||
| if g.suppressed == 0 { | ||
| continue | ||
| } | ||
| _, _ = fmt.Fprintln(c.stderr, dt.OutputWarning{ | ||
| Message: fmt.Sprintf("...and %d more like the above.", g.suppressed), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Name the warning in each summary. If two warning messages share the same grouping context, the collector correctly counts them as separate families. The summaries omit both messages, so users cannot tell which warning each count describes. Include
|
||
| Context: g.context, | ||
| }.FormatWarning()) | ||
| } | ||
| } | ||
|
|
||
| // groupKeyOf builds the family identity of a grouped warning: its message plus the GroupBy context | ||
| // pairs, quoted the same way warningKey quotes, so values containing separators cannot collide. | ||
| func groupKeyOf(w dt.OutputWarning) string { | ||
| var b strings.Builder | ||
|
|
||
| fmt.Fprintf(&b, "%q", w.Message) | ||
|
|
||
| for _, k := range slices.Sorted(slices.Values(w.GroupBy)) { | ||
| fmt.Fprintf(&b, " %q=%q", k, w.Context[k]) | ||
| } | ||
|
|
||
| return b.String() | ||
| } | ||
|
|
||
| // Advisories returns the advisories raised so far, sorted by message and then context (by warningKey), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolve the parent’s scope before setting its namespace.
Could we determine the parent’s scope here? A cluster-scoped XR can compose a namespaced XR. (docs.crossplane.io) In that case, this assigns the child’s namespace to the cluster-scoped parent. The text and structured output then identify the wrong resource. Please leave the namespace empty for a cluster-scoped parent and retain it for a namespaced parent. Thanks!