Skip to content

Commit

Permalink
WIP: Add scale instancegroup command
Browse files Browse the repository at this point in the history
  • Loading branch information
gianrubio committed Jun 16, 2017
1 parent 8fb99a8 commit 2ca448a
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 0 deletions.
43 changes: 43 additions & 0 deletions cmd/kops/scale.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"github.com/spf13/cobra"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
"k8s.io/kubernetes/pkg/util/i18n"
)

var (
//TODO add comments
scale_long = templates.LongDesc(i18n.T(`long description of scale
`))

scale_example = templates.Examples(i18n.T(`example of scale
`))
)

var scaleCmd = &cobra.Command{
Use: "scale",
Short: i18n.T(`Scale instancegroups and other resources`),
Long: scale_long,
Example: scale_example,
}

func init() {
rootCommand.AddCommand(scaleCmd)
}
122 changes: 122 additions & 0 deletions cmd/kops/scale_instancegroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"fmt"

"github.com/spf13/cobra"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
"k8s.io/kubernetes/pkg/util/i18n"
"k8s.io/kops/upup/pkg/fi/cloudup"
"k8s.io/kops/pkg/instancegroups"
)

type ScaleIgCmd struct {
Yes bool
Replicas int64
}

var (
//TODO add comments
scale_instancegroup_long = templates.LongDesc(i18n.T(`
long description...
`))

scale_instancegroup_example = templates.Examples(i18n.T(`
# Scale a ig fixing it to 2 replicas
kops scale ig --name cluster.kops.ddy.systems nodes --replicas=2
`))
)

var scaleIg ScaleIgCmd

func init() {

cmd := &cobra.Command{
Use: "ig",
Aliases: []string{"instancegroup", "instancegroups"},
Short: i18n.T("Scale instances instancegroups"),
Long: scale_instancegroup_long,
Example: scale_instancegroup_example,
Run: func(cmd *cobra.Command, args []string) {

if len(args) == 0 {
exitWithError(fmt.Errorf("Specify name of instance group to edit"))
}

if len(args) != 1 {
exitWithError(fmt.Errorf("Can only specify one instance group at a time"))
}

err := scaleIg.Run(args)
if err != nil {
exitWithError(err)
}
},
}

cmd.Flags().Int64Var(&scaleIg.Replicas, "replicas", 0, i18n.T("The new desired number of replicas. Required."))

scaleCmd.AddCommand(cmd)
}
func (c *ScaleIgCmd) Run(args []string) error {

groupName := args[0]

cluster, err := rootCommand.Cluster()
if err != nil {
return err
}

clientset, err := rootCommand.Clientset()
if err != nil {
return err
}

if groupName == "" {
return fmt.Errorf("name is required")
}

igGroup, err := clientset.InstanceGroups(cluster.ObjectMeta.Name).Get(groupName)
if err != nil {
return fmt.Errorf("error reading InstanceGroup %q: %v", groupName, err)
}
if igGroup == nil {
return fmt.Errorf("InstanceGroup %q not found", groupName)
}

_, err = clientset.InstanceGroups(cluster.ObjectMeta.Name).Update(igGroup)
if err != nil {
return err
}

cloud, err := cloudup.BuildCloud(cluster)
if err != nil {
return err
}

s := &instancegroups.ScaleInstanceGroup{Cluster: cluster, Cloud: cloud, DesiredReplicas: &c.Replicas}
err = s.ScaleInstanceGroup(igGroup)

if err != nil {
return err
}

return nil
}
57 changes: 57 additions & 0 deletions pkg/instancegroups/instancegroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,60 @@ func (g *CloudInstanceGroup) Delete(cloud fi.Cloud) error {

return nil
}

// ScaleInstanceGroup scale the cloud resources for an InstanceGroup
type ScaleInstanceGroup struct {
Cluster *api.Cluster
Cloud fi.Cloud
DesiredReplicas *int64

Clientset simple.Clientset
}

func (c *ScaleInstanceGroup) ScaleInstanceGroup(group *api.InstanceGroup) error {
groups, err := FindCloudInstanceGroups(c.Cloud, c.Cluster, []*api.InstanceGroup{group}, false, nil)
cig := groups[group.ObjectMeta.Name]

if cig == nil {
glog.Warningf("AutoScalingGroup %q not found in cloud - skipping scaling", group.ObjectMeta.Name)
} else {
if len(groups) != 1 {
return fmt.Errorf("Multiple InstanceGroup resources found in cloud")
}

glog.Infof("Scaling autoscaling group %q from %v to %#v", group.ObjectMeta.Name, cig.asg.MaxSize, c.DesiredReplicas)

cig.asg.MaxSize = c.DesiredReplicas
cig.asg.DesiredCapacity = c.DesiredReplicas

err = cig.Scale(c.Cloud)
if err != nil {
return fmt.Errorf("error scaling InstanceGroup: %v", err)
}
}

return nil
}

func (g *CloudInstanceGroup) Scale(cloud fi.Cloud) error {

c := cloud.(awsup.AWSCloud)
// TODO add warning about cluster autoscaling and replicas
{
asgName := aws.StringValue(g.asg.AutoScalingGroupName)
request := &autoscaling.UpdateAutoScalingGroupInput{
AutoScalingGroupName: g.asg.AutoScalingGroupName,
DesiredCapacity: g.asg.DesiredCapacity,
MaxSize: g.asg.MaxSize,
}
_, err := c.Autoscaling().UpdateAutoScalingGroup(request)

if err != nil {
return fmt.Errorf("error scaling autoscaling group %q: %v", asgName, err)
}

// TODO it's important to wait to this command apply or just print a message to the user is enough?
}

return nil
}

0 comments on commit 2ca448a

Please sign in to comment.