Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement LoadBalancer task for ALICloud #5207

Merged
merged 2 commits into from
Jun 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pkg/model/alimodel/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = [
"api_loadbalancer.go",
"context.go",
"convenience.go",
"network.go",
],
importpath = "k8s.io/kops/pkg/model/alimodel",
visibility = ["//visibility:public"],
deps = [
"//pkg/apis/kops:go_default_library",
"//pkg/dns:go_default_library",
"//pkg/model:go_default_library",
"//upup/pkg/fi:go_default_library",
"//upup/pkg/fi/cloudup/alitasks:go_default_library",
"//upup/pkg/fi/fitasks:go_default_library",
],
)
142 changes: 142 additions & 0 deletions pkg/model/alimodel/api_loadbalancer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
Copyright 2018 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package alimodel

import (
"errors"
"fmt"
"strings"

"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/dns"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/alitasks"
"k8s.io/kops/upup/pkg/fi/fitasks"
)

const LoadBalancerListenerStatus = "running"
const LoadBalancerListenerBandwidth = -1

// APILoadBalancerModelBuilder builds a LoadBalancer for accessing the API
type APILoadBalancerModelBuilder struct {
*ALIModelContext
Lifecycle *fi.Lifecycle
}

var _ fi.ModelBuilder = &APILoadBalancerModelBuilder{}

func (b *APILoadBalancerModelBuilder) Build(c *fi.ModelBuilderContext) error {
// Configuration where an ELB fronts the API
if !b.UseLoadBalancerForAPI() {
return nil
}

lbSpec := b.Cluster.Spec.API.LoadBalancer
if lbSpec == nil {
// Skipping API ELB creation; not requested in Spec
return nil
}

switch lbSpec.Type {
case kops.LoadBalancerTypeInternal:
// OK
case kops.LoadBalancerTypePublic:
// OK
default:
return fmt.Errorf("unhandled LoadBalancer type %q", lbSpec.Type)
}

// Create LoadBalancer for API ELB
var loadbalancer *alitasks.LoadBalancer
{

loadbalancer = &alitasks.LoadBalancer{
Name: s(b.GetNameForLoadBalancer()),
Lifecycle: b.Lifecycle,
}

switch lbSpec.Type {
case kops.LoadBalancerTypeInternal:
return errors.New("internal LoadBalancers are not yet supported by kops on ALI")
//loadbalancer.AddressType = s("intranet")
case kops.LoadBalancerTypePublic:
loadbalancer.AddressType = s("internet")
default:
return fmt.Errorf("unknown loadbalancer Type: %q", lbSpec.Type)
}

c.AddTask(loadbalancer)
}

// Create LoadBalancerListener for API ELB
// TODO: Health check
var loadbalancerlistener *alitasks.LoadBalancerListener
{
loadBalancerListenerStatus := LoadBalancerListenerStatus
loadBalancerListenerBandwidth := LoadBalancerListenerBandwidth
loadbalancerlistener = &alitasks.LoadBalancerListener{
Name: s("api." + b.ClusterName()),
Lifecycle: b.Lifecycle,
LoadBalancer: loadbalancer,
ListenerStatus: s(loadBalancerListenerStatus),
ListenerPort: i(443),
BackendServerPort: i(443),
Bandwidth: i(loadBalancerListenerBandwidth),
}
c.AddTask(loadbalancerlistener)
}

// Create LoadBalancerWhiteList for API ELB
var loadbalancerwhiteList *alitasks.LoadBalancerWhiteList
{

sourceItems := ""
var cidrs []string
for _, cidr := range b.Cluster.Spec.KubernetesAPIAccess {
if cidr != "0.0.0.0" && cidr != "0.0.0.0/0" {
cidrs = append(cidrs, cidr)
}
}
sourceItems = strings.Join(cidrs, ",")

loadbalancerwhiteList = &alitasks.LoadBalancerWhiteList{
Name: s("api." + b.ClusterName()),
Lifecycle: b.Lifecycle,
LoadBalancer: loadbalancer,
LoadBalancerListener: loadbalancerlistener,
SourceItems: s(sourceItems),
}
c.AddTask(loadbalancerwhiteList)

}

// Temporarily do not know the role of the following function
if dns.IsGossipHostname(b.Cluster.Name) || b.UsePrivateDNS() {
// Ensure the ELB hostname 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 errors.New("keypair/master task not found")
}
masterKeypair := masterKeypairTask.(*fitasks.Keypair)
masterKeypair.AlternateNameTasks = append(masterKeypair.AlternateNameTasks, loadbalancer)
}

return nil

}
4 changes: 4 additions & 0 deletions pkg/model/alimodel/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ func (c *ALIModelContext) GetNameForVPC() string {
func (c *ALIModelContext) GetNameForVSwitch(subnetName string) string {
return subnetName + "." + c.ClusterName()
}

func (c *ALIModelContext) GetNameForLoadBalancer() string {
return "api." + c.ClusterName()
}
7 changes: 7 additions & 0 deletions upup/pkg/fi/cloudup/alitasks/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ go_library(
srcs = [
"disk.go",
"disk_fitask.go",
"loadbalancer.go",
"loadbalancer_fitask.go",
"loadbalancerlistener.go",
"loadbalancerlistener_fitask.go",
"loadbalancerwhitelist.go",
"loadbalancerwhitelist_fitask.go",
"sshkey.go",
"sshkey_fitask.go",
"vpc.go",
Expand All @@ -20,6 +26,7 @@ go_library(
"//upup/pkg/fi/cloudup/terraform:go_default_library",
"//vendor/github.com/denverdino/aliyungo/common:go_default_library",
"//vendor/github.com/denverdino/aliyungo/ecs:go_default_library",
"//vendor/github.com/denverdino/aliyungo/slb:go_default_library",
"//vendor/github.com/golang/glog:go_default_library",
],
)
Loading