@@ -44,6 +44,7 @@ import (
4444 "sigs.k8s.io/kustomize/kyaml/yaml"
4545
4646 kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
47+ "github.com/fluxcd/kustomize-controller/decryptor"
4748 "github.com/fluxcd/pkg/kustomize"
4849 runclient "github.com/fluxcd/pkg/runtime/client"
4950 ssautil "github.com/fluxcd/pkg/ssa/utils"
@@ -77,15 +78,16 @@ type Builder struct {
7778 kustomizationFile string
7879 ignore []string
7980 // mu is used to synchronize access to the kustomization file
80- mu sync.Mutex
81- action kustomize.Action
82- kustomization * kustomizev1.Kustomization
83- timeout time.Duration
84- spinner * yacspin.Spinner
85- dryRun bool
86- strictSubst bool
87- recursive bool
88- localSources map [string ]string
81+ mu sync.Mutex
82+ action kustomize.Action
83+ kustomization * kustomizev1.Kustomization
84+ timeout time.Duration
85+ spinner * yacspin.Spinner
86+ dryRun bool
87+ strictSubst bool
88+ recursive bool
89+ decryptSecrets bool
90+ localSources map [string ]string
8991 // diff needs to handle kustomizations one by one
9092 singleKustomization bool
9193}
@@ -190,6 +192,14 @@ func WithRecursive(recursive bool) BuilderOptionFunc {
190192 }
191193}
192194
195+ // WithDecryptSecrets sets the decrypt secrets field
196+ func WithDecryptSecrets (decryptSecrets bool ) BuilderOptionFunc {
197+ return func (b * Builder ) error {
198+ b .decryptSecrets = decryptSecrets
199+ return nil
200+ }
201+ }
202+
193203// WithLocalSources sets the local sources field
194204func WithLocalSources (localSources map [string ]string ) BuilderOptionFunc {
195205 return func (b * Builder ) error {
@@ -514,7 +524,36 @@ func (b *Builder) do(ctx context.Context, kustomization kustomizev1.Kustomizatio
514524 return nil , fmt .Errorf ("kustomize build failed: %w" , err )
515525 }
516526
527+ var dec * decryptor.Decryptor
528+ var cleanup func ()
529+ if b .decryptSecrets {
530+ dec , cleanup , err = decryptor .NewTempDecryptor (b .resourcesPath , b .client , b .kustomization )
531+ if err != nil {
532+ return nil , err
533+ }
534+ defer cleanup ()
535+
536+ // Import decryption keys
537+ if err := dec .ImportKeys (ctx ); err != nil {
538+ return nil , err
539+ }
540+ }
541+
517542 for _ , res := range m .Resources () {
543+ if res .GetKind () == "Secret" && b .decryptSecrets {
544+ outRes , err := dec .DecryptResource (res )
545+ if err != nil {
546+ return nil , fmt .Errorf ("decryption failed for '%s': %w" , res .GetName (), err )
547+ }
548+
549+ if outRes != nil {
550+ _ , err = m .Replace (res )
551+ if err != nil {
552+ return nil , err
553+ }
554+ }
555+ }
556+
518557 // run variable substitutions
519558 if kustomization .Spec .PostBuild != nil {
520559 data , err := runtime .DefaultUnstructuredConverter .ToUnstructured (& kustomization )
0 commit comments