redhat-developer / service-binding-operator

[Deprecated] The Service Binding Operator: Connecting Applications with Services, in Kubernetes
https://redhat-developer.github.io/service-binding-operator
Apache License 2.0
109 stars 91 forks source link

"Error calling index" when using customEnvVar #272

Closed pdettori closed 4 years ago

pdettori commented 4 years ago

I am running the service binding operator from master (b0c9641574269f58b6126a406bace6a4b2c07afd on Dec 5) and using make local to run. When I try to run the example in examples/nodejs_postgresql with the following service binding request:

apiVersion: apps.openshift.io/v1alpha1
kind: ServiceBindingRequest
metadata:
  name: binding-request
  namespace: service-binding-demo
spec:
  applicationSelector:
    matchLabels:
      connects-to: postgres
      environment: demo
    group: apps.openshift.io
    version: v1
    resource: deploymentconfigs
  backingServiceSelector:
    group: postgresql.baiju.dev
    version: v1alpha1
    kind: Database
    resourceRef: db-demo
  customEnvVar:
    - name: DB_USER
      value: '{{ index .status.dbConfigMap "db.username" }}'  

I get the following error:

{"level":"error","ts":1575664614.742795,"logger":"reconciler.bind","msg":"On saving secret data..","Request.Namespace":"service-binding-demo","Request.Name":"binding-request","ServiceBindingRequest.Name":"binding-request","error":"template: set:1:3: executing \"set\" at <index .status.dbConfigMap \"db.username\">: error calling index: index of untyped nil","stacktrace": ...
pdettori commented 4 years ago

the problem seems to happen in custom_env_parser.go, in

// Parse interpolates and caches the templates in EnvMap.
func (c *CustomEnvParser) Parse() (map[string]interface{}, error) {
    data := make(map[string]interface{})
    for _, v := range c.EnvMap {
        tmpl, err := template.New("set").Parse(v.Value)
        if err != nil {
            return data, err
        }

        // evaluating template and storing value in a buffer
        buf := new(bytes.Buffer)
        err = tmpl.Execute(buf, c.Cache)
        if err != nil {
            return data, err
        }

        // saving buffer in cache
        data[v.Name] = buf.String()
    }
    return data, nil
}
pdettori commented 4 years ago

Seems like the content of the cache is not what expected, printing c.Cache as json provides:

{
  "DATABASE_CONFIGMAP_DB_HOST": "MTcyLjMwLjY3LjE1NA==",
  "DATABASE_CONFIGMAP_DB_NAME": "ZGItZGVtbw==",
  "DATABASE_CONFIGMAP_DB_PASSWORD": "cGFzc3dvcmQ=",
  "DATABASE_CONFIGMAP_DB_PORT": "NTQzMg==",
  "DATABASE_CONFIGMAP_DB_USERNAME": "cG9zdGdyZXM=",
  "DATABASE_CONFIGMAP_PASSWORD": "cGFzc3dvcmQ=",
  "DATABASE_CONFIGMAP_USER": "cG9zdGdyZXM=",
  "DATABASE_DBCONNECTIONIP": "MTcyLjMwLjY3LjE1NA==",
  "DATABASE_DBCONNECTIONPORT": "NTQzMg==",
  "DATABASE_DBNAME": "ZGItZGVtbw==",
  "DATABASE_SECRET_PASSWORD": "cGFzc3dvcmQ=",
  "DATABASE_SECRET_USER": "cG9zdGdyZXM="
}
pdettori commented 4 years ago

if I go back to older commits for master, that specific error does not show up and printing the cache shows something that makes more sense:

{
  "status": {
    "data.user": {},
    "db.host": {
      "db-demo": {}
    },
    "db.name": {
      "db-demo": {}
    },
    "db.password": {
      "db-demo": {}
    },
    "db.port": {
      "db-demo": {}
    },
    "db.user": {
      "db-demo": {}
    },
    "dbConfigMap": "db-demo",
    "dbConnectionIP": "172.30.67.154",
    "dbConnectionPort": 5432,
    "dbCredentials": {
      "password": "password",
      "user": "postgres"
    },
    "dbName": "db-demo",
    "password": {},
    "user": {}
  }
}
isutton commented 4 years ago

I believe you're being affected by the issue being solved in #275, will look closer to it.

baijum commented 4 years ago

Avni's PR #281 should address this issue. There is some unit test. But no e2e test.

Avni-Sharma commented 4 years ago

@pdettori #281 is merged. Can you try now?

pdettori commented 4 years ago

@Avni-Sharma I just tried with the latest master and it works fine.