Skip to content

Commit bd34dee

Browse files
If revision and config auth secrets match, fetch secret from revision's auth namespace (#1594)
* [HELM] - Release v0.5.3 (#1590) Bumping the numbers to release the v0.5.3 application version * feat: if revision and configuration auth secrets match, fetch secret from revision auth namespace * fix: wording * chore: adding the revision into the captured state method c --------- Co-authored-by: Rohith Jayawardene <gambol99@gmail.com>
1 parent 3324412 commit bd34dee

File tree

8 files changed

+79
-14
lines changed

8 files changed

+79
-14
lines changed

charts/terranetes-controller/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ apiVersion: v2
33
name: terranetes-controller
44
description: Controller used to provision a terraform workflow within kubernetes
55
type: application
6-
version: v0.8.1
7-
appVersion: v0.5.1
6+
version: v0.8.2
7+
appVersion: v0.5.3
88
sources:
99
- https://github.com/appvia/terranetes-controller
1010
- https://github.com/appvia/terranetes

charts/terranetes-controller/values.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ controller:
4343
# policy is image for policy
4444
policy: bridgecrew/checkov:3.2.298
4545
# preload is the image to use for preload data jobs
46-
preload: ghcr.io/appvia/terranetes-executor:v0.5.0
46+
preload: ghcr.io/appvia/terranetes-executor:v0.5.3
4747
# is the controller image
48-
controller: ghcr.io/appvia/terranetes-controller:v0.5.0
48+
controller: ghcr.io/appvia/terranetes-controller:v0.5.3
4949
# The terranetes image used when running jobs
50-
executor: ghcr.io/appvia/terranetes-executor:v0.5.0
50+
executor: ghcr.io/appvia/terranetes-executor:v0.5.3
5151
# Rate limting on configurations to prevent the controller from being overwhelmed. This
5252
# is the percentage of configurations which are permitted to run a plan at any one time.
5353
# Note, zero means no rate limiting is applied.

pkg/apis/terraform/v1alpha1/configuration_types.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,20 @@ 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+
switch {
499+
case c.Spec.Plan == nil:
500+
return false
501+
case c.Spec.Plan.Name == "":
502+
return false
503+
case c.Spec.Plan.Revision == "":
504+
return false
505+
}
506+
507+
return false
508+
}
509+
496510
// GetNamespacedName returns the namespaced resource type
497511
func (c *Configuration) GetNamespacedName() types.NamespacedName {
498512
return types.NamespacedName{

pkg/cmd/tnctl/create/assets/tnctl.revision.yaml.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ spec:
3636
{{- if .Inputs }}
3737

3838
## Inputs dictate the variables which the consumer is permitted, or
39-
## required to provides. It is best to keep this to a minimum; so a developer
39+
## required to provide. It is best to keep this to a minimum; so a developer
4040
## needn't be concerned with the inner workings of the module, just the
4141
## contextual requirements, i.e database name, size etc.
4242
inputs:

pkg/cmd/tnctl/create/revision.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ type RevisionCommand struct {
7373
File string
7474
// Provider is the name of the provider to use
7575
Provider string
76-
// DeleteDownload indicates we should retain the download
76+
// DeleteDownload indicates we should retain the download
7777
DeleteDownload bool
7878
}
7979

@@ -245,7 +245,7 @@ func (o *RevisionCommand) retrieveOutputs(module *tfconfig.Module) error {
245245
// @step: ask the user which outputs should exposed
246246
var selected []string
247247
if err := survey.AskOne(&survey.MultiSelect{
248-
Message: "What outputs should be extract into the secret?",
248+
Message: "What outputs should be extracted into the secret?",
249249
Options: suggestions,
250250
PageSize: 20,
251251
}, &selected, survey.WithKeepFilter(false)); err != nil {
@@ -272,7 +272,7 @@ func (o *RevisionCommand) retrieveRevision() error {
272272
if !found {
273273
if err := survey.AskOne(&survey.Input{
274274
Message: fmt.Sprintf("What is the version of this %s (in semver format)?", color.YellowString("revision")),
275-
Help: "Revisions must have a version, cloud resource reference both the plan and the version",
275+
Help: "Revisions must have a version, cloud resources reference both the plan name and the version",
276276
Default: "v0.0.1",
277277
}, &o.Revision); err != nil {
278278
return err
@@ -299,7 +299,7 @@ func (o *RevisionCommand) retrieveInputs(module *tfconfig.Module) error {
299299
return nil
300300
}
301301

302-
// @step: calculate the max variable size - just of spacing
302+
// @step: calculate the max variable size - just for spacing
303303
for _, x := range module.Variables {
304304
if len(x.Name) > length {
305305
length = len(x.Name)
@@ -430,7 +430,7 @@ func (o *RevisionCommand) retrievePlan() error {
430430

431431
// @step: we an produce a list from the current plans
432432
if err := survey.AskOne(&survey.Select{
433-
Message: fmt.Sprintf("The cluster already contains plans, will the %s will be part of?",
433+
Message: fmt.Sprintf("The cluster already contains plans, which will the %s will be part of?",
434434
color.YellowString("revision"),
435435
),
436436
Options: append(list, "None of these..."),

pkg/controller/configuration/ensure.go

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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)

pkg/controller/configuration/reconcile.go

Lines changed: 2 additions & 0 deletions
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.

pkg/utils/download.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ func Download(ctx context.Context, source, destination string) error {
5858
new(getter.FileDetector),
5959
}),
6060
},
61-
Pwd: pwd,
62-
Src: source,
61+
Pwd: pwd,
62+
Src: source,
6363
}
6464

6565
doneCh := make(chan struct{})

0 commit comments

Comments
 (0)