From c0759525fc71dc9ed043e59f2ecf56b9f090b902 Mon Sep 17 00:00:00 2001 From: Jesse Haka Date: Sat, 21 Sep 2019 11:18:11 +0300 Subject: [PATCH] Use without external router --- pkg/model/openstackmodel/network.go | 2 +- pkg/model/openstackmodel/servergroup.go | 61 +++++++++++++------ upup/pkg/fi/cloudup/openstack/cloud.go | 27 ++++---- upup/pkg/fi/cloudup/openstack/instance.go | 6 +- .../pkg/fi/cloudup/openstacktasks/instance.go | 20 ++++++ 5 files changed, 82 insertions(+), 34 deletions(-) diff --git a/pkg/model/openstackmodel/network.go b/pkg/model/openstackmodel/network.go index 3eb6e004ca..bc334cb142 100644 --- a/pkg/model/openstackmodel/network.go +++ b/pkg/model/openstackmodel/network.go @@ -63,7 +63,7 @@ func (b *NetworkModelBuilder) Build(c *fi.ModelBuilderContext) error { DNSServers: make([]*string, 0), Lifecycle: b.Lifecycle, } - if b.Cluster.Spec.CloudConfig.Openstack.Router.DNSServers != nil { + if b.Cluster.Spec.CloudConfig.Openstack.Router != nil && b.Cluster.Spec.CloudConfig.Openstack.Router.DNSServers != nil { dnsSplitted := strings.Split(fi.StringValue(b.Cluster.Spec.CloudConfig.Openstack.Router.DNSServers), ",") dnsNameSrv := make([]*string, len(dnsSplitted)) for i, ns := range dnsSplitted { diff --git a/pkg/model/openstackmodel/servergroup.go b/pkg/model/openstackmodel/servergroup.go index 119a13da51..726c223246 100644 --- a/pkg/model/openstackmodel/servergroup.go +++ b/pkg/model/openstackmodel/servergroup.go @@ -136,33 +136,41 @@ func (b *ServerGroupModelBuilder) buildInstances(c *fi.ModelBuilderContext, sg * } c.AddTask(instanceTask) - // Associate a floating IP to the master and bastion always, associate it to a node if bastion is not used - switch ig.Spec.Role { - case kops.InstanceGroupRoleBastion: - t := &openstacktasks.FloatingIP{ - Name: fi.String(fmt.Sprintf("%s-%s", "fip", *instanceTask.Name)), - Server: instanceTask, - Lifecycle: b.Lifecycle, - } - c.AddTask(t) - case kops.InstanceGroupRoleMaster: - if b.Cluster.Spec.CloudConfig.Openstack.Loadbalancer == nil { + // Associate a floating IP to the master and bastion always if we have external network in router + // associate it to a node if bastion is not used + if b.Cluster.Spec.CloudConfig.Openstack != nil && b.Cluster.Spec.CloudConfig.Openstack.Router != nil { + switch ig.Spec.Role { + case kops.InstanceGroupRoleBastion: t := &openstacktasks.FloatingIP{ Name: fi.String(fmt.Sprintf("%s-%s", "fip", *instanceTask.Name)), Server: instanceTask, Lifecycle: b.Lifecycle, } c.AddTask(t) - b.associateFIPToKeypair(c, t) - } - default: - if !b.UsesSSHBastion() { - t := &openstacktasks.FloatingIP{ - Name: fi.String(fmt.Sprintf("%s-%s", "fip", *instanceTask.Name)), - Server: instanceTask, - Lifecycle: b.Lifecycle, + case kops.InstanceGroupRoleMaster: + if b.Cluster.Spec.CloudConfig.Openstack.Loadbalancer == nil { + t := &openstacktasks.FloatingIP{ + Name: fi.String(fmt.Sprintf("%s-%s", "fip", *instanceTask.Name)), + Server: instanceTask, + Lifecycle: b.Lifecycle, + } + c.AddTask(t) + b.associateFIPToKeypair(c, t) } - c.AddTask(t) + default: + if !b.UsesSSHBastion() { + t := &openstacktasks.FloatingIP{ + Name: fi.String(fmt.Sprintf("%s-%s", "fip", *instanceTask.Name)), + Server: instanceTask, + Lifecycle: b.Lifecycle, + } + c.AddTask(t) + } + } + } else if b.Cluster.Spec.CloudConfig.Openstack != nil && b.Cluster.Spec.CloudConfig.Openstack.Router == nil { + // No external router, but we need to add master fixed ips to certificates + if ig.Spec.Role == kops.InstanceGroupRoleMaster { + b.associateFixedIPToKeypair(c, instanceTask) } } } @@ -170,6 +178,19 @@ func (b *ServerGroupModelBuilder) buildInstances(c *fi.ModelBuilderContext, sg * return nil } +func (b *ServerGroupModelBuilder) associateFixedIPToKeypair(c *fi.ModelBuilderContext, fipTask *openstacktasks.Instance) error { + // Ensure the floating IP is included in the TLS certificate, + // if we're not going to use an alias for it + // TODO: I don't love this technique for finding the task by name & modifying it + masterKeypairTask, found := c.Tasks["Keypair/master"] + if !found { + return fmt.Errorf("keypair/master task not found") + } + masterKeypair := masterKeypairTask.(*fitasks.Keypair) + masterKeypair.AlternateNameTasks = append(masterKeypair.AlternateNameTasks, fipTask) + return nil +} + func (b *ServerGroupModelBuilder) associateFIPToKeypair(c *fi.ModelBuilderContext, fipTask *openstacktasks.FloatingIP) error { // Ensure the floating IP is included in the TLS certificate, // if we're not going to use an alias for it diff --git a/upup/pkg/fi/cloudup/openstack/cloud.go b/upup/pkg/fi/cloudup/openstack/cloud.go index 064fa459cc..0a4fb2721b 100644 --- a/upup/pkg/fi/cloudup/openstack/cloud.go +++ b/upup/pkg/fi/cloudup/openstack/cloud.go @@ -287,17 +287,18 @@ type OpenstackCloud interface { } type openstackCloud struct { - cinderClient *gophercloud.ServiceClient - neutronClient *gophercloud.ServiceClient - novaClient *gophercloud.ServiceClient - dnsClient *gophercloud.ServiceClient - lbClient *gophercloud.ServiceClient - extNetworkName *string - extSubnetName *string - floatingSubnet *string - tags map[string]string - region string - useOctavia bool + cinderClient *gophercloud.ServiceClient + neutronClient *gophercloud.ServiceClient + novaClient *gophercloud.ServiceClient + dnsClient *gophercloud.ServiceClient + lbClient *gophercloud.ServiceClient + floatingEnabled bool + extNetworkName *string + extSubnetName *string + floatingSubnet *string + tags map[string]string + region string + useOctavia bool } var _ fi.Cloud = &openstackCloud{} @@ -392,11 +393,13 @@ func NewOpenstackCloud(tags map[string]string, spec *kops.ClusterSpec) (Openstac } octavia := false + floatingEnabled := false if spec != nil && spec.CloudConfig != nil && spec.CloudConfig.Openstack != nil && spec.CloudConfig.Openstack.Router != nil { + floatingEnabled = true c.extNetworkName = spec.CloudConfig.Openstack.Router.ExternalNetwork if spec.CloudConfig.Openstack.Router.ExternalSubnet != nil { @@ -423,6 +426,7 @@ func NewOpenstackCloud(tags map[string]string, spec *kops.ClusterSpec) (Openstac } } } + c.floatingEnabled = floatingEnabled c.useOctavia = octavia var lbClient *gophercloud.ServiceClient if spec != nil && spec.CloudConfig != nil && spec.CloudConfig.Openstack != nil { @@ -598,7 +602,6 @@ func (c *openstackCloud) GetApiIngressStatus(cluster *kops.Cluster) ([]kops.ApiI if err != nil { return ingresses, fmt.Errorf("GetApiIngressStatus: Failed to list master nodes: %v", err) } - for _, instance := range instances { val, ok := instance.Metadata["k8s"] val2, ok2 := instance.Metadata["KopsRole"] diff --git a/upup/pkg/fi/cloudup/openstack/instance.go b/upup/pkg/fi/cloudup/openstack/instance.go index c972c0fb63..3e391e4aaa 100644 --- a/upup/pkg/fi/cloudup/openstack/instance.go +++ b/upup/pkg/fi/cloudup/openstack/instance.go @@ -78,7 +78,11 @@ func (c *openstackCloud) ListServerFloatingIPs(instanceID string) ([]*string, er for _, addrList := range addresses { for _, props := range addrList { - if props.IPType == "floating" { + if c.floatingEnabled { + if props.IPType == "floating" { + result = append(result, fi.String(props.Addr)) + } + } else { result = append(result, fi.String(props.Addr)) } } diff --git a/upup/pkg/fi/cloudup/openstacktasks/instance.go b/upup/pkg/fi/cloudup/openstacktasks/instance.go index c569273cec..dc96c776d0 100644 --- a/upup/pkg/fi/cloudup/openstacktasks/instance.go +++ b/upup/pkg/fi/cloudup/openstacktasks/instance.go @@ -46,6 +46,8 @@ type Instance struct { Lifecycle *fi.Lifecycle } +var _ fi.HasAddress = &Instance{} + // GetDependencies returns the dependencies of the Instance task func (e *Instance) GetDependencies(tasks map[string]fi.Task) []fi.Task { var deps []fi.Task @@ -70,6 +72,24 @@ func (e *Instance) CompareWithID() *string { return e.ID } +func (e *Instance) FindIPAddress(context *fi.Context) (*string, error) { + cloud := context.Cloud.(openstack.OpenstackCloud) + if e.Port == nil { + return nil, nil + } + + ports, err := cloud.GetPort(fi.StringValue(e.Port.ID)) + if err != nil { + return nil, err + } + + for _, port := range ports.FixedIPs { + return fi.String(port.IPAddress), nil + } + + return nil, nil +} + func (e *Instance) Find(c *fi.Context) (*Instance, error) { if e == nil || e.Name == nil { return nil, nil