create-setters

command module
v0.0.0-...-3df40d5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 30, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

README

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.

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:unstable --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:unstable -- 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 Setters Resource Field Comment Description
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:unstable --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.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
Code generated by "mdtogo"; DO NOT EDIT.
Code generated by "mdtogo"; DO NOT EDIT.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL