list returns a list of resources in the catalog. It optionally supports filtering by kind, path, and soft-deleted status. The returned list is not sorted. The returned list is always safe to manipulate (e.g. sort/filter), but the resource pointers must not be edited unless clone=true. Unlike other c
(kind, path string, withDeleted, clone bool)
| 160 | // The returned list is always safe to manipulate (e.g. sort/filter), but the resource pointers must not be edited unless clone=true. |
| 161 | // Unlike other catalog functions, it is safe to call list concurrently with calls to get and flush (i.e. under a read lock). |
| 162 | func (c *catalogCache) list(kind, path string, withDeleted, clone bool) []*runtimev1.Resource { |
| 163 | // Estimate number of resources to list |
| 164 | n := 0 |
| 165 | if path != "" { |
| 166 | n = 1 |
| 167 | } else if kind == "" { |
| 168 | for _, rs := range c.resources { |
| 169 | n += len(rs) |
| 170 | } |
| 171 | } else { |
| 172 | n = len(c.resources[kind]) |
| 173 | } |
| 174 | |
| 175 | // Alloc slice for resources |
| 176 | res := make([]*runtimev1.Resource, 0, n) |
| 177 | |
| 178 | // Find resources matching the filters and append to slice |
| 179 | for k, rs := range c.resources { |
| 180 | // Skip if doesn't match kind filter |
| 181 | if kind != "" && k != kind { |
| 182 | continue |
| 183 | } |
| 184 | |
| 185 | for _, r := range rs { |
| 186 | // Skip if doesn't match withDeleted filter |
| 187 | if !withDeleted && r.Meta.DeletedOn != nil { |
| 188 | continue |
| 189 | } |
| 190 | |
| 191 | // Skip if doesn't match path filter |
| 192 | if path != "" { |
| 193 | found := false |
| 194 | for _, p := range r.Meta.FilePaths { |
| 195 | if p == path { |
| 196 | found = true |
| 197 | break |
| 198 | } |
| 199 | } |
| 200 | if !found { |
| 201 | continue |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // Append to res |
| 206 | if clone { |
| 207 | r = c.clone(r) |
| 208 | } |
| 209 | res = append(res, r) |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | return res |
| 214 | } |
| 215 | |
| 216 | // create creates a resource in the catalog. |
| 217 | // It will error if a resource with the same name already exists. |