utils.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Copyright 2016 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 remote
  14. import (
  15. "fmt"
  16. "path/filepath"
  17. "strings"
  18. "k8s.io/klog"
  19. )
  20. // utils.go contains functions used across test suites.
  21. const (
  22. cniVersion = "v0.7.5"
  23. cniArch = "amd64"
  24. cniDirectory = "cni/bin" // The CNI tarball places binaries under directory under "cni/bin".
  25. cniConfDirectory = "cni/net.d"
  26. cniURL = "https://dl.k8s.io/network-plugins/cni-plugins-" + cniArch + "-" + cniVersion + ".tgz"
  27. )
  28. const cniConfig = `{
  29. "name": "mynet",
  30. "type": "bridge",
  31. "bridge": "mynet0",
  32. "isDefaultGateway": true,
  33. "forceAddress": false,
  34. "ipMasq": true,
  35. "hairpinMode": true,
  36. "ipam": {
  37. "type": "host-local",
  38. "subnet": "10.10.0.0/16"
  39. }
  40. }
  41. `
  42. // Install the cni plugin and add basic bridge configuration to the
  43. // configuration directory.
  44. func setupCNI(host, workspace string) error {
  45. klog.V(2).Infof("Install CNI on %q", host)
  46. cniPath := filepath.Join(workspace, cniDirectory)
  47. cmd := getSSHCommand(" ; ",
  48. fmt.Sprintf("mkdir -p %s", cniPath),
  49. fmt.Sprintf("curl -s -L %s | tar -xz -C %s", cniURL, cniPath),
  50. )
  51. if output, err := SSH(host, "sh", "-c", cmd); err != nil {
  52. return fmt.Errorf("failed to install cni plugin on %q: %v output: %q", host, err, output)
  53. }
  54. // The added CNI network config is not needed for kubenet. It is only
  55. // used when testing the CNI network plugin, but is added in both cases
  56. // for consistency and simplicity.
  57. klog.V(2).Infof("Adding CNI configuration on %q", host)
  58. cniConfigPath := filepath.Join(workspace, cniConfDirectory)
  59. cmd = getSSHCommand(" ; ",
  60. fmt.Sprintf("mkdir -p %s", cniConfigPath),
  61. fmt.Sprintf("echo %s > %s", quote(cniConfig), filepath.Join(cniConfigPath, "mynet.conf")),
  62. )
  63. if output, err := SSH(host, "sh", "-c", cmd); err != nil {
  64. return fmt.Errorf("failed to write cni configuration on %q: %v output: %q", host, err, output)
  65. }
  66. return nil
  67. }
  68. // configureFirewall configures iptable firewall rules.
  69. func configureFirewall(host string) error {
  70. klog.V(2).Infof("Configure iptables firewall rules on %q", host)
  71. // TODO: consider calling bootstrap script to configure host based on OS
  72. output, err := SSH(host, "iptables", "-L", "INPUT")
  73. if err != nil {
  74. return fmt.Errorf("failed to get iptables INPUT on %q: %v output: %q", host, err, output)
  75. }
  76. if strings.Contains(output, "Chain INPUT (policy DROP)") {
  77. cmd := getSSHCommand("&&",
  78. "(iptables -C INPUT -w -p TCP -j ACCEPT || iptables -A INPUT -w -p TCP -j ACCEPT)",
  79. "(iptables -C INPUT -w -p UDP -j ACCEPT || iptables -A INPUT -w -p UDP -j ACCEPT)",
  80. "(iptables -C INPUT -w -p ICMP -j ACCEPT || iptables -A INPUT -w -p ICMP -j ACCEPT)")
  81. output, err := SSH(host, "sh", "-c", cmd)
  82. if err != nil {
  83. return fmt.Errorf("failed to configured firewall on %q: %v output: %v", host, err, output)
  84. }
  85. }
  86. output, err = SSH(host, "iptables", "-L", "FORWARD")
  87. if err != nil {
  88. return fmt.Errorf("failed to get iptables FORWARD on %q: %v output: %q", host, err, output)
  89. }
  90. if strings.Contains(output, "Chain FORWARD (policy DROP)") {
  91. cmd := getSSHCommand("&&",
  92. "(iptables -C FORWARD -w -p TCP -j ACCEPT || iptables -A FORWARD -w -p TCP -j ACCEPT)",
  93. "(iptables -C FORWARD -w -p UDP -j ACCEPT || iptables -A FORWARD -w -p UDP -j ACCEPT)",
  94. "(iptables -C FORWARD -w -p ICMP -j ACCEPT || iptables -A FORWARD -w -p ICMP -j ACCEPT)")
  95. output, err = SSH(host, "sh", "-c", cmd)
  96. if err != nil {
  97. return fmt.Errorf("failed to configured firewall on %q: %v output: %v", host, err, output)
  98. }
  99. }
  100. return nil
  101. }
  102. // cleanupNodeProcesses kills all running node processes may conflict with the test.
  103. func cleanupNodeProcesses(host string) {
  104. klog.V(2).Infof("Killing any existing node processes on %q", host)
  105. cmd := getSSHCommand(" ; ",
  106. "pkill kubelet",
  107. "pkill kube-apiserver",
  108. "pkill etcd",
  109. "pkill e2e_node.test",
  110. )
  111. // No need to log an error if pkill fails since pkill will fail if the commands are not running.
  112. // If we are unable to stop existing running k8s processes, we should see messages in the kubelet/apiserver/etcd
  113. // logs about failing to bind the required ports.
  114. SSH(host, "sh", "-c", cmd)
  115. }
  116. // Quotes a shell literal so it can be nested within another shell scope.
  117. func quote(s string) string {
  118. return fmt.Sprintf("'\"'\"'%s'\"'\"'", s)
  119. }