-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NET-7156] Gateways Controllers Reusability (#3574)
* make controller setup for gateway controllers generic and reusable, add indices onto gateway resources in k8s for more efficient lookups * cleanup from PR review * Update control-plane/controllers/resources/gateway_controller_setup.go Co-authored-by: Nathan Coleman <nathan.coleman@hashicorp.com> * Update control-plane/controllers/resources/gateway_indices.go Co-authored-by: Nathan Coleman <nathan.coleman@hashicorp.com> * Update control-plane/controllers/resources/gateway_controller_setup.go Co-authored-by: Nathan Coleman <nathan.coleman@hashicorp.com> * Update control-plane/controllers/resources/gateway_controller_setup.go Co-authored-by: Nathan Coleman <nathan.coleman@hashicorp.com> * clean up from PR review --------- Co-authored-by: Nathan Coleman <nathan.coleman@hashicorp.com>
- Loading branch information
1 parent
feb3f6e
commit f016fd5
Showing
7 changed files
with
251 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
control-plane/controllers/resources/gateway_controller_setup.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package resources | ||
|
||
import ( | ||
"context" | ||
|
||
meshv2beta1 "github.com/hashicorp/consul-k8s/control-plane/api/mesh/v2beta1" | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
rbacv1 "k8s.io/api/rbac/v1" | ||
"k8s.io/apimachinery/pkg/fields" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
"sigs.k8s.io/controller-runtime/pkg/source" | ||
) | ||
|
||
type gatewayList interface { | ||
*meshv2beta1.MeshGatewayList | *meshv2beta1.APIGatewayList | ||
client.ObjectList | ||
ReconcileRequests() []reconcile.Request | ||
} | ||
|
||
func setupGatewayControllerWithManager[L gatewayList](mgr ctrl.Manager, obj client.Object, k8sClient client.Client, gwc reconcile.Reconciler, index indexName) error { | ||
return ctrl.NewControllerManagedBy(mgr). | ||
For(obj). | ||
Owns(&appsv1.Deployment{}). | ||
Owns(&rbacv1.Role{}). | ||
Owns(&rbacv1.RoleBinding{}). | ||
Owns(&corev1.Service{}). | ||
Owns(&corev1.ServiceAccount{}). | ||
Watches( | ||
source.NewKindWithCache(&meshv2beta1.GatewayClass{}, mgr.GetCache()), | ||
handler.EnqueueRequestsFromMapFunc(func(o client.Object) []reconcile.Request { | ||
gc := o.(*meshv2beta1.GatewayClass) | ||
if gc == nil { | ||
return nil | ||
} | ||
|
||
gateways, err := getGatewaysReferencingGatewayClass[L](context.Background(), k8sClient, gc.Name, index) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
return gateways.ReconcileRequests() | ||
})). | ||
Watches( | ||
source.NewKindWithCache(&meshv2beta1.GatewayClassConfig{}, mgr.GetCache()), | ||
handler.EnqueueRequestsFromMapFunc(func(o client.Object) []reconcile.Request { | ||
gcc := o.(*meshv2beta1.GatewayClassConfig) | ||
if gcc == nil { | ||
return nil | ||
} | ||
|
||
classes, err := getGatewayClassesReferencingGatewayClassConfig(context.Background(), k8sClient, gcc.Name) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
var requests []reconcile.Request | ||
for _, class := range classes.Items { | ||
if class == nil { | ||
continue | ||
} | ||
|
||
gateways, err := getGatewaysReferencingGatewayClass[L](context.Background(), k8sClient, class.Name, index) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
requests = append(requests, gateways.ReconcileRequests()...) | ||
} | ||
|
||
return requests | ||
})). | ||
Complete(gwc) | ||
} | ||
|
||
// TODO: uncomment when moving the CRUD hooks from mesh gateway controller | ||
//func getGatewayClassConfigByGatewayClassName(ctx context.Context, k8sClient client.Client, className string) (*meshv2beta1.GatewayClassConfig, error) { | ||
// gatewayClass, err := getGatewayClassByName(ctx, k8sClient, className) | ||
// if err != nil { | ||
// return nil, err | ||
// } | ||
// | ||
// if gatewayClass == nil { | ||
// return nil, nil | ||
// } | ||
// | ||
// gatewayClassConfig := &meshv2beta1.GatewayClassConfig{} | ||
// if ref := gatewayClass.Spec.ParametersRef; ref != nil { | ||
// if ref.Group != meshv2beta1.MeshGroup || ref.Kind != v2beta1.KindGatewayClassConfig { | ||
// // TODO @Gateway-Management additionally check for controller name when available | ||
// return nil, nil | ||
// } | ||
// | ||
// if err := k8sClient.Get(ctx, types.NamespacedName{Name: ref.Name}, gatewayClassConfig); err != nil { | ||
// return nil, client.IgnoreNotFound(err) | ||
// } | ||
// } | ||
// return gatewayClassConfig, nil | ||
//} | ||
// | ||
//func getGatewayClassByName(ctx context.Context, k8sClient client.Client, className string) (*meshv2beta1.GatewayClass, error) { | ||
//var gatewayClass meshv2beta1.GatewayClass | ||
// | ||
// if err := k8sClient.Get(ctx, types.NamespacedName{Name: className}, &gatewayClass); err != nil { | ||
// return nil, client.IgnoreNotFound(err) | ||
// } | ||
// return &gatewayClass, nil | ||
//} | ||
|
||
// getGatewayClassesReferencingGatewayClassConfig queries all GatewayClass resources in the | ||
// cluster and returns any that reference the given GatewayClassConfig by name. | ||
func getGatewayClassesReferencingGatewayClassConfig(ctx context.Context, k8sClient client.Client, configName string) (*meshv2beta1.GatewayClassList, error) { | ||
allClasses := &meshv2beta1.GatewayClassList{} | ||
if err := k8sClient.List(ctx, allClasses, &client.ListOptions{ | ||
FieldSelector: fields.OneTermEqualSelector(string(GatewayClass_GatewayClassConfigIndex), configName), | ||
}); err != nil { | ||
return nil, client.IgnoreNotFound(err) | ||
} | ||
|
||
return allClasses, nil | ||
} | ||
|
||
// getGatewaysReferencingGatewayClass queries all xGateway resources in the cluster | ||
// and returns any that reference the given GatewayClass by name. | ||
func getGatewaysReferencingGatewayClass[T gatewayList](ctx context.Context, k8sClient client.Client, className string, index indexName) (T, error) { | ||
var allGateways T | ||
if err := k8sClient.List(ctx, allGateways, &client.ListOptions{ | ||
FieldSelector: fields.OneTermEqualSelector(string(index), className), | ||
}); err != nil { | ||
return nil, client.IgnoreNotFound(err) | ||
} | ||
|
||
return allGateways, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package resources | ||
|
||
import ( | ||
"context" | ||
|
||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/hashicorp/consul-k8s/control-plane/api/mesh/v2beta1" | ||
meshv2beta1 "github.com/hashicorp/consul-k8s/control-plane/api/mesh/v2beta1" | ||
) | ||
|
||
type indexName string | ||
|
||
const ( | ||
// Naming convention: TARGET_REFERENCE. | ||
GatewayClass_GatewayClassConfigIndex indexName = "__v2_gatewayclass_referencing_gatewayclassconfig" | ||
|
||
APIGateway_GatewayClassIndex indexName = "__v2_api_gateway_referencing_gatewayclass" | ||
MeshGateway_GatewayClassIndex indexName = "__v2_mesh_gateway_referencing_gatewayclass" | ||
) | ||
|
||
// RegisterGatewayFieldIndexes registers all of the field indexes for the xGateway controllers. | ||
// These indexes are similar to indexes used in databases to speed up queries. | ||
// They allow us to quickly find objects based on a field value. | ||
func RegisterGatewayFieldIndexes(ctx context.Context, mgr ctrl.Manager) error { | ||
for _, index := range indexes { | ||
if err := mgr.GetFieldIndexer().IndexField(ctx, index.target, string(index.name), index.indexerFunc); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
type index struct { | ||
name indexName | ||
target client.Object | ||
indexerFunc client.IndexerFunc | ||
} | ||
|
||
var indexes = []index{ | ||
{ | ||
name: GatewayClass_GatewayClassConfigIndex, | ||
target: &meshv2beta1.GatewayClass{}, | ||
indexerFunc: func(o client.Object) []string { | ||
gc := o.(*meshv2beta1.GatewayClass) | ||
|
||
pr := gc.Spec.ParametersRef | ||
if pr != nil && pr.Kind == v2beta1.KindGatewayClassConfig { | ||
return []string{pr.Name} | ||
} | ||
|
||
return []string{} | ||
}, | ||
}, | ||
{ | ||
name: APIGateway_GatewayClassIndex, | ||
target: &meshv2beta1.APIGateway{}, | ||
indexerFunc: func(o client.Object) []string { | ||
g := o.(*meshv2beta1.APIGateway) | ||
return []string{string(g.Spec.GatewayClassName)} | ||
}, | ||
}, | ||
{ | ||
name: MeshGateway_GatewayClassIndex, | ||
target: &meshv2beta1.MeshGateway{}, | ||
indexerFunc: func(o client.Object) []string { | ||
g := o.(*meshv2beta1.MeshGateway) | ||
return []string{string(g.Spec.GatewayClassName)} | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.