Skip to content

Commit b131ab0

Browse files
committed
Add optional built-in substitution of last attempted revision
The purpose of this new feature (which is CLI-conditional, and defaults to false in order to preserve compatibility) is to allow to refer to the revision that is currently being applied in the resource manifests, via the standard substitutions mechanism. Signed-off-by: arikkfir <[email protected]>
1 parent 968df5e commit b131ab0

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

internal/controller/kustomization_controller.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ type KustomizationReconciler struct {
9595
DefaultServiceAccount string
9696
KubeConfigOpts runtimeClient.KubeConfigOptions
9797
ConcurrentSSA int
98+
ImplicitSubstitutions bool
9899
}
99100

100101
// KustomizationReconcilerOptions contains options for the KustomizationReconciler.
@@ -617,6 +618,17 @@ func (r *KustomizationReconciler) build(ctx context.Context,
617618
}
618619
}
619620

621+
// add built-in substitutions
622+
if r.ImplicitSubstitutions {
623+
if obj.Spec.PostBuild == nil {
624+
obj.Spec.PostBuild = &kustomizev1.PostBuild{}
625+
}
626+
if obj.Spec.PostBuild.Substitute == nil {
627+
obj.Spec.PostBuild.Substitute = make(map[string]string)
628+
}
629+
obj.Spec.PostBuild.Substitute["FLUX_LAST_ATTEMPTED_REVISION"] = obj.Status.LastAttemptedRevision
630+
}
631+
620632
// run variable substitutions
621633
if obj.Spec.PostBuild != nil {
622634
outRes, err := generator.SubstituteVariables(ctx, r.Client, u, res, false)

main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func init() {
7575

7676
func main() {
7777
var (
78+
implicitSubstitutions bool
7879
metricsAddr string
7980
eventsAddr string
8081
healthAddr string
@@ -101,6 +102,8 @@ func main() {
101102
flag.IntVar(&concurrent, "concurrent", 4, "The number of concurrent kustomize reconciles.")
102103
flag.IntVar(&concurrentSSA, "concurrent-ssa", 4, "The number of concurrent server-side apply operations.")
103104
flag.DurationVar(&requeueDependency, "requeue-dependency", 30*time.Second, "The interval at which failing dependencies are reevaluated.")
105+
flag.BoolVar(&implicitSubstitutions, "implicit-substitutions", false,
106+
"Perform substitutions of built-in values such as last-attempted-revision; has side effects of ALWAYS performing substitutions!")
104107
flag.BoolVar(&noRemoteBases, "no-remote-bases", false,
105108
"Disallow remote bases usage in Kustomize overlays. When this flag is enabled, all resources must refer to local files included in the source artifact.")
106109
flag.IntVar(&httpRetry, "http-retry", 9, "The maximum number of retries when failing to fetch artifacts over HTTP.")
@@ -223,6 +226,7 @@ func main() {
223226
Metrics: metricsH,
224227
EventRecorder: eventRecorder,
225228
NoCrossNamespaceRefs: aclOptions.NoCrossNamespaceRefs,
229+
ImplicitSubstitutions: implicitSubstitutions,
226230
NoRemoteBases: noRemoteBases,
227231
FailFast: failFast,
228232
ConcurrentSSA: concurrentSSA,

0 commit comments

Comments
 (0)