-
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.
migrate to controller-runtime controllers for k8s watch events
This replaces the internal informers and moves to the controller-runtime controllers setting up Contour to migrate over to the controller-runtime caches. Updates #2683 Signed-off-by: Steve Sloka <slokas@vmware.com>
- Loading branch information
1 parent
acf533c
commit 5c18ee2
Showing
18 changed files
with
1,166 additions
and
118 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,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 backendPolicyReconciler struct { | ||
client client.Client | ||
eventHandler cache.ResourceEventHandler | ||
logrus.FieldLogger | ||
} | ||
|
||
// NewBackendPolicyController creates the backendpolicy controller from mgr. The controller will be pre-configured | ||
// to watch for BackendPolicy objects across all namespaces. | ||
func NewBackendPolicyController(mgr manager.Manager, eventHandler cache.ResourceEventHandler, log logrus.FieldLogger) (controller.Controller, error) { | ||
r := &backendPolicyReconciler{ | ||
client: mgr.GetClient(), | ||
eventHandler: eventHandler, | ||
FieldLogger: log, | ||
} | ||
c, err := controller.New("backendpolicy-controller", mgr, controller.Options{Reconciler: r}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err := c.Watch(&source.Kind{Type: &gatewayapi_v1alpha1.BackendPolicy{}}, &handler.EnqueueRequestForObject{}); err != nil { | ||
return nil, err | ||
} | ||
return c, nil | ||
} | ||
|
||
func (r *backendPolicyReconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { | ||
|
||
// Fetch the BackendPolicy from the cache. | ||
gateway := &gatewayapi_v1alpha1.BackendPolicy{} | ||
err := r.client.Get(ctx, request.NamespacedName, gateway) | ||
if errors.IsNotFound(err) { | ||
r.Error(nil, "Could not find BackendPolicy %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 | ||
} |
Oops, something went wrong.