create-setters

Parameterize the field values by adding [setter] comments.

create-setters #

Overview #

Parameterize the field values by adding setter comments.

FunctionConfig #

We use ConfigMap to configure the create-setters function. Setters information is provided as key-value pairs using data field. Here, the key is the name of the setter, and value is the field value to be parameterized.

apiVersion: v1
kind: ConfigMap
metadata:
  name: create-setters-fn-config
data:
  setter_name1: setter_value1
  setter_name2: setter_value2

create-setters function performs the following steps:

  1. Segregates the input setters into scalar-setters and array-setters.
  2. Searches for the resource field values to be parameterized.
  3. Checks if there is any match considering the following cases.,
    • For a scalar node, performs substring match with scalar setters.
    • For an array node, checks if all values match with any of the array setters.
  4. Adds comments to the fields matching the setter values using setter names as parameters.

? If this function adds setter comments to the fields for which you didn’t intend to parameterize, you can simply review and delete/modify those comments manually.

?> If this function adds setter comments to fields for which you didn’t intend to parameterize, you can simply review and delete/modify those comments manually.

Examples #

Setting comments for scalar nodes #

Let’s start with the input resource in a package

# resources.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
   name: my-nginx
spec:
   replicas: 4
   selector:
      matchLabels:
         app: nginx
   template:
      metadata:
         labels:
            app: nginx
      spec:
         containers:
         - name: nginx
           image: "nginx:1.16.1"
           ports:
           - protocol: TCP
             containerPort: 80

Declare the name of the setter with the value which need to be parameterized.

# create-setters-fn-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: create-setters-fn-config
data:
  nginx-replicas: "4"
  tag: 1.16.1

Invoke the function:

$ kpt fn eval --image gcr.io/kpt-fn/create-setters:v0.1.0 --fn-config ./create-setters-fn-config.yaml

Alternatively, setter values can be passed as key-value pairs in the CLI

$ kpt fn eval --image gcr.io/kpt-fn/create-setters:v0.1.0 -- replicas=4 tag=1.1.2

Modified resource looks like the following:

# resources.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
   name: my-nginx
spec:
   replicas: 4 # kpt-set: ${nginx-replicas}
   selector:
      matchLabels:
         app: nginx
   template:
      metadata:
         labels:
            app: nginx
      spec:
         containers:
         - name: nginx
           image: "nginx:1.16.1" # kpt-set: nginx:${tag}
           ports:
           - protocol: TCP
             containerPort: 80

? This function doesn’t add comments to scalar nodes with multi-line values.

Explanation for the changes:

Comment is added to the Resource Field value node when they match the Scalar Setters.

Scalar SettersResource FieldCommentDescription
replicas: 4
nginx-replicas: 4
# kpt-set: ${nginx-replicas}Setter value of nginx-replicas matches with value of replicas field
tag: 1.1.2
app: “nginx:1.16.1”
# kpt-set: nginx:${tag}Setter value of tag matches the substring of field value nginx:1.16.1

Setting comments for array nodes #

Fields with array values can also be parameterized using setters. Since the values of ConfigMap in the pipeline definition must be of string type, the array values must be wrapped into string.

Let’s start with the input resource

# resources.yaml
apiVersion: v1
kind: MyKind
metadata:
  name: foo
environments:
  - dev
  - stage

Declare the array values, wrapped into string. Here the order of the array values doesn’t matter.

# create-setters-fn-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: create-setters-fn-config
data:
  env: |
    - stage
    - dev    
$ kpt fn eval --image gcr.io/kpt-fn/create-setters:v0.1.0 --fn-config ./create-setters-fn-config.yaml

Modified resource looks like the following:

# resources.yaml
apiVersion: v1
kind: MyKind
metadata:
  name: foo
environments: # kpt-set: ${env}
  - dev
  - stage

Explanation for the changes:

  • As all the array values of environments field match the setter values of env, # kpt-set: ${env} comment is added. Here, the comment is added to the environments field as it is an array node, and the intent is to paremeterize entire array.