Skip to content

Commit

Permalink
Simplify kubelet file config field allowlists
Browse files Browse the repository at this point in the history
  • Loading branch information
liggitt committed Nov 2, 2021
1 parent d9896a2 commit 94d0c0f
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions pkg/kubelet/apis/config/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestKubeletConfigurationPathFields(t *testing.T) {

// ensure that kubeletConfigurationPathFields U kubeletConfigurationNonPathFields == allPrimitiveFieldPaths(KubeletConfiguration)
expect := sets.NewString().Union(kubeletConfigurationPathFieldPaths).Union(kubeletConfigurationNonPathFieldPaths)
result := allPrimitiveFieldPaths(t, reflect.TypeOf(&KubeletConfiguration{}), nil)
result := allPrimitiveFieldPaths(t, expect, reflect.TypeOf(&KubeletConfiguration{}), nil)
if !expect.Equal(result) {
// expected fields missing from result
missing := expect.Difference(result)
Expand All @@ -58,18 +58,26 @@ func TestKubeletConfigurationPathFields(t *testing.T) {
}
}

func allPrimitiveFieldPaths(t *testing.T, tp reflect.Type, path *field.Path) sets.String {
// allPrimitiveFieldPaths returns the set of field paths in type `tp`, rooted at `path`.
// It recursively descends into the definition of type `tp` accumulating paths to primitive leaf fields or paths in `skipRecurseList`.
func allPrimitiveFieldPaths(t *testing.T, skipRecurseList sets.String, tp reflect.Type, path *field.Path) sets.String {
// if the current field path is in the list of paths we should not recurse into,
// return here rather than descending and accumulating child field paths
if pathStr := path.String(); len(pathStr) > 0 && skipRecurseList.Has(pathStr) {
return sets.NewString(pathStr)
}

paths := sets.NewString()
switch tp.Kind() {
case reflect.Ptr:
paths.Insert(allPrimitiveFieldPaths(t, tp.Elem(), path).List()...)
paths.Insert(allPrimitiveFieldPaths(t, skipRecurseList, tp.Elem(), path).List()...)
case reflect.Struct:
for i := 0; i < tp.NumField(); i++ {
field := tp.Field(i)
paths.Insert(allPrimitiveFieldPaths(t, field.Type, path.Child(field.Name)).List()...)
paths.Insert(allPrimitiveFieldPaths(t, skipRecurseList, field.Type, path.Child(field.Name)).List()...)
}
case reflect.Map, reflect.Slice:
paths.Insert(allPrimitiveFieldPaths(t, tp.Elem(), path.Key("*")).List()...)
paths.Insert(allPrimitiveFieldPaths(t, skipRecurseList, tp.Elem(), path.Key("*")).List()...)
case reflect.Interface:
t.Fatalf("unexpected interface{} field %s", path.String())
default:
Expand Down Expand Up @@ -97,6 +105,13 @@ type bar struct {

bars []foo
barMap map[string]foo

skipRecurseStruct foo
skipRecursePointer *foo
skipRecurseList1 []foo
skipRecurseList2 []foo
skipRecurseMap1 map[string]foo
skipRecurseMap2 map[string]foo
}

func TestAllPrimitiveFieldPaths(t *testing.T) {
Expand All @@ -109,8 +124,14 @@ func TestAllPrimitiveFieldPaths(t *testing.T) {
"fooptr.foo",
"bars[*].foo",
"barMap[*].foo",
"skipRecurseStruct", // skip recursing a struct
"skipRecursePointer", // skip recursing a struct pointer
"skipRecurseList1", // skip recursing a list
"skipRecurseList2[*]", // skip recursing list items
"skipRecurseMap1", // skip recursing a map
"skipRecurseMap2[*]", // skip recursing map items
)
result := allPrimitiveFieldPaths(t, reflect.TypeOf(&bar{}), nil)
result := allPrimitiveFieldPaths(t, expect, reflect.TypeOf(&bar{}), nil)
if !expect.Equal(result) {
// expected fields missing from result
missing := expect.Difference(result)
Expand Down Expand Up @@ -233,14 +254,7 @@ var (
"ReadOnlyPort",
"RegistryBurst",
"RegistryPullQPS",
"ReservedMemory[*].Limits[*].Format",
"ReservedMemory[*].Limits[*].d.Dec.scale",
"ReservedMemory[*].Limits[*].d.Dec.unscaled.abs[*]",
"ReservedMemory[*].Limits[*].d.Dec.unscaled.neg",
"ReservedMemory[*].Limits[*].i.scale",
"ReservedMemory[*].Limits[*].i.value",
"ReservedMemory[*].Limits[*].s",
"ReservedMemory[*].NumaNode",
"ReservedMemory",
"ReservedSystemCPUs",
"RuntimeRequestTimeout.Duration",
"RunOnce",
Expand Down

0 comments on commit 94d0c0f

Please sign in to comment.