host_exec.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. import (
  15. "fmt"
  16. "k8s.io/api/core/v1"
  17. "k8s.io/kubernetes/test/e2e/framework"
  18. )
  19. // HostExec represents interface we require to execute commands on remote host.
  20. type HostExec interface {
  21. IssueCommandWithResult(cmd string, node *v1.Node) (string, error)
  22. IssueCommand(cmd string, node *v1.Node) error
  23. Cleanup()
  24. }
  25. // hostExecutor implements HostExec
  26. type hostExecutor struct {
  27. *framework.Framework
  28. nodeExecPods map[string]*v1.Pod
  29. }
  30. // NewHostExec returns a HostExec
  31. func NewHostExec(framework *framework.Framework) HostExec {
  32. return &hostExecutor{
  33. Framework: framework,
  34. nodeExecPods: make(map[string]*v1.Pod),
  35. }
  36. }
  37. // launchNodeExecPod launches a hostexec pod for local PV and waits
  38. // until it's Running.
  39. func (h *hostExecutor) launchNodeExecPod(node string) *v1.Pod {
  40. f := h.Framework
  41. cs := f.ClientSet
  42. ns := f.Namespace
  43. hostExecPod := framework.NewExecPodSpec(ns.Name, fmt.Sprintf("hostexec-%s", node), true)
  44. hostExecPod.Spec.NodeName = node
  45. hostExecPod.Spec.Volumes = []v1.Volume{
  46. {
  47. // Required to enter into host mount namespace via nsenter.
  48. Name: "rootfs",
  49. VolumeSource: v1.VolumeSource{
  50. HostPath: &v1.HostPathVolumeSource{
  51. Path: "/",
  52. },
  53. },
  54. },
  55. }
  56. hostExecPod.Spec.Containers[0].VolumeMounts = []v1.VolumeMount{
  57. {
  58. Name: "rootfs",
  59. MountPath: "/rootfs",
  60. ReadOnly: true,
  61. },
  62. }
  63. hostExecPod.Spec.Containers[0].SecurityContext = &v1.SecurityContext{
  64. Privileged: func(privileged bool) *bool {
  65. return &privileged
  66. }(true),
  67. }
  68. pod, err := cs.CoreV1().Pods(ns.Name).Create(hostExecPod)
  69. framework.ExpectNoError(err)
  70. err = framework.WaitForPodRunningInNamespace(cs, pod)
  71. framework.ExpectNoError(err)
  72. return pod
  73. }
  74. // IssueCommandWithResult issues command on given node and returns stdout.
  75. func (h *hostExecutor) IssueCommandWithResult(cmd string, node *v1.Node) (string, error) {
  76. pod, ok := h.nodeExecPods[node.Name]
  77. if !ok {
  78. pod = h.launchNodeExecPod(node.Name)
  79. if pod == nil {
  80. return "", fmt.Errorf("failed to create hostexec pod for node %q", node)
  81. }
  82. h.nodeExecPods[node.Name] = pod
  83. }
  84. args := []string{
  85. "exec",
  86. fmt.Sprintf("--namespace=%v", pod.Namespace),
  87. pod.Name,
  88. "--",
  89. "nsenter",
  90. "--mount=/rootfs/proc/1/ns/mnt",
  91. "--",
  92. "sh",
  93. "-c",
  94. cmd,
  95. }
  96. return framework.RunKubectl(args...)
  97. }
  98. // IssueCommand works like IssueCommandWithResult, but discards result.
  99. func (h *hostExecutor) IssueCommand(cmd string, node *v1.Node) error {
  100. _, err := h.IssueCommandWithResult(cmd, node)
  101. return err
  102. }
  103. // Cleanup cleanup resources it created during test.
  104. // Note that in most cases it is not necessary to call this because we create
  105. // pods under test namespace which will be destroyed in teardown phase.
  106. func (h *hostExecutor) Cleanup() {
  107. for _, pod := range h.nodeExecPods {
  108. framework.DeletePodOrFail(h.Framework.ClientSet, pod.Namespace, pod.Name)
  109. }
  110. h.nodeExecPods = make(map[string]*v1.Pod)
  111. }