Skip to content

Commit

Permalink
Merge pull request #4661 from justinsb/fix_routetable
Browse files Browse the repository at this point in the history
Fix routetable detection
  • Loading branch information
k8s-ci-robot authored Mar 16, 2018
2 parents f275360 + 785cc81 commit 2b95485
Show file tree
Hide file tree
Showing 40 changed files with 486 additions and 336 deletions.
16 changes: 13 additions & 3 deletions cloudmock/aws/mockec2/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ func (m *MockEC2) AllocateAddressWithContext(aws.Context, *ec2.AllocateAddressIn
return nil, nil
}

func (m *MockEC2) AllocateAddress(request *ec2.AllocateAddressInput) (*ec2.AllocateAddressOutput, error) {
glog.Infof("AllocateAddress: %v", request)
func (m *MockEC2) AllocateAddressWithId(request *ec2.AllocateAddressInput, id string) (*ec2.AllocateAddressOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

m.addressNumber++
n := m.addressNumber
Expand All @@ -52,7 +53,7 @@ func (m *MockEC2) AllocateAddress(request *ec2.AllocateAddressInput) (*ec2.Alloc
}

address := &ec2.Address{
AllocationId: s(fmt.Sprintf("eip-%d", n)),
AllocationId: s(id),
Domain: s("vpc"),
PublicIp: s(publicIP.String()),
}
Expand All @@ -65,6 +66,12 @@ func (m *MockEC2) AllocateAddress(request *ec2.AllocateAddressInput) (*ec2.Alloc
return response, nil
}

func (m *MockEC2) AllocateAddress(request *ec2.AllocateAddressInput) (*ec2.AllocateAddressOutput, error) {
glog.Infof("AllocateAddress: %v", request)
id := m.allocateId("eip")
return m.AllocateAddressWithId(request, id)
}

func (m *MockEC2) AssignPrivateIpAddressesRequest(*ec2.AssignPrivateIpAddressesInput) (*request.Request, *ec2.AssignPrivateIpAddressesOutput) {
panic("Not implemented")
return nil, nil
Expand Down Expand Up @@ -106,6 +113,9 @@ func (m *MockEC2) DescribeAddressesWithContext(aws.Context, *ec2.DescribeAddress
}

func (m *MockEC2) DescribeAddresses(request *ec2.DescribeAddressesInput) (*ec2.DescribeAddressesOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

glog.Infof("DescribeAddresses: %v", request)

var addresses []*ec2.Address
Expand Down
12 changes: 7 additions & 5 deletions cloudmock/aws/mockec2/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,23 @@ type MockEC2 struct {
securityGroupNumber int
SecurityGroups map[string]*ec2.SecurityGroup

subnetNumber int
subnets map[string]*subnetInfo
subnets map[string]*subnetInfo

Volumes map[string]*ec2.Volume

KeyPairs []*ec2.KeyPairInfo

Tags []*ec2.TagDescription

vpcNumber int
Vpcs map[string]*vpcInfo
Vpcs map[string]*vpcInfo

internetGatewayNumber int
InternetGateways map[string]*internetGatewayInfo

NatGateways map[string]*ec2.NatGateway

ids map[string]*idAllocator
idsMutex sync.Mutex
ids map[string]*idAllocator
}

var _ ec2iface.EC2API = &MockEC2{}
Expand All @@ -66,6 +65,9 @@ type idAllocator struct {
}

func (m *MockEC2) allocateId(prefix string) string {
m.idsMutex.Lock()
defer m.idsMutex.Unlock()

ids := m.ids[prefix]
if ids == nil {
if m.ids == nil {
Expand Down
13 changes: 8 additions & 5 deletions cloudmock/aws/mockec2/natgateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,10 @@ import (
"github.com/golang/glog"
)

func (m *MockEC2) CreateNatGateway(request *ec2.CreateNatGatewayInput) (*ec2.CreateNatGatewayOutput, error) {
func (m *MockEC2) CreateNatGatewayWithId(request *ec2.CreateNatGatewayInput, id string) (*ec2.CreateNatGatewayOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

glog.Infof("CreateNatGateway: %v", request)

id := m.allocateId("ngw")

ngw := &ec2.NatGateway{
NatGatewayId: s(id),
SubnetId: request.SubnetId,
Expand Down Expand Up @@ -69,6 +65,13 @@ func (m *MockEC2) CreateNatGateway(request *ec2.CreateNatGatewayInput) (*ec2.Cre
}, nil
}

func (m *MockEC2) CreateNatGateway(request *ec2.CreateNatGatewayInput) (*ec2.CreateNatGatewayOutput, error) {
glog.Infof("CreateNatGateway: %v", request)

id := m.allocateId("nat")
return m.CreateNatGatewayWithId(request, id)
}

func (m *MockEC2) WaitUntilNatGatewayAvailable(request *ec2.DescribeNatGatewaysInput) error {
m.mutex.Lock()
defer m.mutex.Unlock()
Expand Down
8 changes: 1 addition & 7 deletions cloudmock/aws/mockec2/routetable.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package mockec2

import (
"fmt"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
Expand Down Expand Up @@ -65,7 +64,6 @@ func (m *MockEC2) DescribeRouteTables(request *ec2.DescribeRouteTablesInput) (*e
}

response := &ec2.DescribeRouteTablesOutput{}

for _, rt := range m.RouteTables {
allFiltersMatch := true
for _, filter := range request.Filters {
Expand All @@ -86,11 +84,7 @@ func (m *MockEC2) DescribeRouteTables(request *ec2.DescribeRouteTablesInput) (*e
}
}
default:
if strings.HasPrefix(*filter.Name, "tag:") {
match = m.hasTag(ec2.ResourceTypeRouteTable, *rt.RouteTableId, filter)
} else {
return nil, fmt.Errorf("unknown filter name: %q", *filter.Name)
}
match = m.hasTag(ec2.ResourceTypeRouteTable, *rt.RouteTableId, filter)
}

if !match {
Expand Down
3 changes: 3 additions & 0 deletions cloudmock/aws/mockec2/securitygroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ func (m *MockEC2) DescribeSecurityGroupsWithContext(aws.Context, *ec2.DescribeSe
return nil, nil
}
func (m *MockEC2) DescribeSecurityGroups(request *ec2.DescribeSecurityGroupsInput) (*ec2.DescribeSecurityGroupsOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

glog.Infof("DescribeSecurityGroups: %v", request)

if len(request.GroupIds) != 0 {
Expand Down
16 changes: 9 additions & 7 deletions cloudmock/aws/mockec2/subnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,12 @@ func (m *MockEC2) CreateSubnetWithContext(aws.Context, *ec2.CreateSubnetInput, .
return nil, nil
}

func (m *MockEC2) CreateSubnet(request *ec2.CreateSubnetInput) (*ec2.CreateSubnetOutput, error) {
func (m *MockEC2) CreateSubnetWithId(request *ec2.CreateSubnetInput, id string) (*ec2.CreateSubnetOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

glog.Infof("CreateSubnet: %v", request)

m.subnetNumber++
n := m.subnetNumber

subnet := &ec2.Subnet{
SubnetId: s(fmt.Sprintf("subnet-%d", n)),
SubnetId: s(id),
VpcId: request.VpcId,
CidrBlock: request.CidrBlock,
AvailabilityZone: request.AvailabilityZone,
Expand All @@ -88,6 +83,13 @@ func (m *MockEC2) CreateSubnet(request *ec2.CreateSubnetInput) (*ec2.CreateSubne
return response, nil
}

func (m *MockEC2) CreateSubnet(request *ec2.CreateSubnetInput) (*ec2.CreateSubnetOutput, error) {
glog.Infof("CreateSubnet: %v", request)

id := m.allocateId("subnet")
return m.CreateSubnetWithId(request, id)
}

func (m *MockEC2) DescribeSubnetsRequest(*ec2.DescribeSubnetsInput) (*request.Request, *ec2.DescribeSubnetsOutput) {
panic("Not implemented")
return nil, nil
Expand Down
16 changes: 15 additions & 1 deletion cloudmock/aws/mockec2/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (m *MockEC2) addTag(resourceId string, tag *ec2.Tag) {
resourceType = ec2.ResourceTypeVolume
} else if strings.HasPrefix(resourceId, "igw-") {
resourceType = ec2.ResourceTypeInternetGateway
} else if strings.HasPrefix(resourceId, "ngw-") {
} else if strings.HasPrefix(resourceId, "nat-") {
resourceType = ResourceTypeNatGateway
} else if strings.HasPrefix(resourceId, "dopt-") {
resourceType = ec2.ResourceTypeDhcpOptions
Expand Down Expand Up @@ -116,6 +116,20 @@ func (m *MockEC2) hasTag(resourceType string, resourceId string, filter *ec2.Fil
}
}
}
} else if name == "tag-key" {
for _, tag := range m.Tags {
if *tag.ResourceId != resourceId {
continue
}
if *tag.ResourceType != resourceType {
continue
}
for _, v := range filter.Values {
if *tag.Key == *v {
return true
}
}
}
} else {
glog.Fatalf("Unsupported filter: %v", filter)
}
Expand Down
46 changes: 31 additions & 15 deletions cloudmock/aws/mockec2/vpcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,9 @@ func (m *MockEC2) CreateVpcWithContext(aws.Context, *ec2.CreateVpcInput, ...requ
panic("Not implemented")
return nil, nil
}
func (m *MockEC2) CreateVpc(request *ec2.CreateVpcInput) (*ec2.CreateVpcOutput, error) {
glog.Infof("CreateVpc: %v", request)

if request.DryRun != nil {
glog.Fatalf("DryRun")
}

m.vpcNumber++
n := m.vpcNumber

id := fmt.Sprintf("vpc-%d", n)
func (m *MockEC2) CreateVpcWithId(request *ec2.CreateVpcInput, id string) (*ec2.CreateVpcOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

vpc := &vpcInfo{
main: ec2.Vpc{
Expand All @@ -70,8 +62,8 @@ func (m *MockEC2) CreateVpc(request *ec2.CreateVpcInput) (*ec2.CreateVpcOutput,
IsDefault: aws.Bool(false),
},
attributes: ec2.DescribeVpcAttributeOutput{
EnableDnsHostnames: &ec2.AttributeBooleanValue{Value: aws.Bool(false)},
EnableDnsSupport: &ec2.AttributeBooleanValue{Value: aws.Bool(false)},
EnableDnsHostnames: &ec2.AttributeBooleanValue{Value: aws.Bool(true)},
EnableDnsSupport: &ec2.AttributeBooleanValue{Value: aws.Bool(true)},
},
}

Expand All @@ -86,6 +78,18 @@ func (m *MockEC2) CreateVpc(request *ec2.CreateVpcInput) (*ec2.CreateVpcOutput,
return response, nil
}

func (m *MockEC2) CreateVpc(request *ec2.CreateVpcInput) (*ec2.CreateVpcOutput, error) {
glog.Infof("CreateVpc: %v", request)

if request.DryRun != nil {
glog.Fatalf("DryRun")
}

id := m.allocateId("vpc")

return m.CreateVpcWithId(request, id)
}

func (m *MockEC2) DescribeVpcsRequest(*ec2.DescribeVpcsInput) (*request.Request, *ec2.DescribeVpcsOutput) {
panic("Not implemented")
return nil, nil
Expand All @@ -97,16 +101,28 @@ func (m *MockEC2) DescribeVpcsWithContext(aws.Context, *ec2.DescribeVpcsInput, .
}

func (m *MockEC2) DescribeVpcs(request *ec2.DescribeVpcsInput) (*ec2.DescribeVpcsOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

glog.Infof("DescribeVpcs: %v", request)

if len(request.VpcIds) != 0 {
request.Filters = append(request.Filters, &ec2.Filter{Name: s("vpc-id"), Values: request.VpcIds})
}

var vpcs []*ec2.Vpc

for _, vpc := range m.Vpcs {
for k, vpc := range m.Vpcs {
allFiltersMatch := true
for _, filter := range request.Filters {
match := false
switch *filter.Name {

case "vpc-id":
for _, v := range filter.Values {
if k == *v {
match = true
}
}
default:
if strings.HasPrefix(*filter.Name, "tag:") {
match = m.hasTag(ec2.ResourceTypeVpc, *vpc.main.VpcId, filter)
Expand Down
1 change: 1 addition & 0 deletions cmd/kops/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ go_test(
embed = [":go_default_library"],
shard_count = 10,
deps = [
"//cloudmock/aws/mockec2:go_default_library",
"//cmd/kops/util:go_default_library",
"//pkg/apis/kops:go_default_library",
"//pkg/diff:go_default_library",
Expand Down
8 changes: 4 additions & 4 deletions cmd/kops/create_cluster_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ import (
"testing"
"time"

"github.com/golang/glog"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/golang/glog"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/kops/cloudmock/aws/mockec2"
"k8s.io/kops/cmd/kops/util"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/diff"
Expand Down Expand Up @@ -130,9 +130,9 @@ func runCreateClusterIntegrationTest(t *testing.T, srcDir string, version string

cloudTags := map[string]string{}
awsCloud, _ := awsup.NewAWSCloud("us-test-1", cloudTags)
awsCloud.EC2().CreateVpc(&ec2.CreateVpcInput{
(awsCloud.EC2().(*mockec2.MockEC2)).CreateVpcWithId(&ec2.CreateVpcInput{
CidrBlock: aws.String("10.0.0.0/12"),
})
}, "vpc-12345678")

awsCloud.EC2().CreateSubnet(&ec2.CreateSubnetInput{
AvailabilityZone: aws.String("us-test-1a"),
Expand Down
6 changes: 3 additions & 3 deletions cmd/kops/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,20 @@ func TestSharedVPC(t *testing.T) {

// TestPhaseNetwork tests the output of tf for the network phase
func TestPhaseNetwork(t *testing.T) {
runTestPhase(t, "privateweave.example.com", "lifecycle_phases", "v1alpha2", true, 1, cloudup.PhaseNetwork)
runTestPhase(t, "lifecyclephases.example.com", "lifecycle_phases", "v1alpha2", true, 1, cloudup.PhaseNetwork)
}

// TestPhaseIAM tests the output of tf for the iam phase
func TestPhaseIAM(t *testing.T) {
t.Skip("unable to test w/o allowing failed validation")
runTestPhase(t, "privateweave.example.com", "lifecycle_phases", "v1alpha2", true, 1, cloudup.PhaseSecurity)
runTestPhase(t, "lifecyclephases.example.com", "lifecycle_phases", "v1alpha2", true, 1, cloudup.PhaseSecurity)
}

// TestPhaseCluster tests the output of tf for the cluster phase
func TestPhaseCluster(t *testing.T) {
// TODO fix tf for phase, and allow override on validation
t.Skip("unable to test w/o allowing failed validation")
runTestPhase(t, "privateweave.example.com", "lifecycle_phases", "v1alpha2", true, 1, cloudup.PhaseCluster)
runTestPhase(t, "lifecyclephases.example.com", "lifecycle_phases", "v1alpha2", true, 1, cloudup.PhaseCluster)
}

func runTest(t *testing.T, h *testutils.IntegrationTestHarness, clusterName string, srcDir string, version string, private bool, zones int, expectedFilenames []string, tfFileName string, phase *cloudup.Phase) {
Expand Down
19 changes: 18 additions & 1 deletion cmd/kops/lifecycle_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"bytes"
"path"
"strings"
"testing"
"time"

Expand All @@ -41,7 +42,7 @@ func (o *LifecycleTestOptions) AddDefaults() {
o.Version = "v1alpha2"
}
if o.ClusterName == "" {
o.ClusterName = o.SrcDir + ".example.com"
o.ClusterName = strings.Replace(o.SrcDir, "_", "", -1) + ".example.com"
}

o.SrcDir = "../../tests/integration/update_cluster/" + o.SrcDir
Expand All @@ -63,6 +64,22 @@ func TestLifecyclePrivateCalico(t *testing.T) {
})
}

// TestLifecyclePrivateKopeio runs the test on a private topology, with kopeio networking
func TestLifecyclePrivateKopeio(t *testing.T) {
runLifecycleTestAWS(&LifecycleTestOptions{
t: t,
SrcDir: "privatekopeio",
})
}

// TestLifecycleSharedVPC runs the test on a shared VPC
func TestLifecycleSharedVPC(t *testing.T) {
runLifecycleTestAWS(&LifecycleTestOptions{
t: t,
SrcDir: "shared_vpc",
})
}

func runLifecycleTest(h *testutils.IntegrationTestHarness, o *LifecycleTestOptions) {
t := o.t

Expand Down
Loading

0 comments on commit 2b95485

Please sign in to comment.