flexvolume.go 7.5 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 storage
  14. import (
  15. "fmt"
  16. "math/rand"
  17. "net"
  18. "path"
  19. "time"
  20. "github.com/onsi/ginkgo"
  21. v1 "k8s.io/api/core/v1"
  22. apierrs "k8s.io/apimachinery/pkg/api/errors"
  23. clientset "k8s.io/client-go/kubernetes"
  24. "k8s.io/kubernetes/test/e2e/framework"
  25. e2essh "k8s.io/kubernetes/test/e2e/framework/ssh"
  26. "k8s.io/kubernetes/test/e2e/framework/testfiles"
  27. "k8s.io/kubernetes/test/e2e/framework/volume"
  28. "k8s.io/kubernetes/test/e2e/storage/utils"
  29. )
  30. const (
  31. sshPort = "22"
  32. driverDir = "test/e2e/testing-manifests/flexvolume/"
  33. defaultVolumePluginDir = "/usr/libexec/kubernetes/kubelet-plugins/volume/exec"
  34. // TODO: change this and config-test.sh when default flex volume install path is changed for GCI
  35. // On gci, root is read-only and controller-manager containerized. Assume
  36. // controller-manager has started with --flex-volume-plugin-dir equal to this
  37. // (see cluster/gce/config-test.sh)
  38. gciVolumePluginDir = "/home/kubernetes/flexvolume"
  39. detachTimeout = 10 * time.Second
  40. )
  41. // testFlexVolume tests that a client pod using a given flexvolume driver
  42. // successfully mounts it and runs
  43. func testFlexVolume(driver string, cs clientset.Interface, config volume.TestConfig, f *framework.Framework) {
  44. tests := []volume.Test{
  45. {
  46. Volume: v1.VolumeSource{
  47. FlexVolume: &v1.FlexVolumeSource{
  48. Driver: "k8s/" + driver,
  49. },
  50. },
  51. File: "index.html",
  52. // Must match content of examples/volumes/flexvolume/dummy(-attachable) domount
  53. ExpectedContent: "Hello from flexvolume!",
  54. },
  55. }
  56. volume.TestVolumeClient(cs, config, nil, "" /* fsType */, tests)
  57. volume.TestCleanup(f, config)
  58. }
  59. // installFlex installs the driver found at filePath on the node, and restarts
  60. // kubelet if 'restart' is true. If node is nil, installs on the master, and restarts
  61. // controller-manager if 'restart' is true.
  62. func installFlex(c clientset.Interface, node *v1.Node, vendor, driver, filePath string) {
  63. flexDir := getFlexDir(c, node, vendor, driver)
  64. flexFile := path.Join(flexDir, driver)
  65. host := ""
  66. var err error
  67. if node != nil {
  68. host, err = framework.GetNodeExternalIP(node)
  69. if err != nil {
  70. host, err = framework.GetNodeInternalIP(node)
  71. }
  72. } else {
  73. masterHostWithPort := framework.GetMasterHost()
  74. hostName := getHostFromHostPort(masterHostWithPort)
  75. host = net.JoinHostPort(hostName, sshPort)
  76. }
  77. framework.ExpectNoError(err)
  78. cmd := fmt.Sprintf("sudo mkdir -p %s", flexDir)
  79. sshAndLog(cmd, host, true /*failOnError*/)
  80. data := testfiles.ReadOrDie(filePath, ginkgo.Fail)
  81. cmd = fmt.Sprintf("sudo tee <<'EOF' %s\n%s\nEOF", flexFile, string(data))
  82. sshAndLog(cmd, host, true /*failOnError*/)
  83. cmd = fmt.Sprintf("sudo chmod +x %s", flexFile)
  84. sshAndLog(cmd, host, true /*failOnError*/)
  85. }
  86. func uninstallFlex(c clientset.Interface, node *v1.Node, vendor, driver string) {
  87. flexDir := getFlexDir(c, node, vendor, driver)
  88. host := ""
  89. var err error
  90. if node != nil {
  91. host, err = framework.GetNodeExternalIP(node)
  92. if err != nil {
  93. host, err = framework.GetNodeInternalIP(node)
  94. }
  95. } else {
  96. masterHostWithPort := framework.GetMasterHost()
  97. hostName := getHostFromHostPort(masterHostWithPort)
  98. host = net.JoinHostPort(hostName, sshPort)
  99. }
  100. if host == "" {
  101. framework.Failf("Error getting node ip : %v", err)
  102. }
  103. cmd := fmt.Sprintf("sudo rm -r %s", flexDir)
  104. sshAndLog(cmd, host, false /*failOnError*/)
  105. }
  106. func getFlexDir(c clientset.Interface, node *v1.Node, vendor, driver string) string {
  107. volumePluginDir := defaultVolumePluginDir
  108. if framework.ProviderIs("gce") {
  109. volumePluginDir = gciVolumePluginDir
  110. }
  111. flexDir := path.Join(volumePluginDir, fmt.Sprintf("/%s~%s/", vendor, driver))
  112. return flexDir
  113. }
  114. func sshAndLog(cmd, host string, failOnError bool) {
  115. result, err := e2essh.SSH(cmd, host, framework.TestContext.Provider)
  116. e2essh.LogResult(result)
  117. framework.ExpectNoError(err)
  118. if result.Code != 0 && failOnError {
  119. framework.Failf("%s returned non-zero, stderr: %s", cmd, result.Stderr)
  120. }
  121. }
  122. func getHostFromHostPort(hostPort string) string {
  123. // try to split host and port
  124. var host string
  125. var err error
  126. if host, _, err = net.SplitHostPort(hostPort); err != nil {
  127. // if SplitHostPort returns an error, the entire hostport is considered as host
  128. host = hostPort
  129. }
  130. return host
  131. }
  132. var _ = utils.SIGDescribe("Flexvolumes", func() {
  133. f := framework.NewDefaultFramework("flexvolume")
  134. // note that namespace deletion is handled by delete-namespace flag
  135. var cs clientset.Interface
  136. var ns *v1.Namespace
  137. var node v1.Node
  138. var config volume.TestConfig
  139. var suffix string
  140. ginkgo.BeforeEach(func() {
  141. framework.SkipUnlessProviderIs("gce", "local")
  142. framework.SkipUnlessMasterOSDistroIs("debian", "ubuntu", "gci", "custom")
  143. framework.SkipUnlessNodeOSDistroIs("debian", "ubuntu", "gci", "custom")
  144. framework.SkipUnlessSSHKeyPresent()
  145. cs = f.ClientSet
  146. ns = f.Namespace
  147. nodes := framework.GetReadySchedulableNodesOrDie(f.ClientSet)
  148. node = nodes.Items[rand.Intn(len(nodes.Items))]
  149. config = volume.TestConfig{
  150. Namespace: ns.Name,
  151. Prefix: "flex",
  152. ClientNodeName: node.Name,
  153. }
  154. suffix = ns.Name
  155. })
  156. ginkgo.It("should be mountable when non-attachable", func() {
  157. driver := "dummy"
  158. driverInstallAs := driver + "-" + suffix
  159. ginkgo.By(fmt.Sprintf("installing flexvolume %s on node %s as %s", path.Join(driverDir, driver), node.Name, driverInstallAs))
  160. installFlex(cs, &node, "k8s", driverInstallAs, path.Join(driverDir, driver))
  161. testFlexVolume(driverInstallAs, cs, config, f)
  162. ginkgo.By("waiting for flex client pod to terminate")
  163. if err := f.WaitForPodTerminated(config.Prefix+"-client", ""); !apierrs.IsNotFound(err) {
  164. framework.ExpectNoError(err, "Failed to wait client pod terminated: %v", err)
  165. }
  166. ginkgo.By(fmt.Sprintf("uninstalling flexvolume %s from node %s", driverInstallAs, node.Name))
  167. uninstallFlex(cs, &node, "k8s", driverInstallAs)
  168. })
  169. ginkgo.It("should be mountable when attachable", func() {
  170. driver := "dummy-attachable"
  171. driverInstallAs := driver + "-" + suffix
  172. ginkgo.By(fmt.Sprintf("installing flexvolume %s on node %s as %s", path.Join(driverDir, driver), node.Name, driverInstallAs))
  173. installFlex(cs, &node, "k8s", driverInstallAs, path.Join(driverDir, driver))
  174. ginkgo.By(fmt.Sprintf("installing flexvolume %s on master as %s", path.Join(driverDir, driver), driverInstallAs))
  175. installFlex(cs, nil, "k8s", driverInstallAs, path.Join(driverDir, driver))
  176. testFlexVolume(driverInstallAs, cs, config, f)
  177. ginkgo.By("waiting for flex client pod to terminate")
  178. if err := f.WaitForPodTerminated(config.Prefix+"-client", ""); !apierrs.IsNotFound(err) {
  179. framework.ExpectNoError(err, "Failed to wait client pod terminated: %v", err)
  180. }
  181. // Detach might occur after pod deletion. Wait before deleting driver.
  182. time.Sleep(detachTimeout)
  183. ginkgo.By(fmt.Sprintf("uninstalling flexvolume %s from node %s", driverInstallAs, node.Name))
  184. uninstallFlex(cs, &node, "k8s", driverInstallAs)
  185. ginkgo.By(fmt.Sprintf("uninstalling flexvolume %s from master", driverInstallAs))
  186. uninstallFlex(cs, nil, "k8s", driverInstallAs)
  187. })
  188. })