Skip to content

Commit c6e3f70

Browse files
authored
feat(api): add termination grace period to PodOverrides (#244)
Allows users to configure terminationGracePeriodSeconds via spec.server.podOverrides to prevent in-flight requests from being killed during HPA scale-down events. Approved-by: VaishnaviHire Approved-by: derekhiggins
1 parent 2776f87 commit c6e3f70

7 files changed

Lines changed: 132 additions & 3 deletions

File tree

api/v1alpha1/llamastackdistribution_types.go

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/llamastack.io_llamastackdistributions.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,12 @@ spec:
401401
ServiceAccountName allows users to specify their own ServiceAccount
402402
If not specified, the operator will use the default ServiceAccount
403403
type: string
404+
terminationGracePeriodSeconds:
405+
description: |-
406+
TerminationGracePeriodSeconds is the time allowed for graceful pod shutdown.
407+
If not specified, Kubernetes defaults to 30 seconds.
408+
format: int64
409+
type: integer
404410
volumeMounts:
405411
items:
406412
description: VolumeMount describes a mounting of a Volume

controllers/resource_helper.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,11 @@ func configurePodOverrides(instance *llamav1alpha1.LlamaStackDistribution, podSp
532532
podSpec.Containers[0].VolumeMounts = append(podSpec.Containers[0].VolumeMounts, instance.Spec.Server.PodOverrides.VolumeMounts...)
533533
}
534534
}
535+
536+
// Apply termination grace period if specified
537+
if instance.Spec.Server.PodOverrides.TerminationGracePeriodSeconds != nil {
538+
podSpec.TerminationGracePeriodSeconds = instance.Spec.Server.PodOverrides.TerminationGracePeriodSeconds
539+
}
535540
}
536541
}
537542

controllers/resource_helper_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,108 @@ func TestPodOverridesWithoutServiceAccount(t *testing.T) {
650650
}
651651
}
652652

653+
func TestPodOverridesWithTerminationGracePeriod(t *testing.T) {
654+
gracePeriod := int64(120)
655+
instance := &llamav1alpha1.LlamaStackDistribution{
656+
ObjectMeta: metav1.ObjectMeta{
657+
Name: "test-instance",
658+
Namespace: "test-namespace",
659+
},
660+
Spec: llamav1alpha1.LlamaStackDistributionSpec{
661+
Server: llamav1alpha1.ServerSpec{
662+
PodOverrides: &llamav1alpha1.PodOverrides{
663+
TerminationGracePeriodSeconds: &gracePeriod,
664+
},
665+
},
666+
},
667+
}
668+
deployment := &appsv1.Deployment{
669+
Spec: appsv1.DeploymentSpec{
670+
Template: corev1.PodTemplateSpec{
671+
Spec: corev1.PodSpec{
672+
Containers: []corev1.Container{
673+
{
674+
Name: "test-container",
675+
},
676+
},
677+
},
678+
},
679+
},
680+
}
681+
682+
configurePodOverrides(instance, &deployment.Spec.Template.Spec)
683+
684+
require.NotNil(t, deployment.Spec.Template.Spec.TerminationGracePeriodSeconds)
685+
assert.Equal(t, int64(120), *deployment.Spec.Template.Spec.TerminationGracePeriodSeconds)
686+
}
687+
688+
func TestPodOverridesWithTerminationGracePeriodZero(t *testing.T) {
689+
// Ensures we distinguish "not set" (nil) from "set to 0" (immediate termination)
690+
gracePeriod := int64(0)
691+
instance := &llamav1alpha1.LlamaStackDistribution{
692+
ObjectMeta: metav1.ObjectMeta{
693+
Name: "test-instance",
694+
Namespace: "test-namespace",
695+
},
696+
Spec: llamav1alpha1.LlamaStackDistributionSpec{
697+
Server: llamav1alpha1.ServerSpec{
698+
PodOverrides: &llamav1alpha1.PodOverrides{
699+
TerminationGracePeriodSeconds: &gracePeriod,
700+
},
701+
},
702+
},
703+
}
704+
deployment := &appsv1.Deployment{
705+
Spec: appsv1.DeploymentSpec{
706+
Template: corev1.PodTemplateSpec{
707+
Spec: corev1.PodSpec{
708+
Containers: []corev1.Container{
709+
{
710+
Name: "test-container",
711+
},
712+
},
713+
},
714+
},
715+
},
716+
}
717+
718+
configurePodOverrides(instance, &deployment.Spec.Template.Spec)
719+
720+
require.NotNil(t, deployment.Spec.Template.Spec.TerminationGracePeriodSeconds)
721+
assert.Equal(t, int64(0), *deployment.Spec.Template.Spec.TerminationGracePeriodSeconds)
722+
}
723+
724+
func TestPodOverridesWithoutTerminationGracePeriod(t *testing.T) {
725+
instance := &llamav1alpha1.LlamaStackDistribution{
726+
ObjectMeta: metav1.ObjectMeta{
727+
Name: "test-instance",
728+
Namespace: "test-namespace",
729+
},
730+
Spec: llamav1alpha1.LlamaStackDistributionSpec{
731+
Server: llamav1alpha1.ServerSpec{
732+
PodOverrides: &llamav1alpha1.PodOverrides{},
733+
},
734+
},
735+
}
736+
deployment := &appsv1.Deployment{
737+
Spec: appsv1.DeploymentSpec{
738+
Template: corev1.PodTemplateSpec{
739+
Spec: corev1.PodSpec{
740+
Containers: []corev1.Container{
741+
{
742+
Name: "test-container",
743+
},
744+
},
745+
},
746+
},
747+
},
748+
}
749+
750+
configurePodOverrides(instance, &deployment.Spec.Template.Spec)
751+
752+
assert.Nil(t, deployment.Spec.Template.Spec.TerminationGracePeriodSeconds)
753+
}
754+
653755
func TestValidateConfigMapKeys(t *testing.T) {
654756
tests := []struct {
655757
name string

docs/api-overview.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ _Appears in:_
203203
| Field | Description | Default | Validation |
204204
| --- | --- | --- | --- |
205205
| `serviceAccountName` _string_ | ServiceAccountName allows users to specify their own ServiceAccount<br />If not specified, the operator will use the default ServiceAccount | | |
206+
| `terminationGracePeriodSeconds` _integer_ | TerminationGracePeriodSeconds is the time allowed for graceful pod shutdown.<br />If not specified, Kubernetes defaults to 30 seconds. | | |
206207
| `volumes` _[Volume](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.31/#volume-v1-core) array_ | | | |
207208
| `volumeMounts` _[VolumeMount](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.31/#volumemount-v1-core) array_ | | | |
208209

release/operator.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,12 @@ spec:
410410
ServiceAccountName allows users to specify their own ServiceAccount
411411
If not specified, the operator will use the default ServiceAccount
412412
type: string
413+
terminationGracePeriodSeconds:
414+
description: |-
415+
TerminationGracePeriodSeconds is the time allowed for graceful pod shutdown.
416+
If not specified, Kubernetes defaults to 30 seconds.
417+
format: int64
418+
type: integer
413419
volumeMounts:
414420
items:
415421
description: VolumeMount describes a mounting of a Volume

0 commit comments

Comments
 (0)