1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
# Examples for Validator Plugin
Previously, Kustomize suggested to used a transformer plugin to [perform validation](https://github.com/kubernetes-sigs/kustomize/tree/master/examples/validationTransformer). Now we introduce a new type of plugin: validator. As the name says, validator is used to validate the result YAML output. It works in the same way with *transformers* but cannot *modify* the input YAML content. Let's take a look at how it works.
## Make a Place to Work
<!-- @makeWorkplace @validatorPlugin -->
```
DEMO_HOME=$(mktemp -d)
mkdir -p $DEMO_HOME/valid
PLUGINDIR=$DEMO_HOME/kustomize/plugin/someteam.example.com/v1/validator
mkdir -p $PLUGINDIR
```
## Write a Validator Plugin
Kustomize has the following assumption of a validator plugin:
- The resources are passed to the validator plugin from stdin.
- The configuration file for the validator plugin is passed in
as the first argument.
- The working directory of the plugin is the kustomization
directory where it is used as a validator.
- The validated resources are written to stdout by the plugin. Or the validator can print nothing to the stdout if there is no need to change the input.
- Validator can **only** add a label named `validated-by` (case-sensitive) to the **top-level** resources. If there is any other modification in the validator, Kustomize will throw an error.
- If the return code of the transformer plugin is non zero,
Kustomize regards there is an error during the validation.
You can use either exec plugin or Go plugin as a validator. Here we use a bash script as an exec plugin.
<!-- @writePlugin @validatorPlugin -->
```bash
cat <<'EOF' > $PLUGINDIR/Validator
#!/bin/bash
# Do whatever you want here. In this example we
# just print out the input
cat
EOF
chmod +x $PLUGINDIR/Validator
```
## Use the Validator Plugin
Define a kustomization containing a valid ConfigMap
and the transformer plugin.
<!-- @writeKustomization @validatorPlugin -->
```bash
cat <<'EOF' >$DEMO_HOME/valid/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm
data:
foo: bar
EOF
cat <<'EOF' >$DEMO_HOME/valid/validator.yaml
apiVersion: someteam.example.com/v1
kind: Validator
metadata:
name: notImportantHere
EOF
cat <<'EOF' >$DEMO_HOME/valid/kustomization.yaml
resources:
- configmap.yaml
validators:
- validator.yaml
EOF
```
The directory structure is as the following:
```
/tmp/tmp.69tTCuXuYc
├── kustomize
│ └── plugin
│ └── someteam.example.com
│ └── v1
│ └── validator
│ └── Validator
└── valid
├── configmap.yaml
├── kustomization.yaml
└── validator.yaml
```
Define a helper function to run kustomize with the
correct environment and flags for plugins:
<!-- @defineKustomizeBd @validatorPlugin -->
```bash
function kustomizeBd {
XDG_CONFIG_HOME=$DEMO_HOME \
kustomize build \
--enable_alpha_plugins \
$DEMO_HOME/$1
}
```
Build the valid variant
<!-- @buildValid @validatorPlugin -->
```bash
kustomizeBd valid
```
The output contains a ConfigMap as
```yaml
apiVersion: v1
data:
foo: bar
kind: ConfigMap
metadata:
name: cm
```
### Validator Failure
Now lets try a failed validator
```bash
cat <<'EOF' > $PLUGINDIR/Validator
#!/bin/bash
# Non-zero indicates a failed validation
>&2 echo "Validation failed"
exit 1
EOF
chmod +x $PLUGINDIR/Validator
```
Build the valid variant
```bash
kustomizeBd valid
```
The output contains the error information that is printed to stderr
by validator.
```
Validation failed
Error: failure in plugin configured via /tmp/kust-plugin-config-369137659; exit status 1: exit status 1
```
### Input Modification
Typically a validator shouldn't modify the content to be validated. If it does, Kustomize will complain about it.
```bash
cat <<'EOF' > $PLUGINDIR/Validator
#!/bin/bash
# Modify the input content
sed 's/bar/baz/g'
EOF
chmod +x $PLUGINDIR/Validator
```
Then build
```
kustomizeBd valid
```
The error output will indicate you where is modified by the validator
```
Error: validator shouldn't modify the resource map: kunstruct not equal:
-- {"apiVersion":"v1","data":{"foo":"bar"},"kind":"ConfigMap","metadata":{"name":"cm"}}{nsfx:false,beh:unspecified},
-- {"apiVersion":"v1","data":{"foo":"baz"},"kind":"ConfigMap","metadata":{"name":"cm"}}{nsfx:false,beh:unspecified}
--
&resource.Resource{Kunstructured:(*kunstruct.UnstructAdapter)(0xc000118408), originalName:"cm", originalNs:"", options:(*types.GenArgs)(0xc00059e5e8), refBy:[]resid.ResId(nil), refVarNames:[]string(nil), namePrefixes:[]string{""}, nameSuffixes:[]string{""}}
------
&resource.Resource{Kunstructured:(*kunstruct.UnstructAdapter)(0xc000118510), originalName:"cm", originalNs:"", options:(*types.GenArgs)(0xc00059e5e8), refBy:[]resid.ResId(nil), refVarNames:[]string(nil), namePrefixes:[]string{""}, nameSuffixes:[]string{""}}
```
There is an exception that the validator can add a `validated-by` label to the **top** level resources.
<!-- @validatedByLabel @validatorPlugin -->
```bash
cat <<'EOF' > $PLUGINDIR/Validator
#!/usr/bin/bash
sed 's/^ name: cm$/ name: cm\n labels:\n validated-by: whatever/'
EOF
chmod +x $PLUGINDIR/Validator
```
Then build
<!-- @validatedByLabelBuild @validatorPlugin -->
```
kustomizeBd valid
```
The output will be
```yaml
apiVersion: v1
data:
foo: bar
kind: ConfigMap
metadata:
labels:
validated-by: whatever
name: cm
```
## cleanup
<!-- @cleanup @validatorPlugin -->
```
rm -rf $DEMO_HOME
```
|