Skip to content

Commit

Permalink
kustomize fn to transform images
Browse files Browse the repository at this point in the history
* This PR defines a kustomize/kpt function to transform images based on their prefix.

  * This is an alternative to using kfctl mirror overwrite

  * This works as follows.

    i) you define a function config like the following

       ```
        apiVersion: v1alpha1 # Define a transform to change all the image prefixes to use images from a different registry
     kind: ImagePrefix
     metadata:
       name: use-mirror-images-gcr
       annotations:
         config.kubernetes.io/function: |
           container:
             image: gcr.io/kubeflow-images-public/kpt-fns:v1.0-rc.3-58-g616f986-dirty
     spec:
       imageMappings:
       - src: quay.io/jetstack
         dest: gcr.io/gcp-private-dev/jetstack # {"type":"string","x-kustomize":{"setBy":"kpt","partialSetters":[{"name":"gcloud.core.project","value":"gcp-private-dev"}]}}
       - src: gcr.io/kubeflow-images-public
         dest: gcr.io/gcp-private-dev # {"type":"string","x-kustomize":{"setBy":"kpt","partialSetters":[{"name":"gcloud.core.project","value":"gcp-private-dev"}]}}
       ```

   ii) You use kpt to appy the function e.g.

       ```
       kpt fn ${DIR}
       ```

       * Typically you would apply this to the hydrated manfiests
       * The transform will look for any objects with `image-prefix.kubeflow.org` annotation
         and then map the image prefixes specified in that annotation

* The kpt function has a number of advantages over the kfctl functionality

  * In particular the transform configuration (i.e. the annotation) doesn't need to change
    when a user upgrades and the image tags change. Since the transform is applied after
    hydration we would end up applying the transform to any docker images

  * `kpt fns` are more scalable then baking all the functionality into a single go binary

    * It also looks like this functionality will be upstreamed into kustomize

* kubeflow/manifests#1033 proposed a similar transformer for images.

  * This is different only in we are using kpt/kustomize functions rather than kustomize transformations
  * kpt functions are more flexible because its YAML in YAML out; so you could just as easily
  • Loading branch information
Jeremy Lewi committed Jun 4, 2020
1 parent 4c1060b commit 4a696ae
Show file tree
Hide file tree
Showing 18 changed files with 1,059 additions and 27 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ require (
k8s.io/kubernetes v1.16.2
k8s.io/utils v0.0.0-20191030222137-2b95a09bc58d // indirect
sigs.k8s.io/controller-runtime v0.4.0
sigs.k8s.io/kustomize/kyaml v0.1.10
sigs.k8s.io/kustomize/v3 v3.2.0
sigs.k8s.io/yaml v1.1.0
)
Expand All @@ -65,6 +66,7 @@ replace (
github.com/go-openapi/jsonreference => github.com/go-openapi/jsonreference v0.17.0
github.com/go-openapi/spec => github.com/go-openapi/spec v0.18.0
github.com/go-openapi/swag => github.com/go-openapi/swag v0.17.0
github.com/kubeflow/kfctl/v3 => ./
github.com/mitchellh/go-homedir => github.com/mitchellh/go-homedir v1.0.0
github.com/otiai10/copy => github.com/otiai10/copy v1.0.2
github.com/otiai10/mint => github.com/otiai10/mint v1.3.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/PuerkitoBio/goquery v1.5.0/go.mod h1:qD2PgZ9lccMbQlc7eEOjaeRlFQON7xY8kdmcsrnKqMg=
github.com/PuerkitoBio/purell v1.0.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/purell v1.1.0 h1:rmGxhojJlM0tuKtfdvliR84CFHljx9ag64t2xmVkjK4=
github.com/PuerkitoBio/purell v1.1.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI=
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
Expand Down Expand Up @@ -1242,6 +1243,8 @@ sigs.k8s.io/controller-runtime v0.2.0/go.mod h1:ZHqrRDZi3f6BzONcvlUxkqCKgwasGk5F
sigs.k8s.io/controller-tools v0.2.2/go.mod h1:8SNGuj163x/sMwydREj7ld5mIMJu1cDanIfnx6xsU70=
sigs.k8s.io/kustomize v2.0.3+incompatible h1:JUufWFNlI44MdtnjUqVnvh29rR37PQFzPbLXqhyOyX0=
sigs.k8s.io/kustomize v2.0.3+incompatible/go.mod h1:MkjgH3RdOWrievjo6c9T245dYlB5QeXV4WCbnt/PEpU=
sigs.k8s.io/kustomize/kyaml v0.1.10 h1:ZZfBnA/kYa9ZxUFIgI9oq3pWKzzA1gGOKgU0Hv/NhVg=
sigs.k8s.io/kustomize/kyaml v0.1.10/go.mod h1:mPmeBSRy0LTMv6fSrYSoi2yIFNZVouGKDsTekE5kdhs=
sigs.k8s.io/kustomize/kyaml v0.1.11 h1:/VvWxVIgH5gG1K4A7trgbyLgO3tRBiAWNhLFVU1HEmo=
sigs.k8s.io/kustomize/kyaml v0.1.11/go.mod h1:72/rLkSi+L/pHM1oCjwrf3ClU+tH5kZQvvdLSqIHwWU=
sigs.k8s.io/kustomize/v3 v3.2.0 h1:EKcEubO29vCbigcMoNynfyZH+ANWkML2UHWibt1Do7o=
Expand Down
1 change: 1 addition & 0 deletions kustomize-fns/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kustomize-fns
21 changes: 21 additions & 0 deletions kustomize-fns/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2019 The Kubernetes Authors.
# SPDX-License-Identifier: Apache-2.0

# TODO(jlewi): We end up pulling go.mod for all of kfctl which pulls in a lot of dependencies
# which slows down builds. That's pretty unnecessary. The only code shared with kfctl is
# some test utilities in particular utils.PrintDiff it might be worthmaking the test package
# its own go module so we don't have to pull in so many dependencies just to build
# our kustomize functions.

FROM golang:1.13-stretch
ENV CGO_ENABLED=0
WORKDIR /go/src/
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
RUN go build -v -o /usr/local/bin/config-function ./

FROM alpine:latest
COPY --from=0 /usr/local/bin/config-function /usr/local/bin/config-function
CMD ["config-function"]
1 change: 0 additions & 1 deletion kustomize-fns/OWNERS
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@

approvers:
- jlewi
66 changes: 58 additions & 8 deletions kustomize-fns/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,63 @@
# Kustomize FNs
# kpt/kustomize FNs

This directory contains kustomize functions ala
https://github.com/kubernetes-sigs/kustomize/tree/master/functions/examples/injection-tshirt-sizes
This directory contains [kpt functions](https://googlecontainertools.github.io/kpt/reference/fn/)
for apply various transformations to your YAMLs.

Functions are away to define custom transformations
that can be applied via kustomize.
These kustomizations are intended to be applied using kpt. It looks like support for functions
is being upstreamed into kustomize and currently has alpha support.

## Warning this is experimental
## Usage

The use of kustomize functions is still experimental.
The [kpt docs](https://googlecontainertools.github.io/kpt/reference/fn/run/#examples) describe
many ways to invoke kpt functions. For production the most common use will be the
[declarative method](https://googlecontainertools.github.io/kpt/reference/fn/run/#declaratively-run-one-or-more-functions).

The use of kustomize functions is still considered Alpha in kustomize.
1. In the directory you want to apply the transform either define a config specifying the transform

* Depending on the transform this will either be a [ConfigMap](https://googlecontainertools.github.io/kpt/reference/fn/run/#declaratively-run-one-or-more-functions)
or a custom resource

* In both cases the annotation `config.kubernetes.io/function` specifies the
docker image to use for the config.

* As an example for the image prefix transform we define a config like the following

```
apiVersion: v1alpha1 # Define a transform to change all the image prefixes to use images from a different registry
kind: ImagePrefix
metadata:
name: use-mirror-images-gcr
annotations:
config.kubernetes.io/function: |
container:
image: gcr.io/kubeflow-images-public/kpt-fns:v1.0-rc.3-58-g616f986-dirty
spec:
imageMappings:
- src: quay.io/jetstack
dest: gcr.io/gcp-private-dev/jetstack # {"type":"string","x-kustomize":{"setBy":"kpt","partialSetters":[{"name":"gcloud.core.project","value":"gcp-private-dev"}]}}
- src: gcr.io/kubeflow-images-public
dest: gcr.io/gcp-private-dev # {"type":"string","x-kustomize":{"setBy":"kpt","partialSetters":[{"name":"gcloud.core.project","value":"gcp-private-dev"}]}}
```
1. Run kpt fn

```
kpt fn ${DIR}
```

## Development

During development it can useful to run your transforms without building a docker image or
run them in a debugger.

To support this our main binary `kustomize-fns` includes a `debug` subcommand which
allows you to specify the path of a YAML file containing a `ResourceList` defining
the resources and functions to apply. You can produce that file using kpt; e.g.

```
kpt fn source ${DIR} --function-config=${FN_CFG}
```

* *DIR* should be the directory containing the YAML files defining the resources to process
* *FN_CFG* should be a path to a YAML file specifying one or more functions to apply

10 changes: 10 additions & 0 deletions kustomize-fns/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/kubeflow/kfctl/kustomize-fns

go 1.13

require (
github.com/pkg/errors v0.8.0
github.com/spf13/cobra v1.0.0
k8s.io/apimachinery v0.18.3
sigs.k8s.io/kustomize/kyaml v0.1.13
)
Loading

0 comments on commit 4a696ae

Please sign in to comment.