This repository has been archived by the owner on Apr 3, 2023. It is now read-only.
/
api_loadbalancer.go
144 lines (120 loc) · 4 KB
/
api_loadbalancer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
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"
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
}