-
Notifications
You must be signed in to change notification settings - Fork 665
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move to controller-runtime for gateway-api types.
Moves from informers to controller-runtime for gateway-api processing. Signed-off-by: Steve Sloka <slokas@vmware.com>
- Loading branch information
1 parent
b172ac6
commit e3a073b
Showing
4 changed files
with
257 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright Project Contour Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cache | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/sirupsen/logrus" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/client-go/tools/cache" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
"sigs.k8s.io/controller-runtime/pkg/source" | ||
gatewayapi_v1alpha1 "sigs.k8s.io/gateway-api/apis/v1alpha1" | ||
) | ||
|
||
type gatewayReconciler struct { | ||
client client.Client | ||
eventHandler cache.ResourceEventHandler | ||
logrus.FieldLogger | ||
} | ||
|
||
// NewGatewayController creates the gateway controller from mgr. The controller will be pre-configured | ||
// to watch for Gateway objects across all namespaces. | ||
func NewGatewayController(mgr manager.Manager, eventHandler cache.ResourceEventHandler, log logrus.FieldLogger) (controller.Controller, error) { | ||
r := &gatewayReconciler{ | ||
client: mgr.GetClient(), | ||
eventHandler: eventHandler, | ||
FieldLogger: log, | ||
} | ||
c, err := controller.New("gateway-controller", mgr, controller.Options{Reconciler: r}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err := c.Watch(&source.Kind{Type: &gatewayapi_v1alpha1.Gateway{}}, &handler.EnqueueRequestForObject{}); err != nil { | ||
return nil, err | ||
} | ||
return c, nil | ||
} | ||
|
||
func (r *gatewayReconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { | ||
|
||
// Fetch the Gateway from the cache. | ||
gateway := &gatewayapi_v1alpha1.Gateway{} | ||
err := r.client.Get(ctx, request.NamespacedName, gateway) | ||
if errors.IsNotFound(err) { | ||
r.Error(nil, "Could not find Gateway %q in Namespace %q", request.Name, request.Namespace) | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
// Check if object is deleted. | ||
if !gateway.ObjectMeta.DeletionTimestamp.IsZero() { | ||
r.eventHandler.OnDelete(gateway) | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
// Pass the new changed object off to the eventHandler. | ||
r.eventHandler.OnAdd(gateway) | ||
|
||
return reconcile.Result{}, 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 Project Contour Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cache | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/sirupsen/logrus" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/client-go/tools/cache" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
"sigs.k8s.io/controller-runtime/pkg/source" | ||
gatewayapi_v1alpha1 "sigs.k8s.io/gateway-api/apis/v1alpha1" | ||
) | ||
|
||
type httpRouteReconciler struct { | ||
client client.Client | ||
eventHandler cache.ResourceEventHandler | ||
logrus.FieldLogger | ||
} | ||
|
||
// NewHTTPRouteController creates the httproute controller from mgr. The controller will be pre-configured | ||
// to watch for HTTPRoute objects across all namespaces. | ||
func NewHTTPRouteController(mgr manager.Manager, eventHandler cache.ResourceEventHandler, log logrus.FieldLogger) (controller.Controller, error) { | ||
r := &httpRouteReconciler{ | ||
client: mgr.GetClient(), | ||
eventHandler: eventHandler, | ||
FieldLogger: log, | ||
} | ||
c, err := controller.New("httproute-controller", mgr, controller.Options{Reconciler: r}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err := c.Watch(&source.Kind{Type: &gatewayapi_v1alpha1.HTTPRoute{}}, &handler.EnqueueRequestForObject{}); err != nil { | ||
return nil, err | ||
} | ||
return c, nil | ||
} | ||
|
||
func (r *httpRouteReconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { | ||
|
||
// Fetch the HTTPRoute from the cache. | ||
gateway := &gatewayapi_v1alpha1.HTTPRoute{} | ||
err := r.client.Get(ctx, request.NamespacedName, gateway) | ||
if errors.IsNotFound(err) { | ||
r.Error(nil, "Could not find HTTPRoute %q in Namespace %q", request.Name, request.Namespace) | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
// Check if object is deleted. | ||
if !gateway.ObjectMeta.DeletionTimestamp.IsZero() { | ||
r.eventHandler.OnDelete(gateway) | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
// Pass the new changed object off to the eventHandler. | ||
r.eventHandler.OnAdd(gateway) | ||
|
||
return reconcile.Result{}, 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 Project Contour Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cache | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/sirupsen/logrus" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/client-go/tools/cache" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
"sigs.k8s.io/controller-runtime/pkg/source" | ||
gatewayapi_v1alpha1 "sigs.k8s.io/gateway-api/apis/v1alpha1" | ||
) | ||
|
||
type tlsRouteReconciler struct { | ||
client client.Client | ||
eventHandler cache.ResourceEventHandler | ||
logrus.FieldLogger | ||
} | ||
|
||
// NewTLSRouteController creates the tlsroute controller from mgr. The controller will be pre-configured | ||
// to watch for TLSRoute objects across all namespaces. | ||
func NewTLSRouteController(mgr manager.Manager, eventHandler cache.ResourceEventHandler, log logrus.FieldLogger) (controller.Controller, error) { | ||
r := &tlsRouteReconciler{ | ||
client: mgr.GetClient(), | ||
eventHandler: eventHandler, | ||
FieldLogger: log, | ||
} | ||
c, err := controller.New("tlsroute-controller", mgr, controller.Options{Reconciler: r}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err := c.Watch(&source.Kind{Type: &gatewayapi_v1alpha1.TLSRoute{}}, &handler.EnqueueRequestForObject{}); err != nil { | ||
return nil, err | ||
} | ||
return c, nil | ||
} | ||
|
||
func (r *tlsRouteReconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { | ||
|
||
// Fetch the TLSRoute from the cache. | ||
tlsroute := &gatewayapi_v1alpha1.TLSRoute{} | ||
err := r.client.Get(ctx, request.NamespacedName, tlsroute) | ||
if errors.IsNotFound(err) { | ||
r.Error(nil, "Could not find TLSRoute %q in Namespace %q", request.Name, request.Namespace) | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
// Check if object is deleted. | ||
if !tlsroute.ObjectMeta.DeletionTimestamp.IsZero() { | ||
r.eventHandler.OnDelete(tlsroute) | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
// Pass the new changed object off to the eventHandler. | ||
r.eventHandler.OnAdd(tlsroute) | ||
|
||
return reconcile.Result{}, nil | ||
} |