|
| 1 | +/* |
| 2 | +Copyright The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controller |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 25 | + |
| 26 | + "tpu-slice-controller/api/v1alpha1" |
| 27 | + "tpu-slice-controller/internal/util/slices" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + OwnerReferenceUID = "metadata.ownerReferences.uid" |
| 32 | +) |
| 33 | + |
| 34 | +func indexOwnerReferenceUID(obj client.Object) []string { |
| 35 | + return slices.Map(obj.GetOwnerReferences(), func(o *metav1.OwnerReference) string { return string(o.UID) }) |
| 36 | +} |
| 37 | + |
| 38 | +// SetupSliceIndexer configures the indexer to index specific fields for v1alpha1.Slice resources. |
| 39 | +func SetupSliceIndexer(ctx context.Context, indexer client.FieldIndexer) error { |
| 40 | + if err := indexer.IndexField(ctx, &v1alpha1.Slice{}, OwnerReferenceUID, indexOwnerReferenceUID); err != nil { |
| 41 | + return fmt.Errorf("setting index on ownerReferences.uid for Slice: %w", err) |
| 42 | + } |
| 43 | + return nil |
| 44 | +} |
0 commit comments