-
Notifications
You must be signed in to change notification settings - Fork 156
/
migrations.go
134 lines (107 loc) · 3.97 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
/*
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 kubermaticseed
import (
"context"
"fmt"
"github.com/sirupsen/logrus"
kubermaticv1 "k8c.io/kubermatic/v2/pkg/apis/kubermatic/v1"
"k8c.io/kubermatic/v2/pkg/cluster/client"
"k8c.io/kubermatic/v2/pkg/install/stack"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)
const (
cinderCSIDriverName = "cinder.csi.openstack.org"
logrotateAddonName = "logrotate"
)
func migrateOpenStackCSIDrivers(ctx context.Context, logger *logrus.Entry, kubeClient ctrlruntimeclient.Client, opt stack.DeployOptions) error {
userClusterClientProvider, err := client.NewExternal(kubeClient)
if err != nil {
return err
}
clusters := kubermaticv1.ClusterList{}
if err = kubeClient.List(ctx, &clusters); err != nil {
return err
}
for _, cluster := range clusters.Items {
if cluster.Spec.Cloud.Openstack == nil {
// at this point we are only looking for openstack clusters
continue
}
if cluster.Status.Conditions[kubermaticv1.ClusterConditionAddonControllerReconcilingSuccess].Status == corev1.ConditionTrue {
// cluster successfully reconciled addons, no need to migrate CSIDriver
continue
}
userClusterKubeClient, err := userClusterClientProvider.GetClient(ctx, &cluster)
if err != nil {
return fmt.Errorf("failed to get %q cluster client: %w", cluster.Name, err)
}
// delete named CSIDriver and let addons reconcile it back in current form
if err = deleteCSIDriver(ctx, userClusterKubeClient, cinderCSIDriverName); err != nil {
return fmt.Errorf("failed to delete CSIDriver in %q cluster: %w", cluster.Name, err)
}
}
return nil
}
func deleteCSIDriver(ctx context.Context, kubeClient ctrlruntimeclient.Client, name string) error {
drv := storagev1.CSIDriver{}
err := kubeClient.Get(ctx, ctrlruntimeclient.ObjectKey{Name: name}, &drv)
if apierrors.IsNotFound(err) {
return nil
}
if err != nil {
return err
}
return kubeClient.Delete(ctx, &drv)
}
func removeLogrotateAddons(ctx context.Context, logger *logrus.Entry, kubeClient ctrlruntimeclient.Client, opt stack.DeployOptions) error {
clusters := kubermaticv1.ClusterList{}
if err := kubeClient.List(ctx, &clusters); err != nil {
return err
}
// iterates over all user clusters
for _, cluster := range clusters.Items {
// delete the logrotate addon
if err := deleteLogrotateAddon(ctx, kubeClient, logrotateAddonName, cluster.Status.NamespaceName); err != nil {
return err
}
}
return nil
}
func deleteLogrotateAddon(ctx context.Context, kubeClient ctrlruntimeclient.Client, name, namespace string) error {
addon := kubermaticv1.Addon{}
err := kubeClient.Get(ctx, ctrlruntimeclient.ObjectKey{Name: name, Namespace: namespace}, &addon)
if apierrors.IsNotFound(err) {
return nil
}
if err != nil {
return err
}
return kubeClient.Delete(ctx, &addon)
}
func migrateUserClustersData(ctx context.Context, logger *logrus.Entry, kubeClient ctrlruntimeclient.Client, opt stack.DeployOptions) error {
if opt.EnableOpenstackCSIDriverMigration {
if err := migrateOpenStackCSIDrivers(ctx, logger, kubeClient, opt); err != nil {
return fmt.Errorf("failed to migrate OpenStack CSIDrivers: %w", err)
}
}
if opt.EnableLogrotateMigration {
if err := removeLogrotateAddons(ctx, logger, kubeClient, opt); err != nil {
return fmt.Errorf("failed to remove logrotate addons: %w", err)
}
}
return nil
}