/
migrations.go
248 lines (209 loc) · 7.56 KB
/
migrations.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
/*
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 seed
import (
"context"
"fmt"
"sync"
apiv1 "github.com/kubermatic/kubermatic/api/pkg/api/v1"
kubermaticv1 "github.com/kubermatic/kubermatic/api/pkg/crd/kubermatic/v1"
kuberneteshelper "github.com/kubermatic/kubermatic/api/pkg/kubernetes"
kubernetesprovider "github.com/kubermatic/kubermatic/api/pkg/provider/kubernetes"
"github.com/kubermatic/kubermatic/api/pkg/resources"
"github.com/kubermatic/kubermatic/api/pkg/util/workerlabel"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/rest"
"k8s.io/klog"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)
type cleanupContext struct {
client ctrlruntimeclient.Client
ctx context.Context
}
// ClusterTask represents a cleanup action, taking the current cluster for which the cleanup should be executed and the current context.
// In case of an error, the correspondent error will be returned, else nil.
type ClusterTask func(cluster *kubermaticv1.Cluster, ctx *cleanupContext) error
// RunAll runs all migrations
func RunAll(ctx context.Context, config *rest.Config, workerName string) error {
client, err := ctrlruntimeclient.New(config, ctrlruntimeclient.Options{})
if err != nil {
return fmt.Errorf("failed to create client: %v", err)
}
cleanupContext := &cleanupContext{
client: client,
ctx: ctx,
}
if err := cleanupClusters(workerName, cleanupContext); err != nil {
return fmt.Errorf("failed to cleanup clusters: %v", err)
}
return nil
}
func cleanupClusters(workerName string, ctx *cleanupContext) error {
selector, err := workerlabel.LabelSelector(workerName)
if err != nil {
return err
}
clusters := &kubermaticv1.ClusterList{}
if err := ctx.client.List(ctx.ctx, clusters, ctrlruntimeclient.MatchingLabelsSelector{Selector: selector}); err != nil {
return fmt.Errorf("failed to list clusters: %v", err)
}
var errs []error
errLock := &sync.Mutex{}
w := sync.WaitGroup{}
w.Add(len(clusters.Items))
for i := range clusters.Items {
go func(c *kubermaticv1.Cluster) {
defer w.Done()
if err := cleanupCluster(c, ctx); err != nil {
err = fmt.Errorf("failed to cleanup cluster %q: %v", c.Name, err)
errLock.Lock()
defer errLock.Unlock()
errs = append(errs, err)
}
}(&clusters.Items[i])
}
w.Wait()
if len(errs) > 0 {
return fmt.Errorf("%v", errs)
}
return nil
}
func cleanupCluster(cluster *kubermaticv1.Cluster, ctx *cleanupContext) error {
if cluster.Status.NamespaceName == "" {
klog.Infof("Skipping cleanup of cluster %q because its namespace is unset", cluster.Name)
return nil
}
klog.Infof("Cleaning up cluster %s", cluster.Name)
tasks := []ClusterTask{
setExposeStrategyIfEmpty,
setProxyModeIfEmpty,
cleanupDashboardAddon,
migrateClusterUserLabel,
createSecretsForCredentials,
}
w := sync.WaitGroup{}
w.Add(len(tasks))
var errs []error
errLock := &sync.Mutex{}
for _, task := range tasks {
go func(t ClusterTask) {
defer w.Done()
err := t(cluster, ctx)
if err != nil {
klog.Error(err)
errLock.Lock()
defer errLock.Unlock()
errs = append(errs, err)
}
}(task)
}
w.Wait()
if len(errs) > 0 {
return fmt.Errorf("%v", errs)
}
return nil
}
// We started to offer the option to expose the cluster via a LoadBalancer. We need to track
// the expose strategy that is being used for a cluster. If there is none set, we default to NodePort
// as that was initially the only option
func setExposeStrategyIfEmpty(cluster *kubermaticv1.Cluster, cleanupContext *cleanupContext) error {
if cluster.Spec.ExposeStrategy == "" {
cluster.Spec.ExposeStrategy = corev1.ServiceTypeNodePort
if err := cleanupContext.client.Update(cleanupContext.ctx, cluster); err != nil {
return fmt.Errorf("failed to default exposeStrategy to NodePort for cluster %q: %v", cluster.Name, err)
}
namespacedName := types.NamespacedName{Name: cluster.Name}
updatedCluster := &kubermaticv1.Cluster{}
if err := cleanupContext.client.Get(cleanupContext.ctx, namespacedName, updatedCluster); err != nil {
return fmt.Errorf("failed to get cluster %q: %v", cluster.Name, err)
}
*cluster = *updatedCluster
}
return nil
}
// We started to offer a config option for setting the kube-proxy mode.
// If there is none set, we default to iptables as that was initially the
// one being used.
func setProxyModeIfEmpty(cluster *kubermaticv1.Cluster, cleanupContext *cleanupContext) error {
if cluster.Spec.ClusterNetwork.ProxyMode == "" {
cluster.Spec.ClusterNetwork.ProxyMode = resources.IPTablesProxyMode
if err := cleanupContext.client.Update(cleanupContext.ctx, cluster); err != nil {
return fmt.Errorf("failed to default proxyMode to iptables for cluster %q: %v", cluster.Name, err)
}
namespacedName := types.NamespacedName{Name: cluster.Name}
updatedCluster := &kubermaticv1.Cluster{}
if err := cleanupContext.client.Get(cleanupContext.ctx, namespacedName, updatedCluster); err != nil {
return fmt.Errorf("failed to get cluster %q: %v", cluster.Name, err)
}
*cluster = *updatedCluster
}
return nil
}
func cleanupDashboardAddon(cluster *kubermaticv1.Cluster, cleanupContext *cleanupContext) error {
dashboardAddon := &kubermaticv1.Addon{
ObjectMeta: v1.ObjectMeta{
Namespace: cluster.Status.NamespaceName,
Name: "dashboard",
},
}
if err := cleanupContext.client.Delete(cleanupContext.ctx, dashboardAddon); err != nil {
if !kerrors.IsNotFound(err) {
return err
}
}
return nil
}
func migrateClusterUserLabel(cluster *kubermaticv1.Cluster, cleanupContext *cleanupContext) error {
// If there is not label - nothing to migrate
if cluster.Labels == nil {
return nil
}
newLabels := map[string]string{}
userLabelSet := sets.NewString("user", "user_RAW")
for key, value := range cluster.Labels {
if userLabelSet.Has(key) {
continue
}
newLabels[key] = value
}
if len(newLabels) != len(cluster.Labels) {
cluster.Labels = newLabels
if err := cleanupContext.client.Update(cleanupContext.ctx, cluster); err != nil {
return fmt.Errorf("failed to update cluster %q: %v", cluster.Name, err)
}
}
return nil
}
func createSecretsForCredentials(cluster *kubermaticv1.Cluster, cleanupContext *cleanupContext) error {
if cluster.GetDeletionTimestamp() != nil {
return nil
}
if err := kubernetesprovider.CreateOrUpdateCredentialSecretForCluster(cleanupContext.ctx, cleanupContext.client, cluster); err != nil {
return err
}
kuberneteshelper.AddFinalizer(cluster, apiv1.CredentialsSecretsCleanupFinalizer)
if err := cleanupContext.client.Update(cleanupContext.ctx, cluster); err != nil {
return fmt.Errorf("failed to update cluster %q: %v", cluster.Name, err)
}
namespacedName := types.NamespacedName{Name: cluster.Name}
updatedCluster := &kubermaticv1.Cluster{}
if err := cleanupContext.client.Get(cleanupContext.ctx, namespacedName, updatedCluster); err != nil {
return fmt.Errorf("failed to get cluster %q: %v", cluster.Name, err)
}
*cluster = *updatedCluster
return nil
}