|
5 | 5 | "errors"
|
6 | 6 | "fmt"
|
7 | 7 | "io"
|
| 8 | + "slices" |
| 9 | + "strings" |
8 | 10 | "sync"
|
9 | 11 | "time"
|
10 | 12 |
|
@@ -39,6 +41,10 @@ type CloserSyncingSource interface {
|
39 | 41 | }
|
40 | 42 |
|
41 | 43 | type sourcerer interface {
|
| 44 | + // Source returns a CloserSyncingSource for the provided |
| 45 | + // GroupVersionKind. If the CloserSyncingSource encounters an |
| 46 | + // error after having initially synced, it should requeue the |
| 47 | + // provided client.Object and call the provided callback function |
42 | 48 | Source(schema.GroupVersionKind, client.Object, func(context.Context)) (CloserSyncingSource, error)
|
43 | 49 | }
|
44 | 50 |
|
@@ -85,6 +91,11 @@ func (c *cache) Close() error {
|
85 | 91 | errs = append(errs, err)
|
86 | 92 | }
|
87 | 93 | }
|
| 94 | + |
| 95 | + slices.SortFunc(errs, func(a, b error) int { |
| 96 | + return strings.Compare(a.Error(), b.Error()) |
| 97 | + }) |
| 98 | + |
88 | 99 | return errors.Join(errs...)
|
89 | 100 | }
|
90 | 101 |
|
@@ -132,11 +143,19 @@ func (c *cache) startNewSources(ctx context.Context, gvks sets.Set[schema.GroupV
|
132 | 143 | }
|
133 | 144 | }
|
134 | 145 |
|
| 146 | + slices.SortFunc(sourcesErrors, func(a, b error) int { |
| 147 | + return strings.Compare(a.Error(), b.Error()) |
| 148 | + }) |
| 149 | + |
135 | 150 | return errors.Join(sourcesErrors...)
|
136 | 151 | }
|
137 | 152 |
|
138 | 153 | func (c *cache) startNewSource(ctx context.Context, gvk schema.GroupVersionKind, watcher Watcher) (CloserSyncingSource, error) {
|
139 | 154 | s, err := c.sourcerer.Source(gvk, c.owner, func(ctx context.Context) {
|
| 155 | + // this callback function ensures that we remove the source from the |
| 156 | + // cache if it encounters an error after it initially synced successfully |
| 157 | + c.mu.Lock() |
| 158 | + defer c.mu.Unlock() |
140 | 159 | err := c.removeSource(gvk)
|
141 | 160 | if err != nil {
|
142 | 161 | logr := log.FromContext(ctx)
|
@@ -180,6 +199,11 @@ func (c *cache) removeStaleSources(gvks sets.Set[schema.GroupVersionKind]) error
|
180 | 199 | removeErrs = append(removeErrs, err)
|
181 | 200 | }
|
182 | 201 | }
|
| 202 | + |
| 203 | + slices.SortFunc(removeErrs, func(a, b error) int { |
| 204 | + return strings.Compare(a.Error(), b.Error()) |
| 205 | + }) |
| 206 | + |
183 | 207 | return errors.Join(removeErrs...)
|
184 | 208 | }
|
185 | 209 |
|
|
0 commit comments