proxy_test.go 7.4 KB

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