Skip to content

Commit

Permalink
feat: use default name for debug container so we only create one ephe…
Browse files Browse the repository at this point in the history
…meral container
  • Loading branch information
ravilock committed Feb 19, 2025
1 parent e59a136 commit 94f63bb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
29 changes: 19 additions & 10 deletions internal/pkg/rpaas/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/httpstream/spdy"
utilrand "k8s.io/apimachinery/pkg/util/rand"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/watch"
Expand Down Expand Up @@ -81,10 +80,7 @@ const (
nginxContainerName = "nginx"
)

var (
_ RpaasManager = &k8sRpaasManager{}
nameSuffixFunc = utilrand.String
)
var _ RpaasManager = &k8sRpaasManager{}

var podAllowedReasonsToFail = map[string]bool{
"shutdown": true,
Expand Down Expand Up @@ -194,7 +190,7 @@ func (m *k8sRpaasManager) debugPodWithContainerStatus(ctx context.Context, args
if err != nil {
return nil, "", nil, err
}
debugContainerName, err := m.generateDebugContainer(ctx, args, image, instance)
debugContainerName, err := m.getDebugContainer(ctx, args, image, instance)
if err != nil {
return nil, "", nil, err
}
Expand All @@ -219,14 +215,17 @@ func removeCertVolumeMounts(volumeMounts []corev1.VolumeMount) []corev1.VolumeMo
return result
}

func (m *k8sRpaasManager) generateDebugContainer(ctx context.Context, args *CommonTerminalArgs, image string, instance *v1alpha1.RpaasInstance) (string, error) {
rpaasInstanceVolumeMounts := removeCertVolumeMounts(instance.Spec.PodTemplate.VolumeMounts)
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)
if err != nil {
return "", err
}
debugContainerName := fmt.Sprintf("debugger-%s", nameSuffixFunc(5))
debugContainerName := "tsuru-debugger"
if ok := doesEphemeralContainerExist(&instancePod, debugContainerName); ok {
return debugContainerName, nil
}
rpaasInstanceVolumeMounts := removeCertVolumeMounts(instance.Spec.PodTemplate.VolumeMounts)
debugContainer := &corev1.EphemeralContainer{
EphemeralContainerCommon: corev1.EphemeralContainerCommon{
Name: debugContainerName,
Expand All @@ -239,11 +238,21 @@ func (m *k8sRpaasManager) generateDebugContainer(ctx context.Context, args *Comm
}, TargetContainerName: args.Container,
}
instancePodWithDebug := instancePod.DeepCopy()
instancePodWithDebug.Spec.EphemeralContainers = append(instancePod.Spec.EphemeralContainers, *debugContainer)
instancePodWithDebug.Spec.EphemeralContainers = append([]corev1.EphemeralContainer{}, instancePod.Spec.EphemeralContainers...)
instancePodWithDebug.Spec.EphemeralContainers = append([]corev1.EphemeralContainer{}, *debugContainer)
err = m.patchEphemeralContainers(ctx, instancePodWithDebug, instancePod)
return debugContainerName, err
}

func doesEphemeralContainerExist(pod *corev1.Pod, debugContainerName string) bool {
for _, ephemeralContainer := range pod.Spec.EphemeralContainers {
if ephemeralContainer.Name == debugContainerName {
return true
}
}
return false
}

func (m *k8sRpaasManager) patchEphemeralContainers(ctx context.Context, instancePodWithDebug *corev1.Pod, instancePod corev1.Pod) error {
podJS, err := json.Marshal(instancePod)
if err != nil {
Expand Down
27 changes: 10 additions & 17 deletions internal/pkg/rpaas/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5273,13 +5273,6 @@ func Test_k8sRpaasManager_GetAccessControlList(t *testing.T) {
}

func Test_k8sRpaasManager_Debug(t *testing.T) {
defer func(old func(int) string) { nameSuffixFunc = old }(nameSuffixFunc)
var suffixCounter int
nameSuffixFunc = func(int) string {
suffixCounter++
return fmt.Sprint(suffixCounter)
}

volumeMounts := []corev1.VolumeMount{
{
Name: "certs-test",
Expand Down Expand Up @@ -5467,19 +5460,19 @@ func Test_k8sRpaasManager_Debug(t *testing.T) {
args: DebugArgs{CommonTerminalArgs: CommonTerminalArgs{Pod: "pod1", Stdin: &bytes.Buffer{}, Stdout: io.Discard, Stderr: io.Discard}},
pods: func() []corev1.Pod {
pod1Debug := pod1.DeepCopy()
pod1Debug.Spec.EphemeralContainers = append(pod1Debug.Spec.EphemeralContainers, corev1.EphemeralContainer{EphemeralContainerCommon: corev1.EphemeralContainerCommon{Name: "debugger-1"}, TargetContainerName: "nginx"})
pod1Debug.Status.ContainerStatuses = append(pod1Debug.Status.EphemeralContainerStatuses, corev1.ContainerStatus{Name: "debugger-1", Ready: true, State: corev1.ContainerState{Running: &corev1.ContainerStateRunning{StartedAt: staticTimeNow}}})
pod1Debug.Spec.EphemeralContainers = append(pod1Debug.Spec.EphemeralContainers, corev1.EphemeralContainer{EphemeralContainerCommon: corev1.EphemeralContainerCommon{Name: "tsuru-debugger"}, TargetContainerName: "nginx"})
pod1Debug.Status.ContainerStatuses = append(pod1Debug.Status.EphemeralContainerStatuses, corev1.ContainerStatus{Name: "tsuru-debugger", Ready: true, State: corev1.ContainerState{Running: &corev1.ContainerStateRunning{StartedAt: staticTimeNow}}})
return []corev1.Pod{*pod1Debug}
},
assertion: func(t *testing.T, err error, m *k8sRpaasManager, instance *v1alpha1.RpaasInstance, debugContainerName string, debugContainerStatus *corev1.ContainerStatus) {
assert.NoError(t, err)
assert.Equal(t, "debugger-1", debugContainerName)
assert.Equal(t, corev1.ContainerStatus{Name: "debugger-1", Ready: true, State: corev1.ContainerState{Running: &corev1.ContainerStateRunning{StartedAt: staticTimeNow}}}, *debugContainerStatus)
assert.Equal(t, "tsuru-debugger", debugContainerName)
assert.Equal(t, corev1.ContainerStatus{Name: "tsuru-debugger", Ready: true, State: corev1.ContainerState{Running: &corev1.ContainerStateRunning{StartedAt: staticTimeNow}}}, *debugContainerStatus)
instancePod := corev1.Pod{}
err = m.cli.Get(context.Background(), types.NamespacedName{Name: "pod1", Namespace: instance2.Namespace}, &instancePod)
require.NoError(t, err)
expectedEphemerals := []corev1.EphemeralContainer{{EphemeralContainerCommon: corev1.EphemeralContainerCommon{
Name: "debugger-1",
Name: "tsuru-debugger",
Image: "tsuru/netshoot",
ImagePullPolicy: corev1.PullIfNotPresent,
Stdin: true,
Expand All @@ -5494,19 +5487,19 @@ func Test_k8sRpaasManager_Debug(t *testing.T) {
args: DebugArgs{CommonTerminalArgs: CommonTerminalArgs{Stdin: &bytes.Buffer{}, Stdout: io.Discard, Stderr: io.Discard}},
pods: func() []corev1.Pod {
pod1Debug := pod1.DeepCopy()
pod1Debug.Spec.EphemeralContainers = append(pod1Debug.Spec.EphemeralContainers, corev1.EphemeralContainer{EphemeralContainerCommon: corev1.EphemeralContainerCommon{Name: "debugger-2"}, TargetContainerName: "nginx"})
pod1Debug.Status.ContainerStatuses = append(pod1Debug.Status.EphemeralContainerStatuses, corev1.ContainerStatus{Name: "debugger-2", Ready: true, State: corev1.ContainerState{Running: &corev1.ContainerStateRunning{StartedAt: staticTimeNow}}})
pod1Debug.Spec.EphemeralContainers = append(pod1Debug.Spec.EphemeralContainers, corev1.EphemeralContainer{EphemeralContainerCommon: corev1.EphemeralContainerCommon{Name: "tsuru-debugger"}, TargetContainerName: "nginx"})
pod1Debug.Status.ContainerStatuses = append(pod1Debug.Status.EphemeralContainerStatuses, corev1.ContainerStatus{Name: "tsuru-debugger", Ready: true, State: corev1.ContainerState{Running: &corev1.ContainerStateRunning{StartedAt: staticTimeNow}}})
return []corev1.Pod{*pod1Debug}
},
assertion: func(t *testing.T, err error, m *k8sRpaasManager, instance *v1alpha1.RpaasInstance, debugContainerName string, debugContainerStatus *corev1.ContainerStatus) {
assert.NoError(t, err)
assert.Equal(t, "debugger-2", debugContainerName)
assert.Equal(t, corev1.ContainerStatus{Name: "debugger-2", Ready: true, State: corev1.ContainerState{Running: &corev1.ContainerStateRunning{StartedAt: staticTimeNow}}}, *debugContainerStatus)
assert.Equal(t, "tsuru-debugger", debugContainerName)
assert.Equal(t, corev1.ContainerStatus{Name: "tsuru-debugger", Ready: true, State: corev1.ContainerState{Running: &corev1.ContainerStateRunning{StartedAt: staticTimeNow}}}, *debugContainerStatus)
instancePod := corev1.Pod{}
err = m.cli.Get(context.Background(), types.NamespacedName{Name: "pod1", Namespace: instance2.Namespace}, &instancePod)
require.NoError(t, err)
expectedEphemerals := []corev1.EphemeralContainer{{EphemeralContainerCommon: corev1.EphemeralContainerCommon{
Name: "debugger-2",
Name: "tsuru-debugger",
Image: "tsuru/netshoot",
ImagePullPolicy: corev1.PullIfNotPresent,
Stdin: true,
Expand Down

0 comments on commit 94f63bb

Please sign in to comment.