Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new kustomize sample #2136

Merged
merged 9 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions package-examples/helloworld-kustomize/Kptfile

This file was deleted.

74 changes: 0 additions & 74 deletions package-examples/helloworld-kustomize/README.md

This file was deleted.

2 changes: 1 addition & 1 deletion package-examples/helloworld-set/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ setters in it.
Get the example package on to local using `kpt pkg get`

```shell
$ kpt pkg get https://github.com/GoogleContainerTools/kpt.git/package-examples/helloworld-set
$ kpt pkg get https://github.com/GoogleContainerTools/kpt.git/package-examples/helloworld-set@next

fetching package /package-examples/helloworld-set from https://github.com/GoogleContainerTools/kpt to helloworld-set
```
Expand Down
2 changes: 1 addition & 1 deletion package-examples/helloworld-tshirt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ resource sizes.
Get the example package on to local using `kpt pkg get`

```shell
$ kpt pkg get https://github.com/GoogleContainerTools/kpt.git/package-examples/helloworld-tshirt
$ kpt pkg get https://github.com/GoogleContainerTools/kpt.git/package-examples/helloworld-tshirt@next

fetching package /package-examples/helloworld-tshirt from https://github.com/GoogleContainerTools/kpt to helloworld-tshirt
```
Expand Down
2 changes: 1 addition & 1 deletion package-examples/helloworld/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ doesn't do anything beyond declaring the current directory as a `kpt` package.
Get the example package on to local using `kpt pkg get`

```shell
$ kpt pkg get https://github.com/GoogleContainerTools/kpt.git/package-examples/helloworld
$ kpt pkg get https://github.com/GoogleContainerTools/kpt.git/package-examples/helloworld@next

fetching package /package-examples/helloworld from https://github.com/GoogleContainerTools/kpt to helloworld
```
Expand Down
6 changes: 6 additions & 0 deletions package-examples/kustomize-pkg/Kptfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: kpt.dev/v1alpha2
kind: Kptfile
metadata:
name: kustomize-pkg
info:
description: kustomize kpt package
162 changes: 162 additions & 0 deletions package-examples/kustomize-pkg/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# kustomize-pkg

This is a simple package that shows how kpt packages can be used instead of remote bases in kustomize. That allows you to take advantage of kpt's rebase and local, in-place edits.

## Steps

1. [Fetch the package](#fetch-the-package)
2. [View the package contents](#view-the-package-contents)
3. [kustomize the config](#kustomize-the-config)
4. [Render the kustomization](#render-the-kustomization)
5. [Apply the package](#apply-the-package)
6. [Clean up resources](#clean-up-resources)

### Fetch the package

Get the example package on to local using `kpt pkg get`. Note that this package is for this example, wihin that package we are also using the nginx sub-package which is an alternative to having a remote base.

```shell
$ kpt pkg get https://github.com/GoogleContainerTools/kpt.git/package-examples/kustomize-pkg@next

fetching package /package-examples/kustomize-pkg from https://github.com/GoogleContainerTools/kpt to kustomize-pkg
```


### View the package contents

List the package contents in a tree structure.

```shell
$ tree kustomize-pkg

kustomize-pkg
├── Kptfile
├── README.md
├── bases
│   └── nginx
│   ├── Kptfile
│   ├── deployment.yaml
│   ├── kustomization.yaml
│   └── svc.yaml
└── overlays
├── dev
│   ├── Kptfile
│   ├── kustomization.yaml
│   └── pass-patch.yaml
└── prod
├── Kptfile
├── kustomization.yaml
└── pass-patch.yaml
```

### kustomize the config

Kustomize is a great tool for out of place hydration, but we don't recommend that you mix kpt packages and remote bases. It's best to have a clear delienation what you are using each tools for: kpt for packages and remote resources, kustomize for hydration like adding labels, setting namespaces and overlays.

In the example below what was a remote base is now fetched locally as a kpt package and kustomize is used for hydration.

```yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../bases/nginx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This structure is probably ok for an example, but I think the recommended way to do this would be to have each env have separate git references. The structure in this example makes it a bit hard to make changes in the base and then do progressive rollouts into different environments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the layout of a solution like that? Every overlay then gets a separate base? Why create an overlay then? We can just have the final config in every folder. I haven't seen a pattern like that, even though I am not opposed to showing it.

- Kptfile
patches:
- path: pass-patch.yaml
target:
kind: Deployment
commonLabels:
environ: dev
namePrefix: dev-
```

### Render the kustomization

You can see the final configuration that the patch is going to render. The major difference between this and in place edit is that you can't just go to the file and look at it in isolation, you need to run `kustomize build` and examine the final results.

```shell
$ kustomize build kustomize-pkg/overlays/dev

apiVersion: v1
kind: Service
metadata:
labels:
app: nginx
environ: dev
name: dev-my-nginx-svc
spec:
ports:
- port: 80
selector:
app: nginx
environ: dev
type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
environ: dev
name: dev-my-nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
environ: dev
template:
metadata:
labels:
app: nginx
environ: dev
spec:
containers:
- image: nginx:1.14.1
name: nginx
ports:
- containerPort: 80
---
apiVersion: kpt.dev/v1alpha2
info:
description: sample description
kind: Kptfile
metadata:
labels:
environ: dev
name: dev-dev


### Apply the package

It is possible to use kustomize build and kpt live apply, but it does require passing the inventory information to `kpt live apply` from `kustomize build` output. It is best to have Kptfiles with inventory information in overlay folders in case the variants of the package are deployed to the same cluster. Every variant of this application will need to be mapped to it\'s own inventory for pruning. In case you have two variants that use the same inventory information the consequent deploy might wipe out the previous variant.

```shell
$ kpt live init kustomize-pkg/overlays/dev

initializing Kptfile inventory info (namespace: default)...success
```

You might have noticed that the overlays have Kptfiles and they are added to the kustomization.yaml so the contents are passed all the way through the kustomize build.

Kustomize build will need to be piped to kpt live apply:

```shell
$ kustomize build kustomize-pkg/overlays/dev | kpt live apply -

service/dev-my-nginx-svc created
deployment.apps/dev-my-nginx created
2 resource(s) applied. 2 created, 0 unchanged, 0 configured, 0 failed
0 resource(s) pruned, 0 skipped, 0 failed
```

### Clean up resources

You can use kpt to prune and clean up resources from your build by using `kpt live destroy`. As long as the inventory information is passed in kpt will know how to clean everything up:

```shell
$ kustomize build kustomize-pkg/overlays/dev | kpt live destroy -

deployment.apps/dev-my-nginx deleted
service/dev-my-nginx-svc deleted
2 resource(s) deleted, 0 skipped
```
21 changes: 21 additions & 0 deletions package-examples/kustomize-pkg/bases/nginx/Kptfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: kpt.dev/v1alpha2
kind: Kptfile
metadata:
name: nginx
upstream:
type: git
git:
repo: https://github.com/GoogleContainerTools/kpt
directory: package-examples/nginx
ref: v0.2
updateStrategy: resource-merge
upstreamLock:
type: git
git:
repo: https://github.com/GoogleContainerTools/kpt
directory: package-examples/nginx
ref: package-examples/nginx/v0.2
commit: 4d2aa98b45ddee4b5fa45fbca16f2ff887de9efb
pipeline:
validators:
- image: gcr.io/kpt-fn/kubeval:v0.1
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,22 @@
# 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.

# This file configures the hello-world app which serves public web traffic.
apiVersion: apps/v1
kind: Deployment
metadata:
name: helloworld-gke
labels:
app: hello
version: "0.1.0"
metadata: # kpt-merge: /my-nginx
name: my-nginx
spec:
replicas: 5
replicas: 3
selector:
matchLabels:
app: hello
app: nginx
template:
metadata:
labels:
app: hello
app: nginx
spec:
containers:
- name: helloworld-gke
image: gcr.io/kpt-dev/helloworld-gke:0.3.0
- name: nginx
image: nginx:1.14.1
ports:
- name: http
containerPort: 80
env:
- name: PORT
value: "80"
- containerPort: 80
3 changes: 3 additions & 0 deletions package-examples/kustomize-pkg/bases/nginx/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resources:
- deployment.yaml
- svc.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2019 Google LLC
# 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.
Expand All @@ -11,22 +11,15 @@
# 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.

# The hello service provides a load-balancing proxy over the hello-app
# pods. By specifying the type as a 'LoadBalancer', Kubernetes Engine will
# create an external HTTP load balancer.
apiVersion: v1
kind: Service
metadata:
name: helloworld-gke
metadata: # kpt-merge: /my-nginx-svc
name: my-nginx-svc
labels:
app: hello
version: '0.1.0'
app: nginx
spec:
type: LoadBalancer
selector:
app: hello
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: http
- port: 80
6 changes: 6 additions & 0 deletions package-examples/kustomize-pkg/overlays/dev/Kptfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: kpt.dev/v1alpha2
kind: Kptfile
metadata:
name: dev
info:
description: sample description
Loading