github.com/GoogleContainerTools/skaffold/v2@v2.13.2/codelab/01_kpt-validate/tutorial.md (about) 1 # Validate Your Configurations in skaffold Workflow 2 3 <walkthrough-disable-features toc></walkthrough-disable-features> 4 5 ## Introduction 6 7 #### What is kpt? 8 9 Kpt is [an OSS tool](https://github.com/GoogleContainerTools/kpt) for Kubernetes packaging, which uses a standard format to bundle, publish, customize, update, and apply configuration manifests. 10 11 #### What kpt can help you with 12 13 * You will get an hands-on off-the-shelf experience about the **GitOps** CI/CD workflow in skaffold. 14 * You can validate each of your config changes **declaratively**. 15 * You **won't** encounter **version conflict** if the config hydration (a.k.a kustomize) mismatch with the deployment tool (e.g. kubectl). 16 * You can prune your resources accurately with [a three-way merge strategy](https://kubectl.docs.kubernetes.io/pages/app_management/field_merge_semantics.html). 17 18 #### What you'll learn 19 20 * How to add a validation to your config changes. 21 * How to define validation rules in the form of a declarative configuration. 22 * How to use the validation in kustomized resources. 23 24 ## Prerequisites 25 26 If you are new to skaffold, you can check out [the skaffold tutorials](https://skaffold.dev/docs/tutorials/) to get a basic idea. 27 Or just follow this codelab, we will explain what happens in each step. 28 29 #### Install `skaffold` 30 31 * Check if `skaffold` is installed and the installed version is >= v1.17.0 32 ```bash 33 skaffold version 34 ``` 35 36 * If you haven't installed `skaffold` previously, run 37 ```bash 38 curl -Lo skaffold https://storage.googleapis.com/skaffold/builds/latest/skaffold-linux-amd64 && \ 39 sudo install skaffold /usr/bin/ | bash 40 ``` 41 * To upgrade skaffold to a newer version, run 42 ```bash 43 sudo rm -f /usr/bin/skaffold && curl -Lo skaffold https://storage.googleapis.com/skaffold/builds/latest/skaffold-linux-amd64 && \ 44 sudo install skaffold /usr/bin/ | bash 45 ``` 46 47 #### Install `kpt` 48 49 * Check if `kpt` is installed and the installed version is >= 0.34.0 50 ```bash 51 kpt version 52 ``` 53 * If you haven't installed `kpt` previously, run 54 ```bash 55 sudo apt-get install google-cloud-sdk-kpt 56 ``` 57 58 #### Install `kustomize` 59 60 * Check if `kustomize` is installed and the installed version is >= v3.4.3 61 ```bash 62 kustomize version 63 ``` 64 * If you haven't installed `kustomize` previously, run 65 ```bash 66 curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash && sudo mv kustomize /usr/local/go/bin 67 ``` 68 69 ## Getting started 70 71 Time to complete: **About 3 minutes** 72 73 74 * In your 75 <walkthrough-editor-spotlight spotlightId="menu-terminal-new-terminal">terminal</walkthrough-editor-spotlight>, 76 run this command to start your local minikube cluster: 77 78 ```bash 79 minikube start 80 ``` 81 82 * Once your cluster is set up, the following message is displayed: 83 84 ```terminal 85 Done! kubectl is now configured to use "minikube" 86 ``` 87 88 * Now let's use `kpt pkg` to download the application example. 89 90 ```bash 91 kpt pkg get https://github.com/GoogleContainerTools/skaffold.git/codelab/01_kpt-validate/resources/sample-app guestbook-cl && cd guestbook-cl 92 ``` 93 94 <walkthrough-pin-section-icon></walkthrough-pin-section-icon> 95 **Extended Reading** 96 > `kpt pkg` downloads the resource from a remote repository, a branch or a subdirectory. 97 > It does not contain the git version control history but only the specific git reference, 98 > so you can compose your own "package" from multiple repositories. 99 100 > Read more about the kpt package from [here](https://googlecontainertools.github.io/kpt/reference/pkg/) 101 102 ## Taking a deeper look at the skaffold.yaml 103 104 Time to complete: **About 3 minutes** 105 106 107 The example has already configured the <walkthrough-editor-open-file filePath="guestbook-cl/skaffold.yaml">skaffold.yaml</walkthrough-editor-open-file> 108 for you. 109 110 ```yaml 111 apiVersion: skaffold/v2beta8 112 kind: Config 113 metadata: 114 name: kpt-cl 115 build: 116 artifacts: 117 - image: "frontend" 118 context: php-redis 119 deploy: 120 kpt: 121 dir: config 122 ``` 123 124 This file contains two stages: `build` and `deploy`. 125 126 - <walkthrough-editor-select-line filePath="guestbook-cl/skaffold.yaml" startLine="4" endLine="4" startCharacterOffset="0" endCharacterOffset="5">build</walkthrough-editor-select-line> defines the methods to **build** and **upload** the application images (by default, it uses docker) 127 - <walkthrough-editor-select-line filePath="guestbook-cl/skaffold.yaml" startLine="8" endLine="8" startCharacterOffset="0" endCharacterOffset="6">deploy</walkthrough-editor-select-line> defines the methods to **manage** the app configuration and **deploy** the application to the bundled cluster. 128 129 In the example, the configurations will build an image <walkthrough-editor-select-line filePath="guestbook-cl/skaffold.yaml" startLine="6" endLine="6" startCharacterOffset="12" endCharacterOffset="20">frontend</walkthrough-editor-select-line> and use `kpt` to *hydrate[1]* 130 and deploy the applications to the cluster. 131 132 The <walkthrough-editor-select-line filePath="guestbook-cl/skaffold.yaml" startLine="6" endLine="6" startCharacterOffset="12" endCharacterOffset="20">frontend</walkthrough-editor-select-line> source code is stored in 133 <walkthrough-editor-open-file filePath="guestbook-cl/php-redis/guestbook.php">guestbook-cl/php-redis</walkthrough-editor-open-file> 134 and its configurations are stored in <walkthrough-editor-open-file filePath="guestbook-cl/config/frontend/deployment.yaml">guestbook-cl/config</walkthrough-editor-open-file>. 135 136 <walkthrough-pin-section-icon></walkthrough-pin-section-icon> 137 **Glossary** 138 139 > [1] *`Hydrate`* means rendering a *kustomize* directory or a *kpt* package to a flatten 140 > configuration, each of whose resources contains the full set of the object information. 141 142 ## Running skaffold 143 144 Time to complete: **About 1 minutes** 145 146 ```bash 147 skaffold dev 148 ``` 149 150 `skaffold dev` is the essential skaffold command. It builds the application and then deploys 151 the applications to the bundled cluster. 152 153 Once the deployment is complete, you can exit with `Ctrl+C`. 154 155 <walkthrough-notification-menu-icon></walkthrough-notification-menu-icon> 156 **Tips** 157 158 > `skaffold dev` can automatically detect file changes and kick off a re-deploy. 159 > So you don’t need to rerun the command if the file changes. 160 161 ## Validating the config 162 163 Time to complete: **About 10 minutes** 164 165 Validating the configurations helps both the app development and devOps to be efficient in a 166 fragile environment. 167 168 This step uses a `kubeval` example to show how `kpt` functions[2] 169 can validate the app configuration and makes the validation itself **as a declarative config.** 170 171 * Download the kpt function resource 172 173 ```bash 174 kpt pkg get https://github.com/GoogleContainerTools/skaffold.git/codelab/01_kpt-validate/resources/validation-kubeval validation-kubeval 175 ``` 176 177 * Now let's update the skaffold.yaml to use the new validator. Replace the following code with the 178 `.deploy` section in the <walkthrough-editor-select-line filePath="guestbook-cl/skaffold.yaml" startLine="8" endLine="10" startCharacterOffset="0" endCharacterOffset="100">skaffold.yaml</walkthrough-editor-select-line> 179 180 See the full [skaffold.yaml](https://github.com/yuwenma/sample-app/blob/kubeval/skaffold.yaml#L12) 181 ```yaml 182 deploy: 183 kpt: 184 dir: config 185 fn: 186 fnPath: validation-kubeval 187 network: true 188 ``` 189 190 - <walkthrough-editor-select-line filePath="guestbook-cl/skaffold.yaml" startLine="12" endLine="12" startCharacterOffset="6" endCharacterOffset="12">.deploy.kpt.fn.fnPath</walkthrough-editor-select-line> refers to the kpt function directory we just downloaded. 191 - <walkthrough-editor-select-line filePath="guestbook-cl/skaffold.yaml" startLine="13" endLine="13" startCharacterOffset="6" endCharacterOffset="13">.deploy.kpt.fn.network</walkthrough-editor-select-line> enables the kpt access to the network. This is required to run the function in a docker container. 192 193 194 <walkthrough-pin-section-icon></walkthrough-pin-section-icon> 195 **Glossary** 196 > [2] `kpt function` is a kubernetes resource with a `config.kubernetes.io/function` annotation. 197 Read more [here](https://googlecontainertools.github.io/kpt/concepts/functions/) 198 199 200 ## Verifying the validation 201 202 ### "happy path" 203 204 Let's run `skaffold dev`. Now it will validate the resource according to the functions in 205 <walkthrough-editor-select-line filePath="guestbook-cl/skaffold.yaml" startLine="14" endLine="14" startCharacterOffset="7" endCharacterOffset="13">`fnPath`, 206 and *then* deploy the resource to the cluster. 207 208 ```bash 209 skaffold dev 210 ``` 211 212 213 ### "sad path" 214 215 Since the validation passes silently, let's break the kubeval to prove the validation works! 216 217 * In <walkthrough-editor-open-file filePath="guestbook-cl/config/frontend/deployment.yaml">config/frontend/deployment.yaml</walkthrough-editor-open-file>, 218 remove the <walkthrough-editor-select-line filePath="guestbook-cl/config/frontend/deployment.yaml" startLine="18" endLine="18" startCharacterOffset="0" endCharacterOffset="30">spec.template.spec.containers.image</walkthrough-editor-select-line> field (in line 19) 219 220 *You can **either** manually delete the line **or** run the following command* 221 222 ```bash 223 sed -i '19d' ./config/frontend/deployment.yaml 224 ``` 225 226 * Check the skaffold dev output. 227 228 ```bash 229 skaffold dev 230 ``` 231 You should expect the following warning about the missing field. 232 233 ```terminal 234 The Deployment "frontend" is invalid: spec.template.spec.containers[0].image: Required value 235 ``` 236 237 * You can add back the removed line by copying the following command and running it in the terminal. 238 239 ```shell script 240 sed -i '18 a \ image: "frontend"' ./config/frontend/deployment.yaml 241 ``` 242 243 <walkthrough-notification-menu-icon></walkthrough-notification-menu-icon> 244 **Tips** 245 > You can find all the `kpt` validation functions from this [catalog](https://googlecontainertools.github.io/kpt/guides/consumer/function/catalog/validators/) 246 > 247 > Or write your own versions. See [instructions](https://googlecontainertools.github.io/kpt/guides/consumer/function/). 248 249 ## Conclusion 250 251 <walkthrough-conclusion-trophy></walkthrough-conclusion-trophy> 252 253 Congratulations, you known how to validate configurations in skaffold! You can explore the full kpt configuration 254 from the skaffold.yaml [reference doc](https://skaffold.dev/docs/references/yaml/#deploy-kpt). 255 256 257 You can also try out other kpt features like `kpt pkg` and `kpt cfg` from 258 [the user guide](https://googlecontainertools.github.io/kpt/reference/). They will be supported 259 in skaffold soon. Stay tuned!