gce.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. Copyright 2018 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 gce
  14. import (
  15. "context"
  16. "fmt"
  17. "net/http"
  18. "os/exec"
  19. "regexp"
  20. "strings"
  21. "time"
  22. compute "google.golang.org/api/compute/v1"
  23. "google.golang.org/api/googleapi"
  24. v1 "k8s.io/api/core/v1"
  25. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  26. "k8s.io/apimachinery/pkg/util/uuid"
  27. "k8s.io/apimachinery/pkg/util/wait"
  28. clientset "k8s.io/client-go/kubernetes"
  29. "k8s.io/kubernetes/test/e2e/framework"
  30. e2epv "k8s.io/kubernetes/test/e2e/framework/pv"
  31. e2eservice "k8s.io/kubernetes/test/e2e/framework/service"
  32. gcecloud "k8s.io/legacy-cloud-providers/gce"
  33. )
  34. func init() {
  35. framework.RegisterProvider("gce", factory)
  36. framework.RegisterProvider("gke", factory)
  37. }
  38. func factory() (framework.ProviderInterface, error) {
  39. framework.Logf("Fetching cloud provider for %q\r", framework.TestContext.Provider)
  40. zone := framework.TestContext.CloudConfig.Zone
  41. region := framework.TestContext.CloudConfig.Region
  42. var err error
  43. if region == "" {
  44. region, err = gcecloud.GetGCERegion(zone)
  45. if err != nil {
  46. return nil, fmt.Errorf("error parsing GCE/GKE region from zone %q: %v", zone, err)
  47. }
  48. }
  49. managedZones := []string{} // Manage all zones in the region
  50. if !framework.TestContext.CloudConfig.MultiZone {
  51. managedZones = []string{zone}
  52. }
  53. gceCloud, err := gcecloud.CreateGCECloud(&gcecloud.CloudConfig{
  54. APIEndpoint: framework.TestContext.CloudConfig.APIEndpoint,
  55. ProjectID: framework.TestContext.CloudConfig.ProjectID,
  56. Region: region,
  57. Zone: zone,
  58. ManagedZones: managedZones,
  59. NetworkName: "", // TODO: Change this to use framework.TestContext.CloudConfig.Network?
  60. SubnetworkName: "",
  61. NodeTags: nil,
  62. NodeInstancePrefix: "",
  63. TokenSource: nil,
  64. UseMetadataServer: false,
  65. AlphaFeatureGate: gcecloud.NewAlphaFeatureGate([]string{}),
  66. })
  67. if err != nil {
  68. return nil, fmt.Errorf("Error building GCE/GKE provider: %v", err)
  69. }
  70. // Arbitrarily pick one of the zones we have nodes in
  71. if framework.TestContext.CloudConfig.Zone == "" && framework.TestContext.CloudConfig.MultiZone {
  72. zones, err := gceCloud.GetAllZonesFromCloudProvider()
  73. if err != nil {
  74. return nil, err
  75. }
  76. framework.TestContext.CloudConfig.Zone, _ = zones.PopAny()
  77. }
  78. return NewProvider(gceCloud), nil
  79. }
  80. // NewProvider returns a cloud provider interface for GCE
  81. func NewProvider(gceCloud *gcecloud.Cloud) framework.ProviderInterface {
  82. return &Provider{
  83. gceCloud: gceCloud,
  84. }
  85. }
  86. // Provider is a structure to handle GCE clouds for e2e testing
  87. type Provider struct {
  88. framework.NullProvider
  89. gceCloud *gcecloud.Cloud
  90. }
  91. // ResizeGroup resizes an instance group
  92. func (p *Provider) ResizeGroup(group string, size int32) error {
  93. // TODO: make this hit the compute API directly instead of shelling out to gcloud.
  94. // TODO: make gce/gke implement InstanceGroups, so we can eliminate the per-provider logic
  95. zone, err := getGCEZoneForGroup(group)
  96. if err != nil {
  97. return err
  98. }
  99. output, err := exec.Command("gcloud", "compute", "instance-groups", "managed", "resize",
  100. group, fmt.Sprintf("--size=%v", size),
  101. "--project="+framework.TestContext.CloudConfig.ProjectID, "--zone="+zone).CombinedOutput()
  102. if err != nil {
  103. return fmt.Errorf("Failed to resize node instance group %s: %s", group, output)
  104. }
  105. return nil
  106. }
  107. // GetGroupNodes returns a node name for the specified node group
  108. func (p *Provider) GetGroupNodes(group string) ([]string, error) {
  109. // TODO: make this hit the compute API directly instead of shelling out to gcloud.
  110. // TODO: make gce/gke implement InstanceGroups, so we can eliminate the per-provider logic
  111. zone, err := getGCEZoneForGroup(group)
  112. if err != nil {
  113. return nil, err
  114. }
  115. output, err := exec.Command("gcloud", "compute", "instance-groups", "managed",
  116. "list-instances", group, "--project="+framework.TestContext.CloudConfig.ProjectID,
  117. "--zone="+zone).CombinedOutput()
  118. if err != nil {
  119. return nil, fmt.Errorf("Failed to get nodes in instance group %s: %s", group, output)
  120. }
  121. re := regexp.MustCompile(".*RUNNING")
  122. lines := re.FindAllString(string(output), -1)
  123. for i, line := range lines {
  124. lines[i] = line[:strings.Index(line, " ")]
  125. }
  126. return lines, nil
  127. }
  128. // GroupSize returns the size of an instance group
  129. func (p *Provider) GroupSize(group string) (int, error) {
  130. // TODO: make this hit the compute API directly instead of shelling out to gcloud.
  131. // TODO: make gce/gke implement InstanceGroups, so we can eliminate the per-provider logic
  132. zone, err := getGCEZoneForGroup(group)
  133. if err != nil {
  134. return -1, err
  135. }
  136. output, err := exec.Command("gcloud", "compute", "instance-groups", "managed",
  137. "list-instances", group, "--project="+framework.TestContext.CloudConfig.ProjectID,
  138. "--zone="+zone).CombinedOutput()
  139. if err != nil {
  140. return -1, fmt.Errorf("Failed to get group size for group %s: %s", group, output)
  141. }
  142. re := regexp.MustCompile("RUNNING")
  143. return len(re.FindAllString(string(output), -1)), nil
  144. }
  145. // EnsureLoadBalancerResourcesDeleted ensures that cloud load balancer resources that were created
  146. func (p *Provider) EnsureLoadBalancerResourcesDeleted(ip, portRange string) error {
  147. project := framework.TestContext.CloudConfig.ProjectID
  148. region, err := gcecloud.GetGCERegion(framework.TestContext.CloudConfig.Zone)
  149. if err != nil {
  150. return fmt.Errorf("could not get region for zone %q: %v", framework.TestContext.CloudConfig.Zone, err)
  151. }
  152. return wait.Poll(10*time.Second, 5*time.Minute, func() (bool, error) {
  153. computeservice := p.gceCloud.ComputeServices().GA
  154. list, err := computeservice.ForwardingRules.List(project, region).Do()
  155. if err != nil {
  156. return false, err
  157. }
  158. for _, item := range list.Items {
  159. if item.PortRange == portRange && item.IPAddress == ip {
  160. framework.Logf("found a load balancer: %v", item)
  161. return false, nil
  162. }
  163. }
  164. return true, nil
  165. })
  166. }
  167. func getGCEZoneForGroup(group string) (string, error) {
  168. output, err := exec.Command("gcloud", "compute", "instance-groups", "managed", "list",
  169. "--project="+framework.TestContext.CloudConfig.ProjectID, "--format=value(zone)", "--filter=name="+group).CombinedOutput()
  170. if err != nil {
  171. return "", fmt.Errorf("Failed to get zone for node group %s: %s", group, output)
  172. }
  173. return strings.TrimSpace(string(output)), nil
  174. }
  175. // DeleteNode deletes a node which is specified as the argument
  176. func (p *Provider) DeleteNode(node *v1.Node) error {
  177. zone := framework.TestContext.CloudConfig.Zone
  178. project := framework.TestContext.CloudConfig.ProjectID
  179. return p.gceCloud.DeleteInstance(project, zone, node.Name)
  180. }
  181. // CreatePD creates a persistent volume
  182. func (p *Provider) CreatePD(zone string) (string, error) {
  183. pdName := fmt.Sprintf("%s-%s", framework.TestContext.Prefix, string(uuid.NewUUID()))
  184. if zone == "" && framework.TestContext.CloudConfig.MultiZone {
  185. zones, err := p.gceCloud.GetAllZonesFromCloudProvider()
  186. if err != nil {
  187. return "", err
  188. }
  189. zone, _ = zones.PopAny()
  190. }
  191. tags := map[string]string{}
  192. if _, err := p.gceCloud.CreateDisk(pdName, gcecloud.DiskTypeStandard, zone, 2 /* sizeGb */, tags); err != nil {
  193. return "", err
  194. }
  195. return pdName, nil
  196. }
  197. // DeletePD deletes a persistent volume
  198. func (p *Provider) DeletePD(pdName string) error {
  199. err := p.gceCloud.DeleteDisk(pdName)
  200. if err != nil {
  201. if gerr, ok := err.(*googleapi.Error); ok && len(gerr.Errors) > 0 && gerr.Errors[0].Reason == "notFound" {
  202. // PD already exists, ignore error.
  203. return nil
  204. }
  205. framework.Logf("error deleting PD %q: %v", pdName, err)
  206. }
  207. return err
  208. }
  209. // CreatePVSource creates a persistent volume source
  210. func (p *Provider) CreatePVSource(zone, diskName string) (*v1.PersistentVolumeSource, error) {
  211. return &v1.PersistentVolumeSource{
  212. GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{
  213. PDName: diskName,
  214. FSType: "ext3",
  215. ReadOnly: false,
  216. },
  217. }, nil
  218. }
  219. // DeletePVSource deletes a persistent volume source
  220. func (p *Provider) DeletePVSource(pvSource *v1.PersistentVolumeSource) error {
  221. return e2epv.DeletePDWithRetry(pvSource.GCEPersistentDisk.PDName)
  222. }
  223. // CleanupServiceResources cleans up GCE Service Type=LoadBalancer resources with
  224. // the given name. The name is usually the UUID of the Service prefixed with an
  225. // alpha-numeric character ('a') to work around cloudprovider rules.
  226. func (p *Provider) CleanupServiceResources(c clientset.Interface, loadBalancerName, region, zone string) {
  227. if pollErr := wait.Poll(5*time.Second, e2eservice.LoadBalancerCleanupTimeout, func() (bool, error) {
  228. if err := p.cleanupGCEResources(c, loadBalancerName, region, zone); err != nil {
  229. framework.Logf("Still waiting for glbc to cleanup: %v", err)
  230. return false, nil
  231. }
  232. return true, nil
  233. }); pollErr != nil {
  234. framework.Failf("Failed to cleanup service GCE resources.")
  235. }
  236. }
  237. func (p *Provider) cleanupGCEResources(c clientset.Interface, loadBalancerName, region, zone string) (retErr error) {
  238. if region == "" {
  239. // Attempt to parse region from zone if no region is given.
  240. var err error
  241. region, err = gcecloud.GetGCERegion(zone)
  242. if err != nil {
  243. return fmt.Errorf("error parsing GCE/GKE region from zone %q: %v", zone, err)
  244. }
  245. }
  246. if err := p.gceCloud.DeleteFirewall(gcecloud.MakeFirewallName(loadBalancerName)); err != nil &&
  247. !IsGoogleAPIHTTPErrorCode(err, http.StatusNotFound) {
  248. retErr = err
  249. }
  250. if err := p.gceCloud.DeleteRegionForwardingRule(loadBalancerName, region); err != nil &&
  251. !IsGoogleAPIHTTPErrorCode(err, http.StatusNotFound) {
  252. retErr = fmt.Errorf("%v\n%v", retErr, err)
  253. }
  254. if err := p.gceCloud.DeleteRegionAddress(loadBalancerName, region); err != nil &&
  255. !IsGoogleAPIHTTPErrorCode(err, http.StatusNotFound) {
  256. retErr = fmt.Errorf("%v\n%v", retErr, err)
  257. }
  258. clusterID, err := GetClusterID(c)
  259. if err != nil {
  260. retErr = fmt.Errorf("%v\n%v", retErr, err)
  261. return
  262. }
  263. hcNames := []string{gcecloud.MakeNodesHealthCheckName(clusterID)}
  264. hc, getErr := p.gceCloud.GetHTTPHealthCheck(loadBalancerName)
  265. if getErr != nil && !IsGoogleAPIHTTPErrorCode(getErr, http.StatusNotFound) {
  266. retErr = fmt.Errorf("%v\n%v", retErr, getErr)
  267. return
  268. }
  269. if hc != nil {
  270. hcNames = append(hcNames, hc.Name)
  271. }
  272. if err := p.gceCloud.DeleteExternalTargetPoolAndChecks(&v1.Service{}, loadBalancerName, region, clusterID, hcNames...); err != nil &&
  273. !IsGoogleAPIHTTPErrorCode(err, http.StatusNotFound) {
  274. retErr = fmt.Errorf("%v\n%v", retErr, err)
  275. }
  276. return
  277. }
  278. // L4LoadBalancerSrcRanges contains the ranges of ips used by the GCE L4 load
  279. // balancers for proxying client requests and performing health checks.
  280. func (p *Provider) L4LoadBalancerSrcRanges() []string {
  281. return gcecloud.L4LoadBalancerSrcRanges()
  282. }
  283. // EnableAndDisableInternalLB returns functions for both enabling and disabling internal Load Balancer
  284. func (p *Provider) EnableAndDisableInternalLB() (enable, disable func(svc *v1.Service)) {
  285. enable = func(svc *v1.Service) {
  286. svc.ObjectMeta.Annotations = map[string]string{gcecloud.ServiceAnnotationLoadBalancerType: string(gcecloud.LBTypeInternal)}
  287. }
  288. disable = func(svc *v1.Service) {
  289. delete(svc.ObjectMeta.Annotations, gcecloud.ServiceAnnotationLoadBalancerType)
  290. }
  291. return
  292. }
  293. // GetInstanceTags gets tags from GCE instance with given name.
  294. func GetInstanceTags(cloudConfig framework.CloudConfig, instanceName string) *compute.Tags {
  295. gceCloud := cloudConfig.Provider.(*Provider).gceCloud
  296. res, err := gceCloud.ComputeServices().GA.Instances.Get(cloudConfig.ProjectID, cloudConfig.Zone,
  297. instanceName).Do()
  298. if err != nil {
  299. framework.Failf("Failed to get instance tags for %v: %v", instanceName, err)
  300. }
  301. return res.Tags
  302. }
  303. // SetInstanceTags sets tags on GCE instance with given name.
  304. func SetInstanceTags(cloudConfig framework.CloudConfig, instanceName, zone string, tags []string) []string {
  305. gceCloud := cloudConfig.Provider.(*Provider).gceCloud
  306. // Re-get instance everytime because we need the latest fingerprint for updating metadata
  307. resTags := GetInstanceTags(cloudConfig, instanceName)
  308. _, err := gceCloud.ComputeServices().GA.Instances.SetTags(
  309. cloudConfig.ProjectID, zone, instanceName,
  310. &compute.Tags{Fingerprint: resTags.Fingerprint, Items: tags}).Do()
  311. if err != nil {
  312. framework.Failf("failed to set instance tags: %v", err)
  313. }
  314. framework.Logf("Sent request to set tags %v on instance: %v", tags, instanceName)
  315. return resTags.Items
  316. }
  317. // IsGoogleAPIHTTPErrorCode returns true if the error is a google api
  318. // error matching the corresponding HTTP error code.
  319. func IsGoogleAPIHTTPErrorCode(err error, code int) bool {
  320. apiErr, ok := err.(*googleapi.Error)
  321. return ok && apiErr.Code == code
  322. }
  323. // GetGCECloud returns GCE cloud provider
  324. func GetGCECloud() (*gcecloud.Cloud, error) {
  325. p, ok := framework.TestContext.CloudConfig.Provider.(*Provider)
  326. if !ok {
  327. return nil, fmt.Errorf("failed to convert CloudConfig.Provider to GCE provider: %#v", framework.TestContext.CloudConfig.Provider)
  328. }
  329. return p.gceCloud, nil
  330. }
  331. // GetClusterID returns cluster ID
  332. func GetClusterID(c clientset.Interface) (string, error) {
  333. cm, err := c.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(context.TODO(), gcecloud.UIDConfigMapName, metav1.GetOptions{})
  334. if err != nil || cm == nil {
  335. return "", fmt.Errorf("error getting cluster ID: %v", err)
  336. }
  337. clusterID, clusterIDExists := cm.Data[gcecloud.UIDCluster]
  338. providerID, providerIDExists := cm.Data[gcecloud.UIDProvider]
  339. if !clusterIDExists {
  340. return "", fmt.Errorf("cluster ID not set")
  341. }
  342. if providerIDExists {
  343. return providerID, nil
  344. }
  345. return clusterID, nil
  346. }