gather lists tenant HelmReleases and namespaces (metadata only) and groups them per tenant.
(ctx context.Context)
| 170 | // gather lists tenant HelmReleases and namespaces (metadata only) and groups |
| 171 | // them per tenant. |
| 172 | func (r *PlacementReconciler) gather(ctx context.Context) (map[string]*tenantView, error) { |
| 173 | hrs := HelmReleaseMetaList() |
| 174 | if err := r.List(ctx, hrs, client.HasLabels{ShardKeyLabel}); err != nil { |
| 175 | return nil, fmt.Errorf("listing HelmReleases: %w", err) |
| 176 | } |
| 177 | nss := NamespaceMetaList() |
| 178 | if err := r.List(ctx, nss); err != nil { |
| 179 | return nil, fmt.Errorf("listing namespaces: %w", err) |
| 180 | } |
| 181 | |
| 182 | nsByName := make(map[string]*metav1.PartialObjectMetadata, len(nss.Items)) |
| 183 | for i := range nss.Items { |
| 184 | nsByName[nss.Items[i].Name] = &nss.Items[i] |
| 185 | } |
| 186 | |
| 187 | views := map[string]*tenantView{} |
| 188 | for i := range hrs.Items { |
| 189 | hr := &hrs.Items[i] |
| 190 | tenantNS, ok := TenantNamespaceForHR(hr) |
| 191 | if !ok { |
| 192 | continue |
| 193 | } |
| 194 | v := views[tenantNS] |
| 195 | if v == nil { |
| 196 | v = &tenantView{info: TenantInfo{Namespace: tenantNS}} |
| 197 | views[tenantNS] = v |
| 198 | } |
| 199 | v.hrs = append(v.hrs, hr) |
| 200 | v.info.Weight++ |
| 201 | } |
| 202 | |
| 203 | for tenantNS, v := range views { |
| 204 | if ns := nsByName[tenantNS]; ns != nil { |
| 205 | v.nsExists = true |
| 206 | v.info.Deleting = !ns.DeletionTimestamp.IsZero() |
| 207 | v.nsLabel = ns.Labels[TenantShardLabel] |
| 208 | } |
| 209 | // The recorded assignment (namespace label) wins; HR labels are the |
| 210 | // fallback source of truth when no assignment is recorded yet. |
| 211 | if _, ok := ParseShardIndex(v.nsLabel); ok { |
| 212 | v.info.Current = v.nsLabel |
| 213 | } else { |
| 214 | v.info.Current = majorityShard(v.hrs) |
| 215 | } |
| 216 | } |
| 217 | return views, nil |
| 218 | } |
| 219 | |
| 220 | // majorityShard returns the most common canonical shard value among the |
| 221 | // tenant's HelmRelease labels, tie-break lowest shard index. Legacy "tenants" |
no test coverage detected