Skip to content

Commit

Permalink
Use set-label instead of apply-setters in wordpress package. (#2016)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankfarzan committed May 14, 2021
1 parent a68f983 commit 901cf1f
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 117 deletions.
15 changes: 1 addition & 14 deletions package-examples/nginx/deployment.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
apiVersion: apps/v1
kind: Deployment
metadata:
Expand All @@ -27,7 +14,7 @@ spec:
spec:
containers:
- name: nginx
image: 'nginx:1.14.2'
image: "nginx:1.14.2"
ports:
- protocol: TCP
containerPort: 80
13 changes: 0 additions & 13 deletions package-examples/nginx/svc.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
apiVersion: v1
kind: Service
metadata:
Expand Down
5 changes: 2 additions & 3 deletions package-examples/wordpress/Kptfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ info:
description: This is an example wordpress package with mysql subpackage.
pipeline:
mutators:
- image: gcr.io/kpt-fn/apply-setters:v0.1
- image: gcr.io/kpt-fn/set-label:v0.1
configMap:
wp-image: wordpress
wp-tag: 4.8-apache
app: wordpress
validators:
- image: gcr.io/kpt-fn/kubeval:v0.1
15 changes: 1 addition & 14 deletions package-examples/wordpress/deployment/deployment.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
apiVersion: apps/v1
kind: Deployment
metadata:
Expand All @@ -30,7 +17,7 @@ spec:
spec:
containers:
- name: wordpress
image: wordpress:4.8-apache # kpt-set: ${wp-image}:${wp-tag}
image: wordpress:4.8-apache
ports:
- name: wordpress
protocol: TCP
Expand Down
13 changes: 0 additions & 13 deletions package-examples/wordpress/deployment/volume.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
Expand Down
5 changes: 2 additions & 3 deletions package-examples/wordpress/mysql/Kptfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ info:
description: This is an example mysql package.
pipeline:
mutators:
- image: gcr.io/kpt-fn/apply-setters:v0.1
- image: gcr.io/kpt-fn/set-label:v0.1
configMap:
ms-image: mysql
ms-tag: 5.6
tier: mysql
20 changes: 5 additions & 15 deletions package-examples/wordpress/mysql/deployment.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
apiVersion: v1
kind: Service
metadata:
name: wordpress-mysql
labels:
app: wordpress
tier: mysql
spec:
selector:
app: wordpress
Expand All @@ -32,19 +20,21 @@ metadata:
name: mysql-pv-claim
labels:
app: wordpress
tier: mysql
spec:
resources:
requests:
storage: 20Gi
accessModes:
- ReadWriteOnce
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress-mysql
labels:
app: wordpress
tier: mysql
spec:
selector:
matchLabels:
Expand All @@ -58,7 +48,7 @@ spec:
spec:
containers:
- name: mysql
image: mysql:5.6 # kpt-set: ${ms-image}:${ms-tag}
image: mysql:5.6
ports:
- name: mysql
protocol: TCP
Expand Down
10 changes: 4 additions & 6 deletions site/book/03-packages/01-getting-a-package.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ info:
description: This is an example wordpress package with mysql subpackage
pipeline:
mutators:
- image: gcr.io/kpt-fn/apply-setters:v0.1
- image: gcr.io/kpt-fn/set-label:v0.1
configMap:
wp-image: wordpress
wp-tag: 4.8-apache
app: wordpress
```

The `Kptfile` contains two sections to keep track of the upstream package:
Expand All @@ -67,10 +66,9 @@ metadata:
name: mysql
pipeline:
mutators:
- image: gcr.io/kpt-fn/apply-setters:v0.1
- image: gcr.io/kpt-fn/set-label:v0.1
configMap:
ms-image: mysql
ms-tag: 5.6
tier: mysql
```

As you can see, this `Kptfile` doesn't have the `upstream` and `upstreamLock` sections.
Expand Down
46 changes: 18 additions & 28 deletions site/book/04-using-functions/01-declarative-function-execution.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,17 @@ metadata:
name: wordpress
pipeline:
mutators:
- image: gcr.io/kpt-fn/apply-setters:v0.1
- image: gcr.io/kpt-fn/set-label:v0.1
configMap:
wp-image: wordpress
wp-tag: 4.8-apache
app: wordpress
validators:
- image: gcr.io/kpt-fn/kubeval:v0.1
```

This declares two functions:

- `apply-setters` is an example mutator function which provides a form of parameterization. You
don't need to understand the nitty-gritty of how this function works except that it mutates
resources that have been parameterized with a given key (e.g. `wp-tag`) by applying the
corresponding value (e.g. `4.8-apache`).
- `kubeval` is an example validator function which you've seen before. It validates the resources
against their OpenAPI schema.
- `set-label` is a mutator function which adds a set of labels to resources.
- `kubeval` is a validator function which validates the resources against their OpenAPI schema.

?> Refer to the [Functions Catalog][func-cat] for details on how to use a particular function.

Expand All @@ -54,10 +49,9 @@ metadata:
name: mysql
pipeline:
mutators:
- image: gcr.io/kpt-fn/apply-setters:v0.1
- image: gcr.io/kpt-fn/set-label:v0.1
configMap:
ms-image: mysql
ms-tag: 5.6
tier: mysql
```

Now, let's render the package hierarchy:
Expand All @@ -66,11 +60,11 @@ Now, let's render the package hierarchy:
$ kpt fn render wordpress
Package "wordpress/mysql":

[PASS] "gcr.io/kpt-fn/apply-setters:v0.1"
[PASS] "gcr.io/kpt-fn/set-label:v0.1"

Package "wordpress":

[PASS] "gcr.io/kpt-fn/apply-setters:v0.1"
[PASS] "gcr.io/kpt-fn/set-label:v0.1"
[PASS] "gcr.io/kpt-fn/kubeval:v0.1"

Successfully executed 3 function(s) in 2 package(s).
Expand All @@ -92,18 +86,16 @@ When you invoke the `render` command, kpt performs the following steps:
- Resources read from configuration files in the `wordpress` package AND
- Output of the pipeline from the `mysql` package (Step 2).

Note that it is possible for a mutator in the parent package (i.e `wordpress`) to modify resources in the subpackages (i.e. `mysql`).

4. Similarly, execute all the validators declared in the `wordpress` package. The output of the last
validator is the output of the pipeline in the `wordpress` package.
5. Write the output of step 4 by modifying the local filesystem in-place. This can change both
`wordpress` and `mysql` packages.

The end result is that:

1. Resources in the `mysql` package parameterized with `ms-image` and `ms-tag` are modified.
2. Resources in the `wordpress` package parameterized with `wp-image` and `wp-tag` are modified.
3. All resources in `mysql` and `wordpress` are validated against their OpenAPI spec.
1. Resources in the `mysql` package are labelled with `tier: mysql`.
2. Resources in `mysql` and `wordpress` packages are labelled with `app: wordpress`.
3. Resources in `mysql` and `wordpress` packages are validated against their OpenAPI spec.

If any of the functions in the pipeline fails for whatever reason, then the entire pipeline
is aborted and the local filesystem is left intact.
Expand Down Expand Up @@ -132,19 +124,18 @@ metadata:
name: mysql
pipeline:
mutators:
- image: gcr.io/kpt-fn/apply-setters:v0.1
configPath: setters.yaml
- image: gcr.io/kpt-fn/set-label:v0.1
configPath: labels.yaml
```

```yaml
# wordpress/mysql/setters.yaml
# wordpress/mysql/labels.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: setterValues
name: labels
data:
ms-image: mysql
ms-tag: 5.6
tier: mysql
```

### `configMap`
Expand All @@ -162,10 +153,9 @@ metadata:
name: mysql
pipeline:
mutators:
- image: gcr.io/kpt-fn/apply-setters:v0.1
- image: gcr.io/kpt-fn/set-label:v0.1
configMap:
ms-image: mysql
ms-tag: 5.6
tier: mysql
```

[func-cat]: http://catalog.kpt.dev
Expand Down
16 changes: 8 additions & 8 deletions site/book/04-using-functions/03-function-results.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ For example:
$ kpt fn render wordpress --results-dir /tmp
Package "wordpress/mysql":

[PASS] "gcr.io/kpt-fn/apply-setters:v0.1"
[PASS] "gcr.io/kpt-fn/set-label:v0.1"

Package "wordpress":

[PASS] "gcr.io/kpt-fn/apply-setters:v0.1"
[PASS] "gcr.io/kpt-fn/set-label:v0.1"
[PASS] "gcr.io/kpt-fn/kubeval:v0.1"

Successfully executed 3 function(s) in 2 package(s).
Expand All @@ -36,9 +36,9 @@ metadata:
name: fnresults
exitCode: 0
items:
- image: gcr.io/kpt-fn/apply-setters:v0.1
- image: gcr.io/kpt-fn/set-label:v0.1
exitCode: 0
- image: gcr.io/kpt-fn/apply-setters:v0.1
- image: gcr.io/kpt-fn/set-label:v0.1
exitCode: 0
- image: gcr.io/kpt-fn/kubeval:v0.1
exitCode: 0
Expand All @@ -52,11 +52,11 @@ rerun:
$ kpt fn render wordpress --results-dir /tmp
Package "wordpress/mysql":

[PASS] "gcr.io/kpt-fn/apply-setters:v0.1"
[PASS] "gcr.io/kpt-fn/set-label:v0.1"

Package "wordpress":

[PASS] "gcr.io/kpt-fn/apply-setters:v0.1"
[PASS] "gcr.io/kpt-fn/set-label:v0.1"
[FAIL] "gcr.io/kpt-fn/kubeval:v0.1"
Results:
[ERROR] Invalid type. Expected: integer, given: string in object "v1/Service/" in file "service.yaml" in field "spec.ports.0.port"
Expand All @@ -75,9 +75,9 @@ metadata:
name: fnresults
exitCode: 1
items:
- image: gcr.io/kpt-fn/apply-setters:v0.1
- image: gcr.io/kpt-fn/set-label:v0.1
exitCode: 0
- image: gcr.io/kpt-fn/apply-setters:v0.1
- image: gcr.io/kpt-fn/set-label:v0.1
exitCode: 0
- image: gcr.io/kpt-fn/kubeval:v0.1
exitCode: 1
Expand Down

0 comments on commit 901cf1f

Please sign in to comment.