Skip to content

Commit

Permalink
Add optional built-in substitution of last attempted revision
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
arikkfir committed Sep 18, 2023
1 parent 968df5e commit b131ab0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
12 changes: 12 additions & 0 deletions internal/controller/kustomization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type KustomizationReconciler struct {
DefaultServiceAccount string
KubeConfigOpts runtimeClient.KubeConfigOptions
ConcurrentSSA int
ImplicitSubstitutions bool
}

// KustomizationReconcilerOptions contains options for the KustomizationReconciler.
Expand Down Expand Up @@ -617,6 +618,17 @@ func (r *KustomizationReconciler) build(ctx context.Context,
}
}

// add built-in substitutions
if r.ImplicitSubstitutions {
if obj.Spec.PostBuild == nil {
obj.Spec.PostBuild = &kustomizev1.PostBuild{}
}
if obj.Spec.PostBuild.Substitute == nil {
obj.Spec.PostBuild.Substitute = make(map[string]string)
}
obj.Spec.PostBuild.Substitute["FLUX_LAST_ATTEMPTED_REVISION"] = obj.Status.LastAttemptedRevision
}

// run variable substitutions
if obj.Spec.PostBuild != nil {
outRes, err := generator.SubstituteVariables(ctx, r.Client, u, res, false)
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func init() {

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

0 comments on commit b131ab0

Please sign in to comment.