Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor cleanups to #3446 #3493

Merged
merged 4 commits into from
Oct 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/kops/rollingupdatecluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func RunRollingUpdateCluster(f *util.Factory, out io.Writer, options *RollingUpd
return r.InstanceGroup.ObjectMeta.Name
})
t.AddColumn("STATUS", func(r *cloudinstances.CloudInstanceGroup) string {
return r.Status
return r.Status()
})
t.AddColumn("NEEDUPDATE", func(r *cloudinstances.CloudInstanceGroup) string {
return strconv.Itoa(len(r.NeedUpdate))
Expand Down
23 changes: 11 additions & 12 deletions pkg/cloudinstances/cloud_instance_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ type CloudInstanceGroup struct {
InstanceGroup *api.InstanceGroup
GroupName string
GroupTemplateName string
Status string
Ready []*CloudInstanceMember
NeedUpdate []*CloudInstanceMember
Ready []*CloudInstanceGroupMember
NeedUpdate []*CloudInstanceGroupMember
MinSize int
MaxSize int
}

// CloudInstanceGroupInstance describes an instances in a CloudInstanceGroup group.
type CloudInstanceMember struct {
// CloudInstanceGroupMember describes an instance in a CloudInstanceGroup group.
type CloudInstanceGroupMember struct {
ID *string
Node *v1.Node
}
Expand Down Expand Up @@ -72,12 +71,12 @@ func NewCloudInstanceGroup(groupName string, groupTemplateName string, ig *api.I
return cg, nil
}

// NewCloudInstanceMember creates a new CloudInstanceGroupMember
func (c *CloudInstanceGroup) NewCloudInstanceMember(instanceId *string, newGroupName string, currentGroupName string, nodeMap map[string]*v1.Node) error {
// NewCloudInstanceGroupMember creates a new CloudInstanceGroupMember
func (c *CloudInstanceGroup) NewCloudInstanceGroupMember(instanceId *string, newGroupName string, currentGroupName string, nodeMap map[string]*v1.Node) error {
if instanceId == nil {
return fmt.Errorf("instance id for cloud instance member cannot be nil")
}
cm := &CloudInstanceMember{
cm := &CloudInstanceGroupMember{
ID: instanceId,
}
id := *instanceId
Expand All @@ -97,12 +96,12 @@ func (c *CloudInstanceGroup) NewCloudInstanceMember(instanceId *string, newGroup
return nil
}

// MarkIsReady sets the CloudInstanceGroup status for Ready or NeedsUpdate
func (c *CloudInstanceGroup) MarkIsReady() {
// Status returns a human-readable Status indicating whether an update is needed
func (c *CloudInstanceGroup) Status() string {
if len(c.NeedUpdate) == 0 {
c.Status = "Ready"
return "Ready"
} else {
c.Status = "NeedsUpdate"
return "NeedsUpdate"
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/instancegroups/instancegroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (r *RollingUpdateInstanceGroup) ValidateCluster(rollingUpdateData *RollingU
}

// DeleteInstance deletes an Cloud Instance.
func (r *RollingUpdateInstanceGroup) DeleteInstance(u *cloudinstances.CloudInstanceMember) error {
func (r *RollingUpdateInstanceGroup) DeleteInstance(u *cloudinstances.CloudInstanceGroupMember) error {

id := fi.StringValue(u.ID)

Expand All @@ -255,7 +255,7 @@ func (r *RollingUpdateInstanceGroup) DeleteInstance(u *cloudinstances.CloudInsta
}

// DrainNode drains a K8s node.
func (r *RollingUpdateInstanceGroup) DrainNode(u *cloudinstances.CloudInstanceMember, rollingUpdateData *RollingUpdateCluster) error {
func (r *RollingUpdateInstanceGroup) DrainNode(u *cloudinstances.CloudInstanceGroupMember, rollingUpdateData *RollingUpdateCluster) error {
if rollingUpdateData.ClientConfig == nil {
return fmt.Errorf("clientConfig not set")
}
Expand Down
40 changes: 20 additions & 20 deletions pkg/instancegroups/rollingupdate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestRollingUpdateAllNeedUpdate(t *testing.T) {
Role: kopsapi.InstanceGroupRoleNode,
},
},
Ready: []*cloudinstances.CloudInstanceMember{
Ready: []*cloudinstances.CloudInstanceGroupMember{
{
ID: aws.String("node-1a"),
Node: &v1.Node{},
Expand All @@ -120,7 +120,7 @@ func TestRollingUpdateAllNeedUpdate(t *testing.T) {
Node: &v1.Node{},
},
},
NeedUpdate: []*cloudinstances.CloudInstanceMember{
NeedUpdate: []*cloudinstances.CloudInstanceGroupMember{
{
ID: aws.String("node-1a"),
Node: &v1.Node{},
Expand All @@ -143,7 +143,7 @@ func TestRollingUpdateAllNeedUpdate(t *testing.T) {
Role: kopsapi.InstanceGroupRoleNode,
},
},
Ready: []*cloudinstances.CloudInstanceMember{
Ready: []*cloudinstances.CloudInstanceGroupMember{
{
ID: aws.String("node-2a"),
Node: &v1.Node{},
Expand All @@ -153,7 +153,7 @@ func TestRollingUpdateAllNeedUpdate(t *testing.T) {
Node: &v1.Node{},
},
},
NeedUpdate: []*cloudinstances.CloudInstanceMember{
NeedUpdate: []*cloudinstances.CloudInstanceGroupMember{
{
ID: aws.String("node-2a"),
Node: &v1.Node{},
Expand All @@ -176,13 +176,13 @@ func TestRollingUpdateAllNeedUpdate(t *testing.T) {
Role: kopsapi.InstanceGroupRoleMaster,
},
},
Ready: []*cloudinstances.CloudInstanceMember{
Ready: []*cloudinstances.CloudInstanceGroupMember{
{
ID: aws.String("master-1a"),
Node: &v1.Node{},
},
},
NeedUpdate: []*cloudinstances.CloudInstanceMember{
NeedUpdate: []*cloudinstances.CloudInstanceGroupMember{
{
ID: aws.String("master-1a"),
Node: &v1.Node{},
Expand All @@ -201,13 +201,13 @@ func TestRollingUpdateAllNeedUpdate(t *testing.T) {
Role: kopsapi.InstanceGroupRoleBastion,
},
},
Ready: []*cloudinstances.CloudInstanceMember{
Ready: []*cloudinstances.CloudInstanceGroupMember{
{
ID: aws.String("bastion-1a"),
Node: &v1.Node{},
},
},
NeedUpdate: []*cloudinstances.CloudInstanceMember{
NeedUpdate: []*cloudinstances.CloudInstanceGroupMember{
{
ID: aws.String("bastion-1a"),
Node: &v1.Node{},
Expand Down Expand Up @@ -260,7 +260,7 @@ func TestRollingUpdateNoneNeedUpdate(t *testing.T) {
Role: kopsapi.InstanceGroupRoleNode,
},
},
Ready: []*cloudinstances.CloudInstanceMember{
Ready: []*cloudinstances.CloudInstanceGroupMember{
{
ID: aws.String("node-1a"),
Node: &v1.Node{},
Expand All @@ -283,7 +283,7 @@ func TestRollingUpdateNoneNeedUpdate(t *testing.T) {
Role: kopsapi.InstanceGroupRoleNode,
},
},
Ready: []*cloudinstances.CloudInstanceMember{
Ready: []*cloudinstances.CloudInstanceGroupMember{
{
ID: aws.String("node-2a"),
Node: &v1.Node{},
Expand All @@ -306,7 +306,7 @@ func TestRollingUpdateNoneNeedUpdate(t *testing.T) {
Role: kopsapi.InstanceGroupRoleMaster,
},
},
Ready: []*cloudinstances.CloudInstanceMember{
Ready: []*cloudinstances.CloudInstanceGroupMember{
{
ID: aws.String("master-1a"),
Node: &v1.Node{},
Expand All @@ -325,7 +325,7 @@ func TestRollingUpdateNoneNeedUpdate(t *testing.T) {
Role: kopsapi.InstanceGroupRoleBastion,
},
},
Ready: []*cloudinstances.CloudInstanceMember{
Ready: []*cloudinstances.CloudInstanceGroupMember{
{
ID: aws.String("bastion-1a"),
Node: &v1.Node{},
Expand Down Expand Up @@ -406,7 +406,7 @@ func TestRollingUpdateNoneNeedUpdateWithForce(t *testing.T) {
Role: kopsapi.InstanceGroupRoleNode,
},
},
Ready: []*cloudinstances.CloudInstanceMember{
Ready: []*cloudinstances.CloudInstanceGroupMember{
{
ID: aws.String("node-1a"),
Node: &v1.Node{},
Expand All @@ -429,7 +429,7 @@ func TestRollingUpdateNoneNeedUpdateWithForce(t *testing.T) {
Role: kopsapi.InstanceGroupRoleNode,
},
},
Ready: []*cloudinstances.CloudInstanceMember{
Ready: []*cloudinstances.CloudInstanceGroupMember{
{
ID: aws.String("node-2a"),
Node: &v1.Node{},
Expand All @@ -452,7 +452,7 @@ func TestRollingUpdateNoneNeedUpdateWithForce(t *testing.T) {
Role: kopsapi.InstanceGroupRoleMaster,
},
},
Ready: []*cloudinstances.CloudInstanceMember{
Ready: []*cloudinstances.CloudInstanceGroupMember{
{
ID: aws.String("master-1a"),
Node: &v1.Node{},
Expand All @@ -471,7 +471,7 @@ func TestRollingUpdateNoneNeedUpdateWithForce(t *testing.T) {
Role: kopsapi.InstanceGroupRoleBastion,
},
},
Ready: []*cloudinstances.CloudInstanceMember{
Ready: []*cloudinstances.CloudInstanceGroupMember{
{
ID: aws.String("bastion-1a"),
Node: &v1.Node{},
Expand Down Expand Up @@ -585,7 +585,7 @@ func TestRollingUpdateUnknownRole(t *testing.T) {
Role: "Unknown",
},
},
Ready: []*cloudinstances.CloudInstanceMember{
Ready: []*cloudinstances.CloudInstanceGroupMember{
{
ID: aws.String("node-1a"),
Node: &v1.Node{},
Expand All @@ -608,7 +608,7 @@ func TestRollingUpdateUnknownRole(t *testing.T) {
Role: kopsapi.InstanceGroupRoleNode,
},
},
Ready: []*cloudinstances.CloudInstanceMember{
Ready: []*cloudinstances.CloudInstanceGroupMember{
{
ID: aws.String("node-2a"),
Node: &v1.Node{},
Expand All @@ -631,7 +631,7 @@ func TestRollingUpdateUnknownRole(t *testing.T) {
Role: kopsapi.InstanceGroupRoleMaster,
},
},
Ready: []*cloudinstances.CloudInstanceMember{
Ready: []*cloudinstances.CloudInstanceGroupMember{
{
ID: aws.String("master-1a"),
Node: &v1.Node{},
Expand All @@ -650,7 +650,7 @@ func TestRollingUpdateUnknownRole(t *testing.T) {
Role: kopsapi.InstanceGroupRoleBastion,
},
},
Ready: []*cloudinstances.CloudInstanceMember{
Ready: []*cloudinstances.CloudInstanceGroupMember{
{
ID: aws.String("bastion-1a"),
Node: &v1.Node{},
Expand Down
85 changes: 1 addition & 84 deletions pkg/resources/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,71 +232,6 @@ func addUntaggedRouteTables(cloud awsup.AWSCloud, clusterName string, resources
return nil
}

// FindAutoscalingGroups finds autoscaling groups matching the specified tags
// This isn't entirely trivial because autoscaling doesn't let us filter with as much precision as we would like
func FindAutoscalingGroups(cloud awsup.AWSCloud, tags map[string]string) ([]*autoscaling.Group, error) {
var asgs []*autoscaling.Group

glog.V(2).Infof("Listing all Autoscaling groups matching cluster tags")
var asgNames []*string
{
var asFilters []*autoscaling.Filter
for _, v := range tags {
// Not an exact match, but likely the best we can do
asFilters = append(asFilters, &autoscaling.Filter{
Name: aws.String("value"),
Values: []*string{aws.String(v)},
})
}
request := &autoscaling.DescribeTagsInput{
Filters: asFilters,
}

err := cloud.Autoscaling().DescribeTagsPages(request, func(p *autoscaling.DescribeTagsOutput, lastPage bool) bool {
for _, t := range p.Tags {
switch *t.ResourceType {
case "auto-scaling-group":
asgNames = append(asgNames, t.ResourceId)
default:
glog.Warningf("Unknown resource type: %v", *t.ResourceType)

}
}
return true
})
if err != nil {
return nil, fmt.Errorf("error listing autoscaling cluster tags: %v", err)
}
}

if len(asgNames) != 0 {
request := &autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: asgNames,
}
err := cloud.Autoscaling().DescribeAutoScalingGroupsPages(request, func(p *autoscaling.DescribeAutoScalingGroupsOutput, lastPage bool) bool {
for _, asg := range p.AutoScalingGroups {
if !MatchesAsgTags(tags, asg.Tags) {
// We used an inexact filter above
continue
}
// Check for "Delete in progress" (the only use of .Status)
if asg.Status != nil {
glog.Warningf("Skipping ASG %v (which matches tags): %v", *asg.AutoScalingGroupARN, *asg.Status)
continue
}
asgs = append(asgs, asg)
}
return true
})
if err != nil {
return nil, fmt.Errorf("error listing autoscaling groups: %v", err)
}

}

return asgs, nil
}

// FindAutoscalingLaunchConfiguration finds an AWS launch configuration given its name
func FindAutoscalingLaunchConfiguration(cloud awsup.AWSCloud, name string) (*autoscaling.LaunchConfiguration, error) {
glog.V(2).Infof("Retrieving Autoscaling LaunchConfigurations %q", name)
Expand Down Expand Up @@ -325,24 +260,6 @@ func FindAutoscalingLaunchConfiguration(cloud awsup.AWSCloud, name string) (*aut
return results[0], nil
}

func MatchesAsgTags(tags map[string]string, actual []*autoscaling.TagDescription) bool {
for k, v := range tags {
found := false
for _, a := range actual {
if aws.StringValue(a.Key) == k {
if aws.StringValue(a.Value) == v {
found = true
break
}
}
}
if !found {
return false
}
}
return true
}

func matchesElbTags(tags map[string]string, actual []*elb.Tag) bool {
for k, v := range tags {
found := false
Expand Down Expand Up @@ -1365,7 +1282,7 @@ func ListAutoScalingGroups(cloud fi.Cloud, clusterName string) ([]*tracker.Resou

tags := c.Tags()

asgs, err := FindAutoscalingGroups(c, tags)
asgs, err := awsup.FindAutoscalingGroups(c, tags)
if err != nil {
return nil, err
}
Expand Down
Loading