From 6a596f69e06cd43fa0acfc1637eb5421dd4b8e28 Mon Sep 17 00:00:00 2001 From: Nick Sardo Date: Tue, 18 Apr 2017 16:24:55 -0700 Subject: [PATCH] Support migrating single cluster among others --- controllers/gce/cmd/mode-updater/README.md | 12 ++-- .../gce/cmd/mode-updater/mode-updater.go | 57 +++++++------------ 2 files changed, 27 insertions(+), 42 deletions(-) diff --git a/controllers/gce/cmd/mode-updater/README.md b/controllers/gce/cmd/mode-updater/README.md index ef8eb3581..8f5ee36f2 100644 --- a/controllers/gce/cmd/mode-updater/README.md +++ b/controllers/gce/cmd/mode-updater/README.md @@ -31,19 +31,21 @@ There are two GCP requirements that complicate changing the backend service bala #### How to run ```shell -go run main.go {project-id} {region} {target-balance-mode} +go run main.go {project-id} {cluster-id} {region} {target-balance-mode} #Examples +# Fetch cluster id +CLUSTERID=`kubectl get configmaps ingress-uid -o jsonpath='{.data.uid}' --namespace=kube-system` # for upgrading -go run main.go my-project us-central1 RATE +go run main.go my-project $CLUSTERID us-central1 RATE # for reversing -go run main.go my-project us-central1 UTILIZATION +go run main.go my-project $CLUSTERID us-central1 UTILIZATION ``` **Example Run** ```shell -➜ go run mode-updater.go nicksardo-project us-central1 RATE +➜ go run mode-updater.go nicksardo-project c4424dd5f02d3cad us-central1 RATE Backend-Service BalancingMode Updater 0.1 Backend Services: @@ -102,7 +104,7 @@ Step 8: Delete temporary instance groups #### TODO - [x] If only one backend-service exists, just update it in place. - [x] If all backend-services are already the target balancing mode, early return. -- [ ] Wait for op completion instead of sleeping +- [x] Wait for op completion instead of sleeping - [ ] Adjust warning #### Warning diff --git a/controllers/gce/cmd/mode-updater/mode-updater.go b/controllers/gce/cmd/mode-updater/mode-updater.go index f28a870e6..bec17ff93 100644 --- a/controllers/gce/cmd/mode-updater/mode-updater.go +++ b/controllers/gce/cmd/mode-updater/mode-updater.go @@ -1,7 +1,6 @@ package main import ( - "errors" "flag" "fmt" "log" @@ -20,6 +19,7 @@ import ( var ( projectID string + clusterID string regionName string targetBalancingMode string @@ -47,10 +47,10 @@ func main() { flag.Parse() args := flag.Args() - if len(args) != 3 { - log.Fatalf("Expected three arguments: project_id region balancing_mode") + if len(args) != 4 { + log.Fatalf("Expected four arguments: project_id cluster_id region balancing_mode") } - projectID, regionName, targetBalancingMode = args[0], args[1], args[2] + projectID, clusterID, regionName, targetBalancingMode = args[0], args[1], args[2], args[3] switch targetBalancingMode { case balancingModeRATE, balancingModeUTIL: @@ -82,6 +82,12 @@ func main() { } zones = zoneList.Items + if len(zones) == 0 { + panic(fmt.Errorf("Expected at least one zone in region: %v", regionName)) + } + + instanceGroupName = fmt.Sprintf("k8s-ig--%s", clusterID) + // Get instance groups for _, z := range zones { igl, err := s.InstanceGroups.List(projectID, z.Name).Do() @@ -89,14 +95,10 @@ func main() { panic(err) } for _, ig := range igl.Items { - if !strings.HasPrefix(ig.Name, "k8s-ig--") { + if instanceGroupName != ig.Name { continue } - if instanceGroupName == "" { - instanceGroupName = ig.Name - } - // Note instances r := &compute.InstanceGroupsListInstancesRequest{InstanceState: "ALL"} instList, err := s.InstanceGroups.ListInstances(projectID, getResourceName(ig.Zone, "zones"), ig.Name, r).Do() @@ -118,8 +120,12 @@ func main() { } } - if instanceGroupName == "" { - panic(errors.New("Could not determine k8s load balancer instance group")) + if len(igs) == 0 { + panic(fmt.Errorf("Expected at least one instance group named: %v", instanceGroupName)) + } + + if len(instances) == 0 { + panic(fmt.Errorf("Expected at least one instance within instance group: %v", instanceGroupName)) } bs := getBackendServices() @@ -135,7 +141,7 @@ func main() { // Early return for special cases switch len(bs) { case 0: - fmt.Println("There are 0 backend services - no action necessary") + fmt.Println("\nThere are 0 backend services - no action necessary") return case 1: updateSingleBackend(bs[0]) @@ -144,14 +150,7 @@ func main() { // Check there's work to be done if typeOfBackends(bs) == targetBalancingMode { - fmt.Println("Backends are already set to target mode") - return - } - - // Check no orphan instance groups will throw us off - clusters := getIGClusterIds() - if len(clusters) != 1 { - fmt.Println("Expecting only cluster of instance groups in GCE, found", clusters) + fmt.Println("\nBackends are already set to target mode") return } @@ -273,7 +272,7 @@ func getBackendServices() (bs []*compute.BackendService) { for _, bsli := range bsl.Items { // Ignore regional backend-services and only grab Kubernetes resources - if bsli.Region == "" && strings.HasPrefix(bsli.Name, "k8s-be-") { + if bsli.Region == "" && strings.HasPrefix(bsli.Name, "k8s-be-") && strings.HasSuffix(bsli.Name, clusterID) { bs = append(bs, bsli) } } @@ -363,22 +362,6 @@ func updateSingleBackend(bs *compute.BackendService) { fmt.Println("Updated single backend service to target balancing mode.") } -func getIGClusterIds() []string { - clusterIds := make(map[string]struct{}) - for _, ig := range igs { - s := strings.Split(ig.Name, "--") - if len(s) > 2 { - panic(fmt.Errorf("Expected two parts to instance group name, got %v", s)) - } - clusterIds[s[1]] = struct{}{} - } - var ids []string - for v, _ := range clusterIds { - ids = append(ids, v) - } - return ids -} - // Below operations are copied from the GCE CloudProvider and modified to be static func waitForOp(op *compute.Operation, getOperation func(operationName string) (*compute.Operation, error)) error {