local_test.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 etcd
  14. import (
  15. "fmt"
  16. "os"
  17. "path/filepath"
  18. "reflect"
  19. "sort"
  20. "testing"
  21. kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
  22. kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  23. etcdutil "k8s.io/kubernetes/cmd/kubeadm/app/util/etcd"
  24. testutil "k8s.io/kubernetes/cmd/kubeadm/test"
  25. )
  26. func TestGetEtcdPodSpec(t *testing.T) {
  27. // Creates a ClusterConfiguration
  28. cfg := &kubeadmapi.ClusterConfiguration{
  29. KubernetesVersion: "v1.7.0",
  30. Etcd: kubeadmapi.Etcd{
  31. Local: &kubeadmapi.LocalEtcd{
  32. DataDir: "/var/lib/etcd",
  33. },
  34. },
  35. }
  36. endpoint := &kubeadmapi.APIEndpoint{}
  37. // Executes GetEtcdPodSpec
  38. spec := GetEtcdPodSpec(cfg, endpoint, "", []etcdutil.Member{})
  39. // Assert each specs refers to the right pod
  40. if spec.Spec.Containers[0].Name != kubeadmconstants.Etcd {
  41. t.Errorf("getKubeConfigSpecs spec for etcd contains pod %s, expects %s", spec.Spec.Containers[0].Name, kubeadmconstants.Etcd)
  42. }
  43. }
  44. func TestCreateLocalEtcdStaticPodManifestFile(t *testing.T) {
  45. // Create temp folder for the test case
  46. tmpdir := testutil.SetupTempDir(t)
  47. defer os.RemoveAll(tmpdir)
  48. var tests = []struct {
  49. cfg *kubeadmapi.ClusterConfiguration
  50. expectedError bool
  51. }{
  52. {
  53. cfg: &kubeadmapi.ClusterConfiguration{
  54. KubernetesVersion: "v1.7.0",
  55. Etcd: kubeadmapi.Etcd{
  56. Local: &kubeadmapi.LocalEtcd{
  57. DataDir: tmpdir + "/etcd",
  58. },
  59. },
  60. },
  61. expectedError: false,
  62. },
  63. {
  64. cfg: &kubeadmapi.ClusterConfiguration{
  65. KubernetesVersion: "v1.7.0",
  66. Etcd: kubeadmapi.Etcd{
  67. External: &kubeadmapi.ExternalEtcd{
  68. Endpoints: []string{
  69. "https://etcd-instance:2379",
  70. },
  71. CAFile: "/etc/kubernetes/pki/etcd/ca.crt",
  72. CertFile: "/etc/kubernetes/pki/etcd/apiserver-etcd-client.crt",
  73. KeyFile: "/etc/kubernetes/pki/etcd/apiserver-etcd-client.key",
  74. },
  75. },
  76. },
  77. expectedError: true,
  78. },
  79. }
  80. for _, test := range tests {
  81. // Execute createStaticPodFunction
  82. manifestPath := filepath.Join(tmpdir, kubeadmconstants.ManifestsSubDirName)
  83. err := CreateLocalEtcdStaticPodManifestFile(manifestPath, "", test.cfg, &kubeadmapi.APIEndpoint{})
  84. if !test.expectedError {
  85. if err != nil {
  86. t.Errorf("CreateLocalEtcdStaticPodManifestFile failed when not expected: %v", err)
  87. }
  88. // Assert expected files are there
  89. testutil.AssertFilesCount(t, manifestPath, 1)
  90. testutil.AssertFileExists(t, manifestPath, kubeadmconstants.Etcd+".yaml")
  91. } else {
  92. testutil.AssertError(t, err, "etcd static pod manifest cannot be generated for cluster using external etcd")
  93. }
  94. }
  95. }
  96. func TestGetEtcdCommand(t *testing.T) {
  97. var tests = []struct {
  98. name string
  99. advertiseAddress string
  100. nodeName string
  101. extraArgs map[string]string
  102. initialCluster []etcdutil.Member
  103. expected []string
  104. }{
  105. {
  106. name: "Default args - with empty etcd initial cluster",
  107. advertiseAddress: "1.2.3.4",
  108. nodeName: "foo",
  109. expected: []string{
  110. "etcd",
  111. "--name=foo",
  112. fmt.Sprintf("--listen-client-urls=https://127.0.0.1:%d,https://1.2.3.4:%d", kubeadmconstants.EtcdListenClientPort, kubeadmconstants.EtcdListenClientPort),
  113. fmt.Sprintf("--advertise-client-urls=https://1.2.3.4:%d", kubeadmconstants.EtcdListenClientPort),
  114. fmt.Sprintf("--listen-peer-urls=https://1.2.3.4:%d", kubeadmconstants.EtcdListenPeerPort),
  115. fmt.Sprintf("--initial-advertise-peer-urls=https://1.2.3.4:%d", kubeadmconstants.EtcdListenPeerPort),
  116. "--data-dir=/var/lib/etcd",
  117. "--cert-file=" + kubeadmconstants.EtcdServerCertName,
  118. "--key-file=" + kubeadmconstants.EtcdServerKeyName,
  119. "--trusted-ca-file=" + kubeadmconstants.EtcdCACertName,
  120. "--client-cert-auth=true",
  121. "--peer-cert-file=" + kubeadmconstants.EtcdPeerCertName,
  122. "--peer-key-file=" + kubeadmconstants.EtcdPeerKeyName,
  123. "--peer-trusted-ca-file=" + kubeadmconstants.EtcdCACertName,
  124. "--snapshot-count=10000",
  125. "--peer-client-cert-auth=true",
  126. fmt.Sprintf("--initial-cluster=foo=https://1.2.3.4:%d", kubeadmconstants.EtcdListenPeerPort),
  127. },
  128. },
  129. {
  130. name: "Default args - With an existing etcd cluster",
  131. advertiseAddress: "1.2.3.4",
  132. nodeName: "foo",
  133. initialCluster: []etcdutil.Member{
  134. {Name: "foo", PeerURL: fmt.Sprintf("https://1.2.3.4:%d", kubeadmconstants.EtcdListenPeerPort)}, // NB. the joining etcd instance should be part of the initialCluster list
  135. {Name: "bar", PeerURL: fmt.Sprintf("https://5.6.7.8:%d", kubeadmconstants.EtcdListenPeerPort)},
  136. },
  137. expected: []string{
  138. "etcd",
  139. "--name=foo",
  140. fmt.Sprintf("--listen-client-urls=https://127.0.0.1:%d,https://1.2.3.4:%d", kubeadmconstants.EtcdListenClientPort, kubeadmconstants.EtcdListenClientPort),
  141. fmt.Sprintf("--advertise-client-urls=https://1.2.3.4:%d", kubeadmconstants.EtcdListenClientPort),
  142. fmt.Sprintf("--listen-peer-urls=https://1.2.3.4:%d", kubeadmconstants.EtcdListenPeerPort),
  143. fmt.Sprintf("--initial-advertise-peer-urls=https://1.2.3.4:%d", kubeadmconstants.EtcdListenPeerPort),
  144. "--data-dir=/var/lib/etcd",
  145. "--cert-file=" + kubeadmconstants.EtcdServerCertName,
  146. "--key-file=" + kubeadmconstants.EtcdServerKeyName,
  147. "--trusted-ca-file=" + kubeadmconstants.EtcdCACertName,
  148. "--client-cert-auth=true",
  149. "--peer-cert-file=" + kubeadmconstants.EtcdPeerCertName,
  150. "--peer-key-file=" + kubeadmconstants.EtcdPeerKeyName,
  151. "--peer-trusted-ca-file=" + kubeadmconstants.EtcdCACertName,
  152. "--snapshot-count=10000",
  153. "--peer-client-cert-auth=true",
  154. "--initial-cluster-state=existing",
  155. fmt.Sprintf("--initial-cluster=foo=https://1.2.3.4:%d,bar=https://5.6.7.8:%d", kubeadmconstants.EtcdListenPeerPort, kubeadmconstants.EtcdListenPeerPort),
  156. },
  157. },
  158. {
  159. name: "Extra args",
  160. advertiseAddress: "1.2.3.4",
  161. nodeName: "bar",
  162. extraArgs: map[string]string{
  163. "listen-client-urls": "https://10.0.1.10:2379",
  164. "advertise-client-urls": "https://10.0.1.10:2379",
  165. },
  166. expected: []string{
  167. "etcd",
  168. "--name=bar",
  169. "--listen-client-urls=https://10.0.1.10:2379",
  170. "--advertise-client-urls=https://10.0.1.10:2379",
  171. fmt.Sprintf("--listen-peer-urls=https://1.2.3.4:%d", kubeadmconstants.EtcdListenPeerPort),
  172. fmt.Sprintf("--initial-advertise-peer-urls=https://1.2.3.4:%d", kubeadmconstants.EtcdListenPeerPort),
  173. "--data-dir=/var/lib/etcd",
  174. "--cert-file=" + kubeadmconstants.EtcdServerCertName,
  175. "--key-file=" + kubeadmconstants.EtcdServerKeyName,
  176. "--trusted-ca-file=" + kubeadmconstants.EtcdCACertName,
  177. "--client-cert-auth=true",
  178. "--peer-cert-file=" + kubeadmconstants.EtcdPeerCertName,
  179. "--peer-key-file=" + kubeadmconstants.EtcdPeerKeyName,
  180. "--peer-trusted-ca-file=" + kubeadmconstants.EtcdCACertName,
  181. "--snapshot-count=10000",
  182. "--peer-client-cert-auth=true",
  183. fmt.Sprintf("--initial-cluster=bar=https://1.2.3.4:%d", kubeadmconstants.EtcdListenPeerPort),
  184. },
  185. },
  186. {
  187. name: "IPv6 advertise address",
  188. advertiseAddress: "2001:db8::3",
  189. nodeName: "foo",
  190. expected: []string{
  191. "etcd",
  192. "--name=foo",
  193. fmt.Sprintf("--listen-client-urls=https://127.0.0.1:%d,https://[2001:db8::3]:%d", kubeadmconstants.EtcdListenClientPort, kubeadmconstants.EtcdListenClientPort),
  194. fmt.Sprintf("--advertise-client-urls=https://[2001:db8::3]:%d", kubeadmconstants.EtcdListenClientPort),
  195. fmt.Sprintf("--listen-peer-urls=https://[2001:db8::3]:%d", kubeadmconstants.EtcdListenPeerPort),
  196. fmt.Sprintf("--initial-advertise-peer-urls=https://[2001:db8::3]:%d", kubeadmconstants.EtcdListenPeerPort),
  197. "--data-dir=/var/lib/etcd",
  198. "--cert-file=" + kubeadmconstants.EtcdServerCertName,
  199. "--key-file=" + kubeadmconstants.EtcdServerKeyName,
  200. "--trusted-ca-file=" + kubeadmconstants.EtcdCACertName,
  201. "--client-cert-auth=true",
  202. "--peer-cert-file=" + kubeadmconstants.EtcdPeerCertName,
  203. "--peer-key-file=" + kubeadmconstants.EtcdPeerKeyName,
  204. "--peer-trusted-ca-file=" + kubeadmconstants.EtcdCACertName,
  205. "--snapshot-count=10000",
  206. "--peer-client-cert-auth=true",
  207. fmt.Sprintf("--initial-cluster=foo=https://[2001:db8::3]:%d", kubeadmconstants.EtcdListenPeerPort),
  208. },
  209. },
  210. }
  211. for _, rt := range tests {
  212. t.Run(rt.name, func(t *testing.T) {
  213. endpoint := &kubeadmapi.APIEndpoint{
  214. AdvertiseAddress: rt.advertiseAddress,
  215. }
  216. cfg := &kubeadmapi.ClusterConfiguration{
  217. Etcd: kubeadmapi.Etcd{
  218. Local: &kubeadmapi.LocalEtcd{
  219. DataDir: "/var/lib/etcd",
  220. ExtraArgs: rt.extraArgs,
  221. },
  222. },
  223. }
  224. actual := getEtcdCommand(cfg, endpoint, rt.nodeName, rt.initialCluster)
  225. sort.Strings(actual)
  226. sort.Strings(rt.expected)
  227. if !reflect.DeepEqual(actual, rt.expected) {
  228. t.Errorf("failed getEtcdCommand:\nexpected:\n%v\nsaw:\n%v", rt.expected, actual)
  229. }
  230. })
  231. }
  232. }