mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-10 08:20:59 +00:00
Convert plugins to accept bytes instead of unstruct.
This commit is contained in:
@@ -30,7 +30,7 @@ type plugin struct{}
|
||||
var KustomizePlugin plugin
|
||||
|
||||
func (p *plugin) Config(
|
||||
ldr ifc.Loader, rf *resmap.Factory, k ifc.Kunstructured) error {
|
||||
ldr ifc.Loader, rf *resmap.Factory, c []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -26,22 +26,23 @@ import (
|
||||
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||
"sigs.k8s.io/kustomize/pkg/resmap"
|
||||
"sigs.k8s.io/kustomize/pkg/resource"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
type plugin struct {
|
||||
ServiceName string
|
||||
Port string
|
||||
Name string `json:"name,omitempty" yaml:"name,omitempty"`
|
||||
Port string `json:"port,omitempty" yaml:"port,omitempty"`
|
||||
}
|
||||
|
||||
var KustomizePlugin plugin
|
||||
|
||||
var manifest = `
|
||||
const tmpl = `
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
labels:
|
||||
app: dev
|
||||
name: {{.ServiceName}}
|
||||
name: {{.Name}}
|
||||
spec:
|
||||
ports:
|
||||
- port: {{.Port}}
|
||||
@@ -50,23 +51,13 @@ spec:
|
||||
`
|
||||
|
||||
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
|
||||
ldr ifc.Loader, rf *resmap.Factory, config []byte) error {
|
||||
return yaml.Unmarshal(config, p)
|
||||
}
|
||||
|
||||
func (p *plugin) Generate() (resmap.ResMap, error) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
temp := template.Must(template.New("manifest").Parse(manifest))
|
||||
temp := template.Must(template.New("tmpl").Parse(tmpl))
|
||||
err := temp.Execute(&buf, p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -16,44 +16,34 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
// 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"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
type plugin struct {
|
||||
prefix string
|
||||
Metadata metaData `json:"metadata,omitempty" yaml:"metadata,omitempty"`
|
||||
}
|
||||
|
||||
type metaData struct {
|
||||
Name string `json:"name,omitempty" yaml:"name,omitempty"`
|
||||
}
|
||||
|
||||
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
|
||||
ldr ifc.Loader, rf *resmap.Factory, c []byte) error {
|
||||
return yaml.Unmarshal(c, p)
|
||||
}
|
||||
|
||||
func (p *plugin) Transform(m resmap.ResMap) error {
|
||||
tr, err := transformers.NewNamePrefixSuffixTransformer(
|
||||
p.prefix, "",
|
||||
p.Metadata.Name + "-", "",
|
||||
config.MakeDefaultConfig().NamePrefix)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user