proxy_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 proxy
  14. import (
  15. "strings"
  16. "testing"
  17. "time"
  18. apps "k8s.io/api/apps/v1"
  19. apierrors "k8s.io/apimachinery/pkg/api/errors"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/runtime"
  22. clientsetfake "k8s.io/client-go/kubernetes/fake"
  23. clientsetscheme "k8s.io/client-go/kubernetes/scheme"
  24. core "k8s.io/client-go/testing"
  25. kubeadmapiv1beta2 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2"
  26. "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  27. kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
  28. configutil "k8s.io/kubernetes/cmd/kubeadm/app/util/config"
  29. api "k8s.io/kubernetes/pkg/apis/core"
  30. kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config"
  31. "k8s.io/utils/pointer"
  32. )
  33. func TestCreateServiceAccount(t *testing.T) {
  34. tests := []struct {
  35. name string
  36. createErr error
  37. expectErr bool
  38. }{
  39. {
  40. "error-free case",
  41. nil,
  42. false,
  43. },
  44. {
  45. "duplication errors should be ignored",
  46. apierrors.NewAlreadyExists(api.Resource(""), ""),
  47. false,
  48. },
  49. {
  50. "unexpected errors should be returned",
  51. apierrors.NewUnauthorized(""),
  52. true,
  53. },
  54. }
  55. for _, tc := range tests {
  56. t.Run(tc.name, func(t *testing.T) {
  57. client := clientsetfake.NewSimpleClientset()
  58. if tc.createErr != nil {
  59. client.PrependReactor("create", "serviceaccounts", func(action core.Action) (bool, runtime.Object, error) {
  60. return true, nil, tc.createErr
  61. })
  62. }
  63. err := CreateServiceAccount(client)
  64. if tc.expectErr {
  65. if err == nil {
  66. t.Errorf("CreateServiceAccounts(%s) wanted err, got nil", tc.name)
  67. }
  68. return
  69. } else if !tc.expectErr && err != nil {
  70. t.Errorf("CreateServiceAccounts(%s) returned unexpected err: %v", tc.name, err)
  71. }
  72. wantResourcesCreated := 1
  73. if len(client.Actions()) != wantResourcesCreated {
  74. t.Errorf("CreateServiceAccounts(%s) should have made %d actions, but made %d", tc.name, wantResourcesCreated, len(client.Actions()))
  75. }
  76. for _, action := range client.Actions() {
  77. if action.GetVerb() != "create" || action.GetResource().Resource != "serviceaccounts" {
  78. t.Errorf("CreateServiceAccounts(%s) called [%v %v], but wanted [create serviceaccounts]",
  79. tc.name, action.GetVerb(), action.GetResource().Resource)
  80. }
  81. }
  82. })
  83. }
  84. }
  85. func TestCompileManifests(t *testing.T) {
  86. var tests = []struct {
  87. name string
  88. manifest string
  89. data interface{}
  90. }{
  91. {
  92. name: "KubeProxyConfigMap19",
  93. manifest: KubeProxyConfigMap19,
  94. data: struct {
  95. ControlPlaneEndpoint, ProxyConfig, ProxyConfigMap, ProxyConfigMapKey string
  96. }{
  97. ControlPlaneEndpoint: "foo",
  98. ProxyConfig: " bindAddress: 0.0.0.0\n clusterCIDR: 192.168.1.1\n enableProfiling: false",
  99. ProxyConfigMap: "bar",
  100. ProxyConfigMapKey: "baz",
  101. },
  102. },
  103. {
  104. name: "KubeProxyDaemonSet19",
  105. manifest: KubeProxyDaemonSet19,
  106. data: struct{ Image, ProxyConfigMap, ProxyConfigMapKey string }{
  107. Image: "foo",
  108. ProxyConfigMap: "bar",
  109. ProxyConfigMapKey: "baz",
  110. },
  111. },
  112. }
  113. for _, rt := range tests {
  114. t.Run(rt.name, func(t *testing.T) {
  115. _, err := kubeadmutil.ParseTemplate(rt.manifest, rt.data)
  116. if err != nil {
  117. t.Errorf("unexpected ParseTemplate faiure: %+v", err)
  118. }
  119. })
  120. }
  121. }
  122. func TestEnsureProxyAddon(t *testing.T) {
  123. type SimulatedError int
  124. const (
  125. NoError SimulatedError = iota
  126. ServiceAccountError
  127. InvalidControlPlaneEndpoint
  128. IPv6SetBindAddress
  129. )
  130. var testCases = []struct {
  131. name string
  132. simError SimulatedError
  133. expErrString string
  134. expBindAddr string
  135. expClusterCIDR string
  136. }{
  137. {
  138. name: "Successful proxy addon",
  139. simError: NoError,
  140. expErrString: "",
  141. expBindAddr: "0.0.0.0",
  142. expClusterCIDR: "5.6.7.8/24",
  143. }, {
  144. name: "Simulated service account error",
  145. simError: ServiceAccountError,
  146. expErrString: "error when creating kube-proxy service account",
  147. expBindAddr: "0.0.0.0",
  148. expClusterCIDR: "5.6.7.8/24",
  149. }, {
  150. name: "IPv6 AdvertiseAddress address",
  151. simError: IPv6SetBindAddress,
  152. expErrString: "",
  153. expBindAddr: "::",
  154. expClusterCIDR: "2001:101::/96",
  155. },
  156. }
  157. for _, tc := range testCases {
  158. t.Run(tc.name, func(t *testing.T) {
  159. // Create a fake client and set up default test configuration
  160. client := clientsetfake.NewSimpleClientset()
  161. // TODO: Consider using a YAML file instead for this that makes it possible to specify YAML documents for the ComponentConfigs
  162. controlPlaneConfig := &kubeadmapiv1beta2.InitConfiguration{
  163. LocalAPIEndpoint: kubeadmapiv1beta2.APIEndpoint{
  164. AdvertiseAddress: "1.2.3.4",
  165. BindPort: 1234,
  166. },
  167. }
  168. controlPlaneClusterConfig := &kubeadmapiv1beta2.ClusterConfiguration{
  169. Networking: kubeadmapiv1beta2.Networking{
  170. PodSubnet: "5.6.7.8/24",
  171. },
  172. ImageRepository: "someRepo",
  173. KubernetesVersion: constants.MinimumControlPlaneVersion.String(),
  174. }
  175. // Simulate an error if necessary
  176. switch tc.simError {
  177. case ServiceAccountError:
  178. client.PrependReactor("create", "serviceaccounts", func(action core.Action) (bool, runtime.Object, error) {
  179. return true, nil, apierrors.NewUnauthorized("")
  180. })
  181. case InvalidControlPlaneEndpoint:
  182. controlPlaneConfig.LocalAPIEndpoint.AdvertiseAddress = "1.2.3"
  183. case IPv6SetBindAddress:
  184. controlPlaneConfig.LocalAPIEndpoint.AdvertiseAddress = "1:2::3:4"
  185. controlPlaneClusterConfig.Networking.PodSubnet = "2001:101::/96"
  186. }
  187. intControlPlane, err := configutil.DefaultedInitConfiguration(controlPlaneConfig, controlPlaneClusterConfig)
  188. if err != nil {
  189. t.Errorf("test failed to convert external to internal version")
  190. return
  191. }
  192. intControlPlane.ComponentConfigs.KubeProxy = &kubeproxyconfig.KubeProxyConfiguration{
  193. BindAddress: "",
  194. HealthzBindAddress: "0.0.0.0:10256",
  195. MetricsBindAddress: "127.0.0.1:10249",
  196. Conntrack: kubeproxyconfig.KubeProxyConntrackConfiguration{
  197. MaxPerCore: pointer.Int32Ptr(1),
  198. Min: pointer.Int32Ptr(1),
  199. TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
  200. TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
  201. },
  202. }
  203. // Run dynamic defaulting again as we changed the internal cfg
  204. if err := configutil.SetInitDynamicDefaults(intControlPlane); err != nil {
  205. t.Errorf("test failed to set dynamic defaults: %v", err)
  206. return
  207. }
  208. err = EnsureProxyAddon(&intControlPlane.ClusterConfiguration, &intControlPlane.LocalAPIEndpoint, client)
  209. // Compare actual to expected errors
  210. actErr := "No error"
  211. if err != nil {
  212. actErr = err.Error()
  213. }
  214. expErr := "No error"
  215. if tc.expErrString != "" {
  216. expErr = tc.expErrString
  217. }
  218. if !strings.Contains(actErr, expErr) {
  219. t.Errorf(
  220. "%s test failed, expected: %s, got: %s",
  221. tc.name,
  222. expErr,
  223. actErr)
  224. }
  225. if intControlPlane.ComponentConfigs.KubeProxy.BindAddress != tc.expBindAddr {
  226. t.Errorf("%s test failed, expected: %s, got: %s",
  227. tc.name,
  228. tc.expBindAddr,
  229. intControlPlane.ComponentConfigs.KubeProxy.BindAddress)
  230. }
  231. if intControlPlane.ComponentConfigs.KubeProxy.ClusterCIDR != tc.expClusterCIDR {
  232. t.Errorf("%s test failed, expected: %s, got: %s",
  233. tc.name,
  234. tc.expClusterCIDR,
  235. intControlPlane.ComponentConfigs.KubeProxy.ClusterCIDR)
  236. }
  237. })
  238. }
  239. }
  240. func TestDaemonSetsHaveSystemNodeCriticalPriorityClassName(t *testing.T) {
  241. testCases := []struct {
  242. name string
  243. manifest string
  244. data interface{}
  245. }{
  246. {
  247. name: "KubeProxyDaemonSet19",
  248. manifest: KubeProxyDaemonSet19,
  249. data: struct{ Image, ProxyConfigMap, ProxyConfigMapKey string }{
  250. Image: "foo",
  251. ProxyConfigMap: "foo",
  252. ProxyConfigMapKey: "foo",
  253. },
  254. },
  255. }
  256. for _, testCase := range testCases {
  257. t.Run(testCase.name, func(t *testing.T) {
  258. daemonSetBytes, _ := kubeadmutil.ParseTemplate(testCase.manifest, testCase.data)
  259. daemonSet := &apps.DaemonSet{}
  260. if err := runtime.DecodeInto(clientsetscheme.Codecs.UniversalDecoder(), daemonSetBytes, daemonSet); err != nil {
  261. t.Errorf("unexpected error: %v", err)
  262. }
  263. if daemonSet.Spec.Template.Spec.PriorityClassName != "system-node-critical" {
  264. t.Errorf("expected to see system-node-critical priority class name. Got %q instead", daemonSet.Spec.Template.Spec.PriorityClassName)
  265. }
  266. })
  267. }
  268. }