forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addonsconfiguration_resolver.go
149 lines (125 loc) · 5.79 KB
/
addonsconfiguration_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
package servicecatalogaddons
import (
"context"
"strings"
"github.com/golang/glog"
"github.com/kyma-project/kyma/components/console-backend-service/internal/domain/servicecatalogaddons/listener"
"github.com/kyma-project/kyma/components/console-backend-service/internal/domain/servicecatalogaddons/pretty"
"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/pager"
"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=addonsCfgService -output=automock -outpkg=automock -case=underscore
type addonsCfgService interface {
Subscribe(listener resource.Listener)
Unsubscribe(listener resource.Listener)
}
//go:generate mockery -name=addonsCfgLister -output=automock -outpkg=automock -case=underscore
type addonsCfgLister interface {
List(pagingParams pager.PagingParams) ([]*v1.ConfigMap, error)
}
//go:generate mockery -name=addonsCfgUpdater -output=automock -outpkg=automock -case=underscore
type addonsCfgUpdater interface {
AddRepos(name string, url []string) (*v1.ConfigMap, error)
RemoveRepos(name string, urls []string) (*v1.ConfigMap, error)
}
//go:generate mockery -name=addonsCfgMutations -output=automock -outpkg=automock -case=underscore
type addonsCfgMutations interface {
Create(name string, urls []string, labels *gqlschema.Labels) (*v1.ConfigMap, error)
Update(name string, urls []string, labels *gqlschema.Labels) (*v1.ConfigMap, error)
Delete(name string) (*v1.ConfigMap, error)
}
type addonsConfigurationResolver struct {
addonsCfgUpdater addonsCfgUpdater
addonsCfgLister addonsCfgLister
addonsCfgService addonsCfgService
addonsCfgMutations addonsCfgMutations
addonsCfgConverter addonsConfigurationConverter
}
func newAddonsRepoResolver(svc *addonsConfigurationService) *addonsConfigurationResolver {
return &addonsConfigurationResolver{
addonsCfgLister: svc,
addonsCfgUpdater: svc,
addonsCfgService: svc,
addonsCfgMutations: svc,
addonsCfgConverter: addonsConfigurationConverter{},
}
}
func (r *addonsConfigurationResolver) AddonsConfigurationsQuery(ctx context.Context, first *int, offset *int) ([]gqlschema.AddonsConfiguration, error) {
cfgs, err := r.addonsCfgLister.List(pager.PagingParams{First: first, Offset: offset})
if err != nil {
glog.Error(errors.Wrapf(err, "while listing %s", pretty.AddonsConfigurations))
return nil, gqlerror.New(err, pretty.AddonsConfigurations)
}
return r.addonsCfgConverter.ToGQLs(cfgs), nil
}
func (r *addonsConfigurationResolver) CreateAddonsConfiguration(ctx context.Context, name string, urls []string, labels *gqlschema.Labels) (*gqlschema.AddonsConfiguration, error) {
cfg, err := r.addonsCfgMutations.Create(name, urls, labels)
if err != nil {
glog.Error(errors.Wrapf(err, "while creating %s", pretty.AddonsConfiguration))
return nil, gqlerror.New(err, pretty.AddonsConfiguration, gqlerror.WithName(name), gqlerror.WithCustomArgument(addonsCfgKey, strings.Join(urls, "\n")))
}
return r.addonsCfgConverter.ToGQL(cfg), nil
}
func (r *addonsConfigurationResolver) UpdateAddonsConfiguration(ctx context.Context, name string, urls []string, labels *gqlschema.Labels) (*gqlschema.AddonsConfiguration, error) {
cfg, err := r.addonsCfgMutations.Update(name, urls, labels)
if err != nil {
glog.Error(errors.Wrapf(err, "while updating %s", pretty.AddonsConfiguration))
return nil, gqlerror.New(err, pretty.AddonsConfiguration, gqlerror.WithName(name), gqlerror.WithCustomArgument(addonsCfgKey, strings.Join(urls, "\n")))
}
return r.addonsCfgConverter.ToGQL(cfg), nil
}
func (r *addonsConfigurationResolver) DeleteAddonsConfiguration(ctx context.Context, name string) (*gqlschema.AddonsConfiguration, error) {
cfg, err := r.addonsCfgMutations.Delete(name)
if err != nil {
glog.Error(errors.Wrapf(err, "while deleting %s", pretty.AddonsConfiguration))
return nil, gqlerror.New(err, pretty.AddonsConfiguration, gqlerror.WithName(name))
}
return r.addonsCfgConverter.ToGQL(cfg), nil
}
func (r addonsConfigurationResolver) AddAddonsConfigurationURLs(ctx context.Context, name string, urls []string) (*gqlschema.AddonsConfiguration, error) {
cfg, err := r.addonsCfgUpdater.AddRepos(name, urls)
if err != nil {
glog.Error(errors.Wrapf(err, "while adding additional repositories to %s", pretty.AddonsConfiguration))
return nil, gqlerror.New(err, pretty.AddonsConfiguration, gqlerror.WithName(name))
}
return r.addonsCfgConverter.ToGQL(cfg), nil
}
func (r *addonsConfigurationResolver) RemoveAddonsConfigurationURLs(ctx context.Context, name string, urls []string) (*gqlschema.AddonsConfiguration, error) {
cfg, err := r.addonsCfgUpdater.RemoveRepos(name, urls)
if err != nil {
glog.Error(errors.Wrapf(err, "while removing repository from %s", pretty.AddonsConfiguration))
return nil, gqlerror.New(err, pretty.AddonsConfiguration, gqlerror.WithName(name))
}
return r.addonsCfgConverter.ToGQL(cfg), nil
}
func (r *addonsConfigurationResolver) AddonsConfigurationEventSubscription(ctx context.Context) (<-chan gqlschema.AddonsConfigurationEvent, error) {
channel := make(chan gqlschema.AddonsConfigurationEvent, 1)
filter := func(entity *v1.ConfigMap) bool {
if entity == nil {
return false
}
if entity.Namespace != systemNs {
return false
}
val, ok := entity.Labels[addonsCfgLabelKey]
if !ok {
return false
}
if val != addonsCfgLabelValue {
return false
}
return true
}
brokerListener := listener.NewAddonsConfiguration(channel, filter, &r.addonsCfgConverter)
r.addonsCfgService.Subscribe(brokerListener)
go func() {
defer close(channel)
defer r.addonsCfgService.Unsubscribe(brokerListener)
<-ctx.Done()
}()
return channel, nil
}