forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_service.go
152 lines (125 loc) · 3.98 KB
/
api_service.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
package apicontroller
import (
"fmt"
"github.com/kyma-project/kyma/components/console-backend-service/internal/domain/apicontroller/extractor"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/dynamic"
"github.com/kyma-project/kyma/components/api-controller/pkg/apis/gateway.kyma-project.io/v1alpha2"
"github.com/kyma-project/kyma/components/console-backend-service/pkg/resource"
"github.com/pkg/errors"
apiErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/tools/cache"
)
type apiService struct {
informer cache.SharedIndexInformer
client dynamic.NamespaceableResourceInterface
notifier resource.Notifier
extractor extractor.ApiUnstructuredExtractor
}
var apisTypeMeta = metav1.TypeMeta{
Kind: "Api",
APIVersion: "gateway.kyma-project.io/v1alpha2",
}
func newApiService(informer cache.SharedIndexInformer, client dynamic.NamespaceableResourceInterface) *apiService {
notifier := resource.NewNotifier()
informer.AddEventHandler(notifier)
return &apiService{
informer: informer,
client: client,
notifier: notifier,
extractor: extractor.ApiUnstructuredExtractor{},
}
}
func (svc *apiService) List(namespace string, serviceName *string, hostname *string) ([]*v1alpha2.Api, error) {
items, err := svc.informer.GetIndexer().ByIndex("namespace", namespace)
if err != nil {
return nil, err
}
var apis []*v1alpha2.Api
for _, item := range items {
api, ok := item.(*unstructured.Unstructured)
if !ok {
return nil, fmt.Errorf("incorrect item type: %T, should be: *unstructured.Unstructured", item)
}
formattedApi, err := svc.extractor.FromUnstructured(api)
if err != nil {
return nil, err
}
match := true
if serviceName != nil {
if *serviceName != formattedApi.Spec.Service.Name {
match = false
}
}
if hostname != nil {
if *hostname != formattedApi.Spec.Hostname {
match = false
}
}
if match {
apis = append(apis, formattedApi)
}
}
return apis, nil
}
func (svc *apiService) Find(name string, namespace string) (*v1alpha2.Api, error) {
key := fmt.Sprintf("%s/%s", namespace, name)
item, exists, err := svc.informer.GetStore().GetByKey(key)
if err != nil {
return nil, errors.Wrapf(err, "while finding API %s", name)
}
if !exists {
return nil, nil
}
res, err := svc.extractor.Do(item)
if err != nil {
return &v1alpha2.Api{}, err
}
return res, nil
}
func (svc *apiService) Create(api *v1alpha2.Api) (*v1alpha2.Api, error) {
api.TypeMeta = apisTypeMeta
u, err := svc.extractor.ToUnstructured(api)
if err != nil {
return &v1alpha2.Api{}, err
}
created, err := svc.client.Namespace(api.ObjectMeta.Namespace).Create(u, metav1.CreateOptions{})
if err != nil {
return &v1alpha2.Api{}, err
}
return svc.extractor.FromUnstructured(created)
}
func (svc *apiService) Subscribe(listener resource.Listener) {
svc.notifier.AddListener(listener)
}
func (svc *apiService) Unsubscribe(listener resource.Listener) {
svc.notifier.DeleteListener(listener)
}
func (svc *apiService) Update(api *v1alpha2.Api) (*v1alpha2.Api, error) {
oldApi, err := svc.Find(api.ObjectMeta.Name, api.ObjectMeta.Namespace)
if err != nil {
return nil, errors.Wrapf(err, "while finding API %s", api.ObjectMeta.Name)
}
if oldApi == nil {
return nil, apiErrors.NewNotFound(schema.GroupResource{
Group: apisGroupVersionResource.Group,
Resource: apisGroupVersionResource.Resource,
}, api.ObjectMeta.Name)
}
api.ObjectMeta.ResourceVersion = oldApi.ObjectMeta.ResourceVersion
api.TypeMeta = apisTypeMeta
u, err := svc.extractor.ToUnstructured(api)
if err != nil {
return &v1alpha2.Api{}, err
}
updated, err := svc.client.Namespace(api.ObjectMeta.Namespace).Update(u, metav1.UpdateOptions{})
if err != nil {
return &v1alpha2.Api{}, err
}
return svc.extractor.FromUnstructured(updated)
}
func (svc *apiService) Delete(name string, namespace string) error {
return svc.client.Namespace(namespace).Delete(name, nil)
}