Skip to content

Commit

Permalink
Turns the consumer init and setter guides into tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mortent committed Sep 25, 2020
1 parent 5f97bc4 commit b4da328
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 32 deletions.
58 changes: 48 additions & 10 deletions site/content/en/guides/producer/init/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ description: >
Initialize and publish a new package
---

{{% hide %}}
<!-- @makeWorkplace @verifyGuides-->
```
# Set up workspace for the test.
TEST_HOME=$(mktemp -d)
cd $TEST_HOME
```
{{% /hide %}}

A kpt package is published as a git subdirectory containing configuration
files (YAML). Publishers of kpt packages can create or generate YAML files
however they like using the tool of their choice.
Expand Down Expand Up @@ -42,48 +51,77 @@ be individually versioned (as version v1.0.2) by creating the tag `example/v1.0.

## Create a git repo

<!-- @defineEnvVars @verifyGuides-->
```sh
REPO_NAME=my-repo
REPO_URL="<url>"
```

{{% hide %}}
<!-- @setRepoUrlForTest @verifyGuides-->
```
# Set up workspace for the test.
REPO_URL=file://$(pwd)/$REPO_NAME.git
```
{{% /hide %}}

<!-- @setupRepo @verifyGuides-->
```sh
git clone REPO_URL # or create a new repo with `git init`
cd REPO_NAME
mkdir $REPO_NAME # or clone with git `git clone`
git init $REPO_NAME # only if new repo
```

## Create the package

<!-- @createPackage @verifyGuides-->
```sh
mkdir nginx
mkdir $REPO_NAME/nginx
```

Recommended: initialize the package with metadata

<!-- @initPackage @verifyGuides-->
```sh
kpt pkg init nginx --tag kpt.dev/app=nginx --description "kpt nginx package"
kpt pkg init $REPO_NAME/nginx --tag kpt.dev/app=nginx --description "kpt nginx package"
```

## Create configuration

<!-- @addConfig @verifyGuides-->
```sh
curl https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/controllers/nginx-deployment.yaml --output nginx/nginx-deployment.yaml
curl https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/controllers/nginx-deployment.yaml --output $REPO_NAME/nginx/nginx-deployment.yaml
```

## Publish package to git

<!-- @commitRepo @verifyGuides-->
```sh
git add .
git commit -m "Add nginx package"
(cd $REPO_NAME && git add . && git commit -m "Add nginx package")
```

Recommended: tag the commit as a release

<!-- @createTag @verifyGuides-->
```sh
# tag as DIR/VERSION for per-directory versioning
git tag nginx/v0.1.0
git push nginx/v0.1.0
(cd $REPO_NAME && git tag nginx/v0.1.0)
# git push nginx/v0.1.0 # requires an upstream repo
```

## Fetch the released package

<!-- @fetchPackage @verifyGuides-->
```sh
kpt pkg get https://<REPO>/nginx@v0.1.0 nginx
kpt pkg get $REPO_URL/nginx@v0.1.0 nginx
```

{{% hide %}}
<!-- @setRepoUrlForTest @verifyGuides-->
```
grep "ref: v0.1.0" nginx/Kptfile
grep "kpt nginx package" nginx/README.md
grep "name: nginx-deployment" nginx/nginx-deployment.yaml
```
{{% /hide %}}

[tagging]: https://git-scm.com/book/en/v2/Git-Basics-Tagging
112 changes: 91 additions & 21 deletions site/content/en/guides/producer/setters/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ description: >
Create high-level setters to provide imperative configuration editing
commands.
---

{{% hide %}}
<!-- @makeWorkplace @verifyGuides-->
```
# Set up workspace for the test.
TEST_HOME=$(mktemp -d)
cd $TEST_HOME
```
{{% /hide %}}

Setters provide a solution for template-free setting or substitution of field
values through package metadata (OpenAPI). They are a safer alternative to
other substitution techniques which do not have the context of the
Expand All @@ -23,6 +33,13 @@ Creating a setter requires that the package has a Kptfile. If one does
not exist for the package, run `kpt pkg init DIR/` to create one.
{{% /pageinfo %}}

{{% hide %}}
<!-- @createKptfile @verifyGuides-->
```
kpt pkg init .
```
{{% /hide %}}

## Setters explained

Following is a short explanation of the command that will be demonstrated
Expand Down Expand Up @@ -52,19 +69,23 @@ command will:
1. create a new OpenAPI definition for a setter in the Kptfile
2. create references to the setter definition on the resource fields

```yaml
<!-- @createResource @verifyGuides-->
```sh
cat <<EOF >deployment.yaml
# deployment.yaml -- original
kind: Deployment
metadata:
name: foo
spec:
replicas: 3
EOF
```

<!-- @createSetter @verifyGuides-->
```sh
# create or update a setter named "replicas"
# match fields with the value "3"
kpt cfg create-setter hello-world/ replicas 3
kpt cfg create-setter . replicas 3
```

```yaml
Expand All @@ -87,6 +108,15 @@ spec:
replicas: 3 # {"$kpt-set":"replicas"}
```

{{% hide %}}
<!-- @validateCreateSetter @verifyGuides-->
```
grep "io.k8s.cli.setters.replicas" Kptfile
grep 'value: "3"' Kptfile
grep 'replicas: 3 # {"$kpt-set":"replicas"}' deployment.yaml
```
{{% /hide %}}

#### Targeting fields using the path

The basic way to create a setter only matches fields based on the value. But
Expand Down Expand Up @@ -154,32 +184,37 @@ gcloud.compute.zone
#### Invoking a Setter

```yaml
# deployment.yaml -- original
kind: Deployment
metadata:
name: helloworld-gke
labels:
app: hello
name: foo
spec:
replicas: 3 # {"$kpt-set":"replicas"}
replicas: 3 # {"$kpt-set":"replicas"}
```

<!-- @setReplicas @verifyGuides-->
```sh
# set the replicas field to 5
kpt cfg set DIR/ replicas 5
kpt cfg set . replicas 5
```

```yaml
# deployment.yaml -- updated
kind: Deployment
metadata:
name: helloworld-gke
labels:
app: hello
name: foo
spec:
replicas: 5 # {"$kpt-set":"replicas"}
replicas: 3 # {"$kpt-set":"replicas"}
```

{{% hide %}}
<!-- @validateSetSetter @verifyGuides-->
```
grep 'value: "5"' Kptfile
grep 'replicas: 5 # {"$kpt-set":"replicas"}' deployment.yaml
kpt cfg delete-setter . replicas
```
{{% /hide %}}

#### OpenAPI Validations

Users can input any additional validation constraints during `create-setter`
Expand All @@ -188,12 +223,14 @@ constraints can be provided in json file format. The `set` operation validates
the input value against provided schema during setter creation and throws an
error if the input value doesn't meet any of the constraints.

<!-- @createSetterForValidation @verifyGuides-->
```sh
$ cat /path/to/file.json
cat <<EOF >schema.json
{"maximum": 10, "type": "integer"}
EOF

# create setter with openAPI property constraints
kpt cfg create-setter DIR/ replicas 3 --schema-path /path/to/file.json
kpt cfg create-setter . replicas 5 --schema-path schema.json
```

The command creates setter with the following definition
Expand All @@ -207,19 +244,26 @@ openAPI:
x-k8s-cli:
setter:
name: replicas
value: "3"
value: "5"
```

```sh
# try to set value not adhering to the constraints
kpt cfg set DIR/ replicas 11
kpt cfg set . replicas 11
```

```sh
error: The input value doesn't validate against provided OpenAPI schema:
validation failure list: replicas in body should be less than or equal to 10
```
{{% hide %}}
<!-- @testSetSetter @verifyGuides-->
```
kpt cfg set . replicas 11 || echo "Worked" | grep "Worked"
```
{{% /hide %}}
```sh
Example schema for integer validation
Expand Down Expand Up @@ -270,22 +314,28 @@ The list setter will take variable args for its value rather than a single value
**Note:** You should skip passing the value arg while creating array setters. `field`
flag is required for array setters.
```yaml
<!-- @createResourceForListSetter @verifyGuides-->
```sh
cat <<EOF >example.yaml
# example.yaml
apiVersion: example.com/v1beta1
kind: Example
spec:
list:
- "a"
- "b"
EOF
```
```yaml
# Kptfile
kind: Kptfile
```
`$ kpt cfg create-setter DIR/ list --type array --field spec.list`
<!-- @createListSetter @verifyGuides-->
```
kpt cfg create-setter . list --type array --field spec.list
```
```yaml
# example.yaml
Expand All @@ -308,11 +358,23 @@ openAPI:
setter:
name: list
listValues:
- "a"
- "b"
- a
- b
```
`$ kpt cfg set DIR/ list c d e`
{{% hide %}}
<!-- @validateCreateListSetter @verifyGuides-->
```
grep "type: array" Kptfile
grep "listValues:" Kptfile
grep 'list: # {"$kpt-set":"list"}' example.yaml
```
{{% /hide %}}

<!-- @setListSetter @verifyGuides-->
```
kpt cfg set . list c d e
```

```yaml
# example.yaml
Expand Down Expand Up @@ -341,6 +403,14 @@ openAPI:
- "e"
```

{{% hide %}}
<!-- @validateSetListSetter @verifyGuides-->
```
grep '\- "d"' Kptfile
grep '\- "d"' example.yaml
```
{{% /hide %}}

#### Enumerations

Setters may be configured to map an enum input to a different value set
Expand Down
3 changes: 2 additions & 1 deletion site/markdownlint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"MD013": false,
"MD024": false,
"MD025": false,
"MD031": false,
"MD040": false,
"MD046": false
}
}

0 comments on commit b4da328

Please sign in to comment.