func createLoadbalancer(t *testing.T, subnetID string) string { lb, err := loadbalancers.Create(base.Client, loadbalancers.CreateOpts{ VipSubnetID: subnetID, Name: "tmp_loadbalancer", AdminStateUp: loadbalancers.Up, }).Extract() th.AssertNoErr(t, err) t.Logf("Created Loadbalancer, ID %s", lb.ID) return lb.ID }
func resourceLoadBalancerV2Create(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) networkingClient, err := config.networkingV2Client(d.Get("region").(string)) if err != nil { return fmt.Errorf("Error creating OpenStack networking client: %s", err) } adminStateUp := d.Get("admin_state_up").(bool) createOpts := loadbalancers.CreateOpts{ Name: d.Get("name").(string), Description: d.Get("description").(string), VipSubnetID: d.Get("vip_subnet_id").(string), TenantID: d.Get("tenant_id").(string), VipAddress: d.Get("vip_address").(string), AdminStateUp: &adminStateUp, Flavor: d.Get("flavor").(string), Provider: d.Get("provider").(string), } log.Printf("[DEBUG] Create Options: %#v", createOpts) lb, err := loadbalancers.Create(networkingClient, createOpts).Extract() if err != nil { return fmt.Errorf("Error creating OpenStack LoadBalancer: %s", err) } log.Printf("[INFO] LoadBalancer ID: %s", lb.ID) log.Printf("[DEBUG] Waiting for Openstack LoadBalancer (%s) to become available.", lb.ID) stateConf := &resource.StateChangeConf{ Pending: []string{"PENDING_CREATE"}, Target: []string{"ACTIVE"}, Refresh: waitForLoadBalancerActive(networkingClient, lb.ID), Timeout: 20 * time.Minute, Delay: 5 * time.Second, MinTimeout: 3 * time.Second, } _, err = stateConf.WaitForState() if err != nil { return err } d.SetId(lb.ID) return resourceLoadBalancerV2Read(d, meta) }
func (lbaas *LbaasV2) createLoadBalancer(service *v1.Service, name string) (*loadbalancers.LoadBalancer, error) { createOpts := loadbalancers.CreateOpts{ Name: name, Description: fmt.Sprintf("Kubernetes external service %s", name), VipSubnetID: lbaas.opts.SubnetId, } loadBalancerIP := service.Spec.LoadBalancerIP if loadBalancerIP != "" { createOpts.VipAddress = loadBalancerIP } loadbalancer, err := loadbalancers.Create(lbaas.network, createOpts).Extract() if err != nil { return nil, fmt.Errorf("Error creating loadbalancer %v: %v", createOpts, err) } return loadbalancer, nil }
func (lbaas *LbaasV2) EnsureLoadBalancer(apiService *api.Service, hosts []string) (*api.LoadBalancerStatus, error) { glog.V(4).Infof("EnsureLoadBalancer(%v, %v, %v, %v, %v, %v)", apiService.Namespace, apiService.Name, apiService.Spec.LoadBalancerIP, apiService.Spec.Ports, hosts, apiService.Annotations) ports := apiService.Spec.Ports if len(ports) > 1 { return nil, fmt.Errorf("multiple ports are not yet supported in openstack load balancers") } else if len(ports) == 0 { return nil, fmt.Errorf("no ports provided to openstack load balancer") } // The service controller verified all the protocols match on the ports, just check and use the first one // TODO: Convert all error messages to use an event recorder if ports[0].Protocol != api.ProtocolTCP { return nil, fmt.Errorf("Only TCP LoadBalancer is supported for openstack load balancers") } affinity := api.ServiceAffinityNone //apiService.Spec.SessionAffinity var persistence *v2_pools.SessionPersistence switch affinity { case api.ServiceAffinityNone: persistence = nil case api.ServiceAffinityClientIP: persistence = &v2_pools.SessionPersistence{Type: "SOURCE_IP"} default: return nil, fmt.Errorf("unsupported load balancer affinity: %v", affinity) } sourceRanges, err := service.GetLoadBalancerSourceRanges(apiService) if err != nil { return nil, err } if !service.IsAllowAll(sourceRanges) { return nil, fmt.Errorf("Source range restrictions are not supported for openstack load balancers") } glog.V(2).Infof("Checking if openstack load balancer already exists: %s", cloudprovider.GetLoadBalancerName(apiService)) _, exists, err := lbaas.GetLoadBalancer(apiService) if err != nil { return nil, fmt.Errorf("error checking if openstack load balancer already exists: %v", err) } // TODO: Implement a more efficient update strategy for common changes than delete & create // In particular, if we implement hosts update, we can get rid of UpdateHosts if exists { err := lbaas.EnsureLoadBalancerDeleted(apiService) if err != nil { return nil, fmt.Errorf("error deleting existing openstack load balancer: %v", err) } } lbmethod := v2_pools.LBMethod(lbaas.opts.LBMethod) if lbmethod == "" { lbmethod = v2_pools.LBMethodRoundRobin } name := cloudprovider.GetLoadBalancerName(apiService) createOpts := loadbalancers.CreateOpts{ Name: name, Description: fmt.Sprintf("Kubernetes external service %s", name), VipSubnetID: lbaas.opts.SubnetId, } loadBalancerIP := apiService.Spec.LoadBalancerIP if loadBalancerIP != "" { createOpts.VipAddress = loadBalancerIP } loadbalancer, err := loadbalancers.Create(lbaas.network, createOpts).Extract() if err != nil { // cleanup what was created so far _ = lbaas.EnsureLoadBalancerDeleted(apiService) return nil, err } waitLoadbalancerActiveProvisioningStatus(lbaas.network, loadbalancer.ID) listener, err := listeners.Create(lbaas.network, listeners.CreateOpts{ Name: name, Protocol: listeners.ProtocolTCP, ProtocolPort: (int)(ports[0].Port), //TODO: need to handle multi-port LoadbalancerID: loadbalancer.ID, }).Extract() if err != nil { // cleanup what was created so far _ = lbaas.EnsureLoadBalancerDeleted(apiService) return nil, err } waitLoadbalancerActiveProvisioningStatus(lbaas.network, loadbalancer.ID) pool, err := v2_pools.Create(lbaas.network, v2_pools.CreateOpts{ Name: name, Protocol: v2_pools.ProtocolTCP, LBMethod: lbmethod, ListenerID: listener.ID, Persistence: persistence, }).Extract() if err != nil { // cleanup what was created so far _ = lbaas.EnsureLoadBalancerDeleted(apiService) return nil, err } for _, host := range hosts { addr, err := getAddressByName(lbaas.compute, host) if err != nil { return nil, err } waitLoadbalancerActiveProvisioningStatus(lbaas.network, loadbalancer.ID) _, err = v2_pools.CreateAssociateMember(lbaas.network, pool.ID, v2_pools.MemberCreateOpts{ ProtocolPort: int(ports[0].NodePort), //TODO: need to handle multi-port Address: addr, SubnetID: lbaas.opts.SubnetId, }).Extract() if err != nil { // cleanup what was created so far _ = lbaas.EnsureLoadBalancerDeleted(apiService) return nil, err } } if lbaas.opts.CreateMonitor { waitLoadbalancerActiveProvisioningStatus(lbaas.network, loadbalancer.ID) _, err = v2_monitors.Create(lbaas.network, v2_monitors.CreateOpts{ PoolID: pool.ID, Type: monitors.TypeTCP, Delay: int(lbaas.opts.MonitorDelay.Duration.Seconds()), Timeout: int(lbaas.opts.MonitorTimeout.Duration.Seconds()), MaxRetries: int(lbaas.opts.MonitorMaxRetries), }).Extract() if err != nil { // cleanup what was created so far _ = lbaas.EnsureLoadBalancerDeleted(apiService) return nil, err } } status := &api.LoadBalancerStatus{} status.Ingress = []api.LoadBalancerIngress{{IP: loadbalancer.VipAddress}} if lbaas.opts.FloatingNetworkId != "" { portID, err := getPortIDByIP(lbaas.network, loadbalancer.VipAddress) if err != nil { // cleanup what was created so far _ = lbaas.EnsureLoadBalancerDeleted(apiService) return nil, err } floatIPOpts := floatingips.CreateOpts{ FloatingNetworkID: lbaas.opts.FloatingNetworkId, PortID: portID, } floatIP, err := floatingips.Create(lbaas.network, floatIPOpts).Extract() if err != nil { // cleanup what was created so far _ = lbaas.EnsureLoadBalancerDeleted(apiService) return nil, err } status.Ingress = append(status.Ingress, api.LoadBalancerIngress{IP: floatIP.FloatingIP}) } return status, nil }