Skip to content

Filter subscriber reconciler events #18660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 6, 2023
Merged
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
16 changes: 16 additions & 0 deletions components/ws-manager-api/go/crd/v1/workspace_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,22 @@ type PortSpec struct {
Protocol PortProtocol `json:"protocol"`
}

func (ps PortSpec) Equal(other PortSpec) bool {
if ps.Port != other.Port {
return false
}

if ps.Visibility != other.Visibility {
return false
}

if ps.Protocol != other.Protocol {
return false
}

return true
}

// WorkspaceStatus defines the observed state of Workspace
type WorkspaceStatus struct {
PodStarts int `json:"podStarts"`
Expand Down
29 changes: 28 additions & 1 deletion components/ws-manager-mk2/controllers/subscriber_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@ package controllers
import (
"context"
"os"
"reflect"

"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/source"

config "github.com/gitpod-io/gitpod/ws-manager/api/config"
workspacev1 "github.com/gitpod-io/gitpod/ws-manager/api/crd/v1"
"github.com/google/go-cmp/cmp"
)

func NewSubscriberReconciler(c client.Client, cfg *config.Configuration) (*SubscriberReconciler, error) {
Expand Down Expand Up @@ -76,5 +80,28 @@ func (r *SubscriberReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Ma
}
}()

return c.Watch(source.Kind(mgr.GetCache(), &workspacev1.Workspace{}), &handler.EnqueueRequestForObject{})
// we need several reconciliation loops during a workspace creation until it reaches a stable state.
// this introduces the side effect of multiple notifications to the subscribers with partial information.
// the filterByUpdate predicate acts as a filter to avoid this
filterByUpdate := predicate.Funcs{
CreateFunc: func(ce event.CreateEvent) bool {
return true
},
UpdateFunc: func(e event.UpdateEvent) bool {
old := e.ObjectOld.(*workspacev1.Workspace)
new := e.ObjectNew.(*workspacev1.Workspace)

if !cmp.Equal(old.Spec.Ports, new.Spec.Ports) {
return true
}
Comment on lines +94 to +96
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the reason for doing the special comparison on ports? Should we do this comparison on the entire spec instead? E.g. there's the workspace timeout values in the spec that can change as well

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only the failure to compare Ports introduce errors in the integration tests (changing the visibility requires a notification)


// do not notify LastActivity changes
old.Status.LastActivity = nil
new.Status.LastActivity = nil

return !reflect.DeepEqual(old.Status, new.Status)
},
}

return c.Watch(source.Kind(mgr.GetCache(), &workspacev1.Workspace{}), &handler.EnqueueRequestForObject{}, filterByUpdate)
}