Skip to content
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

refactor: remove over-abstraction of indexers #2867

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions cmd/controlplane/garbage_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,32 @@ func (o *garbageCollectorOptions) setupManager(ctx context.Context) (manager.Man
}

// Index Promotions by Stage
if err = indexer.IndexPromotionsByStage(ctx, mgr); err != nil {
if err = mgr.GetFieldIndexer().IndexField(
ctx,
&kargoapi.Promotion{},
indexer.PromotionsByStageField,
indexer.PromotionsByStage,
); err != nil {
return nil, fmt.Errorf("error indexing Promotions by Stage: %w", err)
}

// Index Freight by Warehouse
if err = indexer.IndexFreightByWarehouse(ctx, mgr); err != nil {
if err = mgr.GetFieldIndexer().IndexField(
ctx,
&kargoapi.Freight{},
indexer.FreightByWarehouseField,
indexer.FreightByWarehouse,
); err != nil {
return nil, fmt.Errorf("error indexing Freight by Warehouse: %w", err)
}

// Index Stages by Freight
if err = indexer.IndexStagesByFreight(ctx, mgr); err != nil {
if err = mgr.GetFieldIndexer().IndexField(
ctx,
&kargoapi.Stage{},
indexer.StagesByFreightField,
indexer.StagesByFreight,
); err != nil {
return nil, fmt.Errorf("error indexing Stages by Freight: %w", err)
}
return mgr, nil
Expand Down
7 changes: 6 additions & 1 deletion cmd/controlplane/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ func (o *webhooksServerOptions) run(ctx context.Context) error {
}

// Index Stages by Freight
if err = indexer.IndexStagesByFreight(ctx, mgr); err != nil {
if err = mgr.GetFieldIndexer().IndexField(
ctx,
&kargoapi.Stage{},
indexer.StagesByFreightField,
indexer.StagesByFreight,
); err != nil {
return fmt.Errorf("index Stages by Freight: %w", err)
}

Expand Down
42 changes: 36 additions & 6 deletions internal/api/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,22 +233,52 @@ func newDefaultInternalClient(
}

// Add all indices required by the API server
if err = indexer.IndexPromotionsByStage(ctx, cluster); err != nil {
if err = cluster.GetFieldIndexer().IndexField(
ctx,
&kargoapi.Promotion{},
indexer.PromotionsByStageField,
indexer.PromotionsByStage,
); err != nil {
return nil, fmt.Errorf("error indexing Promotions by Stage: %w", err)
}
if err = indexer.IndexFreightByWarehouse(ctx, cluster); err != nil {
if err = cluster.GetFieldIndexer().IndexField(
ctx,
&kargoapi.Freight{},
indexer.FreightByWarehouseField,
indexer.FreightByWarehouse,
); err != nil {
return nil, fmt.Errorf("error indexing Freight by Warehouse: %w", err)
}
if err = indexer.IndexFreightByVerifiedStages(ctx, cluster); err != nil {
if err = cluster.GetFieldIndexer().IndexField(
ctx,
&kargoapi.Freight{},
indexer.FreightByVerifiedStagesField,
indexer.FreightByVerifiedStages,
); err != nil {
return nil, fmt.Errorf("error indexing Freight by Stages in which it has been verified: %w", err)
}
if err = indexer.IndexFreightByApprovedStages(ctx, cluster); err != nil {
if err = cluster.GetFieldIndexer().IndexField(
ctx,
&kargoapi.Freight{},
indexer.FreightApprovedForStagesField,
indexer.FreightApprovedForStages,
); err != nil {
return nil, fmt.Errorf("error indexing Freight by Stages for which it has been approved: %w", err)
}
if err = indexer.IndexServiceAccountsByOIDCClaims(ctx, cluster); err != nil {
if err = cluster.GetFieldIndexer().IndexField(
ctx,
&corev1.ServiceAccount{},
indexer.ServiceAccountsByOIDCClaimsField,
indexer.ServiceAccountsByOIDCClaims,
); err != nil {
return nil, fmt.Errorf("index ServiceAccounts by OIDC claims: %w", err)
}
if err = indexer.IndexEventsByInvolvedObjectAPIGroup(ctx, cluster); err != nil {
if err = cluster.GetFieldIndexer().IndexField(
ctx,
&corev1.Event{},
indexer.EventsByInvolvedObjectAPIGroupField,
indexer.EventsByInvolvedObjectAPIGroup,
); err != nil {
return nil, fmt.Errorf("error indexing Events by InvolvedObject's API group: %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/api/list_project_events_v1alpha1.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (s *server) ListProjectEvents(
// List Kargo related events only
client.MatchingFieldsSelector{
Selector: fields.OneTermEqualSelector(
indexer.EventsByInvolvedObjectAPIGroupIndexField,
indexer.EventsByInvolvedObjectAPIGroupField,
kargoapi.GroupVersion.Group,
),
},
Expand Down
2 changes: 1 addition & 1 deletion internal/api/list_promotions_v1alpha1.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (s *server) ListPromotions(
client.InNamespace(project),
}
if stage != "" {
opts = append(opts, client.MatchingFields{indexer.PromotionsByStageIndexField: stage})
opts = append(opts, client.MatchingFields{indexer.PromotionsByStageField: stage})
}
if err := s.client.List(ctx, &list, opts...); err != nil {
return nil, fmt.Errorf("list promotions: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions internal/api/option/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,14 @@ func (a *authInterceptor) listServiceAccounts(
for claimName, claimValue := range c {
if claimValuesString, ok := claimValue.(string); ok {
queries = append(queries, libClient.MatchingFields{
indexer.ServiceAccountsByOIDCClaimsIndexField: indexer.FormatClaim(claimName, claimValuesString),
indexer.ServiceAccountsByOIDCClaimsField: indexer.FormatClaim(claimName, claimValuesString),
})
}
if claimValueSlice, ok := claimValue.([]any); ok {
for _, claimValueSliceItem := range claimValueSlice {
if claimValueSliceItemString, ok := claimValueSliceItem.(string); ok {
queries = append(queries, libClient.MatchingFields{
indexer.ServiceAccountsByOIDCClaimsIndexField: indexer.FormatClaim(
indexer.ServiceAccountsByOIDCClaimsField: indexer.FormatClaim(
claimName, claimValueSliceItemString,
),
})
Expand Down
6 changes: 3 additions & 3 deletions internal/api/query_freights_v1alpha1.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (s *server) getAvailableFreightForStage(
&client.ListOptions{
Namespace: project,
FieldSelector: fields.OneTermEqualSelector(
indexer.FreightApprovedForStagesIndexField,
indexer.FreightApprovedForStagesField,
stage,
),
},
Expand Down Expand Up @@ -216,7 +216,7 @@ func (s *server) getFreightFromWarehouses(
&client.ListOptions{
Namespace: project,
FieldSelector: fields.OneTermEqualSelector(
indexer.FreightByWarehouseIndexField,
indexer.FreightByWarehouseField,
warehouse,
),
},
Expand Down Expand Up @@ -247,7 +247,7 @@ func (s *server) getVerifiedFreight(
&client.ListOptions{
Namespace: project,
FieldSelector: fields.OneTermEqualSelector(
indexer.FreightByVerifiedStagesIndexField,
indexer.FreightByVerifiedStagesField,
upstream,
),
},
Expand Down
7 changes: 6 additions & 1 deletion internal/controller/promotions/promotions.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ func SetupReconcilerWithManager(
cfg ReconcilerConfig,
) error {
// Index running Promotions by Argo CD Applications
if err := indexer.IndexRunningPromotionsByArgoCDApplications(ctx, kargoMgr, cfg.ShardName); err != nil {
if err := kargoMgr.GetFieldIndexer().IndexField(
ctx,
&kargoapi.Promotion{},
indexer.RunningPromotionsByArgoCDApplicationsField,
indexer.RunningPromotionsByArgoCDApplications(ctx, cfg.ShardName),
); err != nil {
return fmt.Errorf("index running Promotions by Argo CD Applications: %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/controller/promotions/watches.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (u *UpdatedArgoCDAppHandler[T]) Update(
promotions,
&client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(
indexer.RunningPromotionsByArgoCDApplicationsIndexField,
indexer.RunningPromotionsByArgoCDApplicationsField,
fmt.Sprintf("%s:%s", newApp.Namespace, newApp.Name),
),
},
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/promotions/watches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func TestUpdatedArgoCDAppHandler_Update(t *testing.T) {
WithObjects(tt.applications...).
WithIndex(
&kargoapi.Promotion{},
indexer.RunningPromotionsByArgoCDApplicationsIndexField,
indexer.RunningPromotionsByArgoCDApplicationsField,
tt.indexer,
).
WithInterceptorFuncs(tt.interceptor)
Expand Down
28 changes: 14 additions & 14 deletions internal/controller/stages/control_flow_stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func (r *ControlFlowStageReconciler) SetupWithManager(
if err := sharedIndexer.IndexField(
ctx,
&kargoapi.Freight{},
indexer.FreightByWarehouseIndexField,
indexer.FreightByWarehouseIndexer,
indexer.FreightByWarehouseField,
indexer.FreightByWarehouse,
); err != nil {
return fmt.Errorf("error setting up index for Freight by Warehouse: %w", err)
}
Expand All @@ -75,8 +75,8 @@ func (r *ControlFlowStageReconciler) SetupWithManager(
if err := sharedIndexer.IndexField(
ctx,
&kargoapi.Freight{},
indexer.FreightByVerifiedStagesIndexField,
indexer.FreightByVerifiedStagesIndexer,
indexer.FreightByVerifiedStagesField,
indexer.FreightByVerifiedStages,
); err != nil {
return fmt.Errorf("error setting up index for Freight by verified Stages: %w", err)
}
Expand All @@ -88,8 +88,8 @@ func (r *ControlFlowStageReconciler) SetupWithManager(
if err := sharedIndexer.IndexField(
ctx,
&kargoapi.Freight{},
indexer.FreightApprovedForStagesIndexField,
indexer.FreightApprovedForStagesIndexer,
indexer.FreightApprovedForStagesField,
indexer.FreightApprovedForStages,
); err != nil {
return fmt.Errorf("error setting up index for Freight approved for Stages: %w", err)
}
Expand All @@ -99,8 +99,8 @@ func (r *ControlFlowStageReconciler) SetupWithManager(
if err := sharedIndexer.IndexField(
ctx,
&kargoapi.Stage{},
indexer.StagesByUpstreamStagesIndexField,
indexer.StagesByUpstreamStagesIndexer,
indexer.StagesByUpstreamStagesField,
indexer.StagesByUpstreamStages,
); err != nil {
return fmt.Errorf("error setting up index for Stages by upstream Stages: %w", err)
}
Expand All @@ -110,8 +110,8 @@ func (r *ControlFlowStageReconciler) SetupWithManager(
if err := sharedIndexer.IndexField(
ctx,
&kargoapi.Stage{},
indexer.StagesByWarehouseIndexField,
indexer.StagesByWarehouseIndexer,
indexer.StagesByWarehouseField,
indexer.StagesByWarehouse,
); err != nil {
return fmt.Errorf("error setting up index for Stages by Warehouse: %w", err)
}
Expand Down Expand Up @@ -317,7 +317,7 @@ func (r *ControlFlowStageReconciler) getAvailableFreight(
&directFreight,
client.InNamespace(stage.Namespace),
client.MatchingFieldsSelector{
Selector: fields.OneTermEqualSelector(indexer.FreightByWarehouseIndexField, req.Origin.Name),
Selector: fields.OneTermEqualSelector(indexer.FreightByWarehouseField, req.Origin.Name),
},
); err != nil {
return nil, fmt.Errorf(
Expand Down Expand Up @@ -352,7 +352,7 @@ func (r *ControlFlowStageReconciler) getAvailableFreight(
&verifiedFreight,
client.InNamespace(stage.Namespace),
client.MatchingFieldsSelector{
Selector: fields.OneTermEqualSelector(indexer.FreightByVerifiedStagesIndexField, upstream),
Selector: fields.OneTermEqualSelector(indexer.FreightByVerifiedStagesField, upstream),
},
); err != nil {
return nil, fmt.Errorf(
Expand Down Expand Up @@ -508,7 +508,7 @@ func (r *ControlFlowStageReconciler) clearVerifications(ctx context.Context, sta
client.InNamespace(stage.Namespace),
client.MatchingFieldsSelector{
Selector: fields.OneTermEqualSelector(
indexer.FreightByVerifiedStagesIndexField,
indexer.FreightByVerifiedStagesField,
stage.Name,
),
},
Expand Down Expand Up @@ -552,7 +552,7 @@ func (r *ControlFlowStageReconciler) clearApprovals(ctx context.Context, stage *
client.InNamespace(stage.Namespace),
client.MatchingFieldsSelector{
Selector: fields.OneTermEqualSelector(
indexer.FreightApprovedForStagesIndexField,
indexer.FreightApprovedForStagesField,
stage.Name,
),
},
Expand Down
Loading
Loading