codelab/01_kpt-validate/tutorial.md
<walkthrough-disable-features toc></walkthrough-disable-features>
Kpt is an OSS tool for Kubernetes packaging, which uses a standard format to bundle, publish, customize, update, and apply configuration manifests.
If you are new to skaffold, you can check out the skaffold tutorials to get a basic idea. Or just follow this codelab, we will explain what happens in each step.
skaffoldCheck if skaffold is installed and the installed version is >= v1.17.0
skaffold version
If you haven't installed skaffold previously, run
curl -Lo skaffold https://storage.googleapis.com/skaffold/builds/latest/skaffold-linux-amd64 && \
sudo install skaffold /usr/bin/ | bash
To upgrade skaffold to a newer version, run
sudo rm -f /usr/bin/skaffold && curl -Lo skaffold https://storage.googleapis.com/skaffold/builds/latest/skaffold-linux-amd64 && \
sudo install skaffold /usr/bin/ | bash
kptkpt is installed and the installed version is >= 0.34.0
kpt version
kpt previously, run
sudo apt-get install google-cloud-sdk-kpt
kustomizekustomize is installed and the installed version is >= v3.4.3
kustomize version
kustomize previously, run
curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash && sudo mv kustomize /usr/local/go/bin
Time to complete: About 3 minutes
In your <walkthrough-editor-spotlight spotlightId="menu-terminal-new-terminal">terminal</walkthrough-editor-spotlight>, run this command to start your local minikube cluster:
minikube start
Once your cluster is set up, the following message is displayed:
Done! kubectl is now configured to use "minikube"
Now let's use kpt pkg to download the application example.
kpt pkg get https://github.com/GoogleContainerTools/skaffold.git/codelab/01_kpt-validate/resources/sample-app guestbook-cl && cd guestbook-cl
<walkthrough-pin-section-icon></walkthrough-pin-section-icon> Extended Reading
kpt pkgdownloads the resource from a remote repository, a branch or a subdirectory. It does not contain the git version control history but only the specific git reference, so you can compose your own "package" from multiple repositories.
Read more about the kpt package from here
Time to complete: About 3 minutes
The example has already configured the <walkthrough-editor-open-file filePath="guestbook-cl/skaffold.yaml">skaffold.yaml</walkthrough-editor-open-file> for you.
apiVersion: skaffold/v2beta8
kind: Config
metadata:
name: kpt-cl
build:
artifacts:
- image: "frontend"
context: php-redis
deploy:
kpt:
dir: config
This file contains two stages: build and deploy.
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]
and deploy the applications to the cluster.
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 <walkthrough-editor-open-file filePath="guestbook-cl/php-redis/guestbook.php">guestbook-cl/php-redis</walkthrough-editor-open-file> and its configurations are stored in <walkthrough-editor-open-file filePath="guestbook-cl/config/frontend/deployment.yaml">guestbook-cl/config</walkthrough-editor-open-file>.
<walkthrough-pin-section-icon></walkthrough-pin-section-icon> Glossary
[1]
Hydratemeans rendering a kustomize directory or a kpt package to a flatten configuration, each of whose resources contains the full set of the object information.
Time to complete: About 1 minutes
skaffold dev
skaffold dev is the essential skaffold command. It builds the application and then deploys
the applications to the bundled cluster.
Once the deployment is complete, you can exit with Ctrl+C.
<walkthrough-notification-menu-icon></walkthrough-notification-menu-icon> Tips
skaffold devcan automatically detect file changes and kick off a re-deploy. So you don’t need to rerun the command if the file changes.
Time to complete: About 10 minutes
Validating the configurations helps both the app development and devOps to be efficient in a fragile environment.
This step uses a kubeval example to show how kpt functions[2]
can validate the app configuration and makes the validation itself as a declarative config.
Download the kpt function resource
kpt pkg get https://github.com/GoogleContainerTools/skaffold.git/codelab/01_kpt-validate/resources/validation-kubeval validation-kubeval
Now let's update the skaffold.yaml to use the new validator. Replace the following code with the
.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>
See the full skaffold.yaml
deploy:
kpt:
dir: config
fn:
fnPath: validation-kubeval
network: true
<walkthrough-pin-section-icon></walkthrough-pin-section-icon> Glossary
[2]
kpt functionis a kubernetes resource with aconfig.kubernetes.io/functionannotation. Read more here
Let's run skaffold dev. Now it will validate the resource according to the functions in
<walkthrough-editor-select-line filePath="guestbook-cl/skaffold.yaml" startLine="14" endLine="14" startCharacterOffset="7" endCharacterOffset="13">fnPath,
and then deploy the resource to the cluster.
skaffold dev
Since the validation passes silently, let's break the kubeval to prove the validation works!
In <walkthrough-editor-open-file filePath="guestbook-cl/config/frontend/deployment.yaml">config/frontend/deployment.yaml</walkthrough-editor-open-file>, 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)
You can either manually delete the line or run the following command
sed -i '19d' ./config/frontend/deployment.yaml
Check the skaffold dev output.
skaffold dev
You should expect the following warning about the missing field.
The Deployment "frontend" is invalid: spec.template.spec.containers[0].image: Required value
You can add back the removed line by copying the following command and running it in the terminal.
sed -i '18 a \ image: "frontend"' ./config/frontend/deployment.yaml
<walkthrough-notification-menu-icon></walkthrough-notification-menu-icon> Tips
You can find all the
kptvalidation functions from this catalogOr write your own versions. See instructions.
<walkthrough-conclusion-trophy></walkthrough-conclusion-trophy>
Congratulations, you known how to validate configurations in skaffold! You can explore the full kpt configuration from the skaffold.yaml reference doc.
You can also try out other kpt features like kpt pkg and kpt cfg from
the user guide. They will be supported
in skaffold soon. Stay tuned!