Skip to content
Open
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
1 change: 1 addition & 0 deletions bundle/manifests/amalthea.clusterserviceversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ spec:
- ingresses
verbs:
- create
- delete
- get
- list
- patch
Expand Down
1 change: 1 addition & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ rules:
- ingresses
verbs:
- create
- delete
- get
- list
- patch
Expand Down
2 changes: 1 addition & 1 deletion helm-chart/amalthea-sessions/templates/manager-rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ rules:
- networking.k8s.io
resources:
- ingresses
verbs: [create, get, list, watch, patch, update]
verbs: [create, delete, get, list, watch, patch, update]
# Required for hibernating sessions
- apiGroups: ["apps"]
resources: ["statefulsets"]
Expand Down
9 changes: 7 additions & 2 deletions internal/controller/amaltheasession_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const secretCleanupFinalizerName = "amalthea.dev/secrets-finalizer"
// +kubebuilder:rbac:groups=core,resources=services,verbs=get;list;watch;create;update;patch
// +kubebuilder:rbac:groups=core,resources=events,verbs=get;list;watch
// +kubebuilder:rbac:groups=apps,resources=statefulsets,verbs=get;list;watch;create;update;patch
// +kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses,verbs=get;list;watch;create;update;patch
// +kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=metrics.k8s.io,resources=pods,verbs=get;list;watch

// Reconcile is part of the main kubernetes reconciliation loop which aims to
Expand Down Expand Up @@ -214,6 +214,7 @@ func (r *AmaltheaSessionReconciler) reconcileInner(ctx context.Context, req ctrl
amaltheasession.Status.RunID = runID

children, err := NewChildResources(amaltheasession, r.Configuration)

if err != nil {
logger.Error(
err,
Expand Down Expand Up @@ -276,7 +277,11 @@ func (r *AmaltheaSessionReconciler) reconcileInner(ctx context.Context, req ctrl
// If the status is evolving we should requeue faster
requeueAfter = 0
}
return ctrl.Result{Requeue: true, RequeueAfter: requeueAfter}, nil

if requeueAfter > 0 {
return ctrl.Result{RequeueAfter: requeueAfter}, nil
}
return ctrl.Result{Requeue: true}, nil
}

func (r *AmaltheaSessionReconciler) deleteSecrets(ctx context.Context, cr *amaltheadevv1alpha1.AmaltheaSession) error {
Expand Down
7 changes: 7 additions & 0 deletions internal/controller/children.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ func (c ChildResource[T]) Reconcile(ctx context.Context, clnt client.Client, cr
}
switch current := any(c.Current).(type) {
case *networkingv1.Ingress:
if cr.Spec.Hibernated {
err := clnt.Delete(ctx, current)
if apierrors.IsNotFound(err) {
return ChildResourceUpdate[T]{c.Current, controllerutil.OperationResultNone, nil, nil}
}
return ChildResourceUpdate[T]{c.Current, "deleted", err, nil}
}
res, err := controllerutil.CreateOrPatch(ctx, clnt, current, func() error {
// NOTE: the callback function in CreateOrPatch will load the
// state of the object referenced from k8s, then run the callback to update
Expand Down
69 changes: 65 additions & 4 deletions test/e2e/general_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
schedv1 "k8s.io/api/scheduling/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
resource "k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -216,10 +218,11 @@ var _ = Describe("reconcile strategies", Ordered, func() {
g.Expect(sessionPod).To(BeNil())
}, "60s").WithContext(ctx).Should(Succeed())
By("Resuming the session we should see the new changes")
patched = &amaltheadevv1alpha1.AmaltheaSession{}
Expect(k8sClient.Get(ctx, typeNamespacedName, patched)).To(Succeed())
patched.Spec.Hibernated = false
Expect(k8sClient.Update(ctx, patched)).To(Succeed())
Eventually(func(g Gomega) {
Expect(k8sClient.Get(ctx, typeNamespacedName, patched)).To(Succeed())
patched.Spec.Hibernated = false
Expect(k8sClient.Update(ctx, patched)).To(Succeed())
}, "60s").WithContext(ctx).Should(Succeed())
Eventually(func(g Gomega) {
sessionPod, err = amaltheasession.GetPod(ctx, k8sClient)
g.Expect(err).NotTo(HaveOccurred())
Expand All @@ -228,6 +231,64 @@ var _ = Describe("reconcile strategies", Ordered, func() {
g.Expect(sessionPod.Spec.Containers[0].Resources.Requests.Memory()).To(Equal(&newMemory))
}).WithContext(ctx).WithTimeout(time.Minute).Should(Succeed())
})

It(
"should delete the Ingress when hibernating",
func(ctx SpecContext,
) {
var err error
var sessionPod *corev1.Pod

By("Adding an ingress to the session")
patched := amaltheasession.DeepCopy()
Expect(k8sClient.Get(ctx, typeNamespacedName, patched)).To(Succeed())
patched.Spec.Ingress = &amaltheadevv1alpha1.Ingress{
Host: "amaltheasession.localhost",
}
Expect(k8sClient.Update(ctx, patched)).To(Succeed())

By("Checking the ingress was created")
Eventually(func(g Gomega) {
ingress := &networkingv1.Ingress{}
g.Expect(k8sClient.Get(ctx, typeNamespacedName, ingress)).To(Succeed())
g.Expect(ingress).NotTo(BeNil())
}, "60s").WithContext(ctx).Should(Succeed())

By("Hibernating the session")
Eventually(func(g Gomega) {
patched = &amaltheadevv1alpha1.AmaltheaSession{}
Expect(k8sClient.Get(ctx, typeNamespacedName, patched)).To(Succeed())
patched.Spec.Hibernated = true
Expect(k8sClient.Update(ctx, patched)).To(Succeed())
}, "60s").WithContext(ctx).Should(Succeed())
// Make sure the session has stopped, and the pod has been cleaned up
Eventually(func(g Gomega) {
sessionPod, err = amaltheasession.GetPod(ctx, k8sClient)
g.Expect(err).To(HaveOccurred())
g.Expect(sessionPod).To(BeNil())
}, "60s").WithContext(ctx).Should(Succeed())

By("Checking the ingress has gone")
Eventually(func(g Gomega) {
ingress := &networkingv1.Ingress{}
err := k8sClient.Get(ctx, typeNamespacedName, ingress)
g.Expect(apierrors.IsNotFound(err)).To(BeTrue())
}, "60s").WithContext(ctx).Should(Succeed())

By("Resuming the session we should see the ingress again")
Eventually(func(g Gomega) {
patched = &amaltheadevv1alpha1.AmaltheaSession{}
Expect(k8sClient.Get(ctx, typeNamespacedName, patched)).To(Succeed())
patched.Spec.Hibernated = false
Expect(k8sClient.Update(ctx, patched)).To(Succeed())
}, "60s").WithContext(ctx).Should(Succeed())
Eventually(func(g Gomega) {
ingress := &networkingv1.Ingress{}
g.Expect(k8sClient.Get(ctx, typeNamespacedName, ingress)).To(Succeed())
g.Expect(ingress).NotTo(BeNil())
}).WithContext(ctx).WithTimeout(time.Minute).Should(Succeed())

})
})

Context("When the session is failing", func() {
Expand Down
2 changes: 2 additions & 0 deletions test/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
amaltheadevv1alpha1 "github.com/SwissDataScienceCenter/amalthea/api/v1alpha1"
"github.com/SwissDataScienceCenter/amalthea/internal/controller"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
metricsv "k8s.io/metrics/pkg/client/clientset/versioned"
Expand Down Expand Up @@ -332,6 +333,7 @@ func GetK8sClient(ctx context.Context, namespace string) (client.Client, error)
&amaltheadevv1alpha1.AmaltheaSession{},
&corev1.Pod{},
&corev1.Event{},
&networkingv1.Ingress{},
},
},
},
Expand Down
Loading