mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-23 15:27:01 +00:00
Make plugin dir match Go conventions.
This commit is contained in:
77
plugin/builtin/ChartInflatorExec
Executable file
77
plugin/builtin/ChartInflatorExec
Executable file
@@ -0,0 +1,77 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
# Helm chart inflator
|
||||
|
||||
# Reads a file like this
|
||||
#
|
||||
# apiVersion: kustomize.config.k8s.io/v1
|
||||
# kind: ChartInflatorExec
|
||||
# metadata:
|
||||
# name: notImportantHere
|
||||
# chart: chartName
|
||||
# values: /absolute/path/to/values/file
|
||||
# helmBin: /absolute/path/to/helmBin
|
||||
#
|
||||
# fetches the given chart from stable/$chartName,
|
||||
# and inflates it to stdout, using the given values file.
|
||||
#
|
||||
# Example execution:
|
||||
# ./plugins/kustomize.config.k8s.io/v1/ChartInflatorExec configFile.yaml
|
||||
|
||||
|
||||
|
||||
# Yaml parsing is a ridiculous thing to do in bash,
|
||||
# but let's try:
|
||||
function parseYaml {
|
||||
local file=$1
|
||||
while read -r line
|
||||
do
|
||||
local k=${line%:*}
|
||||
local v=${line#*:}
|
||||
|
||||
[ "$k" == "chart" ] && chartName=$v
|
||||
[ "$k" == "values" ] && valuesFile=$v
|
||||
[ "$k" == "helmBin" ] && helmBin=$v
|
||||
done <"$file"
|
||||
|
||||
# Trim leading space
|
||||
chartName="${chartName#"${chartName%%[![:space:]]*}"}"
|
||||
valuesFile="${valuesFile#"${valuesFile%%[![:space:]]*}"}"
|
||||
helmBin="${helmBin#"${helmBin%%[![:space:]]*}"}"
|
||||
}
|
||||
|
||||
TMP_DIR=$(mktemp -d)
|
||||
|
||||
# Where all the files generated by 'helm init' live.
|
||||
HELM_HOME=$TMP_DIR/dotHelm
|
||||
|
||||
# Where helm charts are unpacked.
|
||||
CHART_HOME=$TMP_DIR/charts
|
||||
|
||||
parseYaml $1
|
||||
|
||||
if [ -z "$helmBin" ]; then
|
||||
helmBin=/usr/local/bin/helm
|
||||
fi
|
||||
|
||||
if [ -z "$valuesFile" ]; then
|
||||
valuesFile=$CHART_HOME/$chartName/values.yaml
|
||||
fi
|
||||
|
||||
function doHelm {
|
||||
$helmBin --home $HELM_HOME $@
|
||||
}
|
||||
|
||||
# The init command is extremely chatty
|
||||
doHelm init --client-only >& /dev/null
|
||||
|
||||
doHelm fetch --untar \
|
||||
--untardir $CHART_HOME \
|
||||
stable/$chartName
|
||||
|
||||
doHelm template \
|
||||
--values $valuesFile \
|
||||
$CHART_HOME/$chartName
|
||||
|
||||
/bin/rm -rf $TMP_DIR
|
||||
62
plugin/builtin/SecretGenerator.go
Normal file
62
plugin/builtin/SecretGenerator.go
Normal file
@@ -0,0 +1,62 @@
|
||||
// +build plugin
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||
"sigs.k8s.io/kustomize/pkg/resmap"
|
||||
"sigs.k8s.io/kustomize/pkg/types"
|
||||
)
|
||||
|
||||
type plugin struct {
|
||||
ldr ifc.Loader
|
||||
rf *resmap.Factory
|
||||
options types.GeneratorOptions
|
||||
args types.SecretArgs
|
||||
}
|
||||
|
||||
var KustomizePlugin plugin
|
||||
|
||||
func (p *plugin) Config(
|
||||
ldr ifc.Loader, rf *resmap.Factory, k ifc.Kunstructured) error {
|
||||
p.ldr = ldr
|
||||
p.rf = rf
|
||||
|
||||
var err error
|
||||
// TODO: Should validate this.
|
||||
p.args.Behavior, err = k.GetFieldValue("behavior")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
envFiles, err := k.GetStringSlice("envFiles")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(envFiles) > 2 {
|
||||
// TODO: refactor to allow this
|
||||
return fmt.Errorf("cannot yet accept more than one envFile")
|
||||
}
|
||||
if len(envFiles) > 0 {
|
||||
p.args.EnvSource = envFiles[0]
|
||||
}
|
||||
|
||||
p.args.FileSources, err = k.GetStringSlice("valueFiles")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.args.LiteralSources, err = k.GetStringSlice("literals")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *plugin) Generate() (resmap.ResMap, error) {
|
||||
argsList := make([]types.SecretArgs, 1)
|
||||
argsList[0] = p.args
|
||||
return p.rf.NewResMapFromSecretArgs(p.ldr, &p.options, argsList)
|
||||
}
|
||||
14
plugin/someteam.example.com/v1/ConfigMapGenerator
Executable file
14
plugin/someteam.example.com/v1/ConfigMapGenerator
Executable file
@@ -0,0 +1,14 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Skip the config file name argument.
|
||||
shift
|
||||
|
||||
echo "
|
||||
kind: ConfigMap
|
||||
apiVersion: v1
|
||||
metadata:
|
||||
name: example-configmap-test
|
||||
data:
|
||||
username: $1
|
||||
password: $2
|
||||
"
|
||||
37
plugin/someteam.example.com/v1/DatePrefixer.go
Normal file
37
plugin/someteam.example.com/v1/DatePrefixer.go
Normal file
@@ -0,0 +1,37 @@
|
||||
// +build plugin
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||
"sigs.k8s.io/kustomize/pkg/resmap"
|
||||
"sigs.k8s.io/kustomize/pkg/transformers"
|
||||
"sigs.k8s.io/kustomize/pkg/transformers/config"
|
||||
)
|
||||
|
||||
type plugin struct{}
|
||||
|
||||
var KustomizePlugin plugin
|
||||
|
||||
func (p *plugin) Config(
|
||||
ldr ifc.Loader, rf *resmap.Factory, k ifc.Kunstructured) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Returns a constant, rather than
|
||||
// time.Now().Format("2006-01-02")
|
||||
// to make tests happy.
|
||||
// This is just an example.
|
||||
func getDate() string {
|
||||
return "2018-05-11"
|
||||
}
|
||||
|
||||
func (p *plugin) Transform(m resmap.ResMap) error {
|
||||
tr, err := transformers.NewNamePrefixSuffixTransformer(
|
||||
getDate()+"-", "",
|
||||
config.MakeDefaultConfig().NamePrefix)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return tr.Transform(m)
|
||||
}
|
||||
13
plugin/someteam.example.com/v1/PrintWorkDir
Executable file
13
plugin/someteam.example.com/v1/PrintWorkDir
Executable file
@@ -0,0 +1,13 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
dir=`pwd`
|
||||
|
||||
echo "
|
||||
kind: WorkDir
|
||||
apiVersion: v1
|
||||
metadata:
|
||||
name: $dir
|
||||
spec:
|
||||
path: ${KUSTOMIZE_PLUGIN_CONFIG_ROOT}
|
||||
"
|
||||
11
plugin/someteam.example.com/v1/SedTransformer
Executable file
11
plugin/someteam.example.com/v1/SedTransformer
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Skip the config file name argument.
|
||||
shift
|
||||
|
||||
args=""
|
||||
for arg in $@; do
|
||||
args="$args -e $arg"
|
||||
done
|
||||
|
||||
cat - | sed $args
|
||||
60
plugin/someteam.example.com/v1/ServiceGenerator.go
Normal file
60
plugin/someteam.example.com/v1/ServiceGenerator.go
Normal file
@@ -0,0 +1,60 @@
|
||||
// +build plugin
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"text/template"
|
||||
|
||||
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
|
||||
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||
"sigs.k8s.io/kustomize/pkg/resmap"
|
||||
"sigs.k8s.io/kustomize/pkg/resource"
|
||||
)
|
||||
|
||||
type plugin struct {
|
||||
ServiceName string
|
||||
Port string
|
||||
}
|
||||
|
||||
var KustomizePlugin plugin
|
||||
|
||||
var manifest = `
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
labels:
|
||||
app: dev
|
||||
name: {{.ServiceName}}
|
||||
spec:
|
||||
ports:
|
||||
- port: {{.Port}}
|
||||
selector:
|
||||
app: dev
|
||||
`
|
||||
|
||||
func (p *plugin) Config(
|
||||
ldr ifc.Loader, rf *resmap.Factory, k ifc.Kunstructured) error {
|
||||
var err error
|
||||
p.ServiceName, err = k.GetFieldValue("service")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.Port, err = k.GetFieldValue("port")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *plugin) Generate() (resmap.ResMap, error) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
temp := template.Must(template.New("manifest").Parse(manifest))
|
||||
err := temp.Execute(&buf, p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rf := resmap.NewFactory(resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl()))
|
||||
return rf.NewResMapFromBytes(buf.Bytes())
|
||||
}
|
||||
46
plugin/someteam.example.com/v1/StringPrefixer.go
Normal file
46
plugin/someteam.example.com/v1/StringPrefixer.go
Normal file
@@ -0,0 +1,46 @@
|
||||
// +build plugin
|
||||
|
||||
// Assuming GOPATH is something like
|
||||
// ~/gopath
|
||||
// and this source file is located at
|
||||
// $GOPATH/src/sigs.k8s.io/kustomize/plugins/StringPrefixer.go,
|
||||
// build it like this:
|
||||
// dir=$GOPATH/src/sigs.k8s.io/kustomize/plugins
|
||||
// go build -buildmode plugin -tags=plugin \
|
||||
// -o $dir/StringPrefixer.so $dir/StringPrefixer.go
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||
"sigs.k8s.io/kustomize/pkg/resmap"
|
||||
"sigs.k8s.io/kustomize/pkg/transformers"
|
||||
"sigs.k8s.io/kustomize/pkg/transformers/config"
|
||||
)
|
||||
|
||||
type plugin struct {
|
||||
prefix string
|
||||
}
|
||||
|
||||
var KustomizePlugin plugin
|
||||
|
||||
func (p *plugin) Config(
|
||||
ldr ifc.Loader, rf *resmap.Factory, k ifc.Kunstructured) error {
|
||||
name := k.GetName()
|
||||
if name == "" {
|
||||
return fmt.Errorf("name cannot be empty")
|
||||
}
|
||||
p.prefix = name + "-"
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *plugin) Transform(m resmap.ResMap) error {
|
||||
tr, err := transformers.NewNamePrefixSuffixTransformer(
|
||||
p.prefix, "",
|
||||
config.MakeDefaultConfig().NamePrefix)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return tr.Transform(m)
|
||||
}
|
||||
27
plugin/someteam.example.com/v1/kvMaker.go
Normal file
27
plugin/someteam.example.com/v1/kvMaker.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// +build plugin
|
||||
|
||||
package main
|
||||
|
||||
var database = map[string]string{
|
||||
"TREE": "oak",
|
||||
"ROCKET": "Saturn V",
|
||||
"FRUIT": "apple",
|
||||
"VEGETABLE": "carrot",
|
||||
"SIMPSON": "homer",
|
||||
}
|
||||
|
||||
type plugin struct{}
|
||||
|
||||
var KVSource plugin
|
||||
|
||||
func (p plugin) Get(
|
||||
root string, args []string) (map[string]string, error) {
|
||||
r := make(map[string]string)
|
||||
for _, k := range args {
|
||||
v, ok := database[k]
|
||||
if ok {
|
||||
r[k] = v
|
||||
}
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
Reference in New Issue
Block a user