Skip to content

Commit 75a4025

Browse files
jentingroboquat
authored andcommitted
Add volume_snapshot_controller
Add volume_snapshot_controller to watch the VolumeSnapshot object. Signed-off-by: JenTing Hsiao <[email protected]>
1 parent e1dea35 commit 75a4025

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

components/ws-manager/cmd/run.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,16 @@ var runCmd = &cobra.Command{
200200
log.WithError(err).Fatal(err, "unable to create controller", "controller", "Pod")
201201
}
202202

203+
err = (&manager.VolumeSnapshotReconciler{
204+
Monitor: monitor,
205+
Client: mgr.GetClient(),
206+
Log: ctrl.Log.WithName("controllers").WithName("VolumeSnapshot"),
207+
Scheme: mgr.GetScheme(),
208+
}).SetupWithManager(mgr)
209+
if err != nil {
210+
log.WithError(err).Fatal(err, "unable to create controller", "controller", "VolumeSnapshot")
211+
}
212+
203213
if cfg.PProf.Addr != "" {
204214
go pprof.Serve(cfg.PProf.Addr)
205215
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 manager
6+
7+
import (
8+
"context"
9+
10+
"github.com/go-logr/logr"
11+
volumesnapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
12+
"k8s.io/apimachinery/pkg/api/errors"
13+
"k8s.io/apimachinery/pkg/runtime"
14+
"k8s.io/apimachinery/pkg/watch"
15+
ctrl "sigs.k8s.io/controller-runtime"
16+
"sigs.k8s.io/controller-runtime/pkg/client"
17+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
18+
)
19+
20+
// VolumeSnapshotReconciler reconciles a VolumeSnapshot object
21+
type VolumeSnapshotReconciler struct {
22+
client.Client
23+
Log logr.Logger
24+
Scheme *runtime.Scheme
25+
26+
Monitor *Monitor
27+
}
28+
29+
// Reconcile performs a reconciliation of a volume snapshot
30+
// For more details, check Reconcile and its Result here:
31+
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
32+
func (r *VolumeSnapshotReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
33+
var vs volumesnapshotv1.VolumeSnapshot
34+
err := r.Client.Get(context.Background(), req.NamespacedName, &vs)
35+
if errors.IsNotFound(err) {
36+
// volume snapshot is gone - that's ok
37+
return reconcile.Result{}, nil
38+
}
39+
40+
r.Monitor.eventpool.Add(vs.Name, watch.Event{
41+
Type: watch.Modified,
42+
Object: &vs,
43+
})
44+
45+
return ctrl.Result{}, nil
46+
}
47+
48+
// SetupWithManager sets up the controller with the Manager.
49+
func (r *VolumeSnapshotReconciler) SetupWithManager(mgr ctrl.Manager) error {
50+
return ctrl.NewControllerManagedBy(mgr).
51+
For(&volumesnapshotv1.VolumeSnapshot{}).
52+
Complete(r)
53+
}

0 commit comments

Comments
 (0)