/
namespace_resolver.go
204 lines (171 loc) · 7.06 KB
/
namespace_resolver.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package k8s
import (
"context"
"github.com/golang/glog"
appPretty "github.com/kyma-project/kyma/components/console-backend-service/internal/domain/application/pretty"
"github.com/kyma-project/kyma/components/console-backend-service/internal/domain/k8s/listener"
"github.com/kyma-project/kyma/components/console-backend-service/internal/domain/k8s/pretty"
"github.com/kyma-project/kyma/components/console-backend-service/internal/domain/shared"
"github.com/kyma-project/kyma/components/console-backend-service/internal/gqlerror"
"github.com/kyma-project/kyma/components/console-backend-service/internal/gqlschema"
"github.com/kyma-project/kyma/components/console-backend-service/internal/module"
"github.com/kyma-project/kyma/components/console-backend-service/pkg/resource"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
)
//go:generate mockery -name=namespaceSvc -output=automock -outpkg=automock -case=underscore
type namespaceSvc interface {
Create(name string, labels gqlschema.Labels) (*v1.Namespace, error)
Update(name string, labels gqlschema.Labels) (*v1.Namespace, error)
List() ([]*v1.Namespace, error)
Find(name string) (*v1.Namespace, error)
Delete(name string) error
Subscribe(listener resource.Listener)
Unsubscribe(listener resource.Listener)
}
type namespaceResolver struct {
namespaceSvc namespaceSvc
appRetriever shared.ApplicationRetriever
namespaceConverter namespaceConverter
systemNamespaces []string
podService podSvc
}
func newNamespaceResolver(namespaceSvc namespaceSvc, appRetriever shared.ApplicationRetriever, systemNamespaces []string, podService podSvc) *namespaceResolver {
return &namespaceResolver{
namespaceSvc: namespaceSvc,
appRetriever: appRetriever,
namespaceConverter: *newNamespaceConverter(systemNamespaces),
systemNamespaces: systemNamespaces,
podService: podService,
}
}
func (r *namespaceResolver) NamespacesQuery(ctx context.Context, withSystemNamespaces *bool, withInactiveStatus *bool) ([]gqlschema.Namespace, error) {
namespaces, err := r.namespaceSvc.List()
if err != nil {
if module.IsDisabledModuleError(err) {
return nil, err
}
glog.Error(errors.Wrapf(err, "while listing %s", pretty.Namespaces))
return nil, gqlerror.New(err, pretty.Namespaces)
}
var filteredNamespaces []*v1.Namespace
for _, ns := range namespaces {
if r.checkNamespace(ns, withSystemNamespaces, withInactiveStatus) {
filteredNamespaces = append(filteredNamespaces, ns)
}
}
converted := r.namespaceConverter.ToGQLs(filteredNamespaces)
return converted, nil
}
func (r *namespaceResolver) ApplicationsField(ctx context.Context, obj *gqlschema.Namespace) ([]string, error) {
if obj == nil {
return nil, errors.New("Cannot get application field for namespace")
}
items, err := r.appRetriever.Application().ListInNamespace(obj.Name)
if err != nil {
if module.IsDisabledModuleError(err) {
return nil, err
}
return nil, errors.Wrapf(err, "while listing %s for namespace %s", appPretty.Application, obj.Name)
}
var appNames []string
for _, app := range items {
appNames = append(appNames, app.Name)
}
return appNames, nil
}
func (r *namespaceResolver) NamespaceQuery(ctx context.Context, name string) (*gqlschema.Namespace, error) {
namespace, err := r.namespaceSvc.Find(name)
if err != nil {
glog.Error(errors.Wrapf(err, "while getting %s with name %s", pretty.Namespace, name))
return nil, gqlerror.New(err, pretty.Namespace, gqlerror.WithName(name))
}
converted := r.namespaceConverter.ToGQL(namespace)
return converted, nil
}
func (r *namespaceResolver) CreateNamespace(ctx context.Context, name string, labels *gqlschema.Labels) (gqlschema.NamespaceMutationOutput, error) {
gqlLabels := r.populateLabels(labels)
ns, err := r.namespaceSvc.Create(name, gqlLabels)
if err != nil {
glog.Error(errors.Wrapf(err, "while creating %s `%s`", pretty.Namespace, name))
return gqlschema.NamespaceMutationOutput{}, gqlerror.New(err, pretty.Namespace, gqlerror.WithName(name))
}
return gqlschema.NamespaceMutationOutput{
Name: name,
Labels: ns.Labels,
}, nil
}
func (r *namespaceResolver) UpdateNamespace(ctx context.Context, name string, labels gqlschema.Labels) (gqlschema.NamespaceMutationOutput, error) {
gqlLabels := r.populateLabels(&labels)
ns, err := r.namespaceSvc.Update(name, gqlLabels)
if err != nil {
glog.Error(errors.Wrapf(err, "while editing %s `%s`", pretty.Namespace, name))
return gqlschema.NamespaceMutationOutput{}, gqlerror.New(err, pretty.Namespace, gqlerror.WithName(name))
}
return gqlschema.NamespaceMutationOutput{
Name: name,
Labels: ns.Labels,
}, nil
}
func (r *namespaceResolver) DeleteNamespace(ctx context.Context, name string) (*gqlschema.Namespace, error) {
namespace, err := r.namespaceSvc.Find(name)
if err != nil {
glog.Error(errors.Wrapf(err, "while getting %s with name %s", pretty.Namespace, name))
return nil, gqlerror.New(err, pretty.Namespace, gqlerror.WithName(name))
}
namespaceCopy := namespace.DeepCopy()
deletedNamespace := r.namespaceConverter.ToGQL(namespaceCopy)
err = r.namespaceSvc.Delete(name)
if err != nil {
glog.Error(errors.Wrapf(err, "while deleting %s `%s`", pretty.Namespace, name))
return nil, gqlerror.New(err, pretty.Namespace, gqlerror.WithName(name))
}
return deletedNamespace, nil
}
func (r *namespaceResolver) NamespaceEventSubscription(ctx context.Context, withSystemNamespaces *bool) (<-chan gqlschema.NamespaceEvent, error) {
namespaceChannel := make(chan gqlschema.NamespaceEvent, 1)
filter := func(namespace *v1.Namespace) bool {
newBool := true
return namespace != nil && r.checkNamespace(namespace, withSystemNamespaces, &newBool)
}
namespaceListener := listener.NewNamespace(namespaceChannel, filter, &r.namespaceConverter, r.systemNamespaces)
allowAll := func(_ *v1.Pod) bool { return true }
podChannel := make(chan gqlschema.PodEvent, 1)
podsListener := listener.NewPod(podChannel, allowAll, &podConverter{})
r.namespaceSvc.Subscribe(namespaceListener)
r.podService.Subscribe(podsListener)
go func() {
defer close(namespaceChannel)
defer close(podChannel)
defer r.namespaceSvc.Unsubscribe(namespaceListener)
defer r.podService.Unsubscribe(podsListener)
for {
select {
case podEvent := <-podChannel:
ns, err := r.namespaceSvc.Find(podEvent.Pod.Namespace)
if err != nil {
continue
}
namespaceListener.OnUpdate(ns, ns)
case <-ctx.Done():
return
}
}
}()
return namespaceChannel, nil
}
func (r *namespaceResolver) populateLabels(givenLabels *gqlschema.Labels) map[string]string {
labels := map[string]string{}
if givenLabels != nil {
for k, v := range *givenLabels {
labels[k] = v
}
}
return labels
}
func (r *namespaceResolver) checkNamespace(ns *v1.Namespace, withSystemNamespaces *bool, withInactiveStatus *bool) bool {
isSystem := isSystemNamespace(*ns, r.systemNamespaces)
passedSystemNamespaceCheck := !isSystem || (withSystemNamespaces != nil && *withSystemNamespaces && isSystem)
passedStatusCheck := ns.Status.Phase == "Active" || (withInactiveStatus != nil && *withInactiveStatus)
return passedSystemNamespaceCheck && passedStatusCheck
}