-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
namespace.go
64 lines (46 loc) · 1.4 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
// Copyright 2020 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package kubernetes
import (
"context"
"strings"
"github.com/clivern/beetle/core/model"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// GetNamespaces gets a list of cluster namespaces
func (c *Cluster) GetNamespaces(ctx context.Context) ([]model.Namespace, error) {
result := []model.Namespace{}
err := c.Config()
if err != nil {
return result, err
}
data, err := c.ClientSet.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
if err != nil {
return result, err
}
for _, namespace := range data.Items {
result = append(result, model.Namespace{
Name: namespace.ObjectMeta.Name,
UID: string(namespace.ObjectMeta.UID),
Status: strings.ToLower(string(namespace.Status.Phase)),
})
}
return result, nil
}
// GetNamespace gets a namespace by name
func (c *Cluster) GetNamespace(ctx context.Context, name string) (model.Namespace, error) {
result := model.Namespace{}
err := c.Config()
if err != nil {
return result, err
}
namespace, err := c.ClientSet.CoreV1().Namespaces().Get(ctx, name, metav1.GetOptions{})
if err != nil {
return result, err
}
result.Name = namespace.ObjectMeta.Name
result.UID = string(namespace.ObjectMeta.UID)
result.Status = strings.ToLower(string(namespace.Status.Phase))
return result, nil
}