Skip to content

Commit

Permalink
Add watch to in-memory server multiplexer
Browse files Browse the repository at this point in the history
Signed-off-by: killianmuldoon <kmuldoon@vmware.com>
  • Loading branch information
killianmuldoon committed Jun 19, 2023
1 parent 238ca15 commit dfac09e
Show file tree
Hide file tree
Showing 9 changed files with 350 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Cache interface {
// Informer forwards events to event handlers.
type Informer interface {
AddEventHandler(handler InformEventHandler) error
RemoveEventHandler(handler InformEventHandler) error
}

// InformEventHandler handle events originated by a source.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,8 @@ func (c *cache) doTryDeleteLocked(resourceGroup string, tracker *resourceGroupTr
delete(tracker.ownedObjects, ownReference{gvk: objGVK, key: objKey})
}

// If the object still has finalizers, only set the deletion timestamp if not already set.
if len(obj.GetFinalizers()) > 0 {
if !obj.GetDeletionTimestamp().IsZero() {
return false, nil
}
// Set the deletion timestamp if not already set.
if obj.GetDeletionTimestamp().IsZero() {
if err := c.beforeDelete(resourceGroup, obj); err != nil {
return false, apierrors.NewBadRequest(err.Error())
}
Expand All @@ -424,13 +421,18 @@ func (c *cache) doTryDeleteLocked(resourceGroup string, tracker *resourceGroupTr
if err := c.beforeUpdate(resourceGroup, oldObj, obj); err != nil {
return false, apierrors.NewBadRequest(err.Error())
}

// TODO: (killianmuldoon) Understand if setting this twice is necessary.
// Required to override default beforeUpdate behaviour
// that prevent changes to automatically managed fields.
obj.SetDeletionTimestamp(&now)

objects[objKey] = obj
c.afterUpdate(resourceGroup, oldObj, obj)
}

// If the object still has finalizers return early.
if len(obj.GetFinalizers()) > 0 {
return false, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,11 @@ func (i *fakeInformer) AddEventHandler(handler InformEventHandler) error {
return nil
}

func (i *fakeInformer) RemoveEventHandler(_ InformEventHandler) error {
i.handler = nil
return nil
}

func (i *fakeInformer) InformCreate(resourceGroup string, obj client.Object) {
i.handler.OnCreate(resourceGroup, obj)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ func (i *informer) AddEventHandler(handler InformEventHandler) error {
return nil
}

func (i *informer) RemoveEventHandler(handler InformEventHandler) error {
i.lock.Lock()
defer i.lock.Unlock()
for j, h := range i.handlers {
if h == handler {
i.handlers = append(i.handlers[:j], i.handlers[j+1:]...)
}
}
return nil
}

func (c *cache) GetInformer(ctx context.Context, obj client.Object) (Informer, error) {
gvk, err := apiutil.GVKForObject(obj, c.scheme)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ func (i *fakeInformer) AddEventHandler(handler ccache.InformEventHandler) error
return nil
}

func (i *fakeInformer) RemoveEventHandler(_ ccache.InformEventHandler) error {
i.handler = nil
return nil
}

func (i *fakeInformer) InformCreate(resourceGroup string, obj client.Object) {
i.handler.OnCreate(resourceGroup, obj)
}
Expand Down
10 changes: 10 additions & 0 deletions test/infrastructure/inmemory/internal/server/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@ func (h *apiServerHandler) apiV1List(req *restful.Request, resp *restful.Respons
return
}

// If the request is a Watch handle it using watchForResource.
if isWatch(req) {
err = h.watchForResource(req, resp, resourceGroup, *gvk)
if err != nil {
_ = resp.WriteErrorString(http.StatusInternalServerError, err.Error())
return
}
return
}

// Reads and returns the requested data.
list := &unstructured.UnstructuredList{}
list.SetAPIVersion(gvk.GroupVersion().String())
Expand Down
186 changes: 186 additions & 0 deletions test/infrastructure/inmemory/internal/server/api/watch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/*
Copyright 2023 The Kubernetes 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 api

import (
"context"
"fmt"
"net/http"
"time"

"github.com/emicklei/go-restful/v3"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// Event records a lifecycle event for a Kubernetes object.
type Event struct {
Type watch.EventType `json:"type,omitempty"`
Object runtime.Object `json:"object,omitempty"`
}

// WatchEventDispatcher dispatches events for a single resourceGroup.
type WatchEventDispatcher struct {
resourceGroup string
events chan (*Event)
}

// OnCreate dispatches Create events.
func (m *WatchEventDispatcher) OnCreate(resourceGroup string, o client.Object) {
if resourceGroup != m.resourceGroup {
return
}
m.events <- &Event{
Type: watch.Added,
Object: o,
}
}

// OnUpdate dispatches Update events.
func (m *WatchEventDispatcher) OnUpdate(resourceGroup string, _, o client.Object) {
if resourceGroup != m.resourceGroup {
return
}
m.events <- &Event{
Type: watch.Modified,
Object: o,
}
}

// OnDelete dispatches Delete events.
func (m *WatchEventDispatcher) OnDelete(resourceGroup string, o client.Object) {
if resourceGroup != m.resourceGroup {
return
}
m.events <- &Event{
Type: watch.Deleted,
Object: o,
}
}

// OnGeneric dispatches Generic events.
func (m *WatchEventDispatcher) OnGeneric(resourceGroup string, o client.Object) {
if resourceGroup != m.resourceGroup {
return
}
m.events <- &Event{
Type: watch.EventType("GENERIC"),
Object: o,
}
}

// isWatch is true if the request contains `watch="true"` as a query parameter.
func isWatch(req *restful.Request) bool {
return req.QueryParameter("watch") == "true"
}

func (h *apiServerHandler) watchForResource(req *restful.Request, resp *restful.Response, resourceGroup string, gvk schema.GroupVersionKind) (reterr error) {
ctx := req.Request.Context()
queryTimeout := req.QueryParameter("timeoutSeconds")
c := h.manager.GetCache()
i, err := c.GetInformerForKind(ctx, gvk)
if err != nil {
return err
}
h.log.Info(fmt.Sprintf("Serving Watch for %v", req.Request.URL))
// This needs to be an unbuffered channel as otherwise it can cause a deadlock.
// TODO: explain this better.
events := make(chan *Event, 10)
watcher := &WatchEventDispatcher{
resourceGroup: resourceGroup,
events: events,
}

if err := i.AddEventHandler(watcher); err != nil {
return err
}

// Defer cleanup which removes the event handler and ensures the channel is empty of events.
defer func() {
reterr = i.RemoveEventHandler(watcher)
// Doing this to ensure the channel is empty.
L:
for {
select {
case <-events:
default:
break L
}
}
}()

watcher.Run(ctx, queryTimeout, *req.Request, resp, h)
return nil
}

// Run serves a series of encoded events via HTTP with Transfer-Encoding: chunked.
func (m *WatchEventDispatcher) Run(ctx context.Context, timeout string, req http.Request, w http.ResponseWriter, h *apiServerHandler) {

Check warning on line 134 in test/infrastructure/inmemory/internal/server/api/watch.go

View workflow job for this annotation

GitHub Actions / lint (test)

unused-parameter: parameter 'req' seems to be unused, consider removing or renaming it as _ (revive)

Check warning on line 134 in test/infrastructure/inmemory/internal/server/api/watch.go

View workflow job for this annotation

GitHub Actions / lint (test)

unused-parameter: parameter 'req' seems to be unused, consider removing or renaming it as _ (revive)
flusher, ok := w.(http.Flusher)
if !ok {
fmt.Printf("unable to start watch - can't get http.Flusher: %#v", w)
return
}
resp, ok := w.(*restful.Response)
if !ok {
fmt.Printf("unable to start watch - can't get restful.Response: %#v", w)
return
}
w.Header().Set("Transfer-Encoding", "chunked")
w.WriteHeader(http.StatusOK)
flusher.Flush()

ctx, timeoutTimer := setTimeouts(ctx, timeout)
defer timeoutTimer.Stop()
for {
select {
case <-ctx.Done():
return
case <-timeoutTimer.C:
return
case event, ok := <-m.events:
if !ok {
// End of results.
return
}
if err := resp.WriteEntity(event); err != nil {
_ = resp.WriteErrorString(http.StatusInternalServerError, err.Error())
}
if len(m.events) == 0 {
flusher.Flush()
}
}
}
}

// setTimeouts adds the passed timeout to the context and creates a Timer with the same timeout.
func setTimeouts(ctx context.Context, timeout string) (context.Context, *time.Timer) {
var defaultTimeout = 120 * time.Second
if timeout == "" {
t := time.NewTimer(defaultTimeout)
return ctx, t
}
seconds, err := time.ParseDuration(fmt.Sprintf("%ss", timeout))
if err != nil {
errors.Wrapf(err, "Could not parse request timeout %s", timeout)

Check failure on line 181 in test/infrastructure/inmemory/internal/server/api/watch.go

View workflow job for this annotation

GitHub Actions / lint (test)

Error return value of `errors.Wrapf` is not checked (errcheck)

Check failure on line 181 in test/infrastructure/inmemory/internal/server/api/watch.go

View workflow job for this annotation

GitHub Actions / lint (test)

Error return value of `errors.Wrapf` is not checked (errcheck)
}
t := time.NewTimer(seconds)
ctx, _ = context.WithTimeout(ctx, seconds)

Check failure on line 184 in test/infrastructure/inmemory/internal/server/api/watch.go

View workflow job for this annotation

GitHub Actions / lint (test)

lostcancel: the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak (govet)

Check failure on line 184 in test/infrastructure/inmemory/internal/server/api/watch.go

View workflow job for this annotation

GitHub Actions / lint (test)

lostcancel: the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak (govet)
return ctx, t
}
25 changes: 25 additions & 0 deletions test/infrastructure/inmemory/internal/server/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,28 @@ func (s *WorkloadClusterListener) GetClient() (client.Client, error) {

return c, nil
}

// GetClientWithWatch returns a client for a WorkloadClusterListener.
func (s *WorkloadClusterListener) GetClientWithWatch() (client.WithWatch, error) {
restConfig, err := s.RESTConfig()
if err != nil {
return nil, err
}

httpClient, err := rest.HTTPClientFor(restConfig)
if err != nil {
return nil, err
}

mapper, err := apiutil.NewDynamicRESTMapper(restConfig, httpClient)
if err != nil {
return nil, err
}

c, err := client.NewWithWatch(restConfig, client.Options{Scheme: s.scheme, Mapper: mapper})
if err != nil {
return nil, err
}

return c, nil
}
Loading

0 comments on commit dfac09e

Please sign in to comment.