Skip to content
Open
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
10 changes: 6 additions & 4 deletions controllers/apps/apimanager_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
subController "github.com/3scale/3scale-operator/controllers/subscription"
"github.com/3scale/3scale-operator/pkg/3scale/amp/component"
"github.com/3scale/3scale-operator/pkg/3scale/amp/operator"
"github.com/3scale/3scale-operator/pkg/handlers"
"github.com/3scale/3scale-operator/pkg/helper"
"github.com/3scale/3scale-operator/pkg/reconcilers"
"github.com/3scale/3scale-operator/version"
Expand Down Expand Up @@ -315,10 +314,10 @@ func (r *APIManagerReconciler) SetupWithManager(mgr ctrl.Manager) error {
Namespace: r.WatchedNamespace,
}

handlers := &handlers.APIManagerRoutesEventMapper{
zyncRouteMapper := &ZyncRouteToAPIManagerMapper{
Context: r.Context(),
K8sClient: r.Client(),
Logger: r.Logger().WithName("APIManagerRoutesHandler"),
Logger: r.Logger().WithName("zyncRouteToAPIManagerMapper"),
}

operatorNamespace, err := helper.GetOperatorNamespace()
Expand All @@ -341,7 +340,10 @@ func (r *APIManagerReconciler) SetupWithManager(mgr ctrl.Manager) error {
builder.WithPredicates(labelSelectorPredicate),
).
Owns(&k8sappsv1.Deployment{}).
Watches(&routev1.Route{}, handler.EnqueueRequestsFromMapFunc(handlers.Map)).
Watches(
&routev1.Route{},
handler.EnqueueRequestsFromMapFunc(zyncRouteMapper.Map),
).
Watches(
&v1.ConfigMap{
ObjectMeta: apimachinerymetav1.ObjectMeta{
Expand Down
10 changes: 7 additions & 3 deletions controllers/apps/apimanager_status_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,15 @@ func getRequiredSecrets(namespace string) []runtime.Object {
}
}

// makeAdmittedRoute returns a Route with the Admitted condition set to True.
// makeAdmittedRoute returns a Route with the Admitted condition set to True and the Zync created-by label.
func makeAdmittedRoute(name, namespace, host string) *routev1.Route {
return &routev1.Route{
ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace},
Spec: routev1.RouteSpec{Host: host},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{zyncCreatedByLabel: zyncCreatedByValue},
},
Spec: routev1.RouteSpec{Host: host},
Status: routev1.RouteStatus{
Ingress: []routev1.RouteIngress{
{Conditions: []routev1.RouteIngressCondition{
Expand Down
57 changes: 57 additions & 0 deletions controllers/apps/zync_route_to_apimanager_event_mapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package controllers

import (
"context"

"github.com/go-logr/logr"
routev1 "github.com/openshift/api/route/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

appsv1alpha1 "github.com/3scale/3scale-operator/apis/apps/v1alpha1"
)

const zyncCreatedByLabel = "3scale.net/created-by"
const zyncCreatedByValue = "zync"

type ZyncRouteToAPIManagerMapper struct {
Context context.Context
K8sClient client.Client
Logger logr.Logger
}

func (h *ZyncRouteToAPIManagerMapper) Map(ctx context.Context, o client.Object) []reconcile.Request {
route, ok := o.(*routev1.Route)
if !ok {
return nil
}

apimanagerList := &appsv1alpha1.APIManagerList{}
if err := h.K8sClient.List(ctx, apimanagerList, client.InNamespace(o.GetNamespace())); err != nil {
h.Logger.Error(err, "failed to list APIManagers")
return nil
}

var requests []reconcile.Request
for i := range apimanagerList.Items {
if routeHostMatchesAPIManager(route.Spec.Host, &apimanagerList.Items[i]) {
requests = append(requests, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: apimanagerList.Items[i].Name,
Namespace: apimanagerList.Items[i].Namespace,
},
})
}
}
return requests
}

func routeHostMatchesAPIManager(routeHost string, apimanager *appsv1alpha1.APIManager) bool {
for _, expectedHost := range apimanagerExpectedRouteHosts(apimanager) {
if expectedHost == routeHost {
return true
}
}
return false
}
132 changes: 132 additions & 0 deletions controllers/apps/zync_route_to_apimanager_event_mapper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package controllers

import (
"context"
"testing"

"github.com/go-logr/logr"
routev1 "github.com/openshift/api/route/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

appsv1alpha1 "github.com/3scale/3scale-operator/apis/apps/v1alpha1"
)

func TestZyncRouteToAPIManagerMapperMap(t *testing.T) {
const namespace = "test-ns"
const otherNamespace = "other-ns"

am := getTestAPIManager(namespace)
am.Name = "main-apimanager"

amOtherNs := getTestAPIManager(otherNamespace)
amOtherNs.Name = "other-apimanager"

s := scheme.Scheme
s.AddKnownTypes(appsv1alpha1.GroupVersion, am, &appsv1alpha1.APIManagerList{})
if err := routev1.Install(s); err != nil {
t.Fatal(err)
}

newRoute := func(host, ns string) *routev1.Route {
return &routev1.Route{
TypeMeta: metav1.TypeMeta{Kind: "Route", APIVersion: "route.openshift.io/v1"},
ObjectMeta: metav1.ObjectMeta{Name: "a-route", Namespace: ns},
Spec: routev1.RouteSpec{Host: host},
}
}

enqueuesMainAM := []reconcile.Request{
{NamespacedName: types.NamespacedName{Name: "main-apimanager", Namespace: namespace}},
}

tests := []struct {
name string
objects []runtime.Object
route *routev1.Route
want []reconcile.Request
}{
{
name: "backend listener route host enqueues APIManager",
objects: []runtime.Object{am},
route: newRoute("backend-3scale.test.example.com", namespace),
want: enqueuesMainAM,
},
{
name: "apicast production route host enqueues APIManager",
objects: []runtime.Object{am},
route: newRoute("api-3scale-apicast-production.test.example.com", namespace),
want: enqueuesMainAM,
},
{
name: "apicast staging route host enqueues APIManager",
objects: []runtime.Object{am},
route: newRoute("api-3scale-apicast-staging.test.example.com", namespace),
want: enqueuesMainAM,
},
{
name: "master portal route host enqueues APIManager",
objects: []runtime.Object{am},
route: newRoute("master.test.example.com", namespace),
want: enqueuesMainAM,
},
{
name: "developer portal route host enqueues APIManager",
objects: []runtime.Object{am},
route: newRoute("3scale.test.example.com", namespace),
want: enqueuesMainAM,
},
{
name: "admin portal route host enqueues APIManager",
objects: []runtime.Object{am},
route: newRoute("3scale-admin.test.example.com", namespace),
want: enqueuesMainAM,
},
{
name: "route host does not match any APIManager — no requests",
objects: []runtime.Object{am},
route: newRoute("unrelated.example.com", namespace),
want: nil,
},
{
name: "no APIManagers in namespace — no requests",
objects: []runtime.Object{},
route: newRoute("backend-3scale.test.example.com", namespace),
want: nil,
},
{
name: "route in different namespace matches only that namespace's APIManager",
objects: []runtime.Object{am, amOtherNs},
route: newRoute("backend-3scale.test.example.com", otherNamespace),
want: []reconcile.Request{
{NamespacedName: types.NamespacedName{Name: "other-apimanager", Namespace: otherNamespace}},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(tt.objects...).Build()
mapper := &ZyncRouteToAPIManagerMapper{
Context: context.TODO(),
K8sClient: cl,
Logger: logr.Discard(),
}

got := mapper.Map(context.TODO(), tt.route)

if len(got) != len(tt.want) {
t.Fatalf("Map() returned %d requests, want %d: got %v", len(got), len(tt.want), got)
}
for i := range tt.want {
if got[i] != tt.want[i] {
t.Errorf("Map()[%d] = %v, want %v", i, got[i], tt.want[i])
}
}
})
}
}
101 changes: 0 additions & 101 deletions pkg/handlers/apimanager_managed_routes_event_mapper.go

This file was deleted.

Loading
Loading