google_compute.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Copyright 2014 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 e2e
  14. import (
  15. "fmt"
  16. "os/exec"
  17. "regexp"
  18. "strings"
  19. "time"
  20. "github.com/golang/glog"
  21. "k8s.io/kubernetes/pkg/cloudprovider/providers/gce"
  22. "k8s.io/kubernetes/test/e2e/framework"
  23. )
  24. // TODO: These should really just use the GCE API client library or at least use
  25. // better formatted output from the --format flag.
  26. func createGCEStaticIP(name string) (string, error) {
  27. // gcloud compute --project "abshah-kubernetes-001" addresses create "test-static-ip" --region "us-central1"
  28. // abshah@abhidesk:~/go/src/code.google.com/p/google-api-go-client/compute/v1$ gcloud compute --project "abshah-kubernetes-001" addresses create "test-static-ip" --region "us-central1"
  29. // Created [https://www.googleapis.com/compute/v1/projects/abshah-kubernetes-001/regions/us-central1/addresses/test-static-ip].
  30. // NAME REGION ADDRESS STATUS
  31. // test-static-ip us-central1 104.197.143.7 RESERVED
  32. var outputBytes []byte
  33. var err error
  34. region, err := gce.GetGCERegion(framework.TestContext.CloudConfig.Zone)
  35. if err != nil {
  36. return "", fmt.Errorf("failed to convert zone to region: %v", err)
  37. }
  38. glog.Infof("Creating static IP with name %q in project %q in region %q", name, framework.TestContext.CloudConfig.ProjectID, region)
  39. for attempts := 0; attempts < 4; attempts++ {
  40. outputBytes, err = exec.Command("gcloud", "compute", "addresses", "create",
  41. name, "--project", framework.TestContext.CloudConfig.ProjectID,
  42. "--region", region, "-q").CombinedOutput()
  43. if err == nil {
  44. break
  45. }
  46. glog.Errorf("output from failed attempt to create static IP: %s", outputBytes)
  47. time.Sleep(time.Duration(5*attempts) * time.Second)
  48. }
  49. if err != nil {
  50. // Ditch the error, since the stderr in the output is what actually contains
  51. // any useful info.
  52. return "", fmt.Errorf("failed to create static IP: %s", outputBytes)
  53. }
  54. output := string(outputBytes)
  55. if strings.Contains(output, "RESERVED") {
  56. r, _ := regexp.Compile("[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+")
  57. staticIP := r.FindString(output)
  58. if staticIP == "" {
  59. return "", fmt.Errorf("static IP not found in gcloud command output: %v", output)
  60. } else {
  61. return staticIP, nil
  62. }
  63. } else {
  64. return "", fmt.Errorf("static IP %q could not be reserved: %v", name, output)
  65. }
  66. }
  67. func deleteGCEStaticIP(name string) error {
  68. // gcloud compute --project "abshah-kubernetes-001" addresses create "test-static-ip" --region "us-central1"
  69. // abshah@abhidesk:~/go/src/code.google.com/p/google-api-go-client/compute/v1$ gcloud compute --project "abshah-kubernetes-001" addresses create "test-static-ip" --region "us-central1"
  70. // Created [https://www.googleapis.com/compute/v1/projects/abshah-kubernetes-001/regions/us-central1/addresses/test-static-ip].
  71. // NAME REGION ADDRESS STATUS
  72. // test-static-ip us-central1 104.197.143.7 RESERVED
  73. region, err := gce.GetGCERegion(framework.TestContext.CloudConfig.Zone)
  74. if err != nil {
  75. return fmt.Errorf("failed to convert zone to region: %v", err)
  76. }
  77. glog.Infof("Deleting static IP with name %q in project %q in region %q", name, framework.TestContext.CloudConfig.ProjectID, region)
  78. outputBytes, err := exec.Command("gcloud", "compute", "addresses", "delete",
  79. name, "--project", framework.TestContext.CloudConfig.ProjectID,
  80. "--region", region, "-q").CombinedOutput()
  81. if err != nil {
  82. // Ditch the error, since the stderr in the output is what actually contains
  83. // any useful info.
  84. return fmt.Errorf("failed to delete static IP %q: %v", name, string(outputBytes))
  85. }
  86. return nil
  87. }