Create helpers for iterating containers in a pod
This commit is contained in:
@@ -19,9 +19,27 @@ package pods
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||
api "k8s.io/kubernetes/pkg/apis/core"
|
||||
"k8s.io/kubernetes/pkg/fieldpath"
|
||||
)
|
||||
|
||||
// ContainerVisitorWithPath is called with each container and the field.Path to that container
|
||||
type ContainerVisitorWithPath func(container *api.Container, path *field.Path)
|
||||
|
||||
// VisitContainersWithPath invokes the visitor function with a pointer to the spec
|
||||
// of every container in the given pod spec and the field.Path to that container.
|
||||
func VisitContainersWithPath(podSpec *api.PodSpec, visitor ContainerVisitorWithPath) {
|
||||
path := field.NewPath("spec", "initContainers")
|
||||
for i := range podSpec.InitContainers {
|
||||
visitor(&podSpec.InitContainers[i], path.Index(i))
|
||||
}
|
||||
path = field.NewPath("spec", "containers")
|
||||
for i := range podSpec.Containers {
|
||||
visitor(&podSpec.Containers[i], path.Index(i))
|
||||
}
|
||||
}
|
||||
|
||||
// ConvertDownwardAPIFieldLabel converts the specified downward API field label
|
||||
// and its value in the pod of the specified version to the internal version,
|
||||
// and returns the converted label and value. This function returns an error if
|
||||
|
@@ -17,9 +17,71 @@ limitations under the License.
|
||||
package pods
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||
api "k8s.io/kubernetes/pkg/apis/core"
|
||||
)
|
||||
|
||||
func TestVisitContainersWithPath(t *testing.T) {
|
||||
testCases := []struct {
|
||||
description string
|
||||
haveSpec *api.PodSpec
|
||||
wantNames []string
|
||||
}{
|
||||
{
|
||||
"empty podspec",
|
||||
&api.PodSpec{},
|
||||
[]string{},
|
||||
},
|
||||
{
|
||||
"regular containers",
|
||||
&api.PodSpec{
|
||||
Containers: []api.Container{
|
||||
{Name: "c1"},
|
||||
{Name: "c2"},
|
||||
},
|
||||
},
|
||||
[]string{"spec.containers[0]", "spec.containers[1]"},
|
||||
},
|
||||
{
|
||||
"init containers",
|
||||
&api.PodSpec{
|
||||
InitContainers: []api.Container{
|
||||
{Name: "i1"},
|
||||
{Name: "i2"},
|
||||
},
|
||||
},
|
||||
[]string{"spec.initContainers[0]", "spec.initContainers[1]"},
|
||||
},
|
||||
{
|
||||
"regular and init containers",
|
||||
&api.PodSpec{
|
||||
Containers: []api.Container{
|
||||
{Name: "c1"},
|
||||
{Name: "c2"},
|
||||
},
|
||||
InitContainers: []api.Container{
|
||||
{Name: "i1"},
|
||||
{Name: "i2"},
|
||||
},
|
||||
},
|
||||
[]string{"spec.initContainers[0]", "spec.initContainers[1]", "spec.containers[0]", "spec.containers[1]"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
gotNames := []string{}
|
||||
VisitContainersWithPath(tc.haveSpec, func(c *api.Container, p *field.Path) {
|
||||
gotNames = append(gotNames, p.String())
|
||||
})
|
||||
if !reflect.DeepEqual(gotNames, tc.wantNames) {
|
||||
t.Errorf("VisitContainersWithPath() for test case %q visited containers %q, wanted to visit %q", tc.description, gotNames, tc.wantNames)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertDownwardAPIFieldLabel(t *testing.T) {
|
||||
testCases := []struct {
|
||||
version string
|
||||
|
Reference in New Issue
Block a user