From c8a620bad334261879c819de7d73d0251f4824ed Mon Sep 17 00:00:00 2001 From: ravilock Date: Thu, 20 Feb 2025 16:10:10 -0300 Subject: [PATCH] fix: use nginx container volume mounts for debug ephemeral container --- internal/pkg/rpaas/k8s.go | 15 ++++++++++++++- internal/pkg/rpaas/k8s_test.go | 26 +++++++++++++++----------- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/internal/pkg/rpaas/k8s.go b/internal/pkg/rpaas/k8s.go index 94e0bb9f..303a09d8 100644 --- a/internal/pkg/rpaas/k8s.go +++ b/internal/pkg/rpaas/k8s.go @@ -215,6 +215,15 @@ func removeCertVolumeMounts(volumeMounts []corev1.VolumeMount) []corev1.VolumeMo return result } +func findNginxContainerFromPod(pod *corev1.Pod) *corev1.Container { + for _, container := range pod.Spec.Containers { + if container.Name == nginxContainerName { + return &container + } + } + return nil +} + func (m *k8sRpaasManager) getDebugContainer(ctx context.Context, args *CommonTerminalArgs, image string, instance *v1alpha1.RpaasInstance) (string, error) { instancePod := corev1.Pod{} err := m.cli.Get(ctx, types.NamespacedName{Name: args.Pod, Namespace: instance.Namespace}, &instancePod) @@ -225,7 +234,11 @@ func (m *k8sRpaasManager) getDebugContainer(ctx context.Context, args *CommonTer if ok := doesEphemeralContainerExist(&instancePod, debugContainerName); ok { return debugContainerName, nil } - rpaasInstanceVolumeMounts := removeCertVolumeMounts(instance.Spec.PodTemplate.VolumeMounts) + nginxContainer := findNginxContainerFromPod(&instancePod) + if nginxContainer == nil { + return "", errors.New("nginx container not found in pod") + } + rpaasInstanceVolumeMounts := removeCertVolumeMounts(nginxContainer.VolumeMounts) debugContainer := &corev1.EphemeralContainer{ EphemeralContainerCommon: corev1.EphemeralContainerCommon{ Name: debugContainerName, diff --git a/internal/pkg/rpaas/k8s_test.go b/internal/pkg/rpaas/k8s_test.go index 0d213f00..c3ec5e10 100644 --- a/internal/pkg/rpaas/k8s_test.go +++ b/internal/pkg/rpaas/k8s_test.go @@ -1060,7 +1060,7 @@ x2cJyBkkBQV9WB34oGtnZzQ0nKtzsY6FVlNGSeyCJ3OD2dHXO5komJY= } } -func newEmptyRpaasInstance(volumeMounts ...corev1.VolumeMount) *v1alpha1.RpaasInstance { +func newEmptyRpaasInstance() *v1alpha1.RpaasInstance { return &v1alpha1.RpaasInstance{ TypeMeta: metav1.TypeMeta{ APIVersion: "extensions.tsuru.io/v1alpha1", @@ -1071,9 +1071,7 @@ func newEmptyRpaasInstance(volumeMounts ...corev1.VolumeMount) *v1alpha1.RpaasIn Namespace: getServiceName(), }, Spec: v1alpha1.RpaasInstanceSpec{ - PodTemplate: nginxv1alpha1.NginxPodTemplateSpec{ - VolumeMounts: volumeMounts, - }, + PodTemplate: nginxv1alpha1.NginxPodTemplateSpec{}, }, } } @@ -5308,15 +5306,15 @@ func Test_k8sRpaasManager_Debug(t *testing.T) { }, } - instance1 := newEmptyRpaasInstance(volumeMounts...) + instance1 := newEmptyRpaasInstance() instance1.ObjectMeta.Name = "instance1" - instance2 := newEmptyRpaasInstance(volumeMounts...) + instance2 := newEmptyRpaasInstance() instance2.ObjectMeta.Name = "instance2" - instance3 := newEmptyRpaasInstance(volumeMounts...) + instance3 := newEmptyRpaasInstance() instance3.ObjectMeta.Name = "instance3" - instance4 := newEmptyRpaasInstance(volumeMounts...) + instance4 := newEmptyRpaasInstance() instance4.ObjectMeta.Name = "instance4" - instance5 := newEmptyRpaasInstance(volumeMounts...) + instance5 := newEmptyRpaasInstance() instance5.ObjectMeta.Name = "instance5" nginx1 := &nginxv1alpha1.Nginx{ @@ -5366,7 +5364,10 @@ func Test_k8sRpaasManager_Debug(t *testing.T) { }, Spec: corev1.PodSpec{ Containers: []corev1.Container{ - {Name: "nginx"}, + { + Name: "nginx", + VolumeMounts: volumeMounts, + }, }, }, } @@ -5392,7 +5393,10 @@ func Test_k8sRpaasManager_Debug(t *testing.T) { }, Spec: corev1.PodSpec{ Containers: []corev1.Container{ - {Name: "nginx"}, + { + Name: "nginx", + VolumeMounts: volumeMounts, + }, }, }, }