local.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. Copyright 2019 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 utils
  14. /*
  15. * Various local test resource implementations.
  16. */
  17. import (
  18. "fmt"
  19. "path/filepath"
  20. "strings"
  21. "github.com/onsi/ginkgo"
  22. "k8s.io/api/core/v1"
  23. "k8s.io/apimachinery/pkg/util/uuid"
  24. "k8s.io/kubernetes/test/e2e/framework"
  25. )
  26. // LocalVolumeType represents type of local volume, e.g. tmpfs, directory,
  27. // block, etc.
  28. type LocalVolumeType string
  29. const (
  30. // A simple directory as local volume
  31. LocalVolumeDirectory LocalVolumeType = "dir"
  32. // Like LocalVolumeDirectory but it's a symbolic link to directory
  33. LocalVolumeDirectoryLink LocalVolumeType = "dir-link"
  34. // Like LocalVolumeDirectory but bind mounted
  35. LocalVolumeDirectoryBindMounted LocalVolumeType = "dir-bindmounted"
  36. // Like LocalVolumeDirectory but it's a symbolic link to self bind mounted directory
  37. // Note that bind mounting at symbolic link actually mounts at directory it
  38. // links to
  39. LocalVolumeDirectoryLinkBindMounted LocalVolumeType = "dir-link-bindmounted"
  40. // Use temporary filesystem as local volume
  41. LocalVolumeTmpfs LocalVolumeType = "tmpfs"
  42. // Block device, creates a local file, and maps it as a block device
  43. LocalVolumeBlock LocalVolumeType = "block"
  44. // Filesystem backed by a block device
  45. LocalVolumeBlockFS LocalVolumeType = "blockfs"
  46. // Use GCE Local SSD as local volume, this is a filesystem
  47. LocalVolumeGCELocalSSD LocalVolumeType = "gce-localssd-scsi-fs"
  48. )
  49. // LocalTestResource represents test resource of a local volume.
  50. type LocalTestResource struct {
  51. VolumeType LocalVolumeType
  52. Node *v1.Node
  53. // Volume path, path to filesystem or block device on the node
  54. Path string
  55. // If volume is backed by a loop device, we create loop device storage file
  56. // under this directory.
  57. loopDir string
  58. }
  59. // LocalTestResourceManager represents interface to create/destroy local test resources on node
  60. type LocalTestResourceManager interface {
  61. Create(node *v1.Node, volumeType LocalVolumeType, parameters map[string]string) *LocalTestResource
  62. Remove(ltr *LocalTestResource)
  63. }
  64. // ltrMgr implements LocalTestResourceManager
  65. type ltrMgr struct {
  66. prefix string
  67. hostExec HostExec
  68. // hostBase represents a writable directory on the host under which we
  69. // create test directories
  70. hostBase string
  71. }
  72. // NewLocalResourceManager returns a instance of LocalTestResourceManager
  73. func NewLocalResourceManager(prefix string, hostExec HostExec, hostBase string) LocalTestResourceManager {
  74. return &ltrMgr{
  75. prefix: prefix,
  76. hostExec: hostExec,
  77. hostBase: hostBase,
  78. }
  79. }
  80. // getTestDir returns a test dir under `hostBase` directory with randome name.
  81. func (l *ltrMgr) getTestDir() string {
  82. testDirName := fmt.Sprintf("%s-%s", l.prefix, string(uuid.NewUUID()))
  83. return filepath.Join(l.hostBase, testDirName)
  84. }
  85. func (l *ltrMgr) setupLocalVolumeTmpfs(node *v1.Node, parameters map[string]string) *LocalTestResource {
  86. hostDir := l.getTestDir()
  87. ginkgo.By(fmt.Sprintf("Creating tmpfs mount point on node %q at path %q", node.Name, hostDir))
  88. err := l.hostExec.IssueCommand(fmt.Sprintf("mkdir -p %q && sudo mount -t tmpfs -o size=10m tmpfs-%q %q", hostDir, hostDir, hostDir), node)
  89. framework.ExpectNoError(err)
  90. return &LocalTestResource{
  91. Node: node,
  92. Path: hostDir,
  93. }
  94. }
  95. func (l *ltrMgr) cleanupLocalVolumeTmpfs(ltr *LocalTestResource) {
  96. ginkgo.By(fmt.Sprintf("Unmount tmpfs mount point on node %q at path %q", ltr.Node.Name, ltr.Path))
  97. err := l.hostExec.IssueCommand(fmt.Sprintf("sudo umount %q", ltr.Path), ltr.Node)
  98. framework.ExpectNoError(err)
  99. ginkgo.By("Removing the test directory")
  100. err = l.hostExec.IssueCommand(fmt.Sprintf("rm -r %s", ltr.Path), ltr.Node)
  101. framework.ExpectNoError(err)
  102. }
  103. // createAndSetupLoopDevice creates an empty file and associates a loop devie with it.
  104. func (l *ltrMgr) createAndSetupLoopDevice(dir string, node *v1.Node, size int) {
  105. ginkgo.By(fmt.Sprintf("Creating block device on node %q using path %q", node.Name, dir))
  106. mkdirCmd := fmt.Sprintf("mkdir -p %s", dir)
  107. count := size / 4096
  108. // xfs requires at least 4096 blocks
  109. if count < 4096 {
  110. count = 4096
  111. }
  112. ddCmd := fmt.Sprintf("dd if=/dev/zero of=%s/file bs=4096 count=%d", dir, count)
  113. losetupCmd := fmt.Sprintf("sudo losetup -f %s/file", dir)
  114. err := l.hostExec.IssueCommand(fmt.Sprintf("%s && %s && %s", mkdirCmd, ddCmd, losetupCmd), node)
  115. framework.ExpectNoError(err)
  116. }
  117. // findLoopDevice finds loop device path by its associated storage directory.
  118. func (l *ltrMgr) findLoopDevice(dir string, node *v1.Node) string {
  119. cmd := fmt.Sprintf("E2E_LOOP_DEV=$(sudo losetup | grep %s/file | awk '{ print $1 }') 2>&1 > /dev/null && echo ${E2E_LOOP_DEV}", dir)
  120. loopDevResult, err := l.hostExec.IssueCommandWithResult(cmd, node)
  121. framework.ExpectNoError(err)
  122. return strings.TrimSpace(loopDevResult)
  123. }
  124. func (l *ltrMgr) setupLocalVolumeBlock(node *v1.Node, parameters map[string]string) *LocalTestResource {
  125. loopDir := l.getTestDir()
  126. l.createAndSetupLoopDevice(loopDir, node, 20*1024*1024)
  127. loopDev := l.findLoopDevice(loopDir, node)
  128. return &LocalTestResource{
  129. Node: node,
  130. Path: loopDev,
  131. loopDir: loopDir,
  132. }
  133. }
  134. // teardownLoopDevice tears down loop device by its associated storage directory.
  135. func (l *ltrMgr) teardownLoopDevice(dir string, node *v1.Node) {
  136. loopDev := l.findLoopDevice(dir, node)
  137. ginkgo.By(fmt.Sprintf("Tear down block device %q on node %q at path %s/file", loopDev, node.Name, dir))
  138. losetupDeleteCmd := fmt.Sprintf("sudo losetup -d %s", loopDev)
  139. err := l.hostExec.IssueCommand(losetupDeleteCmd, node)
  140. framework.ExpectNoError(err)
  141. return
  142. }
  143. func (l *ltrMgr) cleanupLocalVolumeBlock(ltr *LocalTestResource) {
  144. l.teardownLoopDevice(ltr.loopDir, ltr.Node)
  145. ginkgo.By(fmt.Sprintf("Removing the test directory %s", ltr.loopDir))
  146. removeCmd := fmt.Sprintf("rm -r %s", ltr.loopDir)
  147. err := l.hostExec.IssueCommand(removeCmd, ltr.Node)
  148. framework.ExpectNoError(err)
  149. }
  150. func (l *ltrMgr) setupLocalVolumeBlockFS(node *v1.Node, parameters map[string]string) *LocalTestResource {
  151. ltr := l.setupLocalVolumeBlock(node, parameters)
  152. loopDev := ltr.Path
  153. loopDir := ltr.loopDir
  154. // Format and mount at loopDir and give others rwx for read/write testing
  155. cmd := fmt.Sprintf("sudo mkfs -t ext4 %s && sudo mount -t ext4 %s %s && sudo chmod o+rwx %s", loopDev, loopDev, loopDir, loopDir)
  156. err := l.hostExec.IssueCommand(cmd, node)
  157. framework.ExpectNoError(err)
  158. return &LocalTestResource{
  159. Node: node,
  160. Path: loopDir,
  161. loopDir: loopDir,
  162. }
  163. }
  164. func (l *ltrMgr) cleanupLocalVolumeBlockFS(ltr *LocalTestResource) {
  165. umountCmd := fmt.Sprintf("sudo umount %s", ltr.Path)
  166. err := l.hostExec.IssueCommand(umountCmd, ltr.Node)
  167. framework.ExpectNoError(err)
  168. l.cleanupLocalVolumeBlock(ltr)
  169. }
  170. func (l *ltrMgr) setupLocalVolumeDirectory(node *v1.Node, parameters map[string]string) *LocalTestResource {
  171. hostDir := l.getTestDir()
  172. mkdirCmd := fmt.Sprintf("mkdir -p %s", hostDir)
  173. err := l.hostExec.IssueCommand(mkdirCmd, node)
  174. framework.ExpectNoError(err)
  175. return &LocalTestResource{
  176. Node: node,
  177. Path: hostDir,
  178. }
  179. }
  180. func (l *ltrMgr) cleanupLocalVolumeDirectory(ltr *LocalTestResource) {
  181. ginkgo.By("Removing the test directory")
  182. removeCmd := fmt.Sprintf("rm -r %s", ltr.Path)
  183. err := l.hostExec.IssueCommand(removeCmd, ltr.Node)
  184. framework.ExpectNoError(err)
  185. }
  186. func (l *ltrMgr) setupLocalVolumeDirectoryLink(node *v1.Node, parameters map[string]string) *LocalTestResource {
  187. hostDir := l.getTestDir()
  188. hostDirBackend := hostDir + "-backend"
  189. cmd := fmt.Sprintf("mkdir %s && sudo ln -s %s %s", hostDirBackend, hostDirBackend, hostDir)
  190. err := l.hostExec.IssueCommand(cmd, node)
  191. framework.ExpectNoError(err)
  192. return &LocalTestResource{
  193. Node: node,
  194. Path: hostDir,
  195. }
  196. }
  197. func (l *ltrMgr) cleanupLocalVolumeDirectoryLink(ltr *LocalTestResource) {
  198. ginkgo.By("Removing the test directory")
  199. hostDir := ltr.Path
  200. hostDirBackend := hostDir + "-backend"
  201. removeCmd := fmt.Sprintf("sudo rm -r %s && rm -r %s", hostDir, hostDirBackend)
  202. err := l.hostExec.IssueCommand(removeCmd, ltr.Node)
  203. framework.ExpectNoError(err)
  204. }
  205. func (l *ltrMgr) setupLocalVolumeDirectoryBindMounted(node *v1.Node, parameters map[string]string) *LocalTestResource {
  206. hostDir := l.getTestDir()
  207. cmd := fmt.Sprintf("mkdir %s && sudo mount --bind %s %s", hostDir, hostDir, hostDir)
  208. err := l.hostExec.IssueCommand(cmd, node)
  209. framework.ExpectNoError(err)
  210. return &LocalTestResource{
  211. Node: node,
  212. Path: hostDir,
  213. }
  214. }
  215. func (l *ltrMgr) cleanupLocalVolumeDirectoryBindMounted(ltr *LocalTestResource) {
  216. ginkgo.By("Removing the test directory")
  217. hostDir := ltr.Path
  218. removeCmd := fmt.Sprintf("sudo umount %s && rm -r %s", hostDir, hostDir)
  219. err := l.hostExec.IssueCommand(removeCmd, ltr.Node)
  220. framework.ExpectNoError(err)
  221. }
  222. func (l *ltrMgr) setupLocalVolumeDirectoryLinkBindMounted(node *v1.Node, parameters map[string]string) *LocalTestResource {
  223. hostDir := l.getTestDir()
  224. hostDirBackend := hostDir + "-backend"
  225. cmd := fmt.Sprintf("mkdir %s && sudo mount --bind %s %s && sudo ln -s %s %s", hostDirBackend, hostDirBackend, hostDirBackend, hostDirBackend, hostDir)
  226. err := l.hostExec.IssueCommand(cmd, node)
  227. framework.ExpectNoError(err)
  228. return &LocalTestResource{
  229. Node: node,
  230. Path: hostDir,
  231. }
  232. }
  233. func (l *ltrMgr) cleanupLocalVolumeDirectoryLinkBindMounted(ltr *LocalTestResource) {
  234. ginkgo.By("Removing the test directory")
  235. hostDir := ltr.Path
  236. hostDirBackend := hostDir + "-backend"
  237. removeCmd := fmt.Sprintf("sudo rm %s && sudo umount %s && rm -r %s", hostDir, hostDirBackend, hostDirBackend)
  238. err := l.hostExec.IssueCommand(removeCmd, ltr.Node)
  239. framework.ExpectNoError(err)
  240. }
  241. func (l *ltrMgr) setupLocalVolumeGCELocalSSD(node *v1.Node, parameters map[string]string) *LocalTestResource {
  242. res, err := l.hostExec.IssueCommandWithResult("ls /mnt/disks/by-uuid/google-local-ssds-scsi-fs/", node)
  243. framework.ExpectNoError(err)
  244. dirName := strings.Fields(res)[0]
  245. hostDir := "/mnt/disks/by-uuid/google-local-ssds-scsi-fs/" + dirName
  246. return &LocalTestResource{
  247. Node: node,
  248. Path: hostDir,
  249. }
  250. }
  251. func (l *ltrMgr) cleanupLocalVolumeGCELocalSSD(ltr *LocalTestResource) {
  252. // This filesystem is attached in cluster initialization, we clean all files to make it reusable.
  253. removeCmd := fmt.Sprintf("find '%s' -mindepth 1 -maxdepth 1 -print0 | xargs -r -0 rm -rf", ltr.Path)
  254. err := l.hostExec.IssueCommand(removeCmd, ltr.Node)
  255. framework.ExpectNoError(err)
  256. }
  257. func (l *ltrMgr) Create(node *v1.Node, volumeType LocalVolumeType, parameters map[string]string) *LocalTestResource {
  258. var ltr *LocalTestResource
  259. switch volumeType {
  260. case LocalVolumeDirectory:
  261. ltr = l.setupLocalVolumeDirectory(node, parameters)
  262. case LocalVolumeDirectoryLink:
  263. ltr = l.setupLocalVolumeDirectoryLink(node, parameters)
  264. case LocalVolumeDirectoryBindMounted:
  265. ltr = l.setupLocalVolumeDirectoryBindMounted(node, parameters)
  266. case LocalVolumeDirectoryLinkBindMounted:
  267. ltr = l.setupLocalVolumeDirectoryLinkBindMounted(node, parameters)
  268. case LocalVolumeTmpfs:
  269. ltr = l.setupLocalVolumeTmpfs(node, parameters)
  270. case LocalVolumeBlock:
  271. ltr = l.setupLocalVolumeBlock(node, parameters)
  272. case LocalVolumeBlockFS:
  273. ltr = l.setupLocalVolumeBlockFS(node, parameters)
  274. case LocalVolumeGCELocalSSD:
  275. ltr = l.setupLocalVolumeGCELocalSSD(node, parameters)
  276. default:
  277. framework.Failf("Failed to create local test resource on node %q, unsupported volume type: %v is specified", node.Name, volumeType)
  278. return nil
  279. }
  280. if ltr == nil {
  281. framework.Failf("Failed to create local test resource on node %q, volume type: %v, parameters: %v", node.Name, volumeType, parameters)
  282. }
  283. ltr.VolumeType = volumeType
  284. return ltr
  285. }
  286. func (l *ltrMgr) Remove(ltr *LocalTestResource) {
  287. switch ltr.VolumeType {
  288. case LocalVolumeDirectory:
  289. l.cleanupLocalVolumeDirectory(ltr)
  290. case LocalVolumeDirectoryLink:
  291. l.cleanupLocalVolumeDirectoryLink(ltr)
  292. case LocalVolumeDirectoryBindMounted:
  293. l.cleanupLocalVolumeDirectoryBindMounted(ltr)
  294. case LocalVolumeDirectoryLinkBindMounted:
  295. l.cleanupLocalVolumeDirectoryLinkBindMounted(ltr)
  296. case LocalVolumeTmpfs:
  297. l.cleanupLocalVolumeTmpfs(ltr)
  298. case LocalVolumeBlock:
  299. l.cleanupLocalVolumeBlock(ltr)
  300. case LocalVolumeBlockFS:
  301. l.cleanupLocalVolumeBlockFS(ltr)
  302. case LocalVolumeGCELocalSSD:
  303. l.cleanupLocalVolumeGCELocalSSD(ltr)
  304. default:
  305. framework.Failf("Failed to remove local test resource, unsupported volume type: %v is specified", ltr.VolumeType)
  306. }
  307. return
  308. }