mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-10 08:20:59 +00:00
address comments
This commit is contained in:
@@ -134,17 +134,7 @@ func (p *ExecPlugin) writeConfig() (string, error) {
|
||||
}
|
||||
|
||||
func (p *ExecPlugin) Generate() (resmap.ResMap, error) {
|
||||
args, err := p.getArgs()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cmd := exec.Command(p.name, args...)
|
||||
cmd.Env = p.getEnv()
|
||||
cmd.Stderr = os.Stderr
|
||||
if _, err := os.Stat(p.ldr.Root()); err == nil {
|
||||
cmd.Dir = p.ldr.Root()
|
||||
}
|
||||
output, err := cmd.Output()
|
||||
output, err := p.invokePlugin(nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -152,30 +142,42 @@ func (p *ExecPlugin) Generate() (resmap.ResMap, error) {
|
||||
}
|
||||
|
||||
func (p *ExecPlugin) Transform(rm resmap.ResMap) error {
|
||||
args, err := p.getArgs()
|
||||
// add ResIds as annotations to all objects so that we can add them back
|
||||
inputRM, err := p.getResMapWithIdAnnotation(rm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
inputRM := p.getResMapWithIdAnnotation(rm)
|
||||
// encode the ResMap so it can be fed to the plugin
|
||||
resources, err := inputRM.EncodeAsYaml()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd := exec.Command(p.name, args...)
|
||||
cmd.Env = p.getEnv()
|
||||
cmd.Stdin = bytes.NewReader(resources)
|
||||
cmd.Stderr = os.Stderr
|
||||
if _, err := os.Stat(p.ldr.Root()); err == nil {
|
||||
cmd.Dir = p.ldr.Root()
|
||||
}
|
||||
output, err := cmd.Output()
|
||||
// invoke the plugin with resources as the input
|
||||
output, err := p.invokePlugin(resources)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return p.parseResMapFromOutput(output, rm)
|
||||
// update the original ResMap based on the output
|
||||
return p.updateResMapValues(output, rm)
|
||||
}
|
||||
|
||||
// invokePlugin invokes the plugin
|
||||
func (p *ExecPlugin) invokePlugin(input []byte) ([]byte, error) {
|
||||
args, err := p.getArgs()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cmd := exec.Command(p.name, args...)
|
||||
cmd.Env = p.getEnv()
|
||||
cmd.Stdin = bytes.NewReader(input)
|
||||
cmd.Stderr = os.Stderr
|
||||
if _, err := os.Stat(p.ldr.Root()); err == nil {
|
||||
cmd.Dir = p.ldr.Root()
|
||||
}
|
||||
return cmd.Output()
|
||||
}
|
||||
|
||||
// The first arg is always the absolute path to a temporary file
|
||||
@@ -196,32 +198,46 @@ func (p *ExecPlugin) getEnv() []string {
|
||||
return env
|
||||
}
|
||||
|
||||
func (p *ExecPlugin) getResMapWithIdAnnotation(rm resmap.ResMap) resmap.ResMap {
|
||||
// Returns a new copy of the given ResMap with the ResIds annotated in each Resource
|
||||
func (p *ExecPlugin) getResMapWithIdAnnotation(rm resmap.ResMap) (resmap.ResMap, error) {
|
||||
inputRM := rm.DeepCopy(p.rf.RF())
|
||||
for id, r := range inputRM {
|
||||
idString, err := yaml.Marshal(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
annotations := r.GetAnnotations()
|
||||
if annotations == nil {
|
||||
annotations = make(map[string]string)
|
||||
annotations = map[string]string{
|
||||
idAnnotation: string(idString),
|
||||
}
|
||||
r.SetAnnotations(annotations)
|
||||
}
|
||||
annotations[idAnnotation] = id.String()
|
||||
r.SetAnnotations(annotations)
|
||||
annotations[idAnnotation] = string(idString)
|
||||
}
|
||||
return inputRM
|
||||
return inputRM, nil
|
||||
}
|
||||
|
||||
func (p *ExecPlugin) parseResMapFromOutput(output []byte, rm resmap.ResMap) error {
|
||||
/*
|
||||
updateResMapValues updates the Resource value in the given ResMap
|
||||
with the emitted Resource values in output.
|
||||
*/
|
||||
func (p *ExecPlugin) updateResMapValues(output []byte, rm resmap.ResMap) error {
|
||||
outputRM, err := p.rf.NewResMapFromBytes(output)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, r := range outputRM {
|
||||
// for each emitted Resource, find the matching Resource in the original ResMap
|
||||
// using its id
|
||||
annotations := r.GetAnnotations()
|
||||
idString, ok := annotations[idAnnotation]
|
||||
if !ok {
|
||||
return fmt.Errorf("the transformer %s should not remove annotation %s",
|
||||
p.name, idAnnotation)
|
||||
}
|
||||
id, err := resid.NewResIdFromString(idString)
|
||||
id := resid.ResId{}
|
||||
err := yaml.Unmarshal([]byte(idString), &id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -229,8 +245,14 @@ func (p *ExecPlugin) parseResMapFromOutput(output []byte, rm resmap.ResMap) erro
|
||||
if !ok {
|
||||
return fmt.Errorf("unable to find id %s in resource map", id.String())
|
||||
}
|
||||
// remove the annotation set by Kustomize to track the resource
|
||||
delete(annotations, idAnnotation)
|
||||
if len(annotations) == 0 {
|
||||
annotations = nil
|
||||
}
|
||||
r.SetAnnotations(annotations)
|
||||
|
||||
// update the ResMap resource value with the transformed object
|
||||
res.Kunstructured = r.Kunstructured
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user