Library/tool to change a yaml given a rules file

golang-yaml-rules/yaml-transform

Library/tool to change a yaml given a rules file

Using jsonpath ( https://github.com/vmware-labs/yaml-jsonpath ), this tool shows how to transform a yaml source using simple rules

The rules file is a yaml map, the keys being only names.

  • 'set' rule: set/update values (single value, or whole yaml section) within nodes that match a first-level matching JsonPath expression values need not be applied to the matching node itself : a 'subpath' jsonpath expression allows to chose on which subnode you want to set values.

         # Syntax:
         
         - name_of_rule: whatever
           # match is a node selector, starting at your document root ($)
    
           # For some hints on Jsonpath syntax, you can have a look for example at https://support.smartbear.com/alertsite/docs/monitors/api/endpoint/jsonpath.html
    
           #  Here the selector targets all items in the 'containers' array
           match: "$..spec.containers[*]" 
                  set:
                   # Here you may have multiple groups of values you want to apply
                   # for each, you select the subnodes of your matching node (so here, '$' is not the root
                   # of your document, but the yaml node that matched the 'match' jsonpath expression)
                  - subpath: $
                    # Here you provide IMPERATIVE values that will override the potential ones with same name
                    # in your selected subnodes . This values can be simple types or multi-level yaml objects
                    values:
                      someField: a value
                      someOtherField:
                         a_subfield: true
                         other_subfield: 42
                  - subpath: $.some_items_collection[*]
                    values:
                       all_my_subitems_get_an:  A
    
  • 'deleteChildrenThatMatch' rule: removes array children or keys from nodes that match a first-level matching JsonPath expression.

     When applied to an Array, the children to remove are those matching a second-level matching JsonPath (syntax following arrays conditional within the '[]' JsonPath construct)
    
         # Syntax:
         
         - name_of_rule: whatever_delete_rule_name
    
           # match is a node selector, starting at your document root ($)
           
           #  Here the selector targets only some some 'containers' in my array
           # And for these containers, it will remove port 8080 from their ports list
         
           # For some hints on Jsonpath syntax, you can have a look for example at https://support.smartbear.com/alertsite/docs/monitors/api/endpoint/jsonpath.html
    
           match: "$..spec.containers[?(@.my_container_type_field==needs_ports_trimming)]" 
           deleteChildrenThatMatch: "?(@.ports[?(@.target_port==8080)])"
    
     When applied to a Map Object, this rule can also allow to remove a single key by its name. But in this case, wildcard or conditions based on the object content are not supported (only a single fixed key is removed)
    
         # Syntax:
         
         - name_of_rule: whatever_delete_attribute_rule_name
    
           # match is a node selector, starting at your document root ($)
           
           #  Here the selector targets  all 'containers' items
           # and will remove their 'imagePolicy' attribute
           # And for these containers, it will remove port 8080 from their ports list
           match: "$..spec.containers[*]" 
           deleteChildrenThatMatch: "$.imagePolicy"
    

As an example, see "rules_example.yaml", and "myfile_to_transform.yaml"

Usage:

   yaml-transform <rules yaml file path> <document yaml file path>

try out:

   ./yaml-transform  rules_example.yaml myfile_to_transform.yaml

This will output transformed yaml file to stdout

Similar Resources

Represent your rego rules programmatically.

Represent your rego rules programmatically.

Policy Enforcer Policy enforcer is a open source tool that allows you to easily create complex authorization policy. Supports RBAC, ABAC and resource

Jul 5, 2022

K8s-ingress-health-bot - A K8s Ingress Health Bot is a lightweight application to check the health of the ingress endpoints for a given kubernetes namespace.

k8s-ingress-health-bot A K8s Ingress Health Bot is a lightweight application to check the health of qualified ingress endpoints for a given kubernetes

Jan 2, 2022

Count total LoC in a given folder, recursively.

GoLoc A simple CLI tool to calculate the total LoC of a given directory. Usage goloc Flags Flag Description --use-gitignore Use your .gitignore file a

Apr 27, 2022

µTask is an automation engine that models and executes business processes declared in yaml. ✏️📋

µTask is an automation engine that models and executes business processes declared in yaml. ✏️📋

µTask, the Lightweight Automation Engine µTask is an automation engine built for the cloud. It is: simple to operate: only a postgres DB is required s

Dec 29, 2022

How you can use Go to replace YAML files with Kubernetes.

YamYams A small project that is free to use. 🎉 I wanted to offer a starting point for anyone interested in replacing YAML with Go. You can read more

Jan 6, 2023

Not another markup language. Framework for replacing Kubernetes YAML with Go.

Not another markup language. Replace Kubernetes YAML with raw Go! Say so long 👋 to YAML and start using the Go 🎉 programming language to represent a

Jan 3, 2023

kubectl plugin for signing Kubernetes manifest YAML files with sigstore

kubectl plugin for signing Kubernetes manifest YAML files with sigstore

k8s-manifest-sigstore kubectl plugin for signing Kubernetes manifest YAML files with sigstore ⚠️ Still under developement, not ready for production us

Nov 28, 2022

YAML and Golang implementations of common Kubernetes patterns.

Kubernetes Patterns Types Patterns Foundational Patterns Behavioral Patterns Structural Patterns Configuration Patterns Usage To run, simply do go run

Aug 8, 2022

Converts your k8s YAML to a cdk8s Api Object.

kube2cdk8s Converts your k8s YAML to a cdk8s Api Object. Uses Pulumi's kube2pulumi as a base. Dependencies 1. pulumi cli 2. pulumi kubernetes provider

Oct 18, 2022
Feb 12, 2022
Open Source runtime tool which help to detect malware code execution and run time mis-configuration change on a kubernetes cluster
Open Source runtime tool which help to detect malware code execution and run time mis-configuration change on a kubernetes cluster

Kube-Knark Project Trace your kubernetes runtime !! Kube-Knark is an open source tracer uses pcap & ebpf technology to perform runtime tracing on a de

Sep 19, 2022
Knative Controller which emits cloud events when Knative Resources change state

Knative Sample Controller Knative sample-controller defines a few simple resources that are validated by webhook and managed by a controller to demons

Oct 2, 2021
Demo of schema change failures with SQLite INTEGERs.

SQLite Schema Migration Bug This is a repository to reproduce a bug with Ent and the modernc.org/sqlite (non-CGO) SQLite 3 driver. Reproduce Simply ru

Dec 9, 2021
Oc-clusteroperator - OpenShift CLI plugin to change the state of ClusterOperators from managed to unmanaged and back again

oc-clusteroperator OpenShift CLI plugin to change the state of ClusterOperators

Feb 15, 2022
Prestic - Lets you define and run restic commands from a YAML file

Pete's Restic Lets you define and run restic commands from a YAML file. Features

Jan 10, 2022
Frep - Generate file using template from environment, arguments, json/yaml/toml config files

frep Generate file using template from environment, arguments, json/yaml/toml config files. NAME: frep - Generate file using template USAGE: fr

Nov 30, 2022
`runenv` create gcloud run deploy `--set-env-vars=` option and export shell environment from yaml file.

runenv runenv create gcloud run deploy --set-env-vars= option and export shell environment from yaml file. Motivation I want to manage Cloud Run envir

Feb 10, 2022
Generate Prometheus rules for your SLOs

prometheus-slo Generates Prometheus rules for alerting on SLOs. Based on https://developers.soundcloud.com/blog/alerting-on-slos. Usage Build and Run

Nov 27, 2022