/
namespace.go
130 lines (119 loc) · 3.61 KB
/
namespace.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
package kubernetes
import (
log "github.com/sirupsen/logrus"
api_core "k8s.io/api/core/v1"
api_resource "k8s.io/apimachinery/pkg/api/resource"
api_meta "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
quotaName = "quota"
)
//GetNamespaceList returns namespaces list
func (k *Kube) GetNamespaceList(owner string) (*api_core.NamespaceList, error) {
quotas, err := k.CoreV1().Namespaces().List(api_meta.ListOptions{
LabelSelector: getOwnerLabel(owner),
})
if err != nil {
log.WithField("Owner", owner).Error(err)
return nil, err
}
return quotas, nil
}
//GetNamespaceQuotaList returns namespaces (quotas) list
func (k *Kube) GetNamespaceQuotaList(owner string) (*api_core.ResourceQuotaList, error) {
quotas, err := k.CoreV1().ResourceQuotas("").List(api_meta.ListOptions{
LabelSelector: getOwnerLabel(owner),
})
if err != nil {
log.WithField("Owner", owner).Error(err)
return nil, err
}
return quotas, nil
}
//GetNamespace returns namespace
func (k *Kube) GetNamespace(nsName string) (*api_core.Namespace, error) {
ns, err := k.CoreV1().Namespaces().Get(nsName, api_meta.GetOptions{})
if err != nil {
log.WithField("Namespace", ns).Error(err)
return nil, err
}
return ns, nil
}
//GetNamespaceQuota returns namespace (quota)
func (k *Kube) GetNamespaceQuota(ns string) (*api_core.ResourceQuota, error) {
quota, err := k.CoreV1().ResourceQuotas(ns).Get(quotaName, api_meta.GetOptions{})
if err != nil {
log.WithField("Namespace", ns).Error(err)
return nil, err
}
return quota, nil
}
//CreateNamespace creates namespace
func (k *Kube) CreateNamespace(ns *api_core.Namespace) (*api_core.Namespace, error) {
nsAfter, err := k.CoreV1().Namespaces().Create(ns)
if err != nil {
log.WithField("Namespace", ns.Name).Error(err)
return nil, err
}
return nsAfter, nil
}
//CreateNamespaceQuota creates namespace quota
func (k *Kube) CreateNamespaceQuota(nsName string, quota *api_core.ResourceQuota) (*api_core.ResourceQuota, error) {
quotaAfter, err := k.CoreV1().ResourceQuotas(nsName).Create(quota)
if err != nil {
log.WithField("Namespace", nsName).Error(err)
return nil, err
}
return quotaAfter, nil
}
//CreateNamespaceQuota creates namespace quota
func (k *Kube) CreateLimitRange(nsName string) error {
_, err := k.CoreV1().LimitRanges(nsName).Create(&api_core.LimitRange{
TypeMeta: api_meta.TypeMeta{
Kind: "LimitRange",
APIVersion: "v1",
},
ObjectMeta: api_meta.ObjectMeta{
Name: "limitrange",
Namespace: nsName,
},
Spec: api_core.LimitRangeSpec{
Limits: []api_core.LimitRangeItem{
{
Type: api_core.LimitTypeContainer,
Default: api_core.ResourceList{
api_core.ResourceCPU: api_resource.MustParse("200m"),
api_core.ResourceMemory: api_resource.MustParse("256Mi"),
},
DefaultRequest: api_core.ResourceList{
api_core.ResourceCPU: api_resource.MustParse("100m"),
api_core.ResourceMemory: api_resource.MustParse("128Mi"),
},
},
},
},
})
if err != nil {
log.WithField("Namespace", nsName).Error(err)
return err
}
return nil
}
//UpdateNamespaceQuota updates namespace quota
func (k *Kube) UpdateNamespaceQuota(nsName string, quota *api_core.ResourceQuota) (*api_core.ResourceQuota, error) {
quotaAfter, err := k.CoreV1().ResourceQuotas(nsName).Update(quota)
if err != nil {
log.WithField("Namespace", nsName).Error(err)
return nil, err
}
return quotaAfter, nil
}
//DeleteNamespace deletes namespace
func (k *Kube) DeleteNamespace(nsName string) error {
err := k.CoreV1().Namespaces().Delete(nsName, &api_meta.DeleteOptions{})
if err != nil {
log.WithField("Namespace", nsName).Error(err)
return err
}
return nil
}