Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

Add support for windows shells and bash #1749

Merged
merged 1 commit into from
Jan 30, 2021
Merged
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 changelogs/unreleased/1749-GuessWhoSamFoo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added support for windows shells and bash
69 changes: 60 additions & 9 deletions internal/api/terminal_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
"sync"
"time"

corev1 "k8s.io/api/core/v1"

"github.com/vmware-tanzu/octant/internal/util/kubernetes"

"github.com/vmware-tanzu/octant/pkg/event"

"github.com/pkg/errors"
Expand Down Expand Up @@ -125,27 +129,52 @@ func (s *terminalStateManager) SetActiveTerminal(state octant.State, payload act
cancelFn()
}

cancelFn := s.startStream(key, containerName)
objectStore := s.config.ObjectStore()
po := &corev1.Pod{}
pod, err := objectStore.Get(s.ctx, key)
if err != nil {
return err
}
if pod != nil {
err := kubernetes.FromUnstructured(pod, po)
if err != nil {
return err
}
}

cancelFn := s.startStream(po, key, containerName)
s.terminalSubscriptions.Store(eventType, cancelFn)
return nil
}

func (s *terminalStateManager) startStream(key store.Key, container string) context.CancelFunc {
func (s *terminalStateManager) startStream(pod *corev1.Pod, key store.Key, container string) context.CancelFunc {
ctx, cancelFn := context.WithCancel(s.ctx)
logger := log.From(s.ctx).With("startStream", container)

eventType := event.NewTerminalEventType(key.Namespace, key.Name, container)
eventType := event.NewTerminalEventType(pod.Namespace, pod.Name, container)

instance, err := terminal.NewTerminalInstance(ctx, s.config.ClusterClient(), logger, key, container, "/bin/sh", s.chanInstance)
if err != nil {
cancelFn()
return cancelFn
commands := []string{"bash", "sh"}
if IsWindowsContainer(pod) {
commands = []string{"powershell", "cmd"}
}

s.instance = instance
for _, command := range commands {
validInstance, err := terminal.NewTerminalInstance(ctx, s.config.ClusterClient(), logger, key, container, command, s.chanInstance)
if err != nil {
logger.Debugf("streaming: %+v", err)
continue
}

go s.sendTerminalEvents(ctx, eventType, instance, s.chanInstance)
if validInstance != nil {
s.instance = validInstance
go s.sendTerminalEvents(ctx, eventType, s.instance, s.chanInstance)
break
}
}

if s.instance == nil {
cancelFn()
}
return cancelFn
}

Expand Down Expand Up @@ -235,6 +264,7 @@ func newEvent(ctx context.Context, t terminal.Instance, sendScrollback bool) (ev
data.Scrollback = []byte("\n" + "(process exited)")
data.ExitMessage = data.Scrollback
}
t.Stop()
}
}

Expand All @@ -244,3 +274,24 @@ func newEvent(ctx context.Context, t terminal.Instance, sendScrollback bool) (ev
Err: nil,
}, nil
}

func IsWindowsContainer(pod *corev1.Pod) bool {
var hasNodeSelector, hasToleration bool
windowsToleration := corev1.Toleration{
Key: "os",
Operator: corev1.TolerationOpEqual,
Value: "windows",
Effect: corev1.TaintEffectNoSchedule,
}
for k, v := range pod.Spec.NodeSelector {
if k == "kubernetes.io/os" && v == "windows" {
hasNodeSelector = true
}
}
for _, toleration := range pod.Spec.Tolerations {
if toleration == windowsToleration {
hasToleration = true
}
}
return hasNodeSelector && hasToleration
}
48 changes: 47 additions & 1 deletion internal/api/terminal_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import (
"context"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"

"github.com/vmware-tanzu/octant/internal/api"
"github.com/vmware-tanzu/octant/internal/testutil"

"github.com/golang/mock/gomock"

"github.com/vmware-tanzu/octant/internal/api/fake"
configFake "github.com/vmware-tanzu/octant/internal/config/fake"
octantFake "github.com/vmware-tanzu/octant/internal/octant/fake"
Expand All @@ -26,3 +31,44 @@ func Test_TerminalStateManager(t *testing.T) {
ctx := context.Background()
tsm.Start(ctx, state, octantClient)
}

func Test_isWindowsContainer(t *testing.T) {
windowsPod := testutil.CreatePod("pod")
windowsPod.Spec.Tolerations = []corev1.Toleration{
{
Key: "os",
Operator: corev1.TolerationOpEqual,
Value: "windows",
Effect: corev1.TaintEffectNoSchedule,
},
}
windowsPod.Spec.NodeSelector = map[string]string{
"kubernetes.io/os": "windows",
}

pod := testutil.CreatePod("pod")

cases := []struct {
name string
pod *corev1.Pod
isWindows bool
}{
{
name: "windows container",
pod: windowsPod,
isWindows: true,
},
{
name: "not windows container",
pod: pod,
isWindows: false,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.isWindows, api.IsWindowsContainer(tc.pod))
})
}

}