-
Notifications
You must be signed in to change notification settings - Fork 153
/
namespace.go
63 lines (48 loc) · 1.45 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
package server
import (
"context"
"errors"
"github.com/weaveworks/weave-gitops/core/server/types"
pb "github.com/weaveworks/weave-gitops/pkg/api/core"
corev1 "k8s.io/api/core/v1"
)
const (
fluxNamespacePartOf = "flux"
fluxNamespaceInstance = "flux-system"
)
var ErrNamespaceNotFound = errors.New("namespace not found")
func (as *coreServer) GetFluxNamespace(ctx context.Context, msg *pb.GetFluxNamespaceRequest) (*pb.GetFluxNamespaceResponse, error) {
k8s, err := as.k8s.Client(ctx)
if err != nil {
return nil, doClientError(err)
}
nsList := corev1.NamespaceList{}
options := matchLabel(
withPartOfLabel(fluxNamespacePartOf),
withInstanceLabel(fluxNamespaceInstance),
)
if err = k8s.List(ctx, &nsList, &options); err != nil {
return nil, doClientError(err)
}
if len(nsList.Items) == 0 {
return nil, ErrNamespaceNotFound
}
return &pb.GetFluxNamespaceResponse{Name: nsList.Items[0].Name}, nil
}
func (as *coreServer) ListNamespaces(ctx context.Context, msg *pb.ListNamespacesRequest) (*pb.ListNamespacesResponse, error) {
k8s, err := as.k8s.Client(ctx)
if err != nil {
return nil, doClientError(err)
}
nsList := corev1.NamespaceList{}
if err = k8s.List(ctx, &nsList); err != nil {
return nil, doClientError(err)
}
response := &pb.ListNamespacesResponse{
Namespaces: []*pb.Namespace{},
}
for _, ns := range nsList.Items {
response.Namespaces = append(response.Namespaces, types.NamespaceToProto(ns))
}
return response, nil
}