-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add goplugin for exec generators and transformers
- Loading branch information
1 parent
ee68a9c
commit ba43ecb
Showing
7 changed files
with
180 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// +build plugin | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
|
||
"github.com/ghodss/yaml" | ||
"sigs.k8s.io/kustomize/pkg/ifc" | ||
"sigs.k8s.io/kustomize/pkg/pgmconfig" | ||
"sigs.k8s.io/kustomize/pkg/resmap" | ||
) | ||
|
||
type plugin struct { | ||
name string | ||
input string | ||
rf *resmap.Factory | ||
} | ||
|
||
var KustomizePlugin plugin | ||
|
||
func (p *plugin) Config( | ||
ldr ifc.Loader, rf *resmap.Factory, k ifc.Kunstructured) error { | ||
dir := filepath.Join(pgmconfig.ConfigRoot(), "plugins") | ||
id := k.GetGvk() | ||
p.name = filepath.Join(dir, id.Group, id.Version, id.Kind) | ||
content, err := yaml.Marshal(k) | ||
if err != nil { | ||
return err | ||
} | ||
p.input = string(content) | ||
p.rf = rf | ||
return nil | ||
} | ||
|
||
func (p *plugin) Generate() (resmap.ResMap, error) { | ||
return p.run(nil) | ||
} | ||
|
||
func (p *plugin) Transformer(rm resmap.ResMap) error { | ||
result, err := p.run(rm) | ||
if err != nil { | ||
return err | ||
} | ||
for id := range rm { | ||
delete(rm, id) | ||
} | ||
for id, r := range result { | ||
rm[id] = r | ||
} | ||
return nil | ||
} | ||
|
||
func (p *plugin) run(rm resmap.ResMap) (resmap.ResMap, error) { | ||
cmd := exec.Command(p.name, p.input) | ||
cmd.Env = os.Environ() | ||
if rm != nil { | ||
content, err := rm.EncodeAsYaml() | ||
if err != nil { | ||
return nil, err | ||
} | ||
cmd.Stdin = bytes.NewReader(content) | ||
} | ||
output, err := cmd.Output() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return p.rf.NewResMapFromBytes(output) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/bash | ||
|
||
echo " | ||
kind: ConfigMap | ||
apiVersion: v1 | ||
metadata: | ||
name: example-configmap-test | ||
data: | ||
username: admin | ||
password: secret | ||
" |