diff --git a/pkg/model/alimodel/api_loadbalancer.go b/pkg/model/alimodel/api_loadbalancer.go index 3bded43b4c..35784c3962 100644 --- a/pkg/model/alimodel/api_loadbalancer.go +++ b/pkg/model/alimodel/api_loadbalancer.go @@ -73,8 +73,12 @@ func (b *APILoadBalancerModelBuilder) Build(c *fi.ModelBuilderContext) error { switch lbSpec.Type { case kops.LoadBalancerTypeInternal: - return errors.New("internal LoadBalancers are not yet supported by kops on ALI") - //loadbalancer.AddressType = s("intranet") + utilitySubnets := b.GetUtilitySubnets() + if len(utilitySubnets) == 0 { + return errors.New("internal loadbalancer requires at least 1 utility subnet") + } + loadbalancer.AddressType = s("intranet") + loadbalancer.VSwitchId = fi.String(utilitySubnets[0].ProviderID) case kops.LoadBalancerTypePublic: loadbalancer.AddressType = s("internet") default: diff --git a/pkg/model/alimodel/context.go b/pkg/model/alimodel/context.go index 76d34806c9..57714bd6ab 100644 --- a/pkg/model/alimodel/context.go +++ b/pkg/model/alimodel/context.go @@ -76,6 +76,17 @@ func (c *ALIModelContext) GetNameForVSwitchSNAT(subnetName string) string { return subnetName + "." + c.ClusterName() } +func (c *ALIModelContext) GetUtilitySubnets() []*kops.ClusterSubnetSpec { + var subnets []*kops.ClusterSubnetSpec + for i := range c.Cluster.Spec.Subnets { + subnet := &c.Cluster.Spec.Subnets[i] + if subnet.Type == kops.SubnetTypeUtility { + subnets = append(subnets, subnet) + } + } + return subnets +} + // LinkLoadBalancer returns the LoadBalancer object the cluster is located in func (c *ALIModelContext) LinkLoadBalancer() *alitasks.LoadBalancer { return &alitasks.LoadBalancer{Name: s(c.GetNameForLoadBalancer())} diff --git a/upup/pkg/fi/cloudup/alitasks/loadbalancer.go b/upup/pkg/fi/cloudup/alitasks/loadbalancer.go index 34bd0e1e11..44b7d9be19 100644 --- a/upup/pkg/fi/cloudup/alitasks/loadbalancer.go +++ b/upup/pkg/fi/cloudup/alitasks/loadbalancer.go @@ -36,6 +36,7 @@ type LoadBalancer struct { Name *string LoadbalancerId *string AddressType *string + VSwitchId *string LoadBalancerAddress *string Lifecycle *fi.Lifecycle Tags map[string]string @@ -63,21 +64,25 @@ func (l *LoadBalancer) Find(c *fi.Context) (*LoadBalancer, error) { return nil, fmt.Errorf("error finding LoadBalancers: %v", err) } - // Don't exist loadbalancer with specified ClusterTags or Name. + // There's no loadbalancer with specified ClusterTags or Name. if len(responseLoadBalancers) == 0 { + klog.V(4).Infof("can't find loadbalancer with name: %q", *l.Name) return nil, nil } if len(responseLoadBalancers) > 1 { - klog.V(4).Infof("The number of specified loadbalancer with the same name exceeds 1, loadbalancerName:%q", *l.Name) + return nil, fmt.Errorf("more than 1 loadbalancer is found with name: %q", *l.Name) } klog.V(2).Infof("found matching LoadBalancer: %q", *l.Name) + lb := responseLoadBalancers[0] - actual := &LoadBalancer{} - actual.Name = fi.String(responseLoadBalancers[0].LoadBalancerName) - actual.AddressType = fi.String(string(responseLoadBalancers[0].AddressType)) - actual.LoadbalancerId = fi.String(responseLoadBalancers[0].LoadBalancerId) - actual.LoadBalancerAddress = fi.String(responseLoadBalancers[0].Address) + actual := &LoadBalancer{ + Name: fi.String(lb.LoadBalancerName), + AddressType: fi.String(string(lb.AddressType)), + LoadbalancerId: fi.String(lb.LoadBalancerId), + LoadBalancerAddress: fi.String(lb.Address), + VSwitchId: fi.String(lb.VSwitchId), + } describeTagsArgs := &slb.DescribeTagsArgs{ RegionId: common.Region(cloud.Region()), @@ -165,6 +170,7 @@ func (_ *LoadBalancer) RenderALI(t *aliup.ALIAPITarget, a, e, changes *LoadBalan RegionId: common.Region(t.Cloud.Region()), LoadBalancerName: fi.StringValue(e.Name), AddressType: slb.AddressType(fi.StringValue(e.AddressType)), + VSwitchId: fi.StringValue(e.VSwitchId), } response, err := t.Cloud.SlbClient().CreateLoadBalancer(createLoadBalancerArgs) if err != nil {