|
| 1 | +/* |
| 2 | + Copyright 2017 Crunchy Data Solutions, Inc. |
| 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 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | + Unless required by applicable law or agreed to in writing, software |
| 10 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + See the License for the specific language governing permissions and |
| 13 | + limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +package cmd |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + log "github.com/Sirupsen/logrus" |
| 21 | + "github.com/crunchydata/postgres-operator/operator/util" |
| 22 | + "github.com/crunchydata/postgres-operator/tpr" |
| 23 | + "github.com/spf13/cobra" |
| 24 | +) |
| 25 | + |
| 26 | +var ReplicaCount int |
| 27 | + |
| 28 | +var scaleCmd = &cobra.Command{ |
| 29 | + Use: "scale", |
| 30 | + Short: "Scale a Cluster", |
| 31 | + Long: `scale allows you to adjust a Cluster's replica configuration |
| 32 | +For example: |
| 33 | +
|
| 34 | +pgo scale mycluster --replica-count=1 |
| 35 | +.`, |
| 36 | + Run: func(cmd *cobra.Command, args []string) { |
| 37 | + log.Debug("scale called") |
| 38 | + if ReplicaCount < 0 { |
| 39 | + fmt.Println(`--replica-count command line flag is required`) |
| 40 | + } else if len(args) == 0 { |
| 41 | + fmt.Println(`You must specify the clusters to scale.`) |
| 42 | + } else { |
| 43 | + scaleCluster(args) |
| 44 | + } |
| 45 | + }, |
| 46 | +} |
| 47 | + |
| 48 | +func init() { |
| 49 | + RootCmd.AddCommand(scaleCmd) |
| 50 | + |
| 51 | + scaleCmd.Flags().IntVarP(&ReplicaCount, "replica-count", "r", -1, "The replica count to apply to the clusters") |
| 52 | + |
| 53 | +} |
| 54 | + |
| 55 | +func scaleCluster(args []string) { |
| 56 | + //get a list of all clusters |
| 57 | + clusterList := tpr.PgClusterList{} |
| 58 | + err := Tprclient.Get(). |
| 59 | + Resource("pgclusters"). |
| 60 | + Namespace(Namespace). |
| 61 | + Do().Into(&clusterList) |
| 62 | + if err != nil { |
| 63 | + log.Error("error getting list of clusters" + err.Error()) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + if len(clusterList.Items) == 0 { |
| 68 | + fmt.Println("no clusters found") |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + itemFound := false |
| 73 | + |
| 74 | + for _, arg := range args { |
| 75 | + log.Debugf(" %s ReplicaCount is %d\n", arg, ReplicaCount) |
| 76 | + for _, cluster := range clusterList.Items { |
| 77 | + if arg == "all" || cluster.Spec.Name == arg { |
| 78 | + itemFound = true |
| 79 | + fmt.Printf("scaling %s to %d\n", arg, ReplicaCount) |
| 80 | + err = util.ScaleDeployment(Clientset, arg+"-replica", Namespace, ReplicaCount) |
| 81 | + if err != nil { |
| 82 | + log.Error(err.Error()) |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + if !itemFound { |
| 87 | + fmt.Println(arg + " was not found") |
| 88 | + } |
| 89 | + itemFound = false |
| 90 | + |
| 91 | + } |
| 92 | +} |
0 commit comments