Skip to content

Commit

Permalink
Merge pull request #10226 from hakman/automated-cherry-pick-of-#10151…
Browse files Browse the repository at this point in the history
…-upstream-release-1.19

Automated cherry pick of #10151: Use LT versions instead of timestamped LTs
  • Loading branch information
k8s-ci-robot authored Nov 16, 2020
2 parents a0fa6f6 + ce7ea29 commit 6cf691b
Show file tree
Hide file tree
Showing 38 changed files with 402 additions and 356 deletions.
20 changes: 15 additions & 5 deletions cloudmock/aws/mockautoscaling/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (m *MockAutoscaling) AttachInstances(input *autoscaling.AttachInstancesInpu
m.mutex.Lock()
defer m.mutex.Unlock()

klog.V(2).Infof("AttachInstances %v", input)
klog.V(2).Infof("Mock AttachInstances %v", input)

g := m.Groups[aws.StringValue(input.AutoScalingGroupName)]
if g == nil {
Expand All @@ -48,7 +48,8 @@ func (m *MockAutoscaling) CreateAutoScalingGroup(input *autoscaling.CreateAutoSc
m.mutex.Lock()
defer m.mutex.Unlock()

klog.V(2).Infof("CreateAutoScalingGroup %v", input)
klog.V(2).Infof("Mock CreateAutoScalingGroup %v", input)

createdTime := time.Now().UTC()

g := &autoscaling.Group{
Expand All @@ -75,6 +76,13 @@ func (m *MockAutoscaling) CreateAutoScalingGroup(input *autoscaling.CreateAutoSc
VPCZoneIdentifier: input.VPCZoneIdentifier,
}

if input.LaunchTemplate != nil {
g.LaunchTemplate.LaunchTemplateName = input.AutoScalingGroupName
if g.LaunchTemplate.LaunchTemplateId == nil {
return nil, fmt.Errorf("AutoScalingGroup has LaunchTemplate without ID")
}
}

for _, tag := range input.Tags {
g.Tags = append(g.Tags, &autoscaling.TagDescription{
Key: tag.Key,
Expand All @@ -97,7 +105,7 @@ func (m *MockAutoscaling) EnableMetricsCollection(request *autoscaling.EnableMet
m.mutex.Lock()
defer m.mutex.Unlock()

klog.Infof("EnableMetricsCollection: %v", request)
klog.V(2).Infof("Mock EnableMetricsCollection: %v", request)

g := m.Groups[*request.AutoScalingGroupName]
if g == nil {
Expand Down Expand Up @@ -129,7 +137,7 @@ func (m *MockAutoscaling) SuspendProcesses(input *autoscaling.ScalingProcessQuer
m.mutex.Lock()
defer m.mutex.Unlock()

klog.Infof("EnableMetricsCollection: %v", input)
klog.V(2).Infof("Mock SuspendProcesses: %v", input)

g := m.Groups[*input.AutoScalingGroupName]
if g == nil {
Expand Down Expand Up @@ -157,6 +165,8 @@ func (m *MockAutoscaling) DescribeAutoScalingGroups(input *autoscaling.DescribeA
m.mutex.Lock()
defer m.mutex.Unlock()

klog.V(2).Infof("Mock DescribeAutoScalingGroups: %v", input)

groups := []*autoscaling.Group{}
for _, group := range m.Groups {
match := false
Expand Down Expand Up @@ -236,7 +246,7 @@ func (m *MockAutoscaling) DeleteAutoScalingGroup(request *autoscaling.DeleteAuto
m.mutex.Lock()
defer m.mutex.Unlock()

klog.Infof("DeleteAutoScalingGroup: %v", request)
klog.V(2).Infof("Mock DeleteAutoScalingGroup: %v", request)

id := aws.StringValue(request.AutoScalingGroupName)
o := m.Groups[id]
Expand Down
60 changes: 53 additions & 7 deletions cloudmock/aws/mockec2/launch_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,71 @@ package mockec2

import (
"fmt"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"k8s.io/klog/v2"
)

type launchTemplateInfo struct {
data *ec2.ResponseLaunchTemplateData
name *string
}

// DescribeLaunchTemplatesPages mocks the describing the launch templates
func (m *MockEC2) DescribeLaunchTemplatesPages(request *ec2.DescribeLaunchTemplatesInput, callback func(*ec2.DescribeLaunchTemplatesOutput, bool) bool) error {
page, err := m.DescribeLaunchTemplates(request)
if err != nil {
return err
}

callback(page, false)

return nil
}

// DescribeLaunchTemplates mocks the describing the launch templates
func (m *MockEC2) DescribeLaunchTemplates(request *ec2.DescribeLaunchTemplatesInput) (*ec2.DescribeLaunchTemplatesOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

klog.V(2).Infof("Mock DescribeLaunchTemplates: %v", request)

o := &ec2.DescribeLaunchTemplatesOutput{}

if m.LaunchTemplates == nil {
return o, nil
}

for _, ltInfo := range m.LaunchTemplates {
o.LaunchTemplates = append(o.LaunchTemplates, &ec2.LaunchTemplate{
LaunchTemplateName: ltInfo.name,
})
for id, ltInfo := range m.LaunchTemplates {
launchTemplatetName := aws.StringValue(ltInfo.name)

allFiltersMatch := true
for _, filter := range request.Filters {
filterName := aws.StringValue(filter.Name)
filterValue := aws.StringValue(filter.Values[0])

filterMatches := false
if filterName == "tag:Name" && filterValue == launchTemplatetName {
filterMatches = true
}
if strings.HasPrefix(filterName, "tag:kubernetes.io/cluster/") {
filterMatches = true
}

if !filterMatches {
allFiltersMatch = false
break
}
}

if allFiltersMatch {
o.LaunchTemplates = append(o.LaunchTemplates, &ec2.LaunchTemplate{
LaunchTemplateName: aws.String(launchTemplatetName),
LaunchTemplateId: aws.String(id),
})
}
}

return o, nil
Expand All @@ -53,6 +93,8 @@ func (m *MockEC2) DescribeLaunchTemplateVersions(request *ec2.DescribeLaunchTemp
m.mutex.Lock()
defer m.mutex.Unlock()

klog.V(2).Infof("Mock DescribeLaunchTemplateVersions: %v", request)

o := &ec2.DescribeLaunchTemplateVersionsOutput{}

if m.LaunchTemplates == nil {
Expand All @@ -78,6 +120,8 @@ func (m *MockEC2) CreateLaunchTemplate(request *ec2.CreateLaunchTemplateInput) (
m.mutex.Lock()
defer m.mutex.Unlock()

klog.V(2).Infof("Mock CreateLaunchTemplate: %v", request)

m.launchTemplateNumber++
n := m.launchTemplateNumber
id := fmt.Sprintf("lt-%d", n)
Expand All @@ -86,7 +130,7 @@ func (m *MockEC2) CreateLaunchTemplate(request *ec2.CreateLaunchTemplateInput) (
m.LaunchTemplates = make(map[string]*launchTemplateInfo)
}
if m.LaunchTemplates[id] != nil {
return nil, fmt.Errorf("duplicate LaunchTemplateName %s", id)
return nil, fmt.Errorf("duplicate LaunchTemplateId %s", id)
}
resp := &ec2.ResponseLaunchTemplateData{
DisableApiTermination: request.LaunchTemplateData.DisableApiTermination,
Expand Down Expand Up @@ -190,13 +234,15 @@ func (m *MockEC2) DeleteLaunchTemplate(request *ec2.DeleteLaunchTemplateInput) (
m.mutex.Lock()
defer m.mutex.Unlock()

klog.V(2).Infof("Mock DeleteLaunchTemplate: %v", request)

o := &ec2.DeleteLaunchTemplateOutput{}

if m.LaunchTemplates == nil {
return o, nil
}
for id, lt := range m.LaunchTemplates {
if aws.StringValue(lt.name) == aws.StringValue(request.LaunchTemplateName) {
for id := range m.LaunchTemplates {
if id == aws.StringValue(request.LaunchTemplateId) {
delete(m.LaunchTemplates, id)
}
}
Expand Down
64 changes: 26 additions & 38 deletions pkg/resources/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func ListResourcesAWS(cloud awsup.AWSCloud, clusterName string) (map[string]*res
if err != nil {
return nil, err
}
lts, err := FindAutoScalingLaunchTemplateConfigurations(cloud, securityGroups)
lts, err := FindAutoScalingLaunchTemplates(cloud, clusterName)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1179,47 +1179,35 @@ func ListAutoScalingGroups(cloud fi.Cloud, clusterName string) ([]*resources.Res
return resourceTrackers, nil
}

// FindAutoScalingLaunchTemplateConfigurations finds any launch configurations which reference the security groups
func FindAutoScalingLaunchTemplateConfigurations(cloud fi.Cloud, securityGroups sets.String) ([]*resources.Resource, error) {
var list []*resources.Resource
// FindAutoScalingLaunchTemplates finds any launch templates owned by the cluster (by tag).
func FindAutoScalingLaunchTemplates(cloud fi.Cloud, clusterName string) ([]*resources.Resource, error) {
c := cloud.(awsup.AWSCloud)

c, ok := cloud.(awsup.AWSCloud)
if !ok {
return nil, errors.New("expected a aws cloud provider")
}
klog.V(2).Infof("Finding all Autoscaling LaunchTemplates associated to security groups")
klog.V(2).Infof("Finding all AutoScaling LaunchTemplates owned by the cluster")

resp, err := c.EC2().DescribeLaunchTemplates(&ec2.DescribeLaunchTemplatesInput{MaxResults: fi.Int64(100)})
if err != nil {
return list, nil
input := &ec2.DescribeLaunchTemplatesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("tag:kubernetes.io/cluster/" + clusterName),
Values: []*string{aws.String("owned")},
},
},
}

for _, x := range resp.LaunchTemplates {
// @step: grab the actual launch template
req, err := c.EC2().DescribeLaunchTemplateVersions(&ec2.DescribeLaunchTemplateVersionsInput{
LaunchTemplateName: x.LaunchTemplateName,
})
if err != nil {
return list, err
}
for _, j := range req.LaunchTemplateVersions {
// @check if the security group references the security group above
var s []*string
for _, ni := range j.LaunchTemplateData.NetworkInterfaces {
s = append(s, ni.Groups...)
}
s = append(s, j.LaunchTemplateData.SecurityGroupIds...)
for _, y := range s {
if securityGroups.Has(fi.StringValue(y)) {
list = append(list, &resources.Resource{
Name: aws.StringValue(x.LaunchTemplateName),
ID: aws.StringValue(x.LaunchTemplateName),
Type: TypeAutoscalingLaunchConfig,
Deleter: DeleteAutoScalingGroupLaunchTemplate,
})
}
}
var list []*resources.Resource
err := c.EC2().DescribeLaunchTemplatesPages(input, func(p *ec2.DescribeLaunchTemplatesOutput, lastPage bool) (shouldContinue bool) {
for _, lt := range p.LaunchTemplates {
list = append(list, &resources.Resource{
Name: aws.StringValue(lt.LaunchTemplateName),
ID: aws.StringValue(lt.LaunchTemplateId),
Type: TypeAutoscalingLaunchConfig,
Deleter: DeleteAutoScalingGroupLaunchTemplate,
})
}
return true
})
if err != nil {
return nil, fmt.Errorf("error listing AutoScaling LaunchTemplates: %v", err)
}

return list, nil
Expand Down Expand Up @@ -1377,7 +1365,7 @@ func DeleteAutoScalingGroupLaunchTemplate(cloud fi.Cloud, r *resources.Resource)
klog.V(2).Infof("Deleting EC2 LaunchTemplate %q", r.ID)

if _, err := c.EC2().DeleteLaunchTemplate(&ec2.DeleteLaunchTemplateInput{
LaunchTemplateName: fi.String(r.ID),
LaunchTemplateId: fi.String(r.ID),
}); err != nil {
return fmt.Errorf("error deleting ec2 LaunchTemplate %q: %v", r.ID, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ resource "aws_launch_template" "bastion-bastionuserdata-example-com" {
lifecycle {
create_before_destroy = true
}
name_prefix = "bastion.bastionuserdata.example.com-"
name = "bastion.bastionuserdata.example.com"
network_interfaces {
associate_public_ip_address = true
delete_on_termination = true
Expand Down Expand Up @@ -493,7 +493,7 @@ resource "aws_launch_template" "master-us-test-1a-masters-bastionuserdata-exampl
lifecycle {
create_before_destroy = true
}
name_prefix = "master-us-test-1a.masters.bastionuserdata.example.com-"
name = "master-us-test-1a.masters.bastionuserdata.example.com"
network_interfaces {
associate_public_ip_address = false
delete_on_termination = true
Expand Down Expand Up @@ -553,7 +553,7 @@ resource "aws_launch_template" "nodes-bastionuserdata-example-com" {
lifecycle {
create_before_destroy = true
}
name_prefix = "nodes.bastionuserdata.example.com-"
name = "nodes.bastionuserdata.example.com"
network_interfaces {
associate_public_ip_address = false
delete_on_termination = true
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/update_cluster/complex/kubernetes.tf
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ resource "aws_launch_template" "master-us-test-1a-masters-complex-example-com" {
lifecycle {
create_before_destroy = true
}
name_prefix = "master-us-test-1a.masters.complex.example.com-"
name = "master-us-test-1a.masters.complex.example.com"
network_interfaces {
associate_public_ip_address = true
delete_on_termination = true
Expand Down Expand Up @@ -374,7 +374,7 @@ resource "aws_launch_template" "nodes-complex-example-com" {
monitoring {
enabled = true
}
name_prefix = "nodes.complex.example.com-"
name = "nodes.complex.example.com"
network_interfaces {
associate_public_ip_address = true
delete_on_termination = true
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/update_cluster/existing_iam/kubernetes.tf
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ resource "aws_launch_template" "master-us-test-1a-masters-existing-iam-example-c
lifecycle {
create_before_destroy = true
}
name_prefix = "master-us-test-1a.masters.existing-iam.example.com-"
name = "master-us-test-1a.masters.existing-iam.example.com"
network_interfaces {
associate_public_ip_address = true
delete_on_termination = true
Expand Down Expand Up @@ -451,7 +451,7 @@ resource "aws_launch_template" "master-us-test-1b-masters-existing-iam-example-c
lifecycle {
create_before_destroy = true
}
name_prefix = "master-us-test-1b.masters.existing-iam.example.com-"
name = "master-us-test-1b.masters.existing-iam.example.com"
network_interfaces {
associate_public_ip_address = true
delete_on_termination = true
Expand Down Expand Up @@ -515,7 +515,7 @@ resource "aws_launch_template" "master-us-test-1c-masters-existing-iam-example-c
lifecycle {
create_before_destroy = true
}
name_prefix = "master-us-test-1c.masters.existing-iam.example.com-"
name = "master-us-test-1c.masters.existing-iam.example.com"
network_interfaces {
associate_public_ip_address = true
delete_on_termination = true
Expand Down Expand Up @@ -575,7 +575,7 @@ resource "aws_launch_template" "nodes-existing-iam-example-com" {
lifecycle {
create_before_destroy = true
}
name_prefix = "nodes.existing-iam.example.com-"
name = "nodes.existing-iam.example.com"
network_interfaces {
associate_public_ip_address = true
delete_on_termination = true
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/update_cluster/existing_sg/kubernetes.tf
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ resource "aws_launch_template" "master-us-test-1a-masters-existingsg-example-com
lifecycle {
create_before_destroy = true
}
name_prefix = "master-us-test-1a.masters.existingsg.example.com-"
name = "master-us-test-1a.masters.existingsg.example.com"
network_interfaces {
associate_public_ip_address = true
delete_on_termination = true
Expand Down Expand Up @@ -532,7 +532,7 @@ resource "aws_launch_template" "master-us-test-1b-masters-existingsg-example-com
lifecycle {
create_before_destroy = true
}
name_prefix = "master-us-test-1b.masters.existingsg.example.com-"
name = "master-us-test-1b.masters.existingsg.example.com"
network_interfaces {
associate_public_ip_address = true
delete_on_termination = true
Expand Down Expand Up @@ -596,7 +596,7 @@ resource "aws_launch_template" "master-us-test-1c-masters-existingsg-example-com
lifecycle {
create_before_destroy = true
}
name_prefix = "master-us-test-1c.masters.existingsg.example.com-"
name = "master-us-test-1c.masters.existingsg.example.com"
network_interfaces {
associate_public_ip_address = true
delete_on_termination = true
Expand Down Expand Up @@ -656,7 +656,7 @@ resource "aws_launch_template" "nodes-existingsg-example-com" {
lifecycle {
create_before_destroy = true
}
name_prefix = "nodes.existingsg.example.com-"
name = "nodes.existingsg.example.com"
network_interfaces {
associate_public_ip_address = true
delete_on_termination = true
Expand Down
Loading

0 comments on commit 6cf691b

Please sign in to comment.