Skip to content

Commit

Permalink
Doc object importer exporter (#65)
Browse files Browse the repository at this point in the history
* feat(patch): support templated values.

* chore(doc): update sops sample.
  • Loading branch information
Zenithar authored Aug 10, 2021
1 parent 8b9a14e commit f2e8cac
Show file tree
Hide file tree
Showing 5 changed files with 421 additions and 119 deletions.
27 changes: 27 additions & 0 deletions pkg/bundle/patch/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package patch

import (
"encoding/json"
"fmt"
"regexp"

Expand Down Expand Up @@ -192,6 +193,7 @@ func applyPackagePatch(pkg *bundlev1.Package, p *bundlev1.PatchPackage, values m
return nil
}

//nolint:gocyclo // to refactor
func applySecretPatch(secrets *bundlev1.SecretChain, op *bundlev1.PatchSecret, values map[string]interface{}) error {
// Check parameters
if secrets == nil {
Expand Down Expand Up @@ -221,6 +223,31 @@ func applySecretPatch(secrets *bundlev1.SecretChain, op *bundlev1.PatchSecret, v
}
}

// Check template
if op.Template != "" {
// Compile template
rendered, err := engine.Render(op.Template, map[string]interface{}{
"Values": values,
})
if err != nil {
return fmt.Errorf("unable to compile secret template: %w", err)
}

// Unmarshall as kv
var kv map[string]string
if errJSON := json.Unmarshal([]byte(rendered), &kv); errJSON != nil {
return fmt.Errorf("unable to valudate rendered secret template as a valid JSON: %w", errJSON)
}

// Update secret data
if secrets.Data == nil {
secrets.Data = make([]*bundlev1.KV, 0)
}
if secrets.Data, err = updateSecret(secrets.Data, kv); err != nil {
return fmt.Errorf("unable to uppdate kv from template: %w", err)
}
}

// Check K/V
if op.Kv != nil {
var err error
Expand Down
Loading

0 comments on commit f2e8cac

Please sign in to comment.