Merge pull request #6421 from zetaab/scaledown

Ability to scale down instancegroup in openstack
This commit is contained in:
Kubernetes Prow Robot 2019-02-19 13:17:26 -08:00 committed by GitHub
commit 36ea1610dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 13 deletions

View File

@ -154,8 +154,11 @@ func (b *ServerGroupModelBuilder) Build(c *fi.ModelBuilderContext) error {
glog.V(2).Infof("Found instance group with name %s and role %v.", ig.Name, ig.Spec.Role)
sgTask := &openstacktasks.ServerGroup{
Name: s(fmt.Sprintf("%s-%s", clusterName, ig.Name)),
ClusterName: s(clusterName),
IGName: s(ig.Name),
Policies: []string{"anti-affinity"},
Lifecycle: b.Lifecycle,
MaxSize: ig.Spec.MaxSize,
}
c.AddTask(sgTask)

View File

@ -18,9 +18,11 @@ package openstacktasks
import (
"fmt"
"strings"
"github.com/golang/glog"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/servergroups"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/openstack"
)
@ -29,8 +31,11 @@ import (
type ServerGroup struct {
ID *string
Name *string
ClusterName *string
IGName *string
Members []string
Policies []string
MaxSize *int32
Lifecycle *fi.Lifecycle
}
@ -63,16 +68,25 @@ func (s *ServerGroup) Find(context *fi.Context) (*ServerGroup, error) {
}
actual = &ServerGroup{
Name: fi.String(serverGroup.Name),
ClusterName: s.ClusterName,
IGName: s.IGName,
ID: fi.String(serverGroup.ID),
Members: serverGroup.Members,
Lifecycle: s.Lifecycle,
Policies: serverGroup.Policies,
MaxSize: fi.Int32(int32(len(serverGroup.Members))),
}
}
}
if actual == nil {
return nil, nil
}
// ignore if IG is scaled up, this is handled in instancetasks
if fi.Int32Value(actual.MaxSize) < fi.Int32Value(s.MaxSize) {
s.MaxSize = actual.MaxSize
}
s.ID = actual.ID
s.Members = actual.Members
return actual, nil
@ -113,6 +127,31 @@ func (_ *ServerGroup) RenderOpenstack(t *openstack.OpenstackAPITarget, a, e, cha
}
e.ID = fi.String(g.ID)
return nil
} else if changes.MaxSize != nil && fi.Int32Value(a.MaxSize) > fi.Int32Value(changes.MaxSize) {
currentLastIndex := fi.Int32Value(a.MaxSize)
for currentLastIndex > fi.Int32Value(changes.MaxSize) {
iName := strings.ToLower(fmt.Sprintf("%s-%d.%s", fi.StringValue(a.IGName), currentLastIndex, fi.StringValue(a.ClusterName)))
instanceName := strings.Replace(iName, ".", "-", -1)
opts := servers.ListOpts{
Name: fmt.Sprintf("^%s$", instanceName),
}
instances, err := t.Cloud.ListInstances(opts)
if err != nil {
return fmt.Errorf("error fetching instance list: %v", err)
}
if len(instances) == 1 {
glog.V(2).Infof("Openstack task ServerGroup scaling down instance %s", instanceName)
err := t.Cloud.DeleteInstanceWithID(instances[0].ID)
if err != nil {
return fmt.Errorf("Could not delete instance %s: %v", instanceName, err)
}
} else {
return fmt.Errorf("found %d instances with name: %s", len(instances), instanceName)
}
currentLastIndex -= 1
}
}
glog.V(2).Infof("Openstack task ServerGroup::RenderOpenstack did nothing")