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

feat: Do not inject sh -c when it exists #1010

Merged
merged 3 commits into from
Jan 15, 2020
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,6 @@ ehthumbs_vista.db

## Recycle Bin used on file shares
$RECYCLE.BIN/

/katib-controller
/katib-manager
7 changes: 7 additions & 0 deletions pkg/webhook/v1alpha3/pod/inject_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,13 @@ func wrapWorkerContainer(
if err != nil {
return err
}
// If the first two commands are sh -c, we do not inject command.
if args[0] == "sh" || args[0] == "bash" {
if args[1] == "-c" {
command = args[0:2]
args = args[2:]
}
}
if mc.Collector.Kind == common.StdOutCollector {
redirectStr := fmt.Sprintf("1>%s 2>&1", metricsFile)
args = append(args, redirectStr)
Expand Down
155 changes: 155 additions & 0 deletions pkg/webhook/v1alpha3/pod/inject_webhook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package pod

import (
"testing"

common "github.com/kubeflow/katib/pkg/apis/controller/common/v1alpha3"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
)

func TestWrapWorkerContainer(t *testing.T) {
testCases := []struct {
Pod *v1.Pod
Namespace string
JobKind string
MetricsFile string
PathKind common.FileSystemKind
MC common.MetricsCollectorSpec
Expected *v1.Pod
ExpectedError error
Name string
}{
{
Pod: &v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "tensorflow",
Command: []string{
"python main.py",
},
},
},
},
},
Namespace: "nohere",
JobKind: "TFJob",
MetricsFile: "testfile",
PathKind: common.FileKind,
MC: common.MetricsCollectorSpec{
Collector: &common.CollectorSpec{
Kind: common.StdOutCollector,
},
},
Expected: &v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "tensorflow",
Command: []string{
"sh", "-c",
},
Args: []string{
"python main.py 1>testfile 2>&1 && echo completed > $$$$.pid",
},
},
},
},
},
ExpectedError: nil,
Name: "tensorflow container without sh -c",
},
{
Pod: &v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "test",
Command: []string{
"python main.py",
},
},
},
},
},
Namespace: "nohere",
JobKind: "TFJob",
MetricsFile: "testfile",
PathKind: common.FileKind,
MC: common.MetricsCollectorSpec{
Collector: &common.CollectorSpec{
Kind: common.StdOutCollector,
},
},
Expected: &v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "test",
Command: []string{
"python main.py",
},
},
},
},
},
ExpectedError: nil,
Name: "test container without sh -c",
},
{
Pod: &v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "tensorflow",
Command: []string{
"sh", "-c",
"python main.py",
},
},
},
},
},
Namespace: "nohere",
JobKind: "TFJob",
MetricsFile: "testfile",
PathKind: common.FileKind,
MC: common.MetricsCollectorSpec{
Collector: &common.CollectorSpec{
Kind: common.StdOutCollector,
},
},
Expected: &v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "tensorflow",
Command: []string{
"sh", "-c",
},
Args: []string{
"python main.py 1>testfile 2>&1 && echo completed > $$$$.pid",
},
},
},
},
},
ExpectedError: nil,
Name: "Tensorflow container with sh -c",
},
}

for _, c := range testCases {
err := wrapWorkerContainer(c.Pod, c.Namespace, c.JobKind, c.MetricsFile, c.PathKind, c.MC)
if err != c.ExpectedError {
t.Errorf("Expected error %v, got %v", c.ExpectedError, err)
}
if err == nil {
if !equality.Semantic.DeepEqual(c.Pod.Spec.Containers, c.Expected.Spec.Containers) {
t.Errorf("Case %s: Expected pod %v, got %v",
c.Name, c.Expected.Spec.Containers, c.Pod.Spec.Containers)
}
}
}
}