kubernetescrossplane

How to pass parameter in Kubernetes / Crossplane yaml using property file


I have below yaml, I need to set the metadata.name , compartmentIdRef.name, displayName set via a configuration file.

Similarly, we have config.properties in Java or TF_VAR_name in terraform.

apiVersion: core.oci.upbound.io/v1alpha1
kind: Vcn
metadata:
  name: vcn-via-crossplane
spec:
  forProvider:
    compartmentIdRef: 
      name : compartment-via-crossplane
    displayName: vcn-via-crossplane
    cidrBlocks: ["x.x.x.x/x"]

How could we replicate the same in crossplane. Any suggestion would be appreciated.


Solution

  • This could done using Kubernetes configmap or secrets but you need to add extra key-map inside the yaml file for same

    If you are using shell script then you could also use yq, a simple CI tool, it's a lightweight and portable command-line YAML, JSON and XML processor. yq uses jq like syntax but works with yaml files as well as json, xml, properties, csv and tsv.

    https://github.com/mikefarah/yq

    Install in MacOS like:

    brew install yq
    

    Source: https://formulae.brew.sh/formula/yq

    Note: you can install yq in linux and other OS too


    Referencing the YAML in question, Create a property file with the required properties as below:

    metadata.name=crossplane-metadata1
    spec.forProvider.compartmentIdRef.name=compartment-name1
    

    Suppose the file name of the property file is file.yaml

    Now sample code to make changes in YAML using properties file using yq and shell script:

    FILE_DIR=$WORKSPACE/file.yaml
    yq e -i '. *= load_props("'$FILE_PATH'/file.properties")' $FILE_DIR
    echo "====== REPLACED file.yaml file content w.r.t to file.properties file ===="
    yq $FILE_DIR
    

    Source: https://mikefarah.gitbook.io/yq/usage/properties