-
Notifications
You must be signed in to change notification settings - Fork 115
/
namespace.go
46 lines (39 loc) · 1.26 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
package k8s
import (
"context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sapiv1 "github.com/lyft/clutch/backend/api/k8s/v1"
)
func (s *svc) DescribeNamespace(ctx context.Context, clientset, cluster, name string) (*k8sapiv1.Namespace, error) {
cs, err := s.manager.GetK8sClientset(ctx, clientset, cluster, name)
if err != nil {
return nil, err
}
namespaces, err := cs.CoreV1().Namespaces().List(ctx, metav1.ListOptions{
FieldSelector: "metadata.name=" + name,
})
if err != nil {
return nil, err
}
if len(namespaces.Items) == 1 {
return ProtoForNamespace(cs.Cluster(), &namespaces.Items[0]), nil
} else if len(namespaces.Items) > 1 {
return nil, status.Error(codes.FailedPrecondition, "located multiple Namespaces")
}
return nil, status.Error(codes.NotFound, "unable to locate namespaces")
}
func ProtoForNamespace(cluster string, k8snamespace *corev1.Namespace) *k8sapiv1.Namespace {
clusterName := GetKubeClusterName(k8snamespace)
if clusterName == "" {
clusterName = cluster
}
return &k8sapiv1.Namespace{
Cluster: clusterName,
Name: k8snamespace.Name,
Labels: k8snamespace.Labels,
Annotations: k8snamespace.Annotations,
}
}