func (m *AwsVpcBackend) checkMatchingRoutes(instanceID, subnet string, ec2c *ec2.EC2) (bool, error) { filter := ec2.NewFilter() filter.Add("route.destination-cidr-block", subnet) filter.Add("route.state", "active") matchingRouteFound := false resp, err := ec2c.DescribeRouteTables([]string{m.cfg.RouteTableID}, filter) if err != nil { return matchingRouteFound, err } for _, routeTable := range resp.RouteTables { for _, route := range routeTable.Routes { if subnet == route.DestinationCidrBlock && route.State == "active" { if route.InstanceId == instanceID { matchingRouteFound = true break } log.Errorf("Deleting invalid *active* matching route: %s, %s \n", route.DestinationCidrBlock, route.InstanceId) } } } return matchingRouteFound, nil }
func (m *AwsVpcBackend) detectRouteTableID(instanceID string, ec2c *ec2.EC2) error { resp, err := ec2c.Instances([]string{instanceID}, nil) if err != nil { return fmt.Errorf("error getting instance info: %v", err) } if len(resp.Reservations) == 0 { return fmt.Errorf("no reservations found") } if len(resp.Reservations[0].Instances) == 0 { return fmt.Errorf("no matching instance found with id: %v", instanceID) } subnetID := resp.Reservations[0].Instances[0].SubnetId vpcID := resp.Reservations[0].Instances[0].VpcId log.Info("Subnet-ID: ", subnetID) log.Info("VPC-ID: ", vpcID) filter := ec2.NewFilter() filter.Add("association.subnet-id", subnetID) res, err := ec2c.DescribeRouteTables(nil, filter) if err != nil { return fmt.Errorf("error describing routeTables for subnetID %s: %v", subnetID, err) } if len(res.RouteTables) != 0 { m.cfg.RouteTableID = res.RouteTables[0].RouteTableId return nil } filter = ec2.NewFilter() filter.Add("association.main", "true") filter.Add("vpc-id", vpcID) res, err = ec2c.DescribeRouteTables(nil, filter) if err != nil { log.Info("error describing route tables: ", err) } if len(res.RouteTables) == 0 { return fmt.Errorf("main route table not found") } m.cfg.RouteTableID = res.RouteTables[0].RouteTableId return nil }
func (m *AwsVpcBackend) DetectRouteTableID(instanceID string, ec2c *ec2.EC2) (string, error) { resp, err := ec2c.Instances([]string{instanceID}, nil) if err != nil { return "", fmt.Errorf("error getting instance info: %v", err) } subnetID := resp.Reservations[0].Instances[0].SubnetId log.Info("SubnetId: ", subnetID) filter := ec2.NewFilter() filter.Add("association.subnet-id", subnetID) res, err := ec2c.DescribeRouteTables(nil, filter) if err != nil { return "", fmt.Errorf("error describing routeTables for subnetID %s: %v", subnetID, err) } return res.RouteTables[0].RouteTableId, nil }