-
Notifications
You must be signed in to change notification settings - Fork 168
/
tenant.go
293 lines (251 loc) · 8.27 KB
/
tenant.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package v1alpha1
import (
"context"
"reflect"
"sort"
"github.com/caicloud/nirvana/log"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/caicloud/cyclone/pkg/common"
"github.com/caicloud/cyclone/pkg/meta"
api "github.com/caicloud/cyclone/pkg/server/apis/v1alpha1"
"github.com/caicloud/cyclone/pkg/server/biz/integration/cluster"
"github.com/caicloud/cyclone/pkg/server/biz/pvc"
"github.com/caicloud/cyclone/pkg/server/biz/tenant"
svrcommon "github.com/caicloud/cyclone/pkg/server/common"
"github.com/caicloud/cyclone/pkg/server/config"
"github.com/caicloud/cyclone/pkg/server/handler"
"github.com/caicloud/cyclone/pkg/server/handler/v1alpha1/sorter"
"github.com/caicloud/cyclone/pkg/server/types"
"github.com/caicloud/cyclone/pkg/util/cerr"
)
// CreateTenant creates a cyclone tenant
func CreateTenant(ctx context.Context, tenant *api.Tenant) (*api.Tenant, error) {
modifiers := []CreationModifier{GenerateNameModifier, TenantModifier}
for _, modifier := range modifiers {
err := modifier("", "", "", tenant)
if err != nil {
return nil, err
}
}
return tenant, createTenant(tenant)
}
// ListTenants list all tenants' information
func ListTenants(ctx context.Context, query *types.QueryParams) (*types.ListResponse, error) {
namespaces, err := handler.K8sClient.CoreV1().Namespaces().List(meta_v1.ListOptions{
LabelSelector: meta.LabelExistsSelector(meta.LabelTenantName),
})
if err != nil {
log.Errorf("List cyclone namespace error %v", err)
return nil, cerr.ConvertK8sError(err)
}
items := namespaces.Items
if query.Sort {
sort.Sort(sorter.NewNamespaceSorter(items, query.Ascending))
}
var tenants []api.Tenant
for _, namespace := range items {
t, err := tenant.FromNamespace(&namespace)
if err != nil {
log.Errorf("Unmarshal tenant annotation error %v", err)
continue
}
tenants = append(tenants, *t)
}
size := int64(len(tenants))
if query.Start >= size {
return types.NewListResponse(int(size), []api.Tenant{}), nil
}
end := query.Start + query.Limit
if end > size {
end = size
}
return types.NewListResponse(int(size), tenants[query.Start:end]), nil
}
// GetTenant gets information for a specific tenant
func GetTenant(ctx context.Context, name string) (*api.Tenant, error) {
return tenant.Get(handler.K8sClient, name)
}
// UpdateTenant updates information for a specific tenant
func UpdateTenant(ctx context.Context, name string, newTenant *api.Tenant) (*api.Tenant, error) {
// Get old tenant
t, err := tenant.Get(handler.K8sClient, name)
if err != nil {
log.Errorf("get old tenant %s error %v", name, err)
return nil, err
}
integrations := []api.Integration{}
// Update resource quota if necessary
if !reflect.DeepEqual(t.Spec.ResourceQuota, newTenant.Spec.ResourceQuota) {
integrations, err = cluster.GetSchedulableClusters(handler.K8sClient, name)
if err != nil {
return nil, err
}
for _, integration := range integrations {
cluster := integration.Spec.Cluster
if cluster == nil {
log.Warningf("cluster of integration %s is nil", integration.Name)
continue
}
if !cluster.IsWorkerCluster {
log.Infof("%s is not worker cluster, skip", integration.Name)
continue
}
client, err := common.NewClusterClient(&cluster.Credential, cluster.IsControlCluster)
if err != nil {
log.Warningf("new cluster client for integration %s error %v", integration.Name, err)
continue
}
err = svrcommon.UpdateResourceQuota(newTenant, cluster.Namespace, client)
if err != nil {
log.Errorf("update resource quota for tenant %s error %v", name, err)
return nil, err
}
}
}
// Update pvc if necessary
if !reflect.DeepEqual(t.Spec.PersistentVolumeClaim, newTenant.Spec.PersistentVolumeClaim) {
if len(integrations) == 0 {
integrations, err = cluster.GetSchedulableClusters(handler.K8sClient, name)
if err != nil {
return nil, err
}
}
for _, integration := range integrations {
cluster := integration.Spec.Cluster
if cluster == nil {
log.Warningf("cluster of integration %s is nil", integration.Name)
continue
}
if !cluster.IsWorkerCluster {
log.Infof("%s is not worker cluster, skip", integration.Name)
continue
}
client, err := common.NewClusterClient(&cluster.Credential, cluster.IsControlCluster)
if err != nil {
log.Warningf("new cluster client for integration %s error %v", integration.Name, err)
continue
}
log.Infof("old PersistentVolumeClaim: %+v ", t.Spec.PersistentVolumeClaim)
log.Infof("new PersistentVolumeClaim: %+v ", newTenant.Spec.PersistentVolumeClaim)
newPVC := newTenant.Spec.PersistentVolumeClaim
err = pvc.UpdatePVC(t.Name, newPVC.StorageClass, newPVC.Size, cluster.Namespace, client)
if err != nil {
log.Errorf("update pvc for tenant %s error %v", name, err)
return nil, err
}
}
}
// Update namespace
err = tenant.UpdateNamespace(handler.K8sClient, newTenant)
if err != nil {
log.Errorf("Update namespace for tenant %s error %v", name, err)
return nil, err
}
return newTenant, nil
}
// DeleteTenant deletes a tenant
func DeleteTenant(ctx context.Context, name string) error {
// close workload cluster
integrations, err := cluster.GetSchedulableClusters(handler.K8sClient, name)
if err != nil {
log.Errorf("get workload clusters for tenant %s error %v", name, err)
return err
}
for _, integration := range integrations {
if integration.Spec.Cluster == nil {
log.Warningf("cluster of integration %s is nil", integration.Name)
continue
}
err := cluster.Close(handler.K8sClient, &integration, name)
if err != nil {
log.Warningf("close cluster %s for tenant %s error %v", integration.Name, name, err)
continue
}
}
err = deleteCollections(name)
if err != nil {
return err
}
err = handler.K8sClient.CoreV1().Namespaces().Delete(svrcommon.TenantNamespace(name), &meta_v1.DeleteOptions{})
if err != nil {
log.Errorf("Delete namespace for tenant %s error %v", name, err)
return cerr.ConvertK8sError(err)
}
return nil
}
// CreateDefaultTenant creates cyclone default tenant and initialize the tenant:
// - Create namespace
// - Create PVC
func CreateDefaultTenant() error {
ns := svrcommon.TenantNamespace(svrcommon.DefaultTenant)
_, err := handler.K8sClient.CoreV1().Namespaces().Get(ns, meta_v1.GetOptions{})
if err == nil {
log.Infof("Default namespace %s already exist", ns)
return nil
}
annotations := make(map[string]string)
annotations[meta.AnnotationDescription] = "This is the cyclone default tenant."
annotations[meta.AnnotationAlias] = svrcommon.DefaultTenant
tenant := &api.Tenant{
ObjectMeta: meta_v1.ObjectMeta{
Name: svrcommon.DefaultTenant,
Annotations: annotations,
},
Spec: api.TenantSpec{
PersistentVolumeClaim: api.PersistentVolumeClaim{
Size: config.Config.DefaultPVCConfig.Size,
},
ResourceQuota: config.Config.WorkerNamespaceQuota,
},
}
if config.Config.DefaultPVCConfig.StorageClass != "" {
tenant.Spec.PersistentVolumeClaim.StorageClass = config.Config.DefaultPVCConfig.StorageClass
}
return createTenant(tenant)
}
func createControlClusterIntegration(tenant string) error {
annotations := make(map[string]string)
annotations[meta.AnnotationDescription] = "This cluster is integrated by cyclone while creating tenant."
annotations[meta.AnnotationAlias] = common.ControlClusterName
in := &api.Integration{
ObjectMeta: meta_v1.ObjectMeta{
Name: common.ControlClusterName,
Annotations: annotations,
},
Spec: api.IntegrationSpec{
Type: api.Cluster,
IntegrationSource: api.IntegrationSource{
Cluster: &api.ClusterSource{
IsControlCluster: true,
// Cluster by default is not enabled to run workflow, need users to enable it explicitly.
IsWorkerCluster: false,
Namespace: svrcommon.TenantNamespace(tenant),
},
},
},
}
in, err := createIntegration(tenant, false, in)
if err != nil {
return cerr.ErrorCreateIntegration.Error(err)
}
if config.Config.OpenControlCluster {
err := cluster.Open(handler.K8sClient, in, tenant)
if err != nil {
return err
}
}
return nil
}
func createTenant(t *api.Tenant) error {
// create namespace
err := tenant.CreateNamespace(handler.K8sClient, t)
if err != nil {
return err
}
// create cluster integration for control cluster
err = createControlClusterIntegration(t.Name)
if err != nil {
return err
}
return nil
}