Skip to content

Latest commit

 

History

History
110 lines (90 loc) · 2.88 KB

Mongo_Plugin.md

File metadata and controls

110 lines (90 loc) · 2.88 KB

Mongo Plugin

We created a mongo plugin to translate conditions from MAPL v2 into mongo query language.
Querying the db directly speeds up the queries by about a factor of 5 to 10. It is assumed that all of the attributes are of type jsonpath

Examples

We assume that the raw json document is under the "raw" field in the document in the db.

  1. simple condition
 - rule_id: 0
   conditions:
      attribute: jsonpath:$.metadata.labels.foo
      method: EQ
      value: "bar"

Mongo query = {"raw.metadata.labels.foo":{"$eq":"bar"}}

  1. simple condition
 - rule_id: 0
   conditions:
      attribute: jsonpath:$.metadata.labels.foo
      method: NEX      

Mongo query = {"raw.metadata.labels.foo":{"$exists":false}}

  1. condition with "Not Regex"
conditions:
  attribute: jsonpath:$.metadata.labels.foo
  method: NRE
  value: ar2

Mongo query = {"$and":[{"raw.metadata.labels.foo":{"$not":{"$regex":"ar2"}}},{"raw.metadata.labels.foo":{"$exists":true}}]}

  1. condition with "Not-And"
  conditions:
    NOT:
      AND:
      - attribute: "jsonpath:$.metadata.labels.foo"
        method: EQ
        value: bar

      - attribute: "jsonpath:$.spec.replicas"
        method: LT
        value: 4

Mongo query = {"$nor":[{"$and":[{"raw.metadata.labels.foo":{"$eq":"bar"}},{"raw.spec.replicas":{"$lt":4}}]}]}

  1. multilevel:
 conditions:
   ALL:
     parentJsonpathAttribute: "jsonpath:$.spec.containers[:]"
     ANY:
       parentJsonpathAttribute: "jsonpath:$RELATIVE.volumeMounts[:]"
       condition:
         attribute: "jsonpath:$RELATIVE.name"
         method: EQ
         value: "xxx"

Mongo query = {"$nor":[{"raw.spec.containers":{"$elemMatch":{"$nor":[{"volumeMounts":{"$elemMatch":{"name":{"$eq":"xxx"}}}}]}}}]}

  1. key-value:
    conditions:
      ANY:
        parentJsonpathAttribute: "jsonpath:$.metadata.labels"
        condition:
          attribute: "jsonpath:$VALUE"
          method: EQ
          value: "ABC"

Mongo Aggregate query =

  [ 
     {"$addFields":{"addedField.raw.metadata.labels":{"$objectToArray":"$raw.metadata.labels"}}},
     {"$match":{"addedField.raw.metadata.labels.v":{"$eq":"ABC"}}} 
   ]

Mongo Plugin Limitations

  1. Return values are not supported. The complete document is returned.

  2. Comparison with strings that represent numbers with units is not supported.

For example:

  conditions:
   ANY:
     parentJsonpathAttribute: "jsonpath:$.spec.containers[:]"
     condition:
       attribute: "jsonpath:$RELATIVE.resources.limits.cpu"
       method: GT
       value: 0.5 

If the document in the db contains the value 600m [0.6 core] then the query will fail as it tries to compare number (from MAPL) and string (in the db). A special pre-processing is needed before saving the document to the db.

  1. Deepscan is not supported

  2. Key/Value queries are supported only outside of arrays