-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathcluster.go
139 lines (115 loc) · 3.67 KB
/
cluster.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
package common
import (
"context"
"github.com/caicloud/nirvana/log"
core_v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/util/retry"
"github.com/caicloud/cyclone/pkg/meta"
api "github.com/caicloud/cyclone/pkg/server/apis/v1alpha1"
)
// CreateNamespace creates a namespace
func CreateNamespace(tenant string, client *kubernetes.Clientset) error {
namespace := buildNamespace(tenant)
_, err := client.CoreV1().Namespaces().Create(context.TODO(), namespace, meta_v1.CreateOptions{})
if err != nil {
if errors.IsAlreadyExists(err) {
log.Infof("namespace %s already exists", namespace.Name)
return nil
}
log.Errorf("Create namespace %s error %v", namespace.Name, err)
return err
}
return nil
}
func buildNamespace(tenant string) *core_v1.Namespace {
nsname := TenantNamespace(tenant)
return &core_v1.Namespace{
ObjectMeta: meta_v1.ObjectMeta{
Name: nsname,
Labels: map[string]string{
meta.LabelTenantName: tenant,
},
},
}
}
// CreateResourceQuota creates resource quota for tenant
func CreateResourceQuota(tenant *api.Tenant, namespace string, client *kubernetes.Clientset) error {
nsname := TenantNamespace(tenant.Name)
if namespace != "" {
nsname = namespace
}
quota, err := buildResourceQuota(tenant, nsname)
if err != nil {
log.Warningf("Build resource quota for tenant %s error %v", tenant.Name, err)
return err
}
_, err = client.CoreV1().ResourceQuotas(nsname).Create(context.TODO(), quota, meta_v1.CreateOptions{})
if err != nil {
log.Errorf("Create ResourceQuota for tenant %s error %v", tenant.Name, err)
return err
}
return nil
}
func buildResourceQuota(tenant *api.Tenant, namespace string) (*core_v1.ResourceQuota, error) {
// parse resource list
rl, err := ParseResourceList(tenant.Spec.ResourceQuota)
if err != nil {
log.Warningf("Parse resource quota for tenant %s error %v", tenant.Name, err)
return nil, err
}
quotaName := ResourceQuotaName(namespace)
quota := &core_v1.ResourceQuota{
ObjectMeta: meta_v1.ObjectMeta{
Name: quotaName,
},
Spec: core_v1.ResourceQuotaSpec{
Hard: rl,
},
}
return quota, nil
}
// UpdateResourceQuota updates resource quota for tenant
func UpdateResourceQuota(tenant *api.Tenant, namespace string, client *kubernetes.Clientset) error {
nsname := TenantNamespace(tenant.Name)
if namespace != "" {
nsname = namespace
}
// parse resource list
rl, err := ParseResourceList(tenant.Spec.ResourceQuota)
if err != nil {
log.Warningf("Parse resource quota for tenant %s error %v", tenant.Name, err)
return err
}
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
quota, err := client.CoreV1().ResourceQuotas(nsname).Get(
context.TODO(), ResourceQuotaName(nsname), meta_v1.GetOptions{})
if err != nil {
log.Errorf("Get ResourceQuota for tenant %s error %v", tenant.Name, err)
return err
}
quota.Spec.Hard = rl
_, err = client.CoreV1().ResourceQuotas(nsname).Update(context.TODO(), quota, meta_v1.UpdateOptions{})
if err != nil {
log.Errorf("Update ResourceQuota for tenant %s error %v", tenant.Name, err)
return err
}
return nil
})
}
// ParseResourceList parse resouces from 'map[string]string' to 'ResourceList'
func ParseResourceList(resources map[core_v1.ResourceName]string) (map[core_v1.ResourceName]resource.Quantity, error) {
rl := make(map[core_v1.ResourceName]resource.Quantity)
for r, q := range resources {
quantity, err := resource.ParseQuantity(q)
if err != nil {
log.Errorf("Parse %s Quantity %s error %v", r, q, err)
return nil, err
}
rl[r] = quantity
}
return rl, nil
}