helpers_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package config
  14. import (
  15. "reflect"
  16. "strings"
  17. "testing"
  18. "k8s.io/apimachinery/pkg/util/sets"
  19. "k8s.io/apimachinery/pkg/util/validation/field"
  20. )
  21. func TestKubeletConfigurationPathFields(t *testing.T) {
  22. // ensure the intersection of kubeletConfigurationPathFieldPaths and KubeletConfigurationNonPathFields is empty
  23. if i := kubeletConfigurationPathFieldPaths.Intersection(kubeletConfigurationNonPathFieldPaths); len(i) > 0 {
  24. t.Fatalf("expect the intersection of kubeletConfigurationPathFieldPaths and "+
  25. "KubeletConfigurationNonPathFields to be empty, got:\n%s",
  26. strings.Join(i.List(), "\n"))
  27. }
  28. // ensure that kubeletConfigurationPathFields U kubeletConfigurationNonPathFields == allPrimitiveFieldPaths(KubeletConfiguration)
  29. expect := sets.NewString().Union(kubeletConfigurationPathFieldPaths).Union(kubeletConfigurationNonPathFieldPaths)
  30. result := allPrimitiveFieldPaths(t, reflect.TypeOf(&KubeletConfiguration{}), nil)
  31. if !expect.Equal(result) {
  32. // expected fields missing from result
  33. missing := expect.Difference(result)
  34. // unexpected fields in result but not specified in expect
  35. unexpected := result.Difference(expect)
  36. if len(missing) > 0 {
  37. t.Errorf("the following fields were expected, but missing from the result. "+
  38. "If the field has been removed, please remove it from the kubeletConfigurationPathFieldPaths set "+
  39. "and the KubeletConfigurationPathRefs function, "+
  40. "or remove it from the kubeletConfigurationNonPathFieldPaths set, as appropriate:\n%s",
  41. strings.Join(missing.List(), "\n"))
  42. }
  43. if len(unexpected) > 0 {
  44. t.Errorf("the following fields were in the result, but unexpected. "+
  45. "If the field is new, please add it to the kubeletConfigurationPathFieldPaths set "+
  46. "and the KubeletConfigurationPathRefs function, "+
  47. "or add it to the kubeletConfigurationNonPathFieldPaths set, as appropriate:\n%s",
  48. strings.Join(unexpected.List(), "\n"))
  49. }
  50. }
  51. }
  52. func allPrimitiveFieldPaths(t *testing.T, tp reflect.Type, path *field.Path) sets.String {
  53. paths := sets.NewString()
  54. switch tp.Kind() {
  55. case reflect.Ptr:
  56. paths.Insert(allPrimitiveFieldPaths(t, tp.Elem(), path).List()...)
  57. case reflect.Struct:
  58. for i := 0; i < tp.NumField(); i++ {
  59. field := tp.Field(i)
  60. paths.Insert(allPrimitiveFieldPaths(t, field.Type, path.Child(field.Name)).List()...)
  61. }
  62. case reflect.Map, reflect.Slice:
  63. paths.Insert(allPrimitiveFieldPaths(t, tp.Elem(), path.Key("*")).List()...)
  64. case reflect.Interface:
  65. t.Fatalf("unexpected interface{} field %s", path.String())
  66. default:
  67. // if we hit a primitive type, we're at a leaf
  68. paths.Insert(path.String())
  69. }
  70. return paths
  71. }
  72. //lint:file-ignore U1000 Ignore dummy types, used by tests.
  73. // dummy helper types
  74. type foo struct {
  75. foo int
  76. }
  77. type bar struct {
  78. str string
  79. strptr *string
  80. ints []int
  81. stringMap map[string]string
  82. foo foo
  83. fooptr *foo
  84. bars []foo
  85. barMap map[string]foo
  86. }
  87. func TestAllPrimitiveFieldPaths(t *testing.T) {
  88. expect := sets.NewString(
  89. "str",
  90. "strptr",
  91. "ints[*]",
  92. "stringMap[*]",
  93. "foo.foo",
  94. "fooptr.foo",
  95. "bars[*].foo",
  96. "barMap[*].foo",
  97. )
  98. result := allPrimitiveFieldPaths(t, reflect.TypeOf(&bar{}), nil)
  99. if !expect.Equal(result) {
  100. // expected fields missing from result
  101. missing := expect.Difference(result)
  102. // unexpected fields in result but not specified in expect
  103. unexpected := result.Difference(expect)
  104. if len(missing) > 0 {
  105. t.Errorf("the following fields were exepcted, but missing from the result:\n%s", strings.Join(missing.List(), "\n"))
  106. }
  107. if len(unexpected) > 0 {
  108. t.Errorf("the following fields were in the result, but unexpected:\n%s", strings.Join(unexpected.List(), "\n"))
  109. }
  110. }
  111. }
  112. var (
  113. // KubeletConfiguration fields that contain file paths. If you update this, also update KubeletConfigurationPathRefs!
  114. kubeletConfigurationPathFieldPaths = sets.NewString(
  115. "StaticPodPath",
  116. "Authentication.X509.ClientCAFile",
  117. "TLSCertFile",
  118. "TLSPrivateKeyFile",
  119. "ResolverConfig",
  120. )
  121. // KubeletConfiguration fields that do not contain file paths.
  122. kubeletConfigurationNonPathFieldPaths = sets.NewString(
  123. "Address",
  124. "AllowedUnsafeSysctls[*]",
  125. "Authentication.Anonymous.Enabled",
  126. "Authentication.Webhook.CacheTTL.Duration",
  127. "Authentication.Webhook.Enabled",
  128. "Authorization.Mode",
  129. "Authorization.Webhook.CacheAuthorizedTTL.Duration",
  130. "Authorization.Webhook.CacheUnauthorizedTTL.Duration",
  131. "CPUCFSQuota",
  132. "CPUCFSQuotaPeriod.Duration",
  133. "CPUManagerPolicy",
  134. "CPUManagerReconcilePeriod.Duration",
  135. "TopologyManagerPolicy",
  136. "QOSReserved[*]",
  137. "CgroupDriver",
  138. "CgroupRoot",
  139. "CgroupsPerQOS",
  140. "ClusterDNS[*]",
  141. "ClusterDomain",
  142. "ConfigMapAndSecretChangeDetectionStrategy",
  143. "ContainerLogMaxFiles",
  144. "ContainerLogMaxSize",
  145. "ContentType",
  146. "EnableContentionProfiling",
  147. "EnableControllerAttachDetach",
  148. "EnableDebuggingHandlers",
  149. "EnforceNodeAllocatable[*]",
  150. "EventBurst",
  151. "EventRecordQPS",
  152. "EvictionHard[*]",
  153. "EvictionMaxPodGracePeriod",
  154. "EvictionMinimumReclaim[*]",
  155. "EvictionPressureTransitionPeriod.Duration",
  156. "EvictionSoft[*]",
  157. "EvictionSoftGracePeriod[*]",
  158. "FailSwapOn",
  159. "FeatureGates[*]",
  160. "FileCheckFrequency.Duration",
  161. "HTTPCheckFrequency.Duration",
  162. "HairpinMode",
  163. "HealthzBindAddress",
  164. "HealthzPort",
  165. "TLSCipherSuites[*]",
  166. "TLSMinVersion",
  167. "IPTablesDropBit",
  168. "IPTablesMasqueradeBit",
  169. "ImageGCHighThresholdPercent",
  170. "ImageGCLowThresholdPercent",
  171. "ImageMinimumGCAge.Duration",
  172. "KubeAPIBurst",
  173. "KubeAPIQPS",
  174. "KubeReservedCgroup",
  175. "KubeReserved[*]",
  176. "KubeletCgroups",
  177. "MakeIPTablesUtilChains",
  178. "RotateCertificates",
  179. "ServerTLSBootstrap",
  180. "StaticPodURL",
  181. "StaticPodURLHeader[*][*]",
  182. "MaxOpenFiles",
  183. "MaxPods",
  184. "NodeStatusUpdateFrequency.Duration",
  185. "NodeStatusReportFrequency.Duration",
  186. "NodeLeaseDurationSeconds",
  187. "OOMScoreAdj",
  188. "PodCIDR",
  189. "PodPidsLimit",
  190. "PodsPerCore",
  191. "Port",
  192. "ProtectKernelDefaults",
  193. "ReadOnlyPort",
  194. "RegistryBurst",
  195. "RegistryPullQPS",
  196. "ReservedSystemCPUs",
  197. "RuntimeRequestTimeout.Duration",
  198. "SerializeImagePulls",
  199. "StreamingConnectionIdleTimeout.Duration",
  200. "SyncFrequency.Duration",
  201. "SystemCgroups",
  202. "SystemReservedCgroup",
  203. "SystemReserved[*]",
  204. "TypeMeta.APIVersion",
  205. "TypeMeta.Kind",
  206. "VolumeStatsAggPeriod.Duration",
  207. )
  208. )