@@ -23,6 +23,7 @@ import (
2323 "encoding/json"
2424 "errors"
2525 "fmt"
26+ "reflect"
2627 "strconv"
2728 "strings"
2829 "time"
@@ -78,6 +79,25 @@ func (c *Controller) ensureCapturedState(configuration *terraformv1alpha1.Config
7879 return reconcile.Result {}, err
7980 }
8081
82+ // @step: if we are based on a Revision, lets try and grab the definition
83+ if configuration .IsRevisioned () {
84+ revision := & terraformv1alpha1.Revision {}
85+ revision .Name = configuration .Spec .Plan .Revision
86+
87+ found , err := kubernetes .GetIfExists (ctx , c .cc , revision )
88+ if err != nil {
89+ cond .Failed (err , "Failed to retrieve the plan for the configuration" )
90+
91+ return reconcile.Result {}, err
92+ }
93+ if ! found {
94+ cond .ActionRequired ("Revision %q does not exist" , configuration .Spec .Plan .Revision )
95+
96+ return reconcile.Result {RequeueAfter : 5 * time .Minute }, nil
97+ }
98+ state .revision = revision
99+ }
100+
81101 // @step: retrieve a list of all the confugrations in the cluster - shouldn't have much impact
82102 // as it's a cached client and we defer to the cache
83103 configurations := & terraformv1alpha1.ConfigurationList {}
@@ -454,9 +474,38 @@ func (c *Controller) ensureAuthenticationSecret(configuration *terraformv1alpha1
454474 }
455475
456476 secret := & v1.Secret {}
457- secret .Namespace = configuration .Namespace
458477 secret .Name = configuration .Spec .Auth .Name
459478
479+ if configuration .IsRevisioned () {
480+ revision := state .revision
481+
482+ // @step: use the auth from the revision (sourcing secret from different namespace) if it's the same as in the configuration
483+ if revision .Spec .Configuration .Auth != nil {
484+ if reflect .DeepEqual (revision .Spec .Configuration .Auth , configuration .Spec .Auth ) {
485+ secret .Namespace = revision .Spec .Configuration .Auth .Namespace
486+ log .WithFields (log.Fields {
487+ "auth_name" : secret .Name ,
488+ "auth_namespace" : secret .Namespace ,
489+ "name" : configuration .Name ,
490+ "revision" : revision .Name ,
491+ }).Info ("auth secrets match, retrieving from the specified auth namespace, as defined in the revision" )
492+ } else {
493+ secret .Namespace = configuration .Namespace
494+ log .WithFields (log.Fields {
495+ "name" : configuration .Name ,
496+ "namespace" : configuration .Namespace ,
497+ "revision" : revision .Name ,
498+ }).Info ("configuration and revision auth secrets do not match, retrieving from the configuration's namespace" )
499+ }
500+ }
501+ } else {
502+ secret .Namespace = configuration .Namespace
503+ log .WithFields (log.Fields {
504+ "name" : configuration .Name ,
505+ "namespace" : configuration .Namespace ,
506+ }).Info ("no plan referenced, retrieving auth secret from the configuration's namespace" )
507+ }
508+
460509 found , err := kubernetes .GetIfExists (ctx , c .cc , secret )
461510 if err != nil {
462511 cond .Failed (err , "Failed to retrieve the authentication secret: (%s/%s)" , secret .Namespace , secret .Name )
0 commit comments