Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow fetchEnvs to pull data from json strings #125

Open
alewitt2 opened this issue Jul 10, 2020 · 3 comments
Open

Allow fetchEnvs to pull data from json strings #125

alewitt2 opened this issue Jul 10, 2020 · 3 comments
Labels
enhancement New feature or request

Comments

@alewitt2
Copy link
Member

Is your feature request related to a problem? Please describe.
currently there is no way to get data from inside a json string defined in a cm. you can specify type: json, but that only parses the json and leaves its keys as a second class citizen.

Describe the solution you'd like
give a way to pull a single key or all the keys from a json into the top level of the returned fetchEnvs obj.

Describe alternatives you've considered
use fetchEnvs to get the json, then do post processing on the returned object to flatten accordingly

Additional context


example configmap

apiVersion: v1
kind: ConfigMap
metadata:
  name: cluster-info
  namespace: kube-system
data:
  cluster-config: |
    {
      "cluster_id": "1234",
      "cluster_name": "dev-mexico",
      "cluster_type": "dev"
    }

a normal example of using .env in razee

env
  - name: cluster-config # name of the key the fetched data will be assigned to
    valueFrom:
      configMapKeyRef:
        key: cluster-config # key within configmap
        name: cluster-info # configmap name
        namespace: kube-system # configmap namespace
        type: json # tells fetchEnvs to parse the fetched key

with a result of

{
  "cluster-config":
    {
      "cluster_id": "1234",
      "cluster_name": "dev-mexico",
      "cluster_type": "dev"
    }
}

example configmap

apiVersion: v1
kind: ConfigMap
metadata:
  name: cluster-info
  namespace: kube-system
data:
  cluster-config: |
    {
      "cluster_id": "1234",
      "cluster_metadata": {
          "created": "sometimelastweek"
        },
      "cluster_name": "dev-mexico",
      "cluster_type": "dev"
    }

example of using .env and being able to specify the path to a lower level json key

env:
  - name: cluster_created # name of key retrieved data will be assigned to
    valueFrom:
      configMapKeyRef:
        key: cluster-config # key within configmap
        name: cluster-info # configmap name
        namespace: kube-system # configmap namespace
        type: json # tells fetchEnvs to parse the fetched key
        pathStr: 'cluster_metadata.created' # must have type defined

with a result of

{
  "cluster_created": "sometimelastweek"
}

example configmap with . in key name

apiVersion: v1
kind: ConfigMap
metadata:
  name: cluster-info
  namespace: kube-system
data:
  cluster-config: |
    {
      "cluster_id": "1234",
      "cluster.metadata": {
          "created": "sometimelastweek"
        },
      "cluster_name": "dev-mexico",
      "cluster_type": "dev"
    }

example of using .env and being able to specify the path to a lower level json key

env:
  - name: cluster_created # name of key retrieved data will be assigned to
    valueFrom:
      configMapKeyRef:
        key: cluster-config# key within configmap
        name: cluster-info # configmap name
        namespace: kube-system # configmap namespace
        type: json # tells fetchEnvs to parse the fetched key
        path: ['cluster.metadata', 'created'] # must have type defined

with a result of

{
  "cluster_created": "sometimelastweek"
}

example configmap

apiVersion: v1
kind: ConfigMap
metadata:
  name: cluster-info
  namespace: kube-system
data:
  cluster-config: |
    {
      "cluster_id": "1234",
      "cluster_metadata": {
          "created": "sometimelastweek"
        },
      "cluster_name": "dev-mexico",
      "cluster_type": "dev"
    }

example of using .env and being able to specify the path to get all json items

env:
  - name: cluster_created # not used when path all `.` is defined
    valueFrom:
      configMapKeyRef:
        key: cluster-config # key within configmap
        name: cluster-info # configmap name
        namespace: kube-system # configmap namespace
        type: json # tells fetchEnvs to parse the fetched key
        pathStr: '.' # must have type defined

with a result of

    {
      "cluster_id": "1234",
      "cluster_metadata": {
          "created": "sometimelastweek"
        },
      "cluster_name": "dev-mexico",
      "cluster_type": "dev"
    }
@alewitt2 alewitt2 added the enhancement New feature or request label Jul 10, 2020
@david-shepard
Copy link

david-shepard commented Oct 15, 2020

As an alternative, I can probably do this with a helper in https://github.com/razee-io/MustacheTemplate/blob/master/src/handlebar-helpers.js

ie.

var Handlebars.registerHelper('toJSON', function(object){
	return JSON.parse(object);
});

then

 {{ assign "jsonObject" (toJSON JsonString }} 

@alewitt2
Copy link
Member Author

thanks for the suggestion. the actual motivation for this issue was to do with FeatureFlagSetLD. In mustache, it actually works just fine, since you can reference lower level json objects in a template. ie. when you specify type: json today, it already parses the json and adds it to the view, and you can reference anything you want in the view like this {{ cluster-config.cluster_metadata.created }}

we wanted a way to get items from within a json string to the top to be used directly, instead of needing any post processing within FeatureFlagSetLD

@david-shepard
Copy link

Ah I didn't notice that - very cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants