-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor SecurityPolicy controller For Kuadrants
Signed-off-by: Adam Cattermole <[email protected]>
- Loading branch information
1 parent
238acdc
commit 888f81d
Showing
5 changed files
with
260 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package mappers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
kuadrantv1beta1 "github.com/kuadrant/kuadrant-operator/api/v1beta1" | ||
"github.com/kuadrant/kuadrant-operator/pkg/library/kuadrant" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1" | ||
) | ||
|
||
type GatewayToKuadrantEventMapper struct { | ||
opts MapperOptions | ||
} | ||
|
||
func NewGatewayToKuadrantEventMapper(o ...MapperOption) *GatewayToKuadrantEventMapper { | ||
return &GatewayToKuadrantEventMapper{opts: Apply(o...)} | ||
} | ||
|
||
func (m *GatewayToKuadrantEventMapper) Map(ctx context.Context, obj client.Object) []reconcile.Request { | ||
logger := m.opts.Logger.WithValues("object", client.ObjectKeyFromObject(obj)) | ||
|
||
gw, ok := obj.(*gatewayapiv1.Gateway) | ||
if !ok { | ||
logger.Error(fmt.Errorf("%T is not a *gatweayapiv1.Gateway", obj), "cannot map") | ||
return []reconcile.Request{} | ||
} | ||
|
||
if !kuadrant.IsKuadrantManaged(gw) { | ||
logger.V(1).Info("gateway is not kuadrant managed", "gateway", gw) | ||
return []reconcile.Request{} | ||
} | ||
|
||
kuadrantNamespace, err := kuadrant.GetKuadrantNamespace(gw) | ||
if err != nil { | ||
logger.Error(err, "cannot get kuadrant namespace") | ||
return []reconcile.Request{} | ||
} | ||
kuadrantList := &kuadrantv1beta1.KuadrantList{} | ||
err = m.opts.Client.List(ctx, kuadrantList, &client.ListOptions{Namespace: kuadrantNamespace}) | ||
if err != nil { | ||
logger.Error(err, "cannot list kuadrants") | ||
return []reconcile.Request{} | ||
} | ||
if len(kuadrantList.Items) == 0 { | ||
logger.Error(err, "kuadrant does not exist in expected namespace") | ||
return []reconcile.Request{} | ||
} | ||
|
||
return []reconcile.Request{{NamespacedName: client.ObjectKeyFromObject(&kuadrantList.Items[0])}} | ||
} |
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,65 @@ | ||
package mappers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
kuadrantv1beta1 "github.com/kuadrant/kuadrant-operator/api/v1beta1" | ||
kuadrantgatewayapi "github.com/kuadrant/kuadrant-operator/pkg/library/gatewayapi" | ||
"github.com/kuadrant/kuadrant-operator/pkg/library/kuadrant" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1" | ||
) | ||
|
||
type HTTPRouteToKuadrantEventMapper struct { | ||
opts MapperOptions | ||
} | ||
|
||
func NewHTTPRouteToKuadrantEventMapper(o ...MapperOption) *HTTPRouteToKuadrantEventMapper { | ||
return &HTTPRouteToKuadrantEventMapper{opts: Apply(o...)} | ||
} | ||
|
||
func (m *HTTPRouteToKuadrantEventMapper) Map(ctx context.Context, obj client.Object) []reconcile.Request { | ||
logger := m.opts.Logger.WithValues("object", client.ObjectKeyFromObject(obj)) | ||
|
||
httpRoute, ok := obj.(*gatewayapiv1.HTTPRoute) | ||
if !ok { | ||
logger.Error(fmt.Errorf("%T is not a *gatweayapiv1.HTTPRoute", obj), "cannot map") | ||
return []reconcile.Request{} | ||
} | ||
|
||
gatewayKeys := kuadrantgatewayapi.GetRouteAcceptedGatewayParentKeys(httpRoute) | ||
|
||
for _, gatewayKey := range gatewayKeys { | ||
gateway := &gatewayapiv1.Gateway{} | ||
err := m.opts.Client.Get(ctx, gatewayKey, gateway) | ||
if err != nil { | ||
logger.Info("cannot get gateway", "error", err) | ||
continue | ||
} | ||
|
||
if !kuadrant.IsKuadrantManaged(gateway) { | ||
logger.V(1).Info("gateway is not kuadrant managed", "gateway", gateway) | ||
continue | ||
} | ||
kuadrantNamespace, err := kuadrant.GetKuadrantNamespace(gateway) | ||
if err != nil { | ||
logger.Error(err, "cannot get kuadrant namespace") | ||
continue | ||
} | ||
kuadrantList := &kuadrantv1beta1.KuadrantList{} | ||
err = m.opts.Client.List(ctx, kuadrantList, &client.ListOptions{Namespace: kuadrantNamespace}) | ||
if err != nil { | ||
logger.Error(err, "cannot list kuadrants") | ||
return []reconcile.Request{} | ||
} | ||
if len(kuadrantList.Items) == 0 { | ||
logger.Error(err, "kuadrant does not exist in expected namespace") | ||
return []reconcile.Request{} | ||
} | ||
return []reconcile.Request{{NamespacedName: client.ObjectKeyFromObject(&kuadrantList.Items[0])}} | ||
} | ||
logger.V(1).Info("no matching kuadrant instance found") | ||
return []reconcile.Request{} | ||
} |
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,47 @@ | ||
package mappers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
kuadrantv1beta1 "github.com/kuadrant/kuadrant-operator/api/v1beta1" | ||
"github.com/kuadrant/kuadrant-operator/pkg/library/kuadrant" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
) | ||
|
||
type PolicyToKuadrantEventMapper struct { | ||
opts MapperOptions | ||
} | ||
|
||
func NewPolicyToKuadrantEventMapper(o ...MapperOption) *PolicyToKuadrantEventMapper { | ||
return &PolicyToKuadrantEventMapper{opts: Apply(o...)} | ||
} | ||
|
||
func (m *PolicyToKuadrantEventMapper) Map(ctx context.Context, obj client.Object) []reconcile.Request { | ||
logger := m.opts.Logger.WithValues("object", client.ObjectKeyFromObject(obj)) | ||
|
||
policy, ok := obj.(kuadrant.Policy) | ||
if !ok { | ||
logger.Error(fmt.Errorf("%T is not a kuadrant.Policy", obj), "cannot map") | ||
return []reconcile.Request{} | ||
} | ||
|
||
kuadrantNamespace, err := kuadrant.GetKuadrantNamespaceFromPolicyTargetRef(ctx, m.opts.Client, policy) | ||
if err != nil { | ||
logger.Error(err, "cannot get kuadrant namespace") | ||
return []reconcile.Request{} | ||
} | ||
kuadrantList := &kuadrantv1beta1.KuadrantList{} | ||
err = m.opts.Client.List(ctx, kuadrantList, &client.ListOptions{Namespace: kuadrantNamespace}) | ||
if err != nil { | ||
logger.Error(err, "cannot list kuadrants") | ||
return []reconcile.Request{} | ||
} | ||
if len(kuadrantList.Items) == 0 { | ||
logger.Error(err, "kuadrant does not exist in expected namespace") | ||
return []reconcile.Request{} | ||
} | ||
|
||
return []reconcile.Request{{NamespacedName: client.ObjectKeyFromObject(&kuadrantList.Items[0])}} | ||
} |