Skip to content

Commit

Permalink
Fix error checking for pod name & NS
Browse files Browse the repository at this point in the history
Previously it would just check if POD_NAME *or* POD_NAMESPACE was set, which
could lead to very bizarre failure modes.
  • Loading branch information
claytono committed Jul 14, 2017
1 parent d416d02 commit ad583c3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion core/pkg/k8s/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func GetPodDetails(kubeClient clientset.Interface) (*PodInfo, error) {
podName := os.Getenv("POD_NAME")
podNs := os.Getenv("POD_NAMESPACE")

if podName == "" && podNs == "" {
if podName == "" || podNs == "" {
return nil, fmt.Errorf("unable to get POD information (missing POD_NAME or POD_NAMESPACE environment variable")
}

Expand Down
24 changes: 20 additions & 4 deletions core/pkg/k8s/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,30 @@ func TestGetPodDetails(t *testing.T) {
t.Errorf("expected an error but returned nil")
}

// POD not exist
os.Setenv("POD_NAME", "testpod")
// POD_NAME not exist
os.Setenv("POD_NAME", "")
os.Setenv("POD_NAMESPACE", api.NamespaceDefault)
_, err2 := GetPodDetails(testclient.NewSimpleClientset())
if err2 == nil {
t.Errorf("expected an error but returned nil")
}

// POD_NAMESPACE not exist
os.Setenv("POD_NAME", "testpod")
os.Setenv("POD_NAMESPACE", "")
_, err3 := GetPodDetails(testclient.NewSimpleClientset())
if err3 == nil {
t.Errorf("expected an error but returned nil")
}

// POD not exist
os.Setenv("POD_NAME", "testpod")
os.Setenv("POD_NAMESPACE", api.NamespaceDefault)
_, err4 := GetPodDetails(testclient.NewSimpleClientset())
if err4 == nil {
t.Errorf("expected an error but returned nil")
}

// success to get PodInfo
fkClient := testclient.NewSimpleClientset(
&api.PodList{Items: []api.Pod{{
Expand All @@ -331,8 +347,8 @@ func TestGetPodDetails(t *testing.T) {
},
}}})

epi, err3 := GetPodDetails(fkClient)
if err3 != nil {
epi, err5 := GetPodDetails(fkClient)
if err5 != nil {
t.Errorf("expected a PodInfo but returned error")
return
}
Expand Down

0 comments on commit ad583c3

Please sign in to comment.