Skip to content

Commit 71d8f1e

Browse files
committed
feat(controller): ImpVMSnapshot reconciler skeleton
1 parent 4dc73c8 commit 71d8f1e

3 files changed

Lines changed: 110 additions & 0 deletions

File tree

cmd/operator/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ func main() {
162162
os.Exit(1)
163163
}
164164

165+
if err = (&controller.ImpVMSnapshotReconciler{
166+
Client: mgr.GetClient(),
167+
Scheme: mgr.GetScheme(),
168+
}).SetupWithManager(mgr); err != nil {
169+
setupLog.Error(err, "unable to create controller", "controller", "ImpVMSnapshot")
170+
os.Exit(1)
171+
}
172+
165173
if err = builder.WebhookManagedBy(mgr, &impv1alpha1.ImpVM{}).
166174
WithDefaulter(&webhookv1alpha1.ImpVMWebhook{}).
167175
WithValidator(&webhookv1alpha1.ImpVMWebhook{}).
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package controller
2+
3+
import (
4+
"context"
5+
6+
"k8s.io/apimachinery/pkg/runtime"
7+
ctrl "sigs.k8s.io/controller-runtime"
8+
"sigs.k8s.io/controller-runtime/pkg/client"
9+
logf "sigs.k8s.io/controller-runtime/pkg/log"
10+
11+
impv1alpha1 "github.com/syscode-labs/imp/api/v1alpha1"
12+
)
13+
14+
// ImpVMSnapshotReconciler reconciles ImpVMSnapshot objects.
15+
type ImpVMSnapshotReconciler struct {
16+
client.Client
17+
Scheme *runtime.Scheme
18+
}
19+
20+
// +kubebuilder:rbac:groups=imp.dev,resources=impvmsnapshots,verbs=get;list;watch;create;update;patch;delete
21+
// +kubebuilder:rbac:groups=imp.dev,resources=impvmsnapshots/status,verbs=get;update;patch
22+
// +kubebuilder:rbac:groups=imp.dev,resources=impvmsnapshots/finalizers,verbs=update
23+
24+
func (r *ImpVMSnapshotReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
25+
log := logf.FromContext(ctx)
26+
27+
snap := &impv1alpha1.ImpVMSnapshot{}
28+
if err := r.Get(ctx, req.NamespacedName, snap); err != nil {
29+
return ctrl.Result{}, client.IgnoreNotFound(err)
30+
}
31+
32+
// Set initial phase
33+
if snap.Status.Phase == "" {
34+
base := snap.DeepCopy()
35+
snap.Status.Phase = "Pending"
36+
if err := r.Status().Patch(ctx, snap, client.MergeFrom(base)); err != nil {
37+
return ctrl.Result{}, err
38+
}
39+
log.Info("ImpVMSnapshot created, set to Pending", "name", snap.Name)
40+
}
41+
42+
// TODO: trigger agent snapshot, handle cron scheduling, OCI push.
43+
44+
return ctrl.Result{}, nil
45+
}
46+
47+
func (r *ImpVMSnapshotReconciler) SetupWithManager(mgr ctrl.Manager) error {
48+
return ctrl.NewControllerManagedBy(mgr).
49+
For(&impv1alpha1.ImpVMSnapshot{}).
50+
Complete(r)
51+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package controller
2+
3+
import (
4+
"context"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
"k8s.io/apimachinery/pkg/types"
10+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
11+
12+
impv1alpha1 "github.com/syscode-labs/imp/api/v1alpha1"
13+
)
14+
15+
func newSnapshotReconciler() *ImpVMSnapshotReconciler {
16+
return &ImpVMSnapshotReconciler{
17+
Client: k8sClient,
18+
Scheme: k8sClient.Scheme(),
19+
}
20+
}
21+
22+
var _ = Describe("ImpVMSnapshot controller", func() {
23+
ctx := context.Background()
24+
25+
It("sets phase to Pending on creation", func() {
26+
snap := &impv1alpha1.ImpVMSnapshot{
27+
ObjectMeta: metav1.ObjectMeta{
28+
Name: "snap-ctrl-test",
29+
Namespace: "default",
30+
},
31+
Spec: impv1alpha1.ImpVMSnapshotSpec{
32+
SourceVMName: "vm-does-not-exist",
33+
SourceVMNamespace: "default",
34+
Storage: impv1alpha1.SnapshotStorageSpec{Type: "node-local"},
35+
},
36+
}
37+
Expect(k8sClient.Create(ctx, snap)).To(Succeed())
38+
DeferCleanup(func() { k8sClient.Delete(ctx, snap) }) //nolint:errcheck
39+
40+
r := newSnapshotReconciler()
41+
_, err := r.Reconcile(ctx, reconcile.Request{
42+
NamespacedName: types.NamespacedName{Name: "snap-ctrl-test", Namespace: "default"},
43+
})
44+
Expect(err).NotTo(HaveOccurred())
45+
46+
updated := &impv1alpha1.ImpVMSnapshot{}
47+
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: "snap-ctrl-test", Namespace: "default"}, updated)).To(Succeed())
48+
Expect(updated.Status.Phase).NotTo(BeEmpty())
49+
Expect(updated.Status.Phase).To(Equal("Pending"))
50+
})
51+
})

0 commit comments

Comments
 (0)