Skip to content
This repository has been archived by the owner on Jun 26, 2024. It is now read-only.

Commit

Permalink
feat(retriever): ✨ add id field in backing service selector to be use…
Browse files Browse the repository at this point in the history
…d as shortcut in custom environment variables
  • Loading branch information
Igor Sutton Lopes committed May 14, 2020
1 parent 179c2fb commit 3a364e7
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 3 deletions.
4 changes: 4 additions & 0 deletions deploy/crds/apps.openshift.io_servicebindingrequests_crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ spec:
type: string
envVarPrefix:
type: string
id:
type: string
required:
- group
- kind
Expand All @@ -142,6 +144,8 @@ spec:
type: string
envVarPrefix:
type: string
id:
type: string
required:
- group
- kind
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/apps/v1alpha1/servicebindingrequest_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type BackingServiceSelector struct {
// +optional
Namespace *string `json:"namespace,omitempty"`
EnvVarPrefix *string `json:"envVarPrefix,omitempty"`
Id *string `json:"id,omitempty"`
}

// BoundApplication defines the application workloads to which the binding secret has
Expand Down
10 changes: 10 additions & 0 deletions pkg/apis/apps/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions pkg/controller/servicebindingrequest/retriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ func (r *Retriever) processServiceContext(
return nil, nil, err
}

// add an entry in the custom environment variable context with the informed 'id'.
//
// `{{ .db_testing.status.connectionUrl }}`
if svcCtx.Id != nil {
err = unstructured.SetNestedField(
customEnvVarCtx,
svcCtx.Service.Object,
*svcCtx.Id,
)
if err != nil {
return nil, nil, err
}
}

envVars := make(map[string][]byte, len(svcEnvVars))
for k, v := range svcEnvVars {
envVars[k] = []byte(v)
Expand Down
8 changes: 7 additions & 1 deletion pkg/controller/servicebindingrequest/retriever_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestRetriever(t *testing.T) {
logf.SetLogger(logf.ZapLogger(true))

ns := "testing"
backingServiceNs := "backing-servicec-ns"
backingServiceNs := "backing-service-ns"
crName := "db-testing"

f := mocks.NewFake(t, ns)
Expand All @@ -29,12 +29,16 @@ func TestRetriever(t *testing.T) {
crInSameNamespace, err := mocks.UnstructuredDatabaseCRMock(ns, crName)
require.NoError(t, err)

crId := "db_testing"
crInSameNamespaceId := "db_testing_other_ns"
serviceCtxs := ServiceContextList{
{
Service: cr,
Id: &crId,
},
{
Service: crInSameNamespace,
Id: &crInSameNamespaceId,
},
}

Expand All @@ -53,13 +57,15 @@ func TestRetriever(t *testing.T) {
{Name: "SAME_NAMESPACE", Value: toTmpl(crInSameNamespace)},
{Name: "OTHER_NAMESPACE", Value: toTmpl(cr)},
{Name: "DIRECT_ACCESS", Value: `{{ .v1alpha1.postgresql_baiju_dev.Database.db_testing.metadata.name }}`},
{Name: "ID_ACCESS", Value: `{{ .db_testing.metadata.name }}`},
},
)
require.NoError(t, err)
require.Equal(t, map[string][]byte{
"SERVICE_BINDING_SAME_NAMESPACE": []byte(crInSameNamespace.GetName()),
"SERVICE_BINDING_OTHER_NAMESPACE": []byte(cr.GetName()),
"SERVICE_BINDING_DIRECT_ACCESS": []byte(cr.GetName()),
"SERVICE_BINDING_ID_ACCESS": []byte(cr.GetName()),
}, actual)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/servicebindingrequest/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func buildOwnedResourceContext(
) (*ServiceContext, error) {
svcCtx, err := buildServiceContext(
client, obj.GetNamespace(), obj.GetObjectKind().GroupVersionKind(), obj.GetName(),
ownerEnvVarPrefix, restMapper)
ownerEnvVarPrefix, restMapper, nil)
if err != nil {
return nil, err
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/controller/servicebindingrequest/servicecontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type ServiceContext struct {
VolumeKeys []string
// EnvVarPrefix indicates the prefix to use in environment variables.
EnvVarPrefix *string
// Id indicates a name the service can be referred in custom environment variables.
Id *string
}

// ServiceContextList is a list of ServiceContext values.
Expand Down Expand Up @@ -58,7 +60,7 @@ func buildServiceContexts(
ns := stringValueOrDefault(s.Namespace, defaultNs)
gvk := schema.GroupVersionKind{Kind: s.Kind, Version: s.Version, Group: s.Group}
svcCtx, err := buildServiceContext(
client, ns, gvk, s.ResourceRef, s.EnvVarPrefix, restMapper)
client, ns, gvk, s.ResourceRef, s.EnvVarPrefix, restMapper, s.Id)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -127,6 +129,7 @@ func buildServiceContext(
resourceRef string,
envVarPrefix *string,
restMapper meta.RESTMapper,
id *string,
) (*ServiceContext, error) {
obj, err := findService(client, ns, gvk, resourceRef)
if err != nil {
Expand Down Expand Up @@ -202,6 +205,7 @@ func buildServiceContext(
EnvVars: envVars,
VolumeKeys: volumeKeys,
EnvVarPrefix: envVarPrefix,
Id: id,
}

return serviceCtx, nil
Expand Down

0 comments on commit 3a364e7

Please sign in to comment.