Skip to content

Commit

Permalink
chore(provider/openstack): use ips from associated subnets
Browse files Browse the repository at this point in the history
This changes values the provided instance group subnet and uses the IPs
from that subnet.
  • Loading branch information
mitch000001 committed Sep 13, 2019
1 parent 2adca23 commit aa5191e
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pkg/model/openstackmodel/servergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,23 @@ func (b *ServerGroupModelBuilder) buildInstances(c *fi.ModelBuilderContext, sg *
instanceName := fi.String(strings.Replace(iName, ".", "-", -1))

var az *string
var subnets []*openstacktasks.Subnet
if len(ig.Spec.Subnets) > 0 {
subnet := ig.Spec.Subnets[int(i)%len(ig.Spec.Subnets)]
// bastion subnet name is not actual zone name, it contains "utility-" prefix
if ig.Spec.Role == kops.InstanceGroupRoleBastion {
az = fi.String(strings.Replace(ig.Spec.Subnets[int(i)%len(ig.Spec.Subnets)], "utility-", "", 1))
az = fi.String(strings.Replace(subnet, "utility-", "", 1))
} else {
az = fi.String(ig.Spec.Subnets[int(i)%len(ig.Spec.Subnets)])
az = fi.String(subnet)
}
subnets = append(subnets, b.LinkToSubnet(s(fmt.Sprintf("%s.%s", subnet, b.ClusterName()))))
}
// Create instance port task
portTask := &openstacktasks.Port{
Name: fi.String(fmt.Sprintf("%s-%s", "port", *instanceName)),
Network: b.LinkToNetwork(),
SecurityGroups: securityGroups,
Subnets: subnets,
Lifecycle: b.Lifecycle,
}
c.AddTask(portTask)
Expand Down
126 changes: 126 additions & 0 deletions pkg/model/openstackmodel/servergroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ func Test_ServerGroupModelBuilder(t *testing.T) {
{Name: s("master-public-name")},
{Name: s("masters.cluster")},
},
Subnets: []*openstacktasks.Subnet{
{Name: s("subnet.cluster")},
},
Lifecycle: &clusterLifecycle,
}
masterInstance := &openstacktasks.Instance{
Expand Down Expand Up @@ -145,6 +148,9 @@ func Test_ServerGroupModelBuilder(t *testing.T) {
SecurityGroups: []*openstacktasks.SecurityGroup{
{Name: s("nodes.cluster")},
},
Subnets: []*openstacktasks.Subnet{
{Name: s("subnet.cluster")},
},
Lifecycle: &clusterLifecycle,
}
nodeInstance := &openstacktasks.Instance{
Expand Down Expand Up @@ -268,6 +274,9 @@ func Test_ServerGroupModelBuilder(t *testing.T) {
{Name: s("master-public-name")},
{Name: s("masters.cluster")},
},
Subnets: []*openstacktasks.Subnet{
{Name: s("subnet.cluster")},
},
Lifecycle: &clusterLifecycle,
}
masterInstance := &openstacktasks.Instance{
Expand Down Expand Up @@ -310,6 +319,9 @@ func Test_ServerGroupModelBuilder(t *testing.T) {
SecurityGroups: []*openstacktasks.SecurityGroup{
{Name: s("nodes.cluster")},
},
Subnets: []*openstacktasks.Subnet{
{Name: s("subnet.cluster")},
},
Lifecycle: &clusterLifecycle,
}
nodeInstance := &openstacktasks.Instance{
Expand Down Expand Up @@ -347,6 +359,9 @@ func Test_ServerGroupModelBuilder(t *testing.T) {
SecurityGroups: []*openstacktasks.SecurityGroup{
{Name: s("bastion.cluster")},
},
Subnets: []*openstacktasks.Subnet{
{Name: s("utility-subnet.cluster")},
},
Lifecycle: &clusterLifecycle,
}
bastionInstance := &openstacktasks.Instance{
Expand Down Expand Up @@ -504,6 +519,9 @@ func Test_ServerGroupModelBuilder(t *testing.T) {
{Name: s("master-public-name")},
{Name: s("masters.cluster")},
},
Subnets: []*openstacktasks.Subnet{
{Name: s("subnet-a.cluster")},
},
Lifecycle: &clusterLifecycle,
}
masterAInstance := &openstacktasks.Instance{
Expand Down Expand Up @@ -547,6 +565,9 @@ func Test_ServerGroupModelBuilder(t *testing.T) {
{Name: s("master-public-name")},
{Name: s("masters.cluster")},
},
Subnets: []*openstacktasks.Subnet{
{Name: s("subnet-b.cluster")},
},
Lifecycle: &clusterLifecycle,
}
masterBInstance := &openstacktasks.Instance{
Expand Down Expand Up @@ -590,6 +611,9 @@ func Test_ServerGroupModelBuilder(t *testing.T) {
{Name: s("master-public-name")},
{Name: s("masters.cluster")},
},
Subnets: []*openstacktasks.Subnet{
{Name: s("subnet-c.cluster")},
},
Lifecycle: &clusterLifecycle,
}
masterCInstance := &openstacktasks.Instance{
Expand Down Expand Up @@ -632,6 +656,9 @@ func Test_ServerGroupModelBuilder(t *testing.T) {
SecurityGroups: []*openstacktasks.SecurityGroup{
{Name: s("nodes.cluster")},
},
Subnets: []*openstacktasks.Subnet{
{Name: s("subnet-a.cluster")},
},
Lifecycle: &clusterLifecycle,
}
nodeAInstance := &openstacktasks.Instance{
Expand Down Expand Up @@ -674,6 +701,9 @@ func Test_ServerGroupModelBuilder(t *testing.T) {
SecurityGroups: []*openstacktasks.SecurityGroup{
{Name: s("nodes.cluster")},
},
Subnets: []*openstacktasks.Subnet{
{Name: s("subnet-b.cluster")},
},
Lifecycle: &clusterLifecycle,
}
nodeBInstance := &openstacktasks.Instance{
Expand Down Expand Up @@ -716,6 +746,9 @@ func Test_ServerGroupModelBuilder(t *testing.T) {
SecurityGroups: []*openstacktasks.SecurityGroup{
{Name: s("nodes.cluster")},
},
Subnets: []*openstacktasks.Subnet{
{Name: s("subnet-c.cluster")},
},
Lifecycle: &clusterLifecycle,
}
nodeCInstance := &openstacktasks.Instance{
Expand Down Expand Up @@ -888,6 +921,9 @@ func Test_ServerGroupModelBuilder(t *testing.T) {
SecurityGroups: []*openstacktasks.SecurityGroup{
{Name: s("masters.cluster")},
},
Subnets: []*openstacktasks.Subnet{
{Name: s("subnet-a.cluster")},
},
Lifecycle: &clusterLifecycle,
}
masterAInstance := &openstacktasks.Instance{
Expand Down Expand Up @@ -925,6 +961,9 @@ func Test_ServerGroupModelBuilder(t *testing.T) {
SecurityGroups: []*openstacktasks.SecurityGroup{
{Name: s("masters.cluster")},
},
Subnets: []*openstacktasks.Subnet{
{Name: s("subnet-b.cluster")},
},
Lifecycle: &clusterLifecycle,
}
masterBInstance := &openstacktasks.Instance{
Expand Down Expand Up @@ -962,6 +1001,9 @@ func Test_ServerGroupModelBuilder(t *testing.T) {
SecurityGroups: []*openstacktasks.SecurityGroup{
{Name: s("masters.cluster")},
},
Subnets: []*openstacktasks.Subnet{
{Name: s("subnet-c.cluster")},
},
Lifecycle: &clusterLifecycle,
}
masterCInstance := &openstacktasks.Instance{
Expand Down Expand Up @@ -999,6 +1041,9 @@ func Test_ServerGroupModelBuilder(t *testing.T) {
SecurityGroups: []*openstacktasks.SecurityGroup{
{Name: s("nodes.cluster")},
},
Subnets: []*openstacktasks.Subnet{
{Name: s("subnet-a.cluster")},
},
Lifecycle: &clusterLifecycle,
}
nodeAInstance := &openstacktasks.Instance{
Expand Down Expand Up @@ -1041,6 +1086,9 @@ func Test_ServerGroupModelBuilder(t *testing.T) {
SecurityGroups: []*openstacktasks.SecurityGroup{
{Name: s("nodes.cluster")},
},
Subnets: []*openstacktasks.Subnet{
{Name: s("subnet-b.cluster")},
},
Lifecycle: &clusterLifecycle,
}
nodeBInstance := &openstacktasks.Instance{
Expand Down Expand Up @@ -1083,6 +1131,9 @@ func Test_ServerGroupModelBuilder(t *testing.T) {
SecurityGroups: []*openstacktasks.SecurityGroup{
{Name: s("nodes.cluster")},
},
Subnets: []*openstacktasks.Subnet{
{Name: s("subnet-c.cluster")},
},
Lifecycle: &clusterLifecycle,
}
nodeCInstance := &openstacktasks.Instance{
Expand Down Expand Up @@ -1261,6 +1312,9 @@ func Test_ServerGroupModelBuilder(t *testing.T) {
{Name: s("master-public-name")},
{Name: s("masters.cluster")},
},
Subnets: []*openstacktasks.Subnet{
{Name: s("subnet-a.cluster")},
},
Lifecycle: &clusterLifecycle,
}
masterAInstance := &openstacktasks.Instance{
Expand Down Expand Up @@ -1296,6 +1350,9 @@ func Test_ServerGroupModelBuilder(t *testing.T) {
{Name: s("master-public-name")},
{Name: s("masters.cluster")},
},
Subnets: []*openstacktasks.Subnet{
{Name: s("subnet-b.cluster")},
},
Lifecycle: &clusterLifecycle,
}
masterBInstance := &openstacktasks.Instance{
Expand Down Expand Up @@ -1331,6 +1388,9 @@ func Test_ServerGroupModelBuilder(t *testing.T) {
{Name: s("master-public-name")},
{Name: s("masters.cluster")},
},
Subnets: []*openstacktasks.Subnet{
{Name: s("subnet-c.cluster")},
},
Lifecycle: &clusterLifecycle,
}
masterCInstance := &openstacktasks.Instance{
Expand Down Expand Up @@ -1373,6 +1433,9 @@ func Test_ServerGroupModelBuilder(t *testing.T) {
SecurityGroups: []*openstacktasks.SecurityGroup{
{Name: s("nodes.cluster")},
},
Subnets: []*openstacktasks.Subnet{
{Name: s("subnet-a.cluster")},
},
Lifecycle: &clusterLifecycle,
}
nodeAInstance := &openstacktasks.Instance{
Expand Down Expand Up @@ -1407,6 +1470,9 @@ func Test_ServerGroupModelBuilder(t *testing.T) {
SecurityGroups: []*openstacktasks.SecurityGroup{
{Name: s("nodes.cluster")},
},
Subnets: []*openstacktasks.Subnet{
{Name: s("subnet-b.cluster")},
},
Lifecycle: &clusterLifecycle,
}
nodeBInstance := &openstacktasks.Instance{
Expand Down Expand Up @@ -1441,6 +1507,9 @@ func Test_ServerGroupModelBuilder(t *testing.T) {
SecurityGroups: []*openstacktasks.SecurityGroup{
{Name: s("nodes.cluster")},
},
Subnets: []*openstacktasks.Subnet{
{Name: s("subnet-c.cluster")},
},
Lifecycle: &clusterLifecycle,
}
nodeCInstance := &openstacktasks.Instance{
Expand Down Expand Up @@ -1615,6 +1684,63 @@ func comparePorts(t *testing.T, actualTask fi.Task, expected *openstacktasks.Por
t.Fatal("Network is nil")
}
compareStrings(t, "Network name", actual.Network.Name, expected.Network.Name)
if len(actual.Subnets) == len(expected.Subnets) {
for i, subnet := range expected.Subnets {
compareSubnets(t, actual.Subnets[i], subnet)
}
} else {
compareNamedTasks(t, "Subnets", asHasName(actual.Subnets), asHasName(expected.Subnets))
}
}

func asHasName(tasks interface{}) []fi.HasName {
var namedTasks []fi.HasName
rType := reflect.TypeOf(tasks)
if rType.Kind() != reflect.Array && rType.Kind() != reflect.Slice {
fmt.Printf("type is not an array or slice: %v\n", rType.Kind())
return namedTasks
}
rVal := reflect.ValueOf(tasks)
for i := 0; i < rVal.Len(); i++ {
elem := rVal.Index(i)
if named, ok := elem.Interface().(fi.HasName); ok {
namedTasks = append(namedTasks, named)
}
}
return namedTasks
}

func compareNamedTasks(t *testing.T, name string, actual, expected []fi.HasName) {
actualTaskNames := make([]string, len(actual))
for i, task := range actual {
actualTaskNames[i] = *task.GetName()
}
sort.Strings(actualTaskNames)
expectedTaskNames := make([]string, len(expected))
for i, task := range expected {
if task.GetName() == nil {
expectedTaskNames[i] = ""
} else {
expectedTaskNames[i] = *task.GetName()
}
}
sort.Strings(expectedTaskNames)
if !reflect.DeepEqual(expectedTaskNames, actualTaskNames) {
t.Errorf("%s differ: %v instead of %v", name, actualTaskNames, expectedTaskNames)
}
}

func compareSubnets(t *testing.T, actualTask fi.Task, expected *openstacktasks.Subnet) {
t.Helper()
if pointersAreBothNil(t, "Subnet", actualTask, expected) {
return
}
actual, ok := actualTask.(*openstacktasks.Subnet)
if !ok {
t.Fatalf("task is not an Subnet task, got %T", actualTask)
}

compareStrings(t, "Name", actual.Name, expected.Name)
}

func compareInstances(t *testing.T, actualTask fi.Task, expected *openstacktasks.Instance) {
Expand Down
16 changes: 16 additions & 0 deletions upup/pkg/fi/cloudup/openstacktasks/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Port struct {
ID *string
Name *string
Network *Network
Subnets []*Subnet
SecurityGroups []*SecurityGroup
Lifecycle *fi.Lifecycle
}
Expand Down Expand Up @@ -65,12 +66,20 @@ func NewPortTaskFromCloud(cloud openstack.OpenstackCloud, lifecycle *fi.Lifecycl
Lifecycle: lifecycle,
}
}
subnets := make([]*Subnet, len(port.FixedIPs))
for i, subn := range port.FixedIPs {
subnets[i] = &Subnet{
ID: fi.String(subn.SubnetID),
Lifecycle: lifecycle,
}
}

actual := &Port{
ID: fi.String(port.ID),
Name: fi.String(port.Name),
Network: &Network{ID: fi.String(port.NetworkID)},
SecurityGroups: sgs,
Subnets: subnets,
Lifecycle: lifecycle,
}
if find != nil {
Expand Down Expand Up @@ -128,11 +137,18 @@ func (_ *Port) RenderOpenstack(t *openstack.OpenstackAPITarget, a, e, changes *P
for i, sg := range e.SecurityGroups {
sgs[i] = fi.StringValue(sg.ID)
}
fixedIPs := make([]ports.IP, len(e.Subnets))
for i, subn := range e.Subnets {
fixedIPs[i] = ports.IP{
SubnetID: fi.StringValue(subn.ID),
}
}

opt := ports.CreateOpts{
Name: fi.StringValue(e.Name),
NetworkID: fi.StringValue(e.Network.ID),
SecurityGroups: &sgs,
FixedIPs: fixedIPs,
}

v, err := t.Cloud.CreatePort(opt)
Expand Down

0 comments on commit aa5191e

Please sign in to comment.