This repository has been archived by the owner on Dec 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Watch policies configured by Gateway params
- Loading branch information
1 parent
0a747a1
commit 7c23b95
Showing
10 changed files
with
573 additions
and
5 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
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,82 @@ | ||
package policysync | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/go-logr/logr" | ||
|
||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/client-go/dynamic" | ||
"k8s.io/client-go/tools/cache" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1" | ||
) | ||
|
||
type ResourceEventHandler struct { | ||
Log logr.Logger | ||
GVR schema.GroupVersionResource | ||
Client client.Client | ||
DynamicClient dynamic.Interface | ||
Gateway *gatewayv1beta1.Gateway | ||
|
||
Syncer Syncer | ||
} | ||
|
||
var _ cache.ResourceEventHandler = &ResourceEventHandler{} | ||
|
||
func (h *ResourceEventHandler) OnAdd(reqObj interface{}) { | ||
h.Log.Info("Got watch event for policy", "obj", reqObj) | ||
|
||
ctx := context.Background() | ||
|
||
obj, ok := reqObj.(client.Object) | ||
if !ok { | ||
h.Log.Error(fmt.Errorf("object %v does not inplement client.Object", reqObj), "") | ||
return | ||
} | ||
|
||
if err := h.Client.Get(ctx, client.ObjectKeyFromObject(obj), obj); err != nil { | ||
h.Log.Error(err, "failed to get object", "object", obj) | ||
} | ||
|
||
policy, err := NewPolicyFor(obj) | ||
if err != nil { | ||
h.Log.Error(err, "failed to build policy from watched object", "object", obj) | ||
return | ||
} | ||
|
||
if err := h.Syncer.SyncPolicy(context.Background(), h.Client, policy); err != nil { | ||
h.Log.Error(err, "failed to sync policy", "policy", policy) | ||
} | ||
} | ||
|
||
func (h *ResourceEventHandler) OnDelete(obj interface{}) { | ||
h.Log.Info("Got watch event for policy", "obj", obj) | ||
} | ||
|
||
func (h *ResourceEventHandler) OnUpdate(_ interface{}, reqObj interface{}) { | ||
h.Log.Info("Got watch event for policy", "obj", reqObj) | ||
|
||
ctx := context.Background() | ||
|
||
obj, ok := reqObj.(client.Object) | ||
if !ok { | ||
h.Log.Error(fmt.Errorf("object %v does not inplement client.Object", reqObj), "") | ||
return | ||
} | ||
|
||
if err := h.Client.Get(ctx, client.ObjectKeyFromObject(obj), obj); err != nil { | ||
h.Log.Error(err, "failed to get object", "object", obj) | ||
} | ||
|
||
policy, err := NewPolicyFor(obj) | ||
if err != nil { | ||
h.Log.Error(err, "failed to build policy from watched object", "object", obj) | ||
return | ||
} | ||
|
||
if err := h.Syncer.SyncPolicy(context.Background(), h.Client, policy); err != nil { | ||
h.Log.Error(err, "failed to sync policy", "policy", policy) | ||
} | ||
} |
Oops, something went wrong.