Publish a package with subpackages
Notice: Subpackages support is available with kpt version v0.34.0+ for [cfg] commands only
This guide walks you through an example to create, parameterize and publish a kpt package with a subpackage in it.
Steps
Create the package
Initialize a kpt
package with a subpackage in its directory tree.
mkdir wordpress
kpt pkg init wordpress
mkdir wordpress/mysql
kpt pkg init wordpress/mysql
Download the Wordpress
deployment configuration file.
cd wordpress
curl -LO https://k8s.io/examples/application/wordpress/wordpress-deployment.yaml
Download the MySQL
deployment configuration file.
cd mysql
curl -LO https://k8s.io/examples/application/wordpress/mysql-deployment.yaml
cd ../../
Add setters and substitutions
Add annotations
As a package publisher you might want to add [annotations] to kubernetes objects.
You can leverage --recurse-subpackages(-R)
flag to create it recursively in both
wordpress
and mysql
packages.
kpt cfg annotate wordpress/ --kv teamname=YOURTEAM -R
Output:
wordpress/
added annotations in the package
wordpress/mysql/
added annotations in the package
Similarly add projectId
annotation to both the packages.
kpt cfg annotate wordpress/ --kv projectId=PROJECT_ID -R
Output:
wordpress/
added annotations in the package
wordpress/mysql/
added annotations in the package
Create setters
Create Setters for the annotations values which you just added
kpt cfg create-setter wordpress/ teamname YOURTEAM -R --required
Output:
wordpress/
created setter "teamname"
wordpress/mysql/
created setter "teamname"
Similarly create an auto-setter with name gcloud.core.project
.
kpt cfg create-setter wordpress/ gcloud.core.project PROJECT_ID -R
Output:
wordpress/
created setter "gcloud.core.project"
wordpress/mysql/
created setter "gcloud.core.project"
Create substitutions
Create Substitutions so that package consumers can substitute values,
using kpt cfg set
kpt cfg create-subst wordpress image-tag \
--field-value wordpress:4.8-apache \
--pattern \${image}:\${tag}-apache
Output:
wordpress/
unable to find setter with name image, creating new setter with value wordpress
unable to find setter with name tag, creating new setter with value 4.8
created substitution "image-tag"
kpt cfg create-subst wordpress/mysql image-tag \
--field-value mysql:5.6 \
--pattern \${image}:\${tag}
Output:
wordpress/mysql/
unable to find setter with name image, creating new setter with value mysql
unable to find setter with name tag, creating new setter with value 5.6
created substitution "image-tag"
List and verify setters/substitutions
Use list-setters command to verify that the setters and substitutions are created as expected
kpt cfg list-setters wordpress/ --include-subst
Output:
wordpress/
NAME VALUE SET BY DESCRIPTION COUNT REQUIRED
gcloud.core.project PROJECT_ID 3 No
image wordpress 1 No
tag 4.8 1 No
teamname YOURTEAM 3 Yes
--------------- ------------------------ --------------
SUBSTITUTION PATTERN REFERENCES
image-tag ${image}:${tag}-apache [image,tag]
wordpress/mysql/
NAME VALUE SET BY DESCRIPTION COUNT REQUIRED
gcloud.core.project PROJECT_ID 3 No
image mysql 1 No
tag 5.6 1 No
teamname YOURTEAM 3 Yes
--------------- ----------------- --------------
SUBSTITUTION PATTERN REFERENCES
image-tag ${image}:${tag} [image,tag]
Publish the package
Now that as a package creator, you have created and parameterized a kpt
package,
publish it so that package consumers can consume it.
Create a git repo in your profile with name wordpress
cd wordpress/
git init; git add .; git commit -am "Publish package";
git remote add origin <YOUR_GIT_REPO_LINK>
git push origin master
Next steps
Go through the consumer guide to consume the published package