Skip to content

Commit 49f42ae

Browse files
committed
chore: adding the revision into the captured state method
1 parent ca57ae1 commit 49f42ae

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

pkg/apis/terraform/v1alpha1/configuration_types.go

+9
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,15 @@ type ConfigurationStatus struct {
493493
TerraformVersion string `json:"terraformVersion,omitempty"`
494494
}
495495

496+
// IsRevisioned returns true if the configuration is revisioned
497+
func (c *Configuration) IsRevisioned() bool {
498+
if c.Spec.Plan != nil && c.Spec.Plan.Revision != "" {
499+
return true
500+
}
501+
502+
return false
503+
}
504+
496505
// GetNamespacedName returns the namespaced resource type
497506
func (c *Configuration) GetNamespacedName() types.NamespacedName {
498507
return types.NamespacedName{

pkg/controller/configuration/ensure.go

+22-15
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,25 @@ func (c *Controller) ensureCapturedState(configuration *terraformv1alpha1.Config
7979
return reconcile.Result{}, err
8080
}
8181

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+
82101
// @step: retrieve a list of all the confugrations in the cluster - shouldn't have much impact
83102
// as it's a cached client and we defer to the cache
84103
configurations := &terraformv1alpha1.ConfigurationList{}
@@ -457,29 +476,17 @@ func (c *Controller) ensureAuthenticationSecret(configuration *terraformv1alpha1
457476
secret := &v1.Secret{}
458477
secret.Name = configuration.Spec.Auth.Name
459478

460-
if configuration.Spec.Plan != nil && configuration.Spec.Plan.Name != "" {
461-
// @step: retrieve the revision from the plan
462-
revision := &terraformv1alpha1.Revision{}
463-
revision.Name = configuration.GetLabels()[terraformv1alpha1.CloudResourceRevisionNameLabel]
464-
465-
found, err := kubernetes.GetIfExists(ctx, c.cc, revision)
466-
if err != nil {
467-
cond.Failed(err, "Failed to retrieve the revision: %q, which this Configuration %q is part of", revision.Name, configuration.Name)
468-
return reconcile.Result{}, err
469-
}
470-
if !found {
471-
cond.ActionRequired("Revision %q, which this Configuration %q is part of, does not exist", revision.Name, configuration.Name)
472-
return reconcile.Result{RequeueAfter: 5 * time.Minute}, nil
473-
}
479+
if configuration.IsRevisioned() {
480+
revision := state.revision
474481

475482
// @step: use the auth from the revision (sourcing secret from different namespace) if it's the same as in the configuration
476483
if revision.Spec.Configuration.Auth != nil {
477484
if reflect.DeepEqual(revision.Spec.Configuration.Auth, configuration.Spec.Auth) {
478485
secret.Namespace = revision.Spec.Configuration.Auth.Namespace
479486
log.WithFields(log.Fields{
480-
"name": configuration.Name,
481487
"auth_name": secret.Name,
482488
"auth_namespace": secret.Namespace,
489+
"name": configuration.Name,
483490
"revision": revision.Name,
484491
}).Info("auth secrets match, retrieving from the specified auth namespace, as defined in the revision")
485492
} else {

pkg/controller/configuration/reconcile.go

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ type state struct {
3838
configurations *terraformv1alpha1.ConfigurationList
3939
// checkovConstraint is the policy constraint for this configuration
4040
checkovConstraint *terraformv1alpha1.PolicyConstraint
41+
// revision is the Revision we are based from
42+
revision *terraformv1alpha1.Revision
4143
// hasDrift is a flag to indicate if the configuration has drift
4244
hasDrift bool
4345
// backendTemplate is the template to use for the terraform state backend.

0 commit comments

Comments
 (0)