forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clustercontroller.go
180 lines (157 loc) · 6.14 KB
/
clustercontroller.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package cluster
import (
"context"
"fmt"
"reflect"
errorsutil "github.com/pkg/errors"
"github.com/rancher/kontainer-engine/service"
"github.com/rancher/kontainer-engine/types"
"github.com/rancher/rancher/pkg/controllers/management/clusterprovisioner"
"github.com/rancher/rke/cloudprovider/aws"
"github.com/rancher/rke/cloudprovider/azure"
"github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
)
const (
GoogleCloudLoadBalancer = "GCLB"
ElasticLoadBalancer = "ELB"
AzureL4LB = "Azure L4 LB"
NginxIngressProvider = "Nginx"
DefaultNodePortRange = "30000-32767"
)
type controller struct {
clusterClient v3.ClusterInterface
clusterLister v3.ClusterLister
nodeLister v3.NodeLister
kontainerDriverLister v3.KontainerDriverLister
namespaces v1.NamespaceInterface
coreV1 v1.Interface
}
func Register(ctx context.Context, management *config.ManagementContext) {
c := controller{
clusterClient: management.Management.Clusters(""),
clusterLister: management.Management.Clusters("").Controller().Lister(),
nodeLister: management.Management.Nodes("").Controller().Lister(),
kontainerDriverLister: management.Management.KontainerDrivers("").Controller().Lister(),
namespaces: management.Core.Namespaces(""),
coreV1: management.Core,
}
c.clusterClient.AddHandler(ctx, "clusterCreateUpdate", c.capsSync)
}
func (c *controller) capsSync(key string, cluster *v3.Cluster) (runtime.Object, error) {
var err error
if cluster == nil || cluster.DeletionTimestamp != nil {
return nil, nil
}
if cluster.Spec.ImportedConfig != nil {
return nil, nil
}
capabilities := v3.Capabilities{}
capabilities.NodePortRange = DefaultNodePortRange
if cluster.Spec.RancherKubernetesEngineConfig != nil {
if capabilities, err = c.RKECapabilities(capabilities, *cluster.Spec.RancherKubernetesEngineConfig, cluster.Name); err != nil {
return nil, err
}
} else if cluster.Spec.GenericEngineConfig != nil {
driverName, ok := (*cluster.Spec.GenericEngineConfig)["driverName"].(string)
if !ok {
logrus.Warnf("cluster %v had generic engine config but no driver name, k8s capabilities will "+
"not be populated correctly", key)
return nil, nil
}
kontainerDriver, err := c.kontainerDriverLister.Get("", driverName)
if err != nil {
if !errors.IsNotFound(err) {
return nil, errorsutil.WithMessage(err, fmt.Sprintf("error getting kontainer driver: %v", driverName))
}
//do not return not found errors since the driver may have been deleted
return nil, nil
}
driver := service.NewEngineService(
clusterprovisioner.NewPersistentStore(c.namespaces, c.coreV1),
)
k8sCapabilities, err := driver.GetK8sCapabilities(context.Background(), kontainerDriver.Name, kontainerDriver,
cluster.Spec)
if err != nil {
return nil, fmt.Errorf("error getting k8s capabilities: %v", err)
}
capabilities = toCapabilities(k8sCapabilities)
} else {
return nil, nil
}
if !reflect.DeepEqual(capabilities, cluster.Status.Capabilities) {
toUpdateCluster := cluster.DeepCopy()
toUpdateCluster.Status.Capabilities = capabilities
if _, err := c.clusterClient.Update(toUpdateCluster); err != nil {
return nil, err
}
}
return nil, nil
}
func (c *controller) RKECapabilities(capabilities v3.Capabilities, rkeConfig v3.RancherKubernetesEngineConfig, clusterName string) (v3.Capabilities, error) {
switch rkeConfig.CloudProvider.Name {
case aws.AWSCloudProviderName:
capabilities.LoadBalancerCapabilities = c.L4Capability(true, ElasticLoadBalancer, []string{"TCP"}, true)
case azure.AzureCloudProviderName:
capabilities.LoadBalancerCapabilities = c.L4Capability(true, AzureL4LB, []string{"TCP", "UDP"}, true)
default:
capabilities.LoadBalancerCapabilities = c.L4Capability(false, "", []string{}, false)
}
// only if not custom, non custom clusters have nodepools set
nodes, err := c.nodeLister.List(clusterName, labels.Everything())
if err != nil {
return capabilities, err
}
if len(nodes) > 0 {
if nodes[0].Spec.NodePoolName != "" {
capabilities.NodePoolScalingSupported = true
}
}
ingressController := c.IngressCapability(true, NginxIngressProvider, false)
capabilities.IngressCapabilities = []v3.IngressCapabilities{ingressController}
if rkeConfig.Services.KubeAPI.ExtraArgs["service-node-port-range"] != "" {
capabilities.NodePortRange = rkeConfig.Services.KubeAPI.ExtraArgs["service-node-port-range"]
}
return capabilities, nil
}
func (c *controller) L4Capability(enabled bool, providerName string, protocols []string, healthCheck bool) v3.LoadBalancerCapabilities {
l4lb := v3.LoadBalancerCapabilities{
Enabled: &enabled,
Provider: providerName,
ProtocolsSupported: protocols,
HealthCheckSupported: healthCheck,
}
return l4lb
}
func (c *controller) IngressCapability(httpLBEnabled bool, providerName string, customDefaultBackend bool) v3.IngressCapabilities {
ing := v3.IngressCapabilities{
IngressProvider: providerName,
CustomDefaultBackend: customDefaultBackend,
}
return ing
}
func toCapabilities(k8sCapabilities *types.K8SCapabilities) v3.Capabilities {
var controllers []v3.IngressCapabilities
for _, controller := range k8sCapabilities.IngressControllers {
controllers = append(controllers, v3.IngressCapabilities{
CustomDefaultBackend: controller.CustomDefaultBackend,
IngressProvider: controller.IngressProvider,
})
}
return v3.Capabilities{
IngressCapabilities: controllers,
LoadBalancerCapabilities: v3.LoadBalancerCapabilities{
Enabled: &k8sCapabilities.L4LoadBalancer.Enabled,
HealthCheckSupported: k8sCapabilities.L4LoadBalancer.HealthCheckSupported,
ProtocolsSupported: k8sCapabilities.L4LoadBalancer.ProtocolsSupported,
Provider: k8sCapabilities.L4LoadBalancer.Provider,
},
NodePoolScalingSupported: k8sCapabilities.NodePoolScalingSupported,
NodePortRange: k8sCapabilities.NodePortRange,
}
}