This repository has been archived by the owner on Jun 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathretriever.go
149 lines (128 loc) · 4.55 KB
/
retriever.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 servicebindingrequest
import (
"strings"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"github.com/redhat-developer/service-binding-operator/pkg/controller/servicebindingrequest/envvars"
"github.com/redhat-developer/service-binding-operator/pkg/log"
)
// Retriever reads all data referred in plan instance, and store in a secret.
type Retriever struct {
logger *log.Log // logger instance
client dynamic.Interface // Kubernetes API client
}
// createServiceIndexPath returns a string slice with fields representing a path to a resource in the
// environment variable context. This function cleans fields that might contain invalid characters to
// be used in Go template; for example, a Group might contain the "." character, which makes it
// harder to refer using Go template direct accessors and is substituted by an underbar "_".
func createServiceIndexPath(name string, gvk schema.GroupVersionKind) []string {
return []string{
gvk.Version,
strings.ReplaceAll(gvk.Group, ".", "_"),
gvk.Kind,
strings.ReplaceAll(name, "-", "_"),
}
}
func buildServiceEnvVars(svcCtx *ServiceContext, globalEnvVarPrefix string) (map[string]string, error) {
prefixes := []string{}
if len(globalEnvVarPrefix) > 0 {
prefixes = append(prefixes, globalEnvVarPrefix)
}
if svcCtx.EnvVarPrefix != nil && len(*svcCtx.EnvVarPrefix) > 0 {
prefixes = append(prefixes, *svcCtx.EnvVarPrefix)
}
if svcCtx.EnvVarPrefix == nil {
prefixes = append(prefixes, svcCtx.Service.GroupVersionKind().Kind)
}
return envvars.Build(svcCtx.EnvVars, prefixes...)
}
func (r *Retriever) processServiceContext(
svcCtx *ServiceContext,
customEnvVarCtx map[string]interface{},
globalEnvVarPrefix string,
) (map[string][]byte, []string, error) {
svcEnvVars, err := buildServiceEnvVars(svcCtx, globalEnvVarPrefix)
if err != nil {
return nil, nil, err
}
// contribute the entire resource to the context shared with the custom env parser
gvk := svcCtx.Service.GetObjectKind().GroupVersionKind()
// add an entry in the custom environment variable context, allowing the user to use the
// following expression:
//
// `{{ index "v1alpha1" "postgresql.baiju.dev" "Database", "db-testing", "status", "connectionUrl" }}`
err = unstructured.SetNestedField(
customEnvVarCtx, svcCtx.Service.Object, gvk.Version, gvk.Group, gvk.Kind,
svcCtx.Service.GetName())
if err != nil {
return nil, nil, err
}
// add an entry in the custom environment variable context with modified key names (group
// names have the "." separator changed to underbar and "-" in the resource name is changed
// to underbar "_" as well).
//
// `{{ .v1alpha1.postgresql_baiju_dev.Database.db_testing.status.connectionUrl }}`
err = unstructured.SetNestedField(
customEnvVarCtx,
svcCtx.Service.Object,
createServiceIndexPath(svcCtx.Service.GetName(), svcCtx.Service.GroupVersionKind())...,
)
if err != nil {
return nil, nil, err
}
envVars := make(map[string][]byte, len(svcEnvVars))
for k, v := range svcEnvVars {
envVars[k] = []byte(v)
}
var volumeKeys []string
volumeKeys = append(volumeKeys, svcCtx.VolumeKeys...)
return envVars, volumeKeys, nil
}
// ProcessServiceContexts returns environment variables and volume keys from a ServiceContext slice.
func (r *Retriever) ProcessServiceContexts(
globalEnvVarPrefix string,
svcCtxs ServiceContextList,
envVarTemplates []corev1.EnvVar,
) (map[string][]byte, []string, error) {
customEnvVarCtx := make(map[string]interface{})
volumeKeys := make([]string, 0)
envVars := make(map[string][]byte)
for _, svcCtx := range svcCtxs {
s, v, err := r.processServiceContext(svcCtx, customEnvVarCtx, globalEnvVarPrefix)
if err != nil {
return nil, nil, err
}
for k, v := range s {
envVars[k] = []byte(v)
}
volumeKeys = append(volumeKeys, v...)
}
envParser := NewCustomEnvParser(envVarTemplates, customEnvVarCtx)
customEnvVars, err := envParser.Parse()
if err != nil {
r.logger.Error(
err, "Creating envVars", "Templates", envVarTemplates, "TemplateContext", customEnvVarCtx)
return nil, nil, err
}
for k, v := range customEnvVars {
prefix := []string{}
if len(globalEnvVarPrefix) > 0 {
prefix = append(prefix, globalEnvVarPrefix)
}
prefix = append(prefix, k)
k = strings.Join(prefix, "_")
envVars[k] = []byte(v.(string))
}
return envVars, volumeKeys, nil
}
// NewRetriever instantiate a new retriever instance.
func NewRetriever(
client dynamic.Interface,
) *Retriever {
return &Retriever{
logger: log.NewLog("retriever"),
client: client,
}
}