|
| 1 | +// Copyright (c) 2022 Gitpod GmbH. All rights reserved. |
| 2 | +// Licensed under the GNU Affero General Public License (AGPL). |
| 3 | +// See License-AGPL.txt in the project root for license information. |
| 4 | + |
| 5 | +package controllers |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "os" |
| 10 | + |
| 11 | + "k8s.io/apimachinery/pkg/api/errors" |
| 12 | + "k8s.io/apimachinery/pkg/runtime" |
| 13 | + "k8s.io/client-go/tools/record" |
| 14 | + ctrl "sigs.k8s.io/controller-runtime" |
| 15 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/controller" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 19 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 20 | + "sigs.k8s.io/controller-runtime/pkg/source" |
| 21 | + |
| 22 | + config "github.com/gitpod-io/gitpod/ws-manager/api/config" |
| 23 | + workspacev1 "github.com/gitpod-io/gitpod/ws-manager/api/crd/v1" |
| 24 | +) |
| 25 | + |
| 26 | +func NewSubscriberReconciler(c client.Client, scheme *runtime.Scheme, recorder record.EventRecorder, cfg *config.Configuration) (*SubscriberReconciler, error) { |
| 27 | + reconciler := &SubscriberReconciler{ |
| 28 | + Client: c, |
| 29 | + Scheme: scheme, |
| 30 | + Config: cfg, |
| 31 | + Recorder: recorder, |
| 32 | + } |
| 33 | + |
| 34 | + return reconciler, nil |
| 35 | +} |
| 36 | + |
| 37 | +type SubscriberReconciler struct { |
| 38 | + client.Client |
| 39 | + Scheme *runtime.Scheme |
| 40 | + |
| 41 | + Config *config.Configuration |
| 42 | + Recorder record.EventRecorder |
| 43 | + OnReconcile func(ctx context.Context, ws *workspacev1.Workspace) |
| 44 | +} |
| 45 | + |
| 46 | +// Reconcile is part of the main kubernetes reconciliation loop which aims to |
| 47 | +// move the current state of the cluster closer to the desired state. |
| 48 | +// Modify the Reconcile function to compare the state specified by |
| 49 | +// the Workspace object against the actual cluster state, and then |
| 50 | +// perform operations to make the cluster state reflect the state specified by |
| 51 | +// the user. |
| 52 | +// |
| 53 | +// For more details, check Reconcile and its Result here: |
| 54 | +// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile |
| 55 | +func (r *SubscriberReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 56 | + log := log.FromContext(ctx) |
| 57 | + |
| 58 | + var workspace workspacev1.Workspace |
| 59 | + if err := r.Get(ctx, req.NamespacedName, &workspace); err != nil { |
| 60 | + if !errors.IsNotFound(err) { |
| 61 | + log.Error(err, "unable to fetch workspace") |
| 62 | + } |
| 63 | + |
| 64 | + return ctrl.Result{}, client.IgnoreNotFound(err) |
| 65 | + } |
| 66 | + |
| 67 | + if r.OnReconcile != nil { |
| 68 | + // Publish to subscribers in a goroutine, to prevent blocking the main reconcile loop. |
| 69 | + ws := workspace.DeepCopy() |
| 70 | + go func() { |
| 71 | + r.OnReconcile(ctx, ws) |
| 72 | + }() |
| 73 | + } |
| 74 | + |
| 75 | + return reconcile.Result{}, nil |
| 76 | +} |
| 77 | + |
| 78 | +// SetupWithManager sets up the controller with the Manager. |
| 79 | +func (r *SubscriberReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error { |
| 80 | + c, err := controller.NewUnmanaged("maintenance-controller", mgr, controller.Options{Reconciler: r}) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + go func() { |
| 86 | + err = c.Start(ctx) |
| 87 | + if err != nil { |
| 88 | + log.FromContext(ctx).Error(err, "cannot start maintenance reconciler") |
| 89 | + os.Exit(1) |
| 90 | + } |
| 91 | + }() |
| 92 | + |
| 93 | + return c.Watch(source.Kind(mgr.GetCache(), &workspacev1.Workspace{}), &handler.EnqueueRequestForObject{}) |
| 94 | +} |
0 commit comments