/
service.go
283 lines (250 loc) · 7.39 KB
/
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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package service
import (
"context"
"reflect"
"strings"
"github.com/redhat-developer/service-binding-operator/pkg/binding"
"github.com/redhat-developer/service-binding-operator/pkg/binding/registry"
"github.com/redhat-developer/service-binding-operator/pkg/client/kubernetes"
"github.com/redhat-developer/service-binding-operator/pkg/reconcile/pipeline"
"github.com/redhat-developer/service-binding-operator/pkg/util"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/util/jsonpath"
)
var _ pipeline.Service = &service{}
type CrdReader func(gvk *schema.GroupVersionResource) (*unstructured.Unstructured, error)
type Builder interface {
WithClient(client dynamic.Interface) Builder
WithCrdReader(reader CrdReader) Builder
Build(content *unstructured.Unstructured, options ...buildOption) (pipeline.Service, error)
LookOwnedResources(val bool) Builder
}
type buildOption func(s *service)
func Id(v string) buildOption {
return func(s *service) {
s.id = &v
}
}
func CrdReaderOption(reader CrdReader) buildOption {
return func(s *service) {
s.CrdReader = reader
}
}
type builder struct {
client dynamic.Interface
typeLookup kubernetes.K8STypeLookup
crdReader CrdReader
lookForOwnedResources bool
}
func NewBuilder(typeLookup kubernetes.K8STypeLookup) Builder {
return &builder{
typeLookup: typeLookup,
}
}
func (b *builder) WithClient(client dynamic.Interface) Builder {
b.client = client
return b
}
func (b *builder) WithCrdReader(reader CrdReader) Builder {
b.crdReader = reader
return b
}
func (b *builder) LookOwnedResources(val bool) Builder {
b.lookForOwnedResources = val
return b
}
func (b *builder) Build(content *unstructured.Unstructured, options ...buildOption) (pipeline.Service, error) {
gvr, err := b.typeLookup.ResourceForKind(content.GroupVersionKind())
if err != nil {
return nil, err
}
s := &service{
client: b.client,
resource: content,
groupVersionResource: gvr,
lookForOwnedResources: b.lookForOwnedResources,
namespace: content.GetNamespace(),
}
for _, o := range options {
o(s)
}
if s.CrdReader == nil {
if b.crdReader == nil {
b.crdReader = func(gvr *schema.GroupVersionResource) (*unstructured.Unstructured, error) {
var err error
var u *unstructured.Unstructured
for _, crd := range crdGVRs {
u, err = b.client.Resource(crd).Get(context.Background(), gvr.GroupResource().String(), metav1.GetOptions{})
if err == nil {
return u, nil
}
}
return nil, err
}
}
s.CrdReader = b.crdReader
}
return s, nil
}
var crdGVRs = []schema.GroupVersionResource{
{
Group: "apiextensions.k8s.io",
Version: "v1",
Resource: "customresourcedefinitions",
},
{
Group: "apiextensions.k8s.io",
Version: "v1beta1",
Resource: "customresourcedefinitions",
},
}
var bindableResourceGVRs = []schema.GroupVersionResource{
{Group: "", Version: "v1", Resource: "configmaps"},
{Group: "", Version: "v1", Resource: "secrets"},
{Group: "", Version: "v1", Resource: "services"},
{Group: "route.openshift.io", Version: "v1", Resource: "routes"},
}
type service struct {
client dynamic.Interface
namespace string
resource *unstructured.Unstructured
groupVersionResource *schema.GroupVersionResource
CrdReader
crd pipeline.CRD
crdLookup bool
lookForOwnedResources bool
bindingDefinitions []binding.Definition
id *string
}
func (s *service) OwnedResources() ([]*unstructured.Unstructured, error) {
uid := s.Resource().GetUID()
var result []*unstructured.Unstructured
if !s.lookForOwnedResources {
return result, nil
}
for _, gvr := range bindableResourceGVRs {
list, err := s.client.Resource(gvr).Namespace(s.namespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
if errors.IsNotFound(err) {
continue
}
return nil, err
}
for i := range list.Items {
item := list.Items[i]
for _, ownerRef := range item.GetOwnerReferences() {
if reflect.DeepEqual(ownerRef.UID, uid) {
result = append(result, &item)
}
}
}
}
return result, nil
}
func (s *service) Id() *string {
return s.id
}
func (s *service) Resource() *unstructured.Unstructured {
return s.resource
}
func (s *service) CustomResourceDefinition() (pipeline.CRD, error) {
if s.crd == nil {
if s.crdLookup {
return nil, nil
}
u, err := s.CrdReader(s.groupVersionResource)
if errors.IsNotFound(err) {
s.crdLookup = true
return nil, nil
}
if err != nil {
return nil, err
}
s.crd = &customResourceDefinition{resource: u, client: s.client, ns: s.namespace, serviceGVR: s.groupVersionResource}
return s.crd, err
}
return s.crd, nil
}
func (s *service) AddBindingDef(def binding.Definition) {
s.bindingDefinitions = append(s.bindingDefinitions, def)
}
func (s *service) BindingDefs() []binding.Definition {
return s.bindingDefinitions
}
func (s *service) IsBindable() (bool, error) {
crd, err := s.CustomResourceDefinition()
if err != nil {
return false, err
}
return crd.IsBindable()
}
type customResourceDefinition struct {
resource *unstructured.Unstructured
serviceGVR *schema.GroupVersionResource
client dynamic.Interface
ns string
}
func (c *customResourceDefinition) Resource() *unstructured.Unstructured {
if b, err := c.IsBindable(); b && err == nil {
return c.resource
}
gvk := c.serviceGVR.GroupVersion().WithKind(c.kind())
if annotations, found := registry.ServiceAnnotations.GetAnnotations(gvk); found {
c.resource.SetAnnotations(util.MergeMaps(c.resource.GetAnnotations(), annotations))
}
return c.resource
}
func (c *customResourceDefinition) kind() string {
val, found, _ := unstructured.NestedString(c.resource.Object, "spec", "names", "kind")
if found {
return val
}
return ""
}
// getValuesByJSONPath returns values from the given map matching the provided JSONPath
// 'path' argument takes JSONPath expressions enclosed by curly braces {}
// see https://kubernetes.io/docs/reference/kubectl/jsonpath/ for more details
// It returns zero or more filtered values back,
// or error if the jsonpath is invalid or it cannot be applied on the given map
func getValuesByJSONPath(obj map[string]interface{}, path string) ([]reflect.Value, error) {
j := jsonpath.New("")
err := j.Parse(path)
if err != nil {
return nil, err
}
result, err := j.FindResults(obj)
if err != nil {
return nil, err
}
if len(result) > 1 {
w := strings.Builder{}
for i := range result {
if err := j.PrintResults(&w, result[i]); err != nil {
return nil, err
}
}
return []reflect.Value{reflect.ValueOf(w.String())}, nil
}
return result[0], nil
}
func (c *customResourceDefinition) IsBindable() (bool, error) {
value, err := getValuesByJSONPath(c.resource.Object, "{..schema.openAPIV3Schema.properties.status.properties.binding.properties.name.type}")
if err == nil && len(value) > 0 && value[0].Interface().(string) == "string" {
return true, nil
}
annotations := make(map[string]string)
util.MergeMaps(annotations, c.resource.GetAnnotations())
if len(annotations) == 0 {
return false, nil
}
for k := range annotations {
if ok, err := binding.IsServiceBindingAnnotation(k); ok && err == nil {
return true, nil
}
}
return false, nil
}