Skip to content
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

Deleting HPA object when NIMservice is updated #143

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
42 changes: 42 additions & 0 deletions internal/controller/platform/standalone/nimservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ func (r *NIMServiceReconciler) reconcileNIMService(ctx context.Context, nimServi
if err != nil {
return ctrl.Result{}, err
}
} else {
err = r.cleanupResource(ctx, &networkingv1.Ingress{}, namespacedName)
slu2011 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil && !errors.IsNotFound(err) {
return ctrl.Result{}, err
}
}

// Sync HPA
Expand All @@ -131,6 +136,12 @@ func (r *NIMServiceReconciler) reconcileNIMService(ctx context.Context, nimServi
if err != nil {
return ctrl.Result{}, err
}
} else {
// If autoscaling is disabled, ensure the HPA is deleted
err = r.cleanupResource(ctx, &autoscalingv2.HorizontalPodAutoscaler{}, namespacedName)
slu2011 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return ctrl.Result{}, err
}
}

// Sync Service Monitor
Expand Down Expand Up @@ -342,6 +353,37 @@ func (r *NIMServiceReconciler) syncResource(ctx context.Context, obj client.Obje
return nil
}

// cleanupResource deletes the given Kubernetes resource if it exists.
// If the resource does not exist or an error occurs during deletion, the function returns nil or the error.
//
// Parameters:
// ctx (context.Context): The context for the operation.
// obj (client.Object): The Kubernetes resource to delete.
// namespacedName (types.NamespacedName): The namespaced name of the resource.
//
// Returns:
// error: An error if the resource deletion fails, or nil if the resource is not found or deletion is successful.
func (r *NIMServiceReconciler) cleanupResource(ctx context.Context, obj client.Object, namespacedName types.NamespacedName) error {

logger := log.FromContext(ctx)

err := r.Get(ctx, namespacedName, obj)
if err != nil && !errors.IsNotFound(err) {
return err
}

if errors.IsNotFound(err) {
return nil
}

err = r.Delete(ctx, obj)
if err != nil {
return err
}
logger.V(2).Info("NIM Service object changed, deleting ", "obj", obj)
return nil
}

// getNIMCachePVC returns PVC backing the NIM cache instance
func (r *NIMServiceReconciler) getNIMCachePVC(ctx context.Context, nimService *appsv1alpha1.NIMService) (*appsv1alpha1.PersistentVolumeClaim, error) {
logger := log.FromContext(ctx)
Expand Down
72 changes: 72 additions & 0 deletions internal/controller/platform/standalone/nimservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -174,6 +175,36 @@ var _ = Describe("NIMServiceReconciler for a standalone platform", func() {
Effect: corev1.TaintEffectNoSchedule,
},
},

Expose: appsv1alpha1.Expose{
Ingress: appsv1alpha1.Ingress{
Enabled: ptr.To[bool](true),
Spec: networkingv1.IngressSpec{
Rules: []networkingv1.IngressRule{
{
Host: "test-nimservice.default.example.com",
IngressRuleValue: networkingv1.IngressRuleValue{
HTTP: &networkingv1.HTTPIngressRuleValue{
Paths: []networkingv1.HTTPIngressPath{
{
Path: "/",
Backend: networkingv1.IngressBackend{
Service: &networkingv1.IngressServiceBackend{
Name: "test-nimservice",
Port: networkingv1.ServiceBackendPort{
Number: 8080,
},
},
},
},
},
},
},
},
},
},
},
},
Scale: appsv1alpha1.Autoscaling{
Enabled: ptr.To[bool](true),
HPA: appsv1alpha1.HorizontalPodAutoscalerSpec{
Expand Down Expand Up @@ -405,6 +436,46 @@ var _ = Describe("NIMServiceReconciler for a standalone platform", func() {
err = reconciler.cleanupNIMService(context.TODO(), nimService)
Expect(err).NotTo(HaveOccurred())
})

It("should delete HPA when NIMService is updated", func() {
namespacedName := types.NamespacedName{Name: nimService.Name, Namespace: nimService.Namespace}
err := client.Create(context.TODO(), nimService)
Expect(err).NotTo(HaveOccurred())

result, err := reconciler.reconcileNIMService(context.TODO(), nimService)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(ctrl.Result{}))

// HPA should be deployed
hpa := &autoscalingv2.HorizontalPodAutoscaler{}
err = client.Get(context.TODO(), namespacedName, hpa)
Expect(err).NotTo(HaveOccurred())
Expect(hpa.Name).To(Equal(nimService.GetName()))
Expect(hpa.Namespace).To(Equal(nimService.GetNamespace()))
Expect(*hpa.Spec.MinReplicas).To(Equal(int32(1)))
Expect(hpa.Spec.MaxReplicas).To(Equal(int32(10)))

nimService := &appsv1alpha1.NIMService{}
err = client.Get(context.TODO(), namespacedName, nimService)
Expect(err).NotTo(HaveOccurred())
nimService.Spec.Scale.Enabled = ptr.To[bool](false)
nimService.Spec.Expose.Ingress.Enabled = ptr.To[bool](false)
err = client.Update(context.TODO(), nimService)
Expect(err).NotTo(HaveOccurred())

result, err = reconciler.reconcileNIMService(context.TODO(), nimService)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(ctrl.Result{}))
hpa = &autoscalingv2.HorizontalPodAutoscaler{}
err = client.Get(context.TODO(), namespacedName, hpa)
Expect(err).To(HaveOccurred())
Expect(errors.IsNotFound(err)).To(Equal(true))
ingress := &networkingv1.Ingress{}
err = client.Get(context.TODO(), namespacedName, ingress)
Expect(err).To(HaveOccurred())
Expect(errors.IsNotFound(err)).To(Equal(true))
})

})

Describe("isDeploymentReady for setting status on NIMService", func() {
Expand Down Expand Up @@ -525,4 +596,5 @@ var _ = Describe("NIMServiceReconciler for a standalone platform", func() {
Expect(msg).To(Equal(fmt.Sprintf("deployment %q successfully rolled out\n", deployment.Name)))
})
})

})