forked from kubermatic/kubermatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud_controller.go
253 lines (221 loc) · 9.03 KB
/
cloud_controller.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
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 cloud
import (
"context"
"fmt"
"reflect"
"time"
"go.uber.org/zap"
kubermaticapiv1 "github.com/kubermatic/kubermatic/api/pkg/api/v1"
kubermaticv1 "github.com/kubermatic/kubermatic/api/pkg/crd/kubermatic/v1"
kubermaticv1helper "github.com/kubermatic/kubermatic/api/pkg/crd/kubermatic/v1/helper"
"github.com/kubermatic/kubermatic/api/pkg/provider"
"github.com/kubermatic/kubermatic/api/pkg/provider/cloud"
"github.com/kubermatic/kubermatic/api/pkg/provider/cloud/aws"
"github.com/kubermatic/kubermatic/api/pkg/provider/cloud/azure"
"github.com/kubermatic/kubermatic/api/pkg/provider/cloud/openstack"
providerconfig "github.com/kubermatic/machine-controller/pkg/providerconfig/types"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)
const (
ControllerName = "kubermatic_cloud_controller"
// icmpMigrationRevision is the migration revision that will be set on the cluster after its
// security group was migrated to contain allow rules for ICMP
icmpMigrationRevision = 1
// awsHarcodedAZMigrationRevision is the migration revision for moving AWS clusters away from
// hardcoded AZs and Subnets towards multi-AZ support.
awsHarcodedAZMigrationRevision = 2
// currentMigrationRevision describes the current migration revision. If this is set on the
// cluster, certain migrations wont get executed. This must never be decremented.
CurrentMigrationRevision = awsHarcodedAZMigrationRevision
)
// Check if the Reconciler fullfills the interface
// at compile time
var _ reconcile.Reconciler = &Reconciler{}
type Reconciler struct {
client.Client
log *zap.SugaredLogger
recorder record.EventRecorder
seedGetter provider.SeedGetter
workerName string
}
func Add(
mgr manager.Manager,
log *zap.SugaredLogger,
numWorkers int,
seedGetter provider.SeedGetter,
workerName string,
) error {
reconciler := &Reconciler{
Client: mgr.GetClient(),
log: log.Named(ControllerName),
recorder: mgr.GetEventRecorderFor(ControllerName),
seedGetter: seedGetter,
workerName: workerName,
}
c, err := controller.New(ControllerName, mgr, controller.Options{Reconciler: reconciler, MaxConcurrentReconciles: numWorkers})
if err != nil {
return err
}
return c.Watch(&source.Kind{Type: &kubermaticv1.Cluster{}}, &handler.EnqueueRequestForObject{})
}
func (r *Reconciler) Reconcile(request reconcile.Request) (reconcile.Result, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
log := r.log.With("request", request)
log.Debug("Processing")
cluster := &kubermaticv1.Cluster{}
if err := r.Get(ctx, request.NamespacedName, cluster); err != nil {
if kerrors.IsNotFound(err) {
return reconcile.Result{}, nil
}
return reconcile.Result{}, err
}
log = log.With("cluster", cluster.Name)
// Add a wrapping here so we can emit an event on error
result, err := kubermaticv1helper.ClusterReconcileWrapper(
ctx,
r.Client,
r.workerName,
cluster,
kubermaticv1.ClusterConditionCloudControllerReconcilingSuccess,
func() (*reconcile.Result, error) {
return r.reconcile(ctx, log, cluster)
},
)
if result == nil {
result = &reconcile.Result{}
}
if err != nil {
r.recorder.Event(cluster, corev1.EventTypeWarning, "ReconcilingError", err.Error())
log.Errorw("Reconciling failed", zap.Error(err))
}
return *result, err
}
func (r *Reconciler) reconcile(ctx context.Context, log *zap.SugaredLogger, cluster *kubermaticv1.Cluster) (*reconcile.Result, error) {
seed, err := r.seedGetter()
if err != nil {
return nil, fmt.Errorf("failed to get seeds: %v", err)
}
datacenter, found := seed.Spec.Datacenters[cluster.Spec.Cloud.DatacenterName]
if !found {
return nil, fmt.Errorf("couldn't find datacenter %q for cluster %q", cluster.Spec.Cloud.DatacenterName, cluster.Name)
}
prov, err := cloud.Provider(datacenter.DeepCopy(), r.getGlobalSecretKeySelectorValue)
if err != nil {
return nil, fmt.Errorf("failed to create cloud provider: %v", err)
}
if cluster.DeletionTimestamp != nil {
log.Debug("Cleaning up cloud provider")
finalizers := sets.NewString(cluster.Finalizers...)
if finalizers.Has(kubermaticapiv1.InClusterLBCleanupFinalizer) ||
finalizers.Has(kubermaticapiv1.InClusterPVCleanupFinalizer) ||
finalizers.Has(kubermaticapiv1.NodeDeletionFinalizer) {
return &reconcile.Result{RequeueAfter: 5 * time.Second}, nil
}
if _, err := prov.CleanUpCloudProvider(cluster, r.updateCluster); err != nil {
return nil, fmt.Errorf("failed cloud provider cleanup: %v", err)
}
return nil, nil
}
// We do the migration inside the controller because it has a decent potential to fail (e.G. due
// to invalid credentials) and may take some time and we do not want to block the startup just
// because one cluster can not be migrated
if cluster.Status.CloudMigrationRevision < icmpMigrationRevision {
if err := r.migrateICMP(ctx, log, cluster, prov); err != nil {
return nil, err
}
}
if cluster.Status.CloudMigrationRevision < awsHarcodedAZMigrationRevision {
if err := r.migrateAWSMultiAZ(ctx, cluster, prov); err != nil {
return nil, err
}
}
if _, err := prov.InitializeCloudProvider(cluster, r.updateCluster); err != nil {
return nil, fmt.Errorf("failed cloud provider init: %v", err)
}
if _, err := r.updateCluster(cluster.Name, func(c *kubermaticv1.Cluster) {
c.Status.ExtendedHealth.CloudProviderInfrastructure = kubermaticv1.HealthStatusUp
}); err != nil {
return nil, fmt.Errorf("failed to set cluster health: %v", err)
}
return nil, nil
}
func (r *Reconciler) migrateICMP(ctx context.Context, log *zap.SugaredLogger, cluster *kubermaticv1.Cluster, cloudProvider provider.CloudProvider) error {
switch prov := cloudProvider.(type) {
case *aws.AmazonEC2:
if err := prov.AddICMPRulesIfRequired(cluster); err != nil {
return fmt.Errorf("failed to ensure ICMP rules for cluster %q: %v", cluster.Name, err)
}
log.Info("Successfully ensured ICMP rules in security group of cluster")
case *openstack.Provider:
if err := prov.AddICMPRulesIfRequired(cluster); err != nil {
return fmt.Errorf("failed to ensure ICMP rules for cluster %q: %v", cluster.Name, err)
}
log.Info("Successfully ensured ICMP rules in security group of cluster")
case *azure.Azure:
if err := prov.AddICMPRulesIfRequired(cluster); err != nil {
return fmt.Errorf("failed to ensure ICMP rules for cluster %q: %v", cluster.Name, err)
}
log.Info("Successfully ensured ICMP rules in security group of cluster %q", cluster.Name)
}
var err error
if cluster, err = r.updateCluster(cluster.Name, func(c *kubermaticv1.Cluster) {
c.Status.CloudMigrationRevision = icmpMigrationRevision
}); err != nil {
return fmt.Errorf("failed to update cluster %q after successfully executing its cloudProvider migration: %v",
cluster.Name, err)
}
return nil
}
func (r *Reconciler) migrateAWSMultiAZ(ctx context.Context, cluster *kubermaticv1.Cluster, cloudProvider provider.CloudProvider) error {
if prov, ok := cloudProvider.(*aws.AmazonEC2); ok {
if err := prov.MigrateToMultiAZ(cluster, r.updateCluster); err != nil {
return fmt.Errorf("failed to migrate AWS cluster %q to multi-AZ: %q", cluster.Name, err)
}
}
var err error
if cluster, err = r.updateCluster(cluster.Name, func(c *kubermaticv1.Cluster) {
c.Status.CloudMigrationRevision = awsHarcodedAZMigrationRevision
}); err != nil {
return fmt.Errorf("failed to update cluster %q after successfully executing its cloudProvider migration: %v",
cluster.Name, err)
}
return nil
}
func (r *Reconciler) updateCluster(name string, modify func(*kubermaticv1.Cluster)) (*kubermaticv1.Cluster, error) {
cluster := &kubermaticv1.Cluster{}
if err := r.Get(context.Background(), types.NamespacedName{Name: name}, cluster); err != nil {
return nil, err
}
oldCluster := cluster.DeepCopy()
modify(cluster)
if reflect.DeepEqual(oldCluster, cluster) {
return cluster, nil
}
return cluster, r.Patch(context.Background(), cluster, client.MergeFrom(oldCluster))
}
func (r *Reconciler) getGlobalSecretKeySelectorValue(configVar *providerconfig.GlobalSecretKeySelector, key string) (string, error) {
return provider.SecretKeySelectorValueFuncFactory(context.Background(), r.Client)(configVar, key)
}