-
Notifications
You must be signed in to change notification settings - Fork 32
/
informer_service.go
147 lines (127 loc) · 5.09 KB
/
informer_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
package service
import (
toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1"
"github.com/codeready-toolchain/registration-service/pkg/application/service"
"github.com/codeready-toolchain/registration-service/pkg/application/service/base"
servicecontext "github.com/codeready-toolchain/registration-service/pkg/application/service/context"
"github.com/codeready-toolchain/registration-service/pkg/configuration"
"github.com/codeready-toolchain/registration-service/pkg/informers"
"github.com/codeready-toolchain/registration-service/pkg/kubeclient/resources"
"github.com/codeready-toolchain/registration-service/pkg/log"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
)
type Option func(f *ServiceImpl)
// ServiceImpl represents the implementation of the informer service.
type ServiceImpl struct { // nolint:revive
base.BaseService
informer informers.Informer
}
// NewInformerService creates a service object for getting resources via shared informers
func NewInformerService(context servicecontext.ServiceContext, options ...Option) service.InformerService {
si := &ServiceImpl{
BaseService: base.NewBaseService(context),
informer: context.Informer(),
}
for _, o := range options {
o(si)
}
return si
}
func (s *ServiceImpl) GetProxyPluginConfig(name string) (*toolchainv1alpha1.ProxyPlugin, error) {
obj, err := s.informer.ProxyPluginConfig.ByNamespace(configuration.Namespace()).Get(name)
if err != nil {
return nil, err
}
unobj := obj.(*unstructured.Unstructured)
config := &toolchainv1alpha1.ProxyPlugin{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unobj.UnstructuredContent(), config); err != nil {
log.Errorf(nil, err, "failed to get Proxy Plugin config %q", name)
return nil, err
}
return config, err
}
func (s *ServiceImpl) GetMasterUserRecord(name string) (*toolchainv1alpha1.MasterUserRecord, error) {
obj, err := s.informer.Masteruserrecord.ByNamespace(configuration.Namespace()).Get(name)
if err != nil {
return nil, err
}
unobj := obj.(*unstructured.Unstructured)
mur := &toolchainv1alpha1.MasterUserRecord{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unobj.UnstructuredContent(), mur); err != nil {
log.Errorf(nil, err, "failed to get MasterUserRecord '%s'", name)
return nil, err
}
return mur, err
}
func (s *ServiceImpl) GetSpace(name string) (*toolchainv1alpha1.Space, error) {
obj, err := s.informer.Space.ByNamespace(configuration.Namespace()).Get(name)
if err != nil {
return nil, err
}
unobj := obj.(*unstructured.Unstructured)
space := &toolchainv1alpha1.Space{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unobj.UnstructuredContent(), space); err != nil {
log.Errorf(nil, err, "failed to get Space '%s'", name)
return nil, err
}
return space, err
}
func (s *ServiceImpl) GetToolchainStatus() (*toolchainv1alpha1.ToolchainStatus, error) {
obj, err := s.informer.ToolchainStatus.ByNamespace(configuration.Namespace()).Get(resources.ToolchainStatusName)
if err != nil {
return nil, err
}
unobj := obj.(*unstructured.Unstructured)
stat := &toolchainv1alpha1.ToolchainStatus{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unobj.UnstructuredContent(), stat); err != nil {
log.Errorf(nil, err, "failed to get ToolchainStatus %s", resources.ToolchainStatusName)
return nil, err
}
return stat, err
}
func (s *ServiceImpl) GetUserSignup(name string) (*toolchainv1alpha1.UserSignup, error) {
obj, err := s.informer.UserSignup.ByNamespace(configuration.Namespace()).Get(name)
if err != nil {
return nil, err
}
unobj := obj.(*unstructured.Unstructured)
us := &toolchainv1alpha1.UserSignup{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unobj.UnstructuredContent(), us); err != nil {
log.Errorf(nil, err, "failed to get UserSignup '%s'", name)
return nil, err
}
return us, err
}
func (s *ServiceImpl) ListSpaceBindings(reqs ...labels.Requirement) ([]toolchainv1alpha1.SpaceBinding, error) {
selector := labels.NewSelector().Add(reqs...)
objs, err := s.informer.SpaceBinding.ByNamespace(configuration.Namespace()).List(selector)
if err != nil {
return nil, err
}
sbs := []toolchainv1alpha1.SpaceBinding{}
for _, obj := range objs {
unobj := obj.(*unstructured.Unstructured)
sb := &toolchainv1alpha1.SpaceBinding{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unobj.UnstructuredContent(), sb); err != nil {
log.Errorf(nil, err, "failed to list SpaceBindings")
return nil, err
}
sbs = append(sbs, *sb)
}
return sbs, err
}
func (s *ServiceImpl) GetNSTemplateTier(name string) (*toolchainv1alpha1.NSTemplateTier, error) {
obj, err := s.informer.NSTemplateTier.ByNamespace(configuration.Namespace()).Get(name)
if err != nil {
return nil, err
}
unobj := obj.(*unstructured.Unstructured)
tier := &toolchainv1alpha1.NSTemplateTier{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unobj.UnstructuredContent(), tier); err != nil {
log.Errorf(nil, err, "failed to get NSTemplateTier '%s'", name)
return nil, err
}
return tier, err
}