cmd/config: Add examples and tutorials for config functions

- Add examples under `functions`
- Add built-in tutorial for functions
This commit is contained in:
Phillip Wittrock
2019-11-26 20:08:23 -08:00
parent 5876a8cce0
commit dc66de6bf3
34 changed files with 1524 additions and 2 deletions

View File

@@ -0,0 +1,15 @@
# Copyright 2019 The Kubernetes Authors.
# SPDX-License-Identifier: Apache-2.0
FROM golang:1.13-stretch
ENV CGO_ENABLED=0
WORKDIR /go/src/
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY main.go .
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"]

View File

@@ -0,0 +1,5 @@
module sigs.k8s.io/kustomize/functions/examples/template-go-nginx
go 1.12
require sigs.k8s.io/kustomize/kyaml v0.0.0-20191126203124-d1b33e746881

View File

@@ -0,0 +1,27 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w=
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca h1:1CFlNzQhALwjS9mBAUkycX616GzgsuYUOCHA5+HSlXI=
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20191026110619-0b21df46bc1d h1:LCPbGQ34PMrwad11aMZ+dbz5SAsq/0ySjRwQ8I9Qwd8=
gopkg.in/yaml.v3 v3.0.0-20191026110619-0b21df46bc1d/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
sigs.k8s.io/kustomize/kyaml v0.0.0-20191126203124-d1b33e746881 h1:dmMlmv+TXMSAtCf6Za1GKNAjCssKtgnVkAYRiKdnjIY=
sigs.k8s.io/kustomize/kyaml v0.0.0-20191126203124-d1b33e746881/go.mod h1:rywm/rcR5LmCBghz9956tE45OdUPChFoXVVs+WmhMTI=

View File

@@ -0,0 +1,169 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strconv"
"text/template"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/kio/filters"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
func main() {
rw := &kio.ByteReadWriter{
Reader: os.Stdin,
Writer: os.Stdout,
KeepReaderAnnotations: true,
}
err := kio.Pipeline{
Inputs: []kio.Reader{rw},
Filters: []kio.Filter{
&filter{rw: rw}, // generate the Resources from the template
filters.MergeFilter{}, // merge the generated template
// set Resource filenames
&filters.FileSetter{FilenamePattern: filepath.Join("config", "%n.yaml")},
filters.FormatFilter{}, // format the output
},
Outputs: []kio.Writer{rw},
}.Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
// define the input API schema as a struct
type API struct {
Metadata struct {
// Name is the Deployment Resource and Container name
Name string `yaml:"name"`
} `yaml:"metadata"`
Spec struct {
// Replicas is the number of Deployment replicas
// Defaults to the REPLICAS env var, or 1
Replicas *int `yaml:"replicas"`
} `yaml:"spec"`
}
// filter implements kio.Filter
type filter struct {
rw *kio.ByteReadWriter
}
// Filter checks each input and ensures that all containers have cpu and memory
// reservations set, otherwise it returns an error.
func (f *filter) Filter(in []*yaml.RNode) ([]*yaml.RNode, error) {
api := f.parseAPI()
// execute the service template
buff := &bytes.Buffer{}
t := template.Must(template.New("nginx-service").Parse(serviceTemplate))
if err := t.Execute(buff, api); err != nil {
return nil, err
}
s, err := yaml.Parse(buff.String())
if err != nil {
return nil, err
}
// execute the deployment template
buff = &bytes.Buffer{}
t = template.Must(template.New("nginx-deployment").Parse(deploymentTemplate))
if err := t.Execute(buff, api); err != nil {
return nil, err
}
d, err := yaml.Parse(buff.String())
if err != nil {
return nil, err
}
// add the template generated Resources to the output -- these will get merged by the next
// filter
in = append(in, s, d)
return in, nil
}
// parseAPI parses the functionConfig into an API struct, and validates the input
func (f *filter) parseAPI() API {
// parse the input function config -- TODO: simplify this
var api API
if err := yaml.Unmarshal([]byte(f.rw.FunctionConfig.MustString()), &api); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
// Default functionConfig values from environment variables if they are not set
// in the functionConfig
r := os.Getenv("REPLICAS")
if r != "" && api.Spec.Replicas == nil {
replicas, err := strconv.Atoi(r)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
api.Spec.Replicas = &replicas
}
if api.Spec.Replicas == nil {
r := 1
api.Spec.Replicas = &r
}
if api.Metadata.Name == "" {
fmt.Fprintf(os.Stderr, "must specify metadata.name\n")
os.Exit(1)
}
return api
}
var serviceTemplate = `
apiVersion: v1
kind: Service
metadata:
name: {{ .Metadata.Name }}
labels:
app: nginx
instance: {{ .Metadata.Name }}
spec:
ports:
- port: 80
targetPort: 80
name: http
selector:
app: nginx
instance: {{ .Metadata.Name }}
`
var deploymentTemplate = `apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Metadata.Name }}
labels:
app: nginx
instance: {{ .Metadata.Name }}
spec:
replicas: {{ .Spec.Replicas }}
selector:
matchLabels:
app: nginx
instance: {{ .Metadata.Name }}
template:
metadata:
labels:
app: nginx
instance: {{ .Metadata.Name }}
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
`