-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Enable leader election in ws-manager-mk2 (v3) #18539
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
Changes from all commits
766766b
16b1aad
201990a
0cd9523
1594aac
615faa8
1067f75
ec385e4
c152222
51aa070
07c995f
477b688
ecf1b95
08ae889
1ef3f80
08537ae
d390827
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
|
||
package controllers | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"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/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"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" | ||
) | ||
|
||
func NewSubscriberReconciler(c client.Client, cfg *config.Configuration) (*SubscriberReconciler, error) { | ||
reconciler := &SubscriberReconciler{ | ||
Client: c, | ||
Config: cfg, | ||
} | ||
|
||
return reconciler, nil | ||
} | ||
|
||
type SubscriberReconciler struct { | ||
client.Client | ||
|
||
Config *config.Configuration | ||
|
||
OnReconcile func(ctx context.Context, ws *workspacev1.Workspace) | ||
} | ||
|
||
func (r *SubscriberReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
log := log.FromContext(ctx) | ||
|
||
var workspace workspacev1.Workspace | ||
if err := r.Get(ctx, req.NamespacedName, &workspace); err != nil { | ||
if !errors.IsNotFound(err) { | ||
log.Error(err, "unable to fetch workspace") | ||
} | ||
|
||
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
} | ||
|
||
if workspace.Status.Conditions == nil { | ||
workspace.Status.Conditions = []metav1.Condition{} | ||
} | ||
|
||
if r.OnReconcile != nil { | ||
r.OnReconcile(ctx, &workspace) | ||
aledbf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
aledbf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return ctrl.Result{}, nil | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *SubscriberReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error { | ||
c, err := controller.NewUnmanaged("subscribers-controller", mgr, controller.Options{Reconciler: r}) | ||
aledbf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
go func() { | ||
err = c.Start(ctx) | ||
if err != nil { | ||
log.FromContext(ctx).Error(err, "cannot start Subscriber reconciler") | ||
os.Exit(1) | ||
} | ||
}() | ||
|
||
return c.Watch(source.Kind(mgr.GetCache(), &workspacev1.Workspace{}), &handler.EnqueueRequestForObject{}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,6 @@ import ( | |
"sigs.k8s.io/controller-runtime/pkg/metrics" | ||
|
||
"github.com/gitpod-io/gitpod/common-go/util" | ||
"github.com/gitpod-io/gitpod/ws-manager-mk2/pkg/activity" | ||
"github.com/gitpod-io/gitpod/ws-manager/api/config" | ||
workspacev1 "github.com/gitpod-io/gitpod/ws-manager/api/crd/v1" | ||
//+kubebuilder:scaffold:imports | ||
|
@@ -50,10 +49,9 @@ func TestAPIs(t *testing.T) { | |
} | ||
|
||
var ( | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
wsActivity *activity.WorkspaceActivity | ||
wsMetrics *controllerMetrics | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
wsMetrics *controllerMetrics | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how do the ws-manager metrics behave when two replicas are enabled? Do both report metrics, or only one? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics will be scrapped from all available replicas There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will break some of the dashboards then (and maybe alerts too). For instance we sum all workspaces by cluster, this will then double the amount of reported workspaces There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please check this gist (integration tests are running there) Only the leader will have metrics related to the controller. If the current leader is not elected anymore, the pod is restarted. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for the gists. Both replicas report workspace metrics, e.g.:
is in both. Summing them as done in dashboards/alerts would give 2 replicas, while there's only 1 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. gist updated after running the integration test |
||
) | ||
|
||
var _ = BeforeSuite(func() { | ||
|
@@ -111,8 +109,7 @@ var _ = BeforeSuite(func() { | |
Expect(err).ToNot(HaveOccurred()) | ||
Expect(wsReconciler.SetupWithManager(k8sManager)).To(Succeed()) | ||
|
||
wsActivity = activity.NewWorkspaceActivity() | ||
timeoutReconciler, err := NewTimeoutReconciler(k8sManager.GetClient(), k8sManager.GetEventRecorderFor("workspace"), conf, wsActivity, maintenance) | ||
timeoutReconciler, err := NewTimeoutReconciler(k8sManager.GetClient(), k8sManager.GetEventRecorderFor("workspace"), conf, maintenance) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(timeoutReconciler.SetupWithManager(k8sManager)).To(Succeed()) | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.